GraphPanel.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. if (data) {
  14. for (const series of data) {
  15. const timeColumn = getFirstTimeField(series);
  16. if (timeColumn < 0) {
  17. continue;
  18. }
  19. for (let i = 0; i < series.fields.length; i++) {
  20. const field = series.fields[i];
  21. // Show all numeric columns
  22. if (field.type === FieldType.number) {
  23. // Use external calculator just to make sure it works :)
  24. const points = getFlotPairs({
  25. series,
  26. xIndex: timeColumn,
  27. yIndex: i,
  28. nullValueMode: NullValueMode.Null,
  29. });
  30. if (points.length > 0) {
  31. graphs.push({
  32. label: field.name,
  33. data: points,
  34. color: colors[graphs.length % colors.length],
  35. });
  36. }
  37. }
  38. }
  39. }
  40. }
  41. if (graphs.length < 1) {
  42. return (
  43. <div className="panel-empty">
  44. <p>No data found in response</p>
  45. </div>
  46. );
  47. }
  48. return (
  49. <Graph
  50. series={graphs}
  51. timeRange={timeRange}
  52. showLines={showLines}
  53. showPoints={showPoints}
  54. showBars={showBars}
  55. width={width}
  56. height={height}
  57. />
  58. );
  59. }
  60. }