Flot.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import $ from 'jquery';
  2. import React, { Component } from 'react';
  3. import PropTypes from 'prop-types';
  4. import deepEqual from 'deep-equal';
  5. import './chart-flot.scss';
  6. /**
  7. * Wrapper component for jquery-flot plugin
  8. */
  9. class FlotChart extends Component {
  10. static propTypes = {
  11. /** data to display */
  12. data: PropTypes.array.isRequired,
  13. /** flot options object */
  14. options: PropTypes.object.isRequired,
  15. /** height of the container element */
  16. height: PropTypes.string,
  17. /** width of the container element */
  18. width: PropTypes.string
  19. }
  20. static defaultProps = {
  21. height: null,
  22. width: '100%'
  23. }
  24. componentDidMount() {
  25. // Flot Charts
  26. require('flot/jquery.flot.js');
  27. require('flot/jquery.flot.categories.js');
  28. require('flot/jquery.flot.pie.js');
  29. require('flot/jquery.flot.resize.js');
  30. require('flot/jquery.flot.time.js');
  31. require('jquery.flot.spline/jquery.flot.spline.js');
  32. require('jquery.flot.tooltip/js/jquery.flot.tooltip.min.js');
  33. setTimeout(() => {
  34. this.drawChart();
  35. }, 100);
  36. }
  37. componentDidUpdate(prevProps) {
  38. if (!deepEqual(prevProps.data, this.props.data) || !deepEqual(prevProps.options, this.props.options)) {
  39. this.drawChart();
  40. }
  41. }
  42. componentWillUnmount() {
  43. $(this.flotElement).data('plot').shutdown();
  44. }
  45. drawChart(nextProps) {
  46. const data = (nextProps && nextProps.data) || this.props.data;
  47. const options = (nextProps && nextProps.options) || this.props.options;
  48. $.plot(this.flotElement, data, options);
  49. }
  50. setRef = node => {
  51. this.flotElement = node;
  52. }
  53. render() {
  54. const style = {
  55. height: this.props.height,
  56. width: this.props.width
  57. };
  58. return (
  59. <div ref={this.setRef} style={style} {...this.props}/>
  60. );
  61. }
  62. }
  63. export default FlotChart;