2019-08-06 16:49:54 +08:00
|
|
|
import { colors, getFlotPairs, getColorFromHexRgbOrName, getDisplayProcessor, PanelData } from '@grafana/ui';
|
|
|
|
|
import { NullValueMode, reduceField, FieldCache, FieldType, DisplayValue, GraphSeriesXY } from '@grafana/data';
|
|
|
|
|
|
2019-04-24 16:14:18 +08:00
|
|
|
import { SeriesOptions, GraphOptions } from './types';
|
|
|
|
|
import { GraphLegendEditorLegendOptions } from './GraphLegendEditor';
|
|
|
|
|
|
|
|
|
|
export const getGraphSeriesModel = (
|
|
|
|
|
data: PanelData,
|
|
|
|
|
seriesOptions: SeriesOptions,
|
|
|
|
|
graphOptions: GraphOptions,
|
|
|
|
|
legendOptions: GraphLegendEditorLegendOptions
|
|
|
|
|
) => {
|
|
|
|
|
const graphs: GraphSeriesXY[] = [];
|
|
|
|
|
|
|
|
|
|
const displayProcessor = getDisplayProcessor({
|
2019-05-04 16:08:48 +08:00
|
|
|
field: {
|
|
|
|
|
decimals: legendOptions.decimals,
|
|
|
|
|
},
|
2019-04-24 16:14:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const series of data.series) {
|
2019-05-01 00:21:22 +08:00
|
|
|
const fieldCache = new FieldCache(series.fields);
|
|
|
|
|
const timeColumn = fieldCache.getFirstFieldOfType(FieldType.time);
|
|
|
|
|
if (!timeColumn) {
|
2019-04-24 16:14:18 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 00:21:22 +08:00
|
|
|
const numberFields = fieldCache.getFields(FieldType.number);
|
|
|
|
|
for (let i = 0; i < numberFields.length; i++) {
|
|
|
|
|
const field = numberFields[i];
|
|
|
|
|
// Use external calculator just to make sure it works :)
|
|
|
|
|
const points = getFlotPairs({
|
2019-08-13 13:32:43 +08:00
|
|
|
rows: series.rows,
|
2019-05-01 00:21:22 +08:00
|
|
|
xIndex: timeColumn.index,
|
|
|
|
|
yIndex: field.index,
|
|
|
|
|
nullValueMode: NullValueMode.Null,
|
|
|
|
|
});
|
2019-04-24 16:14:18 +08:00
|
|
|
|
2019-05-01 00:21:22 +08:00
|
|
|
if (points.length > 0) {
|
2019-05-04 04:16:33 +08:00
|
|
|
const seriesStats = reduceField({
|
|
|
|
|
series,
|
|
|
|
|
reducers: legendOptions.stats,
|
|
|
|
|
fieldIndex: field.index,
|
|
|
|
|
});
|
|
|
|
|
let statsDisplayValues: DisplayValue[];
|
2019-04-24 16:14:18 +08:00
|
|
|
|
2019-05-01 00:21:22 +08:00
|
|
|
if (legendOptions.stats) {
|
|
|
|
|
statsDisplayValues = legendOptions.stats.map<DisplayValue>(stat => {
|
|
|
|
|
const statDisplayValue = displayProcessor(seriesStats[stat]);
|
2019-04-24 16:14:18 +08:00
|
|
|
|
2019-05-01 00:21:22 +08:00
|
|
|
return {
|
|
|
|
|
...statDisplayValue,
|
|
|
|
|
text: statDisplayValue.text,
|
|
|
|
|
title: stat,
|
|
|
|
|
};
|
2019-04-24 16:14:18 +08:00
|
|
|
});
|
|
|
|
|
}
|
2019-05-01 00:21:22 +08:00
|
|
|
|
|
|
|
|
const seriesColor =
|
|
|
|
|
seriesOptions[field.name] && seriesOptions[field.name].color
|
|
|
|
|
? getColorFromHexRgbOrName(seriesOptions[field.name].color)
|
|
|
|
|
: colors[graphs.length % colors.length];
|
|
|
|
|
|
|
|
|
|
graphs.push({
|
|
|
|
|
label: field.name,
|
|
|
|
|
data: points,
|
|
|
|
|
color: seriesColor,
|
|
|
|
|
info: statsDisplayValues,
|
|
|
|
|
isVisible: true,
|
2019-08-13 13:32:43 +08:00
|
|
|
yAxis: {
|
|
|
|
|
index: (seriesOptions[field.name] && seriesOptions[field.name].yAxis) || 1,
|
|
|
|
|
},
|
2019-05-01 00:21:22 +08:00
|
|
|
});
|
2019-04-24 16:14:18 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return graphs;
|
|
|
|
|
};
|