Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #87 from pierosavi/improve-display-values
Browse files Browse the repository at this point in the history
Improve display value
  • Loading branch information
pierosavi authored Mar 31, 2021
2 parents 9402922 + be100db commit 43a89c1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v1.0.3

## Improvements

* Better data format to support more data sources

# v1.0.2

## Fix
Expand Down
37 changes: 27 additions & 10 deletions src/ImageItPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import React, { useEffect, useRef, useState } from 'react';
import { PanelProps } from '@grafana/data';
import { PanelProps, getFieldDisplayValues, ReducerID } from '@grafana/data';
import { SimpleOptions } from './types/SimpleOptions';
import { css, cx } from 'emotion';
import _ from 'lodash';
// import { stylesFactory, useTheme } from '@grafana/ui';
import { stylesFactory } from '@grafana/ui';
import { stylesFactory, useTheme } from '@grafana/ui';
import { Sensor } from './Sensor';
import SensorType from './types/Sensor';

interface Props extends PanelProps<SimpleOptions> {}

export const ImageItPanel: React.FC<Props> = ({ options, data, width, height, onOptionsChange, fieldConfig }) => {
export const ImageItPanel: React.FC<Props> = ({
options,
data,
width,
height,
onOptionsChange,
fieldConfig,
replaceVariables,
}) => {
const { forceImageRefresh, lockSensors, mappings, sensors, sensorsTextSize } = options;
// const theme = useTheme();
const theme = useTheme();
const styles = getStyles();

const imageRef = useRef<HTMLImageElement>(null);
Expand Down Expand Up @@ -69,12 +77,21 @@ export const ImageItPanel: React.FC<Props> = ({ options, data, width, height, on
sensor.query.id ? sensor.query.id === serie.refId : sensor.query.alias === serie.name
);

// Assume value is in field with name 'Value'
// This could be a problem for some data sources
const field = serie?.fields.find(field => field.name === 'Value');

// Get last value of values array
const value = field?.values.get(field.values.length - 1);
let value = undefined;

if (serie !== undefined) {
const fieldDisplay = getFieldDisplayValues({
data: [serie],
reduceOptions: {
calcs: [ReducerID.last],
},
fieldConfig,
replaceVariables,
theme,
})[0];

value = fieldDisplay.display.numeric;
}

// Get mapping by id || undefined
const mapping = sensor.mappingId ? mappings.find(mapping => sensor.mappingId === mapping.id) : undefined;
Expand Down

0 comments on commit 43a89c1

Please sign in to comment.