Graph.tsx 5.0 KB

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