Graph.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 * as dateMath from 'app/core/utils/datemath';
  8. import TimeSeries from 'app/core/time_series2';
  9. import Legend from './Legend';
  10. const MAX_NUMBER_OF_TIME_SERIES = 20;
  11. // Copied from graph.ts
  12. function time_format(ticks, min, max) {
  13. if (min && max && ticks) {
  14. const range = max - min;
  15. const secPerTick = range / ticks / 1000;
  16. const oneDay = 86400000;
  17. const oneYear = 31536000000;
  18. if (secPerTick <= 45) {
  19. return '%H:%M:%S';
  20. }
  21. if (secPerTick <= 7200 || range <= oneDay) {
  22. return '%H:%M';
  23. }
  24. if (secPerTick <= 80000) {
  25. return '%m/%d %H:%M';
  26. }
  27. if (secPerTick <= 2419200 || range <= oneYear) {
  28. return '%m/%d';
  29. }
  30. return '%Y-%m';
  31. }
  32. return '%H:%M';
  33. }
  34. const FLOT_OPTIONS = {
  35. legend: {
  36. show: false,
  37. },
  38. series: {
  39. lines: {
  40. linewidth: 1,
  41. zero: false,
  42. },
  43. shadowSize: 0,
  44. },
  45. grid: {
  46. minBorderMargin: 0,
  47. markings: [],
  48. backgroundColor: null,
  49. borderWidth: 0,
  50. // hoverable: true,
  51. clickable: true,
  52. color: '#a1a1a1',
  53. margin: { left: 0, right: 0 },
  54. labelMarginX: 0,
  55. },
  56. // selection: {
  57. // mode: 'x',
  58. // color: '#666',
  59. // },
  60. // crosshair: {
  61. // mode: 'x',
  62. // },
  63. };
  64. interface GraphProps {
  65. data: any[];
  66. height?: string; // e.g., '200px'
  67. id?: string;
  68. loading?: boolean;
  69. options: any;
  70. split?: boolean;
  71. size?: { width: number; height: number };
  72. }
  73. interface GraphState {
  74. showAllTimeSeries: boolean;
  75. }
  76. export class Graph extends PureComponent<GraphProps, GraphState> {
  77. state = {
  78. showAllTimeSeries: false,
  79. };
  80. getGraphData() {
  81. const { data } = this.props;
  82. return this.state.showAllTimeSeries ? data : data.slice(0, MAX_NUMBER_OF_TIME_SERIES);
  83. }
  84. componentDidMount() {
  85. this.draw();
  86. }
  87. componentDidUpdate(prevProps: GraphProps) {
  88. if (
  89. prevProps.data !== this.props.data ||
  90. prevProps.options !== this.props.options ||
  91. prevProps.split !== this.props.split ||
  92. prevProps.height !== this.props.height ||
  93. (prevProps.size && prevProps.size.width !== this.props.size.width)
  94. ) {
  95. this.draw();
  96. }
  97. }
  98. onShowAllTimeSeries = () => {
  99. this.setState(
  100. {
  101. showAllTimeSeries: true,
  102. },
  103. this.draw
  104. );
  105. };
  106. draw() {
  107. const { options: userOptions, size } = this.props;
  108. const data = this.getGraphData();
  109. const $el = $(`#${this.props.id}`);
  110. if (!data) {
  111. $el.empty();
  112. return;
  113. }
  114. const series = data.map((ts: TimeSeries) => ({
  115. color: ts.color,
  116. label: ts.label,
  117. data: ts.getFlotPairs('null'),
  118. }));
  119. const ticks = (size.width || 0) / 100;
  120. let { from, to } = userOptions.range;
  121. if (!moment.isMoment(from)) {
  122. from = dateMath.parse(from, false);
  123. }
  124. if (!moment.isMoment(to)) {
  125. to = dateMath.parse(to, true);
  126. }
  127. const min = from.valueOf();
  128. const max = to.valueOf();
  129. const dynamicOptions = {
  130. xaxis: {
  131. mode: 'time',
  132. min: min,
  133. max: max,
  134. label: 'Datetime',
  135. ticks: ticks,
  136. timeformat: time_format(ticks, min, max),
  137. },
  138. };
  139. const options = {
  140. ...FLOT_OPTIONS,
  141. ...dynamicOptions,
  142. ...userOptions,
  143. };
  144. $.plot($el, series, options);
  145. }
  146. render() {
  147. const { height = '100px', id = 'graph', loading = false } = this.props;
  148. const data = this.getGraphData();
  149. if (!loading && data.length === 0) {
  150. return (
  151. <div className="panel-container">
  152. <div className="muted m-a-1">The queries returned no time series to graph.</div>
  153. </div>
  154. );
  155. }
  156. return (
  157. <div>
  158. {this.props.data.length > MAX_NUMBER_OF_TIME_SERIES &&
  159. !this.state.showAllTimeSeries && (
  160. <div className="time-series-disclaimer">
  161. <i className="fa fa-fw fa-warning disclaimer-icon" />
  162. {`Showing only ${MAX_NUMBER_OF_TIME_SERIES} time series. `}
  163. <span className="show-all-time-series" onClick={this.onShowAllTimeSeries}>{`Show all ${
  164. this.props.data.length
  165. }`}</span>
  166. </div>
  167. )}
  168. <div className="panel-container">
  169. <div id={id} className="explore-graph" style={{ height }} />
  170. <Legend data={data} />
  171. </div>
  172. </div>
  173. );
  174. }
  175. }
  176. export default withSize()(Graph);