Graph.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import $ from 'jquery';
  2. import React, { Component } from 'react';
  3. import moment from 'moment';
  4. import 'vendor/flot/jquery.flot';
  5. import 'vendor/flot/jquery.flot.time';
  6. import * as dateMath from 'app/core/utils/datemath';
  7. import TimeSeries from 'app/core/time_series2';
  8. import Legend from './Legend';
  9. // Copied from graph.ts
  10. function time_format(ticks, min, max) {
  11. if (min && max && ticks) {
  12. const range = max - min;
  13. const secPerTick = range / ticks / 1000;
  14. const oneDay = 86400000;
  15. const oneYear = 31536000000;
  16. if (secPerTick <= 45) {
  17. return '%H:%M:%S';
  18. }
  19. if (secPerTick <= 7200 || range <= oneDay) {
  20. return '%H:%M';
  21. }
  22. if (secPerTick <= 80000) {
  23. return '%m/%d %H:%M';
  24. }
  25. if (secPerTick <= 2419200 || range <= oneYear) {
  26. return '%m/%d';
  27. }
  28. return '%Y-%m';
  29. }
  30. return '%H:%M';
  31. }
  32. const FLOT_OPTIONS = {
  33. legend: {
  34. show: false,
  35. },
  36. series: {
  37. lines: {
  38. linewidth: 1,
  39. zero: false,
  40. },
  41. shadowSize: 0,
  42. },
  43. grid: {
  44. minBorderMargin: 0,
  45. markings: [],
  46. backgroundColor: null,
  47. borderWidth: 0,
  48. // hoverable: true,
  49. clickable: true,
  50. color: '#a1a1a1',
  51. margin: { left: 0, right: 0 },
  52. labelMarginX: 0,
  53. },
  54. // selection: {
  55. // mode: 'x',
  56. // color: '#666',
  57. // },
  58. // crosshair: {
  59. // mode: 'x',
  60. // },
  61. };
  62. class Graph extends Component<any, any> {
  63. componentDidMount() {
  64. this.draw();
  65. }
  66. componentDidUpdate(prevProps) {
  67. if (
  68. prevProps.data !== this.props.data ||
  69. prevProps.options !== this.props.options ||
  70. prevProps.split !== this.props.split ||
  71. prevProps.height !== this.props.height
  72. ) {
  73. this.draw();
  74. }
  75. }
  76. draw() {
  77. const { data, options: userOptions } = this.props;
  78. const $el = $(`#${this.props.id}`);
  79. if (!data) {
  80. $el.empty();
  81. return;
  82. }
  83. const series = data.map((ts: TimeSeries) => ({
  84. color: ts.color,
  85. label: ts.label,
  86. data: ts.getFlotPairs('null'),
  87. }));
  88. const ticks = $el.width() / 100;
  89. let { from, to } = userOptions.range;
  90. if (!moment.isMoment(from)) {
  91. from = dateMath.parse(from, false);
  92. }
  93. if (!moment.isMoment(to)) {
  94. to = dateMath.parse(to, true);
  95. }
  96. const min = from.valueOf();
  97. const max = to.valueOf();
  98. const dynamicOptions = {
  99. xaxis: {
  100. mode: 'time',
  101. min: min,
  102. max: max,
  103. label: 'Datetime',
  104. ticks: ticks,
  105. timeformat: time_format(ticks, min, max),
  106. },
  107. };
  108. const options = {
  109. ...FLOT_OPTIONS,
  110. ...dynamicOptions,
  111. ...userOptions,
  112. };
  113. $.plot($el, series, options);
  114. }
  115. render() {
  116. const { data, height, loading } = this.props;
  117. if (!loading && data && data.length === 0) {
  118. return (
  119. <div className="panel-container">
  120. <div className="muted m-a-1">The queries returned no time series to graph.</div>
  121. </div>
  122. );
  123. }
  124. return (
  125. <div className="panel-container">
  126. <div id={this.props.id} className="explore-graph" style={{ height }} />
  127. <Legend data={data} />
  128. </div>
  129. );
  130. }
  131. }
  132. export default Graph;