VectorMap.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import $ from 'jquery';
  4. import './vector-map.scss';
  5. /** Wrapper component for jquery-vectormap plugin */
  6. class VectorMap extends Component {
  7. static propTypes = {
  8. /** series entry of options object */
  9. series: PropTypes.object.isRequired,
  10. /** markers entry of options object */
  11. markers: PropTypes.array.isRequired,
  12. /** jvectormap options object */
  13. options: PropTypes.object.isRequired,
  14. /** height of the container element */
  15. height: PropTypes.string
  16. }
  17. static defaultProps = {
  18. height: '300px'
  19. }
  20. componentDidMount() {
  21. // jquery Vector Map
  22. require('ika.jvectormap/jquery-jvectormap-1.2.2.min.js');
  23. require('ika.jvectormap/jquery-jvectormap-world-mill-en.js');
  24. require('ika.jvectormap/jquery-jvectormap-us-mill-en.js');
  25. require('ika.jvectormap/jquery-jvectormap-1.2.2.css');
  26. window.requestAnimationFrame(() => this.drawMap());
  27. }
  28. drawMap() {
  29. this.props.options.markers = this.props.markers;
  30. this.props.options.series = this.props.series;
  31. $(this.mapElement).vectorMap(this.props.options);
  32. }
  33. componentWillUnmount() {
  34. const map = $(this.mapElement).vectorMap('get', 'mapObject');
  35. map.remove()
  36. }
  37. setRef = node => this.mapElement = node
  38. render() {
  39. return (
  40. <div ref={this.setRef} style={{height: this.props.height}}/>
  41. )
  42. }
  43. }
  44. export default VectorMap;