Graph.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Libraries
  2. import $ from 'jquery';
  3. import React, { PureComponent } from 'react';
  4. import { withSize } from 'react-sizeme';
  5. import 'vendor/flot/jquery.flot';
  6. import 'vendor/flot/jquery.flot.time';
  7. // Types
  8. import { TimeRange, TimeSeriesVMs } from 'app/types';
  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. };
  55. interface GraphProps {
  56. timeSeries: TimeSeriesVMs;
  57. timeRange: TimeRange;
  58. size?: { width: number; height: number };
  59. }
  60. export class Graph extends PureComponent<GraphProps> {
  61. element: any;
  62. componentDidUpdate(prevProps: GraphProps) {
  63. if (
  64. prevProps.timeSeries !== this.props.timeSeries ||
  65. prevProps.timeRange !== this.props.timeRange ||
  66. prevProps.size !== this.props.size
  67. ) {
  68. this.draw();
  69. }
  70. }
  71. componentDidMount() {
  72. this.draw();
  73. }
  74. draw() {
  75. const { size, timeSeries, timeRange } = this.props;
  76. if (!size) {
  77. return;
  78. }
  79. const ticks = (size.width || 0) / 100;
  80. const min = timeRange.from.valueOf();
  81. const max = timeRange.to.valueOf();
  82. const dynamicOptions = {
  83. xaxis: {
  84. mode: 'time',
  85. min: min,
  86. max: max,
  87. label: 'Datetime',
  88. ticks: ticks,
  89. timeformat: time_format(ticks, min, max),
  90. },
  91. };
  92. const options = {
  93. ...FLOT_OPTIONS,
  94. ...dynamicOptions,
  95. };
  96. console.log('plot', timeSeries, options);
  97. $.plot(this.element, timeSeries, options);
  98. }
  99. render() {
  100. return (
  101. <div className="graph-panel">
  102. <div className="graph-panel__chart" ref={e => (this.element = e)} />
  103. </div>
  104. );
  105. }
  106. }
  107. export default withSize()(Graph);