Skip to content

Commit

Permalink
linetemporalchart: Add 'NAN' to display hyphen for the data non existed
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengYanJin committed Aug 31, 2021
1 parent e1fbc00 commit 25ed15b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/lib/components/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ export const queryTimeSpansCodes = [
frequency: SAMPLE_FREQUENCY_LAST_ONE_HOUR,
},
];

export const NAN_STRING = 'NAN';
13 changes: 6 additions & 7 deletions src/lib/components/linetemporalchart/ChartUtil.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//@flow
import { type Series } from './LineTemporalChart.component.js';

import { NAN_STRING } from '../constants.js';
export type VegaData = {
timestamp: number,
label: string, // same as the tooltip label
value: number | null, // the value can be null, in order to not display the line during the downtime of the platform
value: number | 'NAN', // the "NAN" is used by the tooltip to display a dash for the data which are not exist.
isNegativeValue: boolean, // if the metricPrefix is read and out, we need to convert the value to negative before assigning it to the vega-lite spec
}[];

Expand All @@ -17,7 +17,8 @@ export function convert2VegaData(
const obj = {
timestamp: datum[0] * 1000, // convert to million second
label: line.getTooltipLabel(line.metricPrefix, line.resource),
value: datum[1] ? Number(datum[1]) : null,
value:
datum[1] && datum[1] !== NAN_STRING ? Number(datum[1]) : NAN_STRING,
isNegativeValue:
line.metricPrefix === 'read' || line.metricPrefix === 'out',
};
Expand All @@ -32,7 +33,7 @@ export function convertDataBaseValue(data: VegaData, base: number): VegaData {
return data.map((datum) => {
return {
...datum,
value: datum.value ? datum.value / base : null,
value: typeof datum.value === 'number' ? datum.value / base : NAN_STRING,
};
});
}
Expand Down Expand Up @@ -91,7 +92,6 @@ export function addMissingDataPoint(
): [number, string | null][] {
if (
!orginalValues ||
orginalValues.length === 0 ||
startingTimeStamp === undefined ||
!sampleDuration ||
!sampleFrequency ||
Expand All @@ -108,7 +108,7 @@ export function addMissingDataPoint(

// initialize the array with all `null` value
for (let i = 0; i < numberOfDataPoints; i++) {
newValues.push([samplingPointTime, null]);
newValues.push([samplingPointTime, NAN_STRING]);
samplingPointTime += sampleFrequency;
}

Expand All @@ -120,7 +120,6 @@ export function addMissingDataPoint(
orginalValues[nextIndex] &&
newValues[i][0] === orginalValues[nextIndex][0]
) {
// $FlowFixMe get flow error because assign a value to null
newValues[i][1] = orginalValues[nextIndex][1];
nextIndex++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ function LineTemporalChart({
vegaData.map(function (datum: {
timestamp: number,
label: string,
value: number | null,
value: number | 'NAN',
}): number {
if (datum.value) {
if (datum.value && typeof datum.value === 'number') {
return datum.value;
}
return 0;
Expand All @@ -217,10 +217,14 @@ function LineTemporalChart({
(data: {
timestamp: number,
label: string, // same as the tooltip label
value: number | null, // the value can be null, in order to not display the line during the downtime of the platform
value: number | 'NAN', // manually set it to a string. It's for the tooltip to display a hyphen for the data that don't exist
isNegativeValue: boolean,
}) => {
if (data.isNegativeValue && data.value) {
if (
data.isNegativeValue &&
data.value &&
typeof data.value === 'number'
) {
return { ...data, value: 0 - data.value };
} else return { ...data };
},
Expand Down
14 changes: 12 additions & 2 deletions src/lib/components/vegachartv2/VegaChartV2.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ function VegaChart(
.insert(themedSpec.data.values); //only the data.values changes trigger the graph's repaint
// For some reason source_0 is the default dataset name
view.change('source_0', changeset).run();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
// eslint-disable-next-line
JSON.stringify(themedSpec.data.values),
vegaInstance,
]);

useLayoutEffect(() => {
if (vegaInstance.current) {
const view = vegaInstance.current.view;

// when the mouse go out, we trigger the event to set cursorX to null
if (
!themedSpec.params.find((param) => param.name === 'cursorX').value ||
Expand Down Expand Up @@ -214,8 +226,6 @@ function VegaChart(
}, [
// eslint-disable-next-line
JSON.stringify(themedSpec),
// eslint-disable-next-line
JSON.stringify(tooltipOptions),
vegaInstance,
]);
return (
Expand Down

0 comments on commit 25ed15b

Please sign in to comment.