Graph.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. 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?: string; // e.g., '200px'
  70. id?: string;
  71. range: RawTimeRange;
  72. split?: boolean;
  73. size?: { width: number; height: number };
  74. userOptions?: any;
  75. onChangeTime?: (range: RawTimeRange) => void;
  76. }
  77. interface GraphState {
  78. showAllTimeSeries: boolean;
  79. }
  80. export class Graph extends PureComponent<GraphProps, GraphState> {
  81. $el: any;
  82. state = {
  83. showAllTimeSeries: false,
  84. };
  85. getGraphData() {
  86. const { data } = this.props;
  87. return this.state.showAllTimeSeries ? data : data.slice(0, MAX_NUMBER_OF_TIME_SERIES);
  88. }
  89. componentDidMount() {
  90. this.draw();
  91. this.$el = $(`#${this.props.id}`);
  92. this.$el.bind('plotselected', this.onPlotSelected);
  93. }
  94. componentDidUpdate(prevProps: GraphProps) {
  95. if (
  96. prevProps.data !== this.props.data ||
  97. prevProps.range !== this.props.range ||
  98. prevProps.split !== this.props.split ||
  99. prevProps.height !== this.props.height ||
  100. (prevProps.size && prevProps.size.width !== this.props.size.width)
  101. ) {
  102. this.draw();
  103. }
  104. }
  105. componentWillUnmount() {
  106. this.$el.unbind('plotselected', this.onPlotSelected);
  107. }
  108. onPlotSelected = (event, ranges) => {
  109. if (this.props.onChangeTime) {
  110. const range = {
  111. from: moment(ranges.xaxis.from),
  112. to: moment(ranges.xaxis.to),
  113. };
  114. this.props.onChangeTime(range);
  115. }
  116. };
  117. onShowAllTimeSeries = () => {
  118. this.setState(
  119. {
  120. showAllTimeSeries: true,
  121. },
  122. this.draw
  123. );
  124. };
  125. draw() {
  126. const { range, size, userOptions = {} } = this.props;
  127. const data = this.getGraphData();
  128. const $el = $(`#${this.props.id}`);
  129. let series = [{ data: [[0, 0]] }];
  130. if (data && data.length > 0) {
  131. series = data.map((ts: TimeSeries) => ({
  132. color: ts.color,
  133. label: ts.label,
  134. data: ts.getFlotPairs('null'),
  135. }));
  136. }
  137. const ticks = (size.width || 0) / 100;
  138. let { from, to } = range;
  139. if (!moment.isMoment(from)) {
  140. from = dateMath.parse(from, false);
  141. }
  142. if (!moment.isMoment(to)) {
  143. to = dateMath.parse(to, true);
  144. }
  145. const min = from.valueOf();
  146. const max = to.valueOf();
  147. const dynamicOptions = {
  148. xaxis: {
  149. mode: 'time',
  150. min: min,
  151. max: max,
  152. label: 'Datetime',
  153. ticks: ticks,
  154. timezone: 'browser',
  155. timeformat: time_format(ticks, min, max),
  156. },
  157. };
  158. const options = {
  159. ...FLOT_OPTIONS,
  160. ...dynamicOptions,
  161. ...userOptions,
  162. };
  163. $.plot($el, series, options);
  164. }
  165. render() {
  166. const { height = '100px', id = 'graph' } = this.props;
  167. const data = this.getGraphData();
  168. return (
  169. <>
  170. {this.props.data &&
  171. this.props.data.length > MAX_NUMBER_OF_TIME_SERIES &&
  172. !this.state.showAllTimeSeries && (
  173. <div className="time-series-disclaimer">
  174. <i className="fa fa-fw fa-warning disclaimer-icon" />
  175. {`Showing only ${MAX_NUMBER_OF_TIME_SERIES} time series. `}
  176. <span className="show-all-time-series" onClick={this.onShowAllTimeSeries}>{`Show all ${
  177. this.props.data.length
  178. }`}</span>
  179. </div>
  180. )}
  181. <div id={id} className="explore-graph" style={{ height }} />
  182. <Legend data={data} />
  183. </>
  184. );
  185. }
  186. }
  187. export default withSize()(Graph);