GraphPanel.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.series) {
  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. if (graphs.length < 1) {
  40. return (
  41. <div className="panel-empty">
  42. <p>No data found in response</p>
  43. </div>
  44. );
  45. }
  46. return (
  47. <Graph
  48. series={graphs}
  49. timeRange={timeRange}
  50. showLines={showLines}
  51. showPoints={showPoints}
  52. showBars={showBars}
  53. width={width}
  54. height={height}
  55. />
  56. );
  57. }
  58. }