VectorMap.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. require('@/components/Maps/jquery-jvectormap.id.js');
  27. window.requestAnimationFrame(() => this.drawMap());
  28. }
  29. drawMap() {
  30. this.props.options.markers = this.props.markers;
  31. this.props.options.series = this.props.series;
  32. $(this.mapElement).vectorMap(this.props.options);
  33. }
  34. componentWillUnmount() {
  35. const map = $(this.mapElement).vectorMap('get', 'mapObject');
  36. map.remove()
  37. }
  38. setRef = node => this.mapElement = node
  39. render() {
  40. return (
  41. <div ref={this.setRef} style={{ height: this.props.height }} />
  42. )
  43. }
  44. }
  45. export default VectorMap;