在Google地理图表中向下钻取

时间:2019-09-27 06:28:59

标签: javascript reactjs charts google-visualization drilldown

我已经在我的React应用程序中实现了Google地理图表, 现在,单击特定区域或国家/地区后,我想在视图中向下钻取到该特定区域。

我能想到的一件事是,每当触发单击事件时,都要更改“ region”参数的值。

我尝试了下面的代码,但是对于设置区域值的部分无效。

import React, { Component } from "react";
import { Chart } from "react-google-charts";

const data = [
  ["Region", "Health"],
  ["Canada", 400],
  ["United States", 700],
  ["United Kingdom", 400],
  ["Europe", 400],
  ["Vietnam", 500],
  ["Portugal", 300],
  ["Japan", 200],
  ["India", 100],
  ["Australia", 300]
];

class MapChart extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  render() {
    return (
      <div>
        <Chart
          chartEvents={[
            {
              eventName: "select",
              callback: ({ chartWrapper }) => {
                const chart = chartWrapper.getChart();
                const selection = chart.getSelection();
                if (selection.length === 0) return;
                const region = data[selection[0].row + 1];
                console.log(selection);
                alert("Selected : " + region);
                if (region[0] == "India") {
                  let drilldownValue = "IN";
                }
              }
            }
          ]}
          chartType="GeoChart"
          width="100%"
          position="relative"
          data={data}
          options={{
            // region: "150", // somehow set my drilldownValue parameter here
            colorAxis: { colors: ["#00b857", "#ffba00", "#d80000"] },
            datalessRegionColor: "#FFFFFF",
            defaultColor: "#FFFFFF",
            legend: "none",
            enableRegionInteractivity: true
          }}
        />
      </div>
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您需要在select事件中设置区域选项,
然后重新绘制图表。

eventName: "select",
callback: ({ chartWrapper }) => {
  const chart = chartWrapper.getChart();
  const selection = chart.getSelection();
  if (selection.length === 0) return;
  const region = data[selection[0].row + 1];
  console.log(selection);
  alert("Selected : " + region);
  if (region[0] == "India") {
    let drilldownValue = "IN";

    // set region option, draw chart
    chartWrapper.setOption('region', drilldownValue):
    chartWrapper.draw();
  }
}