getGraphSeriesModel.ts 2.3 KB

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