Graph.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Libraries
  2. import $ from 'jquery';
  3. import React, { PureComponent } from 'react';
  4. import 'vendor/flot/jquery.flot';
  5. import 'vendor/flot/jquery.flot.time';
  6. // Types
  7. import { TimeRange, TimeSeriesVMs } from 'app/types';
  8. interface GraphProps {
  9. timeSeries: TimeSeriesVMs;
  10. timeRange: TimeRange;
  11. showLines?: boolean;
  12. showPoints?: boolean;
  13. showBars?: boolean;
  14. width: number;
  15. height: number;
  16. }
  17. export class Graph extends PureComponent<GraphProps> {
  18. static defaultProps = {
  19. showLines: true,
  20. showPoints: false,
  21. showBars: false,
  22. };
  23. element: HTMLElement;
  24. componentDidUpdate() {
  25. this.draw();
  26. }
  27. componentDidMount() {
  28. this.draw();
  29. }
  30. draw() {
  31. const { width, timeSeries, timeRange, showLines, showBars, showPoints } = this.props;
  32. if (!width) {
  33. return;
  34. }
  35. const ticks = width / 100;
  36. const min = timeRange.from.valueOf();
  37. const max = timeRange.to.valueOf();
  38. const flotOptions = {
  39. legend: {
  40. show: false,
  41. },
  42. series: {
  43. lines: {
  44. show: showLines,
  45. linewidth: 1,
  46. zero: false,
  47. },
  48. points: {
  49. show: showPoints,
  50. fill: 1,
  51. fillColor: false,
  52. radius: 2,
  53. },
  54. bars: {
  55. show: showBars,
  56. fill: 1,
  57. barWidth: 1,
  58. zero: false,
  59. lineWidth: 0,
  60. },
  61. shadowSize: 0,
  62. },
  63. xaxis: {
  64. mode: 'time',
  65. min: min,
  66. max: max,
  67. label: 'Datetime',
  68. ticks: ticks,
  69. timeformat: time_format(ticks, min, max),
  70. },
  71. grid: {
  72. minBorderMargin: 0,
  73. markings: [],
  74. backgroundColor: null,
  75. borderWidth: 0,
  76. // hoverable: true,
  77. clickable: true,
  78. color: '#a1a1a1',
  79. margin: { left: 0, right: 0 },
  80. labelMarginX: 0,
  81. },
  82. };
  83. try {
  84. console.log('Graph render');
  85. $.plot(this.element, timeSeries, flotOptions);
  86. } catch (err) {
  87. console.log('Graph rendering error', err, flotOptions, timeSeries);
  88. }
  89. }
  90. render() {
  91. return (
  92. <div className="graph-panel">
  93. <div className="graph-panel__chart" ref={e => (this.element = e)} />
  94. </div>
  95. );
  96. }
  97. }
  98. // Copied from graph.ts
  99. function time_format(ticks, min, max) {
  100. if (min && max && ticks) {
  101. const range = max - min;
  102. const secPerTick = range / ticks / 1000;
  103. const oneDay = 86400000;
  104. const oneYear = 31536000000;
  105. if (secPerTick <= 45) {
  106. return '%H:%M:%S';
  107. }
  108. if (secPerTick <= 7200 || range <= oneDay) {
  109. return '%H:%M';
  110. }
  111. if (secPerTick <= 80000) {
  112. return '%m/%d %H:%M';
  113. }
  114. if (secPerTick <= 2419200 || range <= oneYear) {
  115. return '%m/%d';
  116. }
  117. return '%Y-%m';
  118. }
  119. return '%H:%M';
  120. }
  121. export default Graph;