将Highcharts地图与react组件集成

时间:2019-02-28 08:01:48

标签: javascript reactjs highcharts react-highcharts

我正在尝试将highcarts集成到react组件中。

这是我的react组件代码

import * as Highcharts from 'highcharts/highmaps'
class RealTime extends Component {
  componentDidMount() {
      $.getJSON(     'https://cdn.jsdelivr.net/gh/highcharts/highcharts@v7.0.0/samples/data/world-population-density.json',
        function(data) {
          $.each(data, function() {
            this.value = this.value < 1 ? 1 : this.value
          })
          Highcharts.mapChart('world_map', {
            chart: {
              map: 'custom/world'
            },
            title: {
              text: 'Fixed tooltip with HTML'
            },
            legend: {
              title: {
                text: 'Population density per km²',
                style: {
                  color:
                    (Highcharts.theme && Highcharts.theme.textColor) || 'black'
                }
              }
            },
            mapNavigation: {
              enabled: true,
              buttonOptions: {
                verticalAlign: 'bottom'
              }
            }, 
            series: [
              {
                data: data,
                mapData: Highcharts.maps['custom/world'],
                joinBy: ['iso-a3', 'code3'],
                name: 'Population density',
                states: {
                  hover: {
                    color: '#a4edba'
                  }
                }
              }
            ]
          })
        }
      )
  }

  render() {
    return (
      <div>
        <div className="map_bg" id="world_map"/>
      </div>
    )
  }
}

但是上面的代码没有显示map或我的react组件中的任何错误。谁能帮帮我吗 我在这里想念什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

您需要添加和导入word.js脚本:

import Highcharts from "highcharts/highmaps";
import customWord from "./word.js";

实时演示:https://codesandbox.io/s/v0x5zx6q05

此外,我建议您使用highcharts-react-official包装器:https://www.npmjs.com/package/highcharts-react-official