Graph.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import $ from 'jquery';
  2. import React, { PureComponent } from 'react';
  3. import moment from 'moment';
  4. import 'vendor/flot/jquery.flot';
  5. import 'vendor/flot/jquery.flot.time';
  6. import 'vendor/flot/jquery.flot.selection';
  7. import 'vendor/flot/jquery.flot.stack';
  8. import { RawTimeRange } from '@grafana/ui';
  9. import * as dateMath from 'app/core/utils/datemath';
  10. import TimeSeries from 'app/core/time_series2';
  11. import Legend from './Legend';
  12. import { equal, intersect } from './utils/set';
  13. const MAX_NUMBER_OF_TIME_SERIES = 20;
  14. // Copied from graph.ts
  15. function time_format(ticks, min, max) {
  16. if (min && max && ticks) {
  17. const range = max - min;
  18. const secPerTick = range / ticks / 1000;
  19. const oneDay = 86400000;
  20. const oneYear = 31536000000;
  21. if (secPerTick <= 45) {
  22. return '%H:%M:%S';
  23. }
  24. if (secPerTick <= 7200 || range <= oneDay) {
  25. return '%H:%M';
  26. }
  27. if (secPerTick <= 80000) {
  28. return '%m/%d %H:%M';
  29. }
  30. if (secPerTick <= 2419200 || range <= oneYear) {
  31. return '%m/%d';
  32. }
  33. return '%Y-%m';
  34. }
  35. return '%H:%M';
  36. }
  37. const FLOT_OPTIONS = {
  38. legend: {
  39. show: false,
  40. },
  41. series: {
  42. lines: {
  43. linewidth: 1,
  44. zero: false,
  45. },
  46. shadowSize: 0,
  47. },
  48. grid: {
  49. minBorderMargin: 0,
  50. markings: [],
  51. backgroundColor: null,
  52. borderWidth: 0,
  53. // hoverable: true,
  54. clickable: true,
  55. color: '#a1a1a1',
  56. margin: { left: 0, right: 0 },
  57. labelMarginX: 0,
  58. },
  59. selection: {
  60. mode: 'x',
  61. color: '#666',
  62. },
  63. // crosshair: {
  64. // mode: 'x',
  65. // },
  66. };
  67. interface GraphProps {
  68. data: any[];
  69. height?: number;
  70. width?: number;
  71. id?: string;
  72. range: RawTimeRange;
  73. split?: boolean;
  74. userOptions?: any;
  75. onChangeTime?: (range: RawTimeRange) => void;
  76. onToggleSeries?: (alias: string, hiddenSeries: Set<string>) => 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.width !== this.props.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, width } = this.props;
  128. const ticks = (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, onToggleSeries } = props;
  161. const { hiddenSeries } = state;
  162. // Deduplicate series as visibility tracks the alias property
  163. const oneSeriesVisible = hiddenSeries.size === new Set(data.map(d => d.alias)).size - 1;
  164. let nextHiddenSeries = new Set();
  165. if (exclusive) {
  166. if (hiddenSeries.has(series.alias) || !oneSeriesVisible) {
  167. nextHiddenSeries = new Set(data.filter(d => d.alias !== series.alias).map(d => d.alias));
  168. }
  169. } else {
  170. // Prune hidden series no longer part of those available from the most recent query
  171. const availableSeries = new Set(data.map(d => d.alias));
  172. nextHiddenSeries = intersect(new Set(hiddenSeries), availableSeries);
  173. if (nextHiddenSeries.has(series.alias)) {
  174. nextHiddenSeries.delete(series.alias);
  175. } else {
  176. nextHiddenSeries.add(series.alias);
  177. }
  178. }
  179. if (onToggleSeries) {
  180. onToggleSeries(series.alias, nextHiddenSeries);
  181. }
  182. return {
  183. hiddenSeries: nextHiddenSeries,
  184. };
  185. }, this.draw);
  186. };
  187. draw() {
  188. const { userOptions = {} } = this.props;
  189. const { hiddenSeries } = this.state;
  190. const data = this.getGraphData();
  191. const $el = $(`#${this.props.id}`);
  192. let series = [{ data: [[0, 0]] }];
  193. if (data && data.length > 0) {
  194. series = data.filter((ts: TimeSeries) => !hiddenSeries.has(ts.alias)).map((ts: TimeSeries) => ({
  195. color: ts.color,
  196. label: ts.label,
  197. data: ts.getFlotPairs('null'),
  198. }));
  199. }
  200. this.dynamicOptions = this.getDynamicOptions();
  201. const options = {
  202. ...FLOT_OPTIONS,
  203. ...this.dynamicOptions,
  204. ...userOptions,
  205. };
  206. $.plot($el, series, options);
  207. }
  208. render() {
  209. const { height = 100, id = 'graph' } = this.props;
  210. const { hiddenSeries } = this.state;
  211. const data = this.getGraphData();
  212. return (
  213. <>
  214. {this.props.data &&
  215. this.props.data.length > MAX_NUMBER_OF_TIME_SERIES &&
  216. !this.state.showAllTimeSeries && (
  217. <div className="time-series-disclaimer">
  218. <i className="fa fa-fw fa-warning disclaimer-icon" />
  219. {`Showing only ${MAX_NUMBER_OF_TIME_SERIES} time series. `}
  220. <span className="show-all-time-series" onClick={this.onShowAllTimeSeries}>{`Show all ${
  221. this.props.data.length
  222. }`}</span>
  223. </div>
  224. )}
  225. <div id={id} className="explore-graph" style={{ height }} />
  226. <Legend data={data} hiddenSeries={hiddenSeries} onToggleSeries={this.onToggleSeries} />
  227. </>
  228. );
  229. }
  230. }
  231. export default Graph;