Graph.tsx 6.6 KB

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