getGraphSeriesModel.ts 2.2 KB

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