GraphPanel.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Libraries
  2. import _ from 'lodash';
  3. import React, { PureComponent } from 'react';
  4. import { Graph, PanelProps, NullValueMode, colors, GraphSeriesXY, FieldType, getFirstTimeField } from '@grafana/ui';
  5. import { Options } from './types';
  6. import { getFlotPairs } from '@grafana/ui/src/utils/flotPairs';
  7. interface Props extends PanelProps<Options> {}
  8. export class GraphPanel extends PureComponent<Props> {
  9. render() {
  10. const { data, timeRange, width, height } = this.props;
  11. const { showLines, showBars, showPoints } = this.props.options;
  12. const graphs: GraphSeriesXY[] = [];
  13. for (const series of data) {
  14. const timeColumn = getFirstTimeField(series);
  15. if (timeColumn < 0) {
  16. continue;
  17. }
  18. for (let i = 0; i < series.fields.length; i++) {
  19. const field = series.fields[i];
  20. // Show all numeric columns
  21. if (field.type === FieldType.number) {
  22. // Use external calculator just to make sure it works :)
  23. const points = getFlotPairs({
  24. series,
  25. xIndex: timeColumn,
  26. yIndex: i,
  27. nullValueMode: NullValueMode.Null,
  28. });
  29. if (points.length > 0) {
  30. graphs.push({
  31. label: field.name,
  32. data: points,
  33. color: colors[graphs.length % colors.length],
  34. });
  35. }
  36. }
  37. }
  38. }
  39. return (
  40. <Graph
  41. series={graphs}
  42. timeRange={timeRange}
  43. showLines={showLines}
  44. showPoints={showPoints}
  45. showBars={showBars}
  46. width={width}
  47. height={height}
  48. />
  49. );
  50. }
  51. }