Graph.tsx 2.4 KB

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