Graph.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. var range = max - min;
  13. var secPerTick = range / ticks / 1000;
  14. var oneDay = 86400000;
  15. var 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. if (!data) {
  79. return;
  80. }
  81. const series = data.map((ts: TimeSeries) => ({
  82. color: ts.color,
  83. label: ts.label,
  84. data: ts.getFlotPairs('null'),
  85. }));
  86. const $el = $(`#${this.props.id}`);
  87. const ticks = $el.width() / 100;
  88. let { from, to } = userOptions.range;
  89. if (!moment.isMoment(from)) {
  90. from = dateMath.parse(from, false);
  91. }
  92. if (!moment.isMoment(to)) {
  93. to = dateMath.parse(to, true);
  94. }
  95. const min = from.valueOf();
  96. const max = to.valueOf();
  97. const dynamicOptions = {
  98. xaxis: {
  99. mode: 'time',
  100. min: min,
  101. max: max,
  102. label: 'Datetime',
  103. ticks: ticks,
  104. timeformat: time_format(ticks, min, max),
  105. },
  106. };
  107. const options = {
  108. ...FLOT_OPTIONS,
  109. ...dynamicOptions,
  110. ...userOptions,
  111. };
  112. $.plot($el, series, options);
  113. }
  114. render() {
  115. const { data, height } = this.props;
  116. return (
  117. <div className="panel-container">
  118. <div id={this.props.id} className="explore-graph" style={{ height }} />
  119. <Legend data={data} />
  120. </div>
  121. );
  122. }
  123. }
  124. export default Graph;