Graph.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import $ from 'jquery';
  2. import React, { PureComponent } from 'react';
  3. import 'vendor/flot/jquery.flot';
  4. import 'vendor/flot/jquery.flot.time';
  5. import 'vendor/flot/jquery.flot.selection';
  6. import 'vendor/flot/jquery.flot.stack';
  7. import { TimeZone, AbsoluteTimeRange } from '@grafana/ui';
  8. import TimeSeries from 'app/core/time_series2';
  9. import Legend from './Legend';
  10. import { equal, intersect } from './utils/set';
  11. const MAX_NUMBER_OF_TIME_SERIES = 20;
  12. // Copied from graph.ts
  13. function time_format(ticks, min, max) {
  14. if (min && max && ticks) {
  15. const range = max - min;
  16. const secPerTick = range / ticks / 1000;
  17. const oneDay = 86400000;
  18. const oneYear = 31536000000;
  19. if (secPerTick <= 45) {
  20. return '%H:%M:%S';
  21. }
  22. if (secPerTick <= 7200 || range <= oneDay) {
  23. return '%H:%M';
  24. }
  25. if (secPerTick <= 80000) {
  26. return '%m/%d %H:%M';
  27. }
  28. if (secPerTick <= 2419200 || range <= oneYear) {
  29. return '%m/%d';
  30. }
  31. return '%Y-%m';
  32. }
  33. return '%H:%M';
  34. }
  35. const FLOT_OPTIONS = {
  36. legend: {
  37. show: false,
  38. },
  39. series: {
  40. lines: {
  41. linewidth: 1,
  42. zero: false,
  43. },
  44. shadowSize: 0,
  45. },
  46. grid: {
  47. minBorderMargin: 0,
  48. markings: [],
  49. backgroundColor: null,
  50. borderWidth: 0,
  51. // hoverable: true,
  52. clickable: true,
  53. color: '#a1a1a1',
  54. margin: { left: 0, right: 0 },
  55. labelMarginX: 0,
  56. },
  57. selection: {
  58. mode: 'x',
  59. color: '#666',
  60. },
  61. // crosshair: {
  62. // mode: 'x',
  63. // },
  64. };
  65. interface GraphProps {
  66. data: any[];
  67. height?: number;
  68. width?: number;
  69. id?: string;
  70. range: AbsoluteTimeRange;
  71. timeZone: TimeZone;
  72. split?: boolean;
  73. userOptions?: any;
  74. onChangeTime?: (range: AbsoluteTimeRange) => void;
  75. onToggleSeries?: (alias: string, hiddenSeries: Set<string>) => void;
  76. }
  77. interface GraphState {
  78. /**
  79. * Type parameter refers to the `alias` property of a `TimeSeries`.
  80. * Consequently, all series sharing the same alias will share visibility state.
  81. */
  82. hiddenSeries: Set<string>;
  83. showAllTimeSeries: boolean;
  84. }
  85. export class Graph extends PureComponent<GraphProps, GraphState> {
  86. $el: any;
  87. dynamicOptions = null;
  88. state = {
  89. hiddenSeries: new Set(),
  90. showAllTimeSeries: false,
  91. };
  92. getGraphData() {
  93. const { data } = this.props;
  94. return this.state.showAllTimeSeries ? data : data.slice(0, MAX_NUMBER_OF_TIME_SERIES);
  95. }
  96. componentDidMount() {
  97. this.draw();
  98. this.$el = $(`#${this.props.id}`);
  99. this.$el.bind('plotselected', this.onPlotSelected);
  100. }
  101. componentDidUpdate(prevProps: GraphProps, prevState: GraphState) {
  102. if (
  103. prevProps.data !== this.props.data ||
  104. prevProps.range !== this.props.range ||
  105. prevProps.split !== this.props.split ||
  106. prevProps.height !== this.props.height ||
  107. prevProps.width !== this.props.width ||
  108. !equal(prevState.hiddenSeries, this.state.hiddenSeries)
  109. ) {
  110. this.draw();
  111. }
  112. }
  113. componentWillUnmount() {
  114. this.$el.unbind('plotselected', this.onPlotSelected);
  115. }
  116. onPlotSelected = (event, ranges) => {
  117. const { onChangeTime } = this.props;
  118. if (onChangeTime) {
  119. this.props.onChangeTime({
  120. from: ranges.xaxis.from,
  121. to: ranges.xaxis.to,
  122. });
  123. }
  124. };
  125. getDynamicOptions() {
  126. const { range, width, timeZone } = this.props;
  127. const ticks = (width || 0) / 100;
  128. const min = range.from;
  129. const max = range.to;
  130. return {
  131. xaxis: {
  132. mode: 'time',
  133. min: min,
  134. max: max,
  135. label: 'Datetime',
  136. ticks: ticks,
  137. timezone: timeZone.raw,
  138. timeformat: time_format(ticks, min, max),
  139. },
  140. };
  141. }
  142. onShowAllTimeSeries = () => {
  143. this.setState(
  144. {
  145. showAllTimeSeries: true,
  146. },
  147. this.draw
  148. );
  149. };
  150. onToggleSeries = (series: TimeSeries, exclusive: boolean) => {
  151. this.setState((state, props) => {
  152. const { data, onToggleSeries } = props;
  153. const { hiddenSeries } = state;
  154. // Deduplicate series as visibility tracks the alias property
  155. const oneSeriesVisible = hiddenSeries.size === new Set(data.map(d => d.alias)).size - 1;
  156. let nextHiddenSeries = new Set();
  157. if (exclusive) {
  158. if (hiddenSeries.has(series.alias) || !oneSeriesVisible) {
  159. nextHiddenSeries = new Set(data.filter(d => d.alias !== series.alias).map(d => d.alias));
  160. }
  161. } else {
  162. // Prune hidden series no longer part of those available from the most recent query
  163. const availableSeries = new Set(data.map(d => d.alias));
  164. nextHiddenSeries = intersect(new Set(hiddenSeries), availableSeries);
  165. if (nextHiddenSeries.has(series.alias)) {
  166. nextHiddenSeries.delete(series.alias);
  167. } else {
  168. nextHiddenSeries.add(series.alias);
  169. }
  170. }
  171. if (onToggleSeries) {
  172. onToggleSeries(series.alias, nextHiddenSeries);
  173. }
  174. return {
  175. hiddenSeries: nextHiddenSeries,
  176. };
  177. }, this.draw);
  178. };
  179. draw() {
  180. const { userOptions = {} } = this.props;
  181. const { hiddenSeries } = this.state;
  182. const data = this.getGraphData();
  183. const $el = $(`#${this.props.id}`);
  184. let series = [{ data: [[0, 0]] }];
  185. if (data && data.length > 0) {
  186. series = data
  187. .filter((ts: TimeSeries) => !hiddenSeries.has(ts.alias))
  188. .map((ts: TimeSeries) => ({
  189. color: ts.color,
  190. label: ts.label,
  191. data: ts.getFlotPairs('null'),
  192. }));
  193. }
  194. this.dynamicOptions = this.getDynamicOptions();
  195. const options = {
  196. ...FLOT_OPTIONS,
  197. ...this.dynamicOptions,
  198. ...userOptions,
  199. };
  200. $.plot($el, series, options);
  201. }
  202. render() {
  203. const { height = 100, id = 'graph' } = this.props;
  204. const { hiddenSeries } = this.state;
  205. const data = this.getGraphData();
  206. return (
  207. <>
  208. {this.props.data && this.props.data.length > MAX_NUMBER_OF_TIME_SERIES && !this.state.showAllTimeSeries && (
  209. <div className="time-series-disclaimer">
  210. <i className="fa fa-fw fa-warning disclaimer-icon" />
  211. {`Showing only ${MAX_NUMBER_OF_TIME_SERIES} time series. `}
  212. <span className="show-all-time-series" onClick={this.onShowAllTimeSeries}>{`Show all ${
  213. this.props.data.length
  214. }`}</span>
  215. </div>
  216. )}
  217. <div id={id} className="explore-graph" style={{ height }} />
  218. <Legend data={data} hiddenSeries={hiddenSeries} onToggleSeries={this.onToggleSeries} />
  219. </>
  220. );
  221. }
  222. }
  223. export default Graph;