Morris.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* global Morris */
  2. import React, { Component } from 'react';
  3. import PropTypes from 'prop-types';
  4. /**
  5. * Wrapper for morris chart plugin
  6. */
  7. class MorrisChart extends Component {
  8. static propTypes = {
  9. /** id of the container element */
  10. id: PropTypes.string.isRequired,
  11. /** data to display */
  12. data: PropTypes.array.isRequired,
  13. /** morris option object */
  14. options: PropTypes.object.isRequired,
  15. /** chart type */
  16. type: PropTypes.oneOf(['Line', 'Area', 'Donut', 'Bar']).isRequired
  17. };
  18. componentDidMount() {
  19. // Morris.js
  20. require('morris.js.so/morris.js');
  21. require('morris.js.so/morris.css');
  22. window.requestAnimationFrame(() => this.drawChart());
  23. }
  24. drawChart() {
  25. const element = { element: this.props.id };
  26. const data = { data: this.props.data };
  27. this.chart = new Morris[this.props.type]({
  28. ...element,
  29. ...data,
  30. ...this.props.options
  31. });
  32. }
  33. render() {
  34. return <div id={this.props.id} />;
  35. }
  36. }
  37. export default MorrisChart;