getGraphSeriesModel.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { colors, getFlotPairs, getColorFromHexRgbOrName, getDisplayProcessor, PanelData } from '@grafana/ui';
  2. import { NullValueMode, reduceField, FieldType, DisplayValue, GraphSeriesXY, getTimeField } from '@grafana/data';
  3. import { SeriesOptions, GraphOptions } from './types';
  4. import { GraphLegendEditorLegendOptions } from './GraphLegendEditor';
  5. export const getGraphSeriesModel = (
  6. data: PanelData,
  7. seriesOptions: SeriesOptions,
  8. graphOptions: GraphOptions,
  9. legendOptions: GraphLegendEditorLegendOptions
  10. ) => {
  11. const graphs: GraphSeriesXY[] = [];
  12. const displayProcessor = getDisplayProcessor({
  13. field: {
  14. decimals: legendOptions.decimals,
  15. },
  16. });
  17. for (const series of data.series) {
  18. const { timeField } = getTimeField(series);
  19. if (!timeField) {
  20. continue;
  21. }
  22. for (const field of series.fields) {
  23. if (field.type !== FieldType.number) {
  24. continue;
  25. }
  26. // Use external calculator just to make sure it works :)
  27. const points = getFlotPairs({
  28. xField: timeField,
  29. yField: field,
  30. nullValueMode: NullValueMode.Null,
  31. });
  32. if (points.length > 0) {
  33. const seriesStats = reduceField({ field, reducers: legendOptions.stats });
  34. let statsDisplayValues: DisplayValue[];
  35. if (legendOptions.stats) {
  36. statsDisplayValues = legendOptions.stats.map<DisplayValue>(stat => {
  37. const statDisplayValue = displayProcessor(seriesStats[stat]);
  38. return {
  39. ...statDisplayValue,
  40. text: statDisplayValue.text,
  41. title: stat,
  42. };
  43. });
  44. }
  45. const seriesColor =
  46. seriesOptions[field.name] && seriesOptions[field.name].color
  47. ? getColorFromHexRgbOrName(seriesOptions[field.name].color)
  48. : colors[graphs.length % colors.length];
  49. graphs.push({
  50. label: field.name,
  51. data: points,
  52. color: seriesColor,
  53. info: statsDisplayValues,
  54. isVisible: true,
  55. yAxis: {
  56. index: (seriesOptions[field.name] && seriesOptions[field.name].yAxis) || 1,
  57. },
  58. });
  59. }
  60. }
  61. }
  62. return graphs;
  63. };