React-google-chart 不占用选项

React-google-chart not taking up the options

我正在使用 react-google-chart 以图形形式(条形图)显示我的数据,根据要求,我必须制作 dual-y-axis chart

我已经制作了那个图表,但问题是图表没有占用 options。它是一个 Bar 图表,当我将图表作为 ColumnChart 然后它采用选项但不呈现为 dual-y-axis-chart 并且如果我将 Bar 作为图表类型它是不选择选项

我错过了什么?这是我的代码:

import React from "react";
import ReactDOM from "react-dom";
import Chart from "react-google-charts";
const data = [
  ["Month", "CTC", "Gross Salary", "Variation of CTC", "Total No of Employes"],
  ["Jan", 35000, 27000, 10000, 3],
  ["feb", 30000, 24000, 8000, 4],
  ["Mar", 50000, 37000, 7000, 5],
  ["May", 20000, 17000, 5000, 6],
  ["June", 20000, 17000, 5000, 5],
  ["July", 20000, 17000, 5000, 10],
  ["August", 20000, 17000, 5000, 7],
  ["Sep", 20000, 17000, 5000, 5],
  ["Nov", 20000, 17000, 5000, 5],
  ["Dec", 20000, 17000, 5000, 9]
];
const options = {
  width: 900,
  title: "Nearby galaxies",
  curveType: "function",
  series: {
    3: {
      axis: "test"
    }
  },
  legend: { position: "bottom" }
};
class App extends React.Component {
  render() {
    return (
      <div className="App">
        <Chart
          chartType="Bar"
          width="100%"
          height="400px"
          data={data}
          options={options}
          legendToggle
        />
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

工作example

编辑

它甚至没有采取 chartEvent 无论如何我想实现这个东西,如果不是 react-google-chart 那么我对任何开源图表库开放的任何其他库。

这里重要的是他们对 Bar 图表的评论:

Note here we use Bar instead of BarChart to load the material design version

https://react-google-charts.com/bar-chart#right-y-axis

因此,选项有点不同。而不是将 title 传递给根对象,它应该在 chart.

options={{
  // Material chart options
  chart: {
    title: 'Population of Largest U.S. Cities',
    subtitle: 'Based on most recent and previous census data',
  },
}

但是,似乎它并不尊重所有选项(例如legend)。

关于 width,您将 width 属性设置为 100%,并在选项中设置为 900px。设置 width, usewidth` 属性。

示例:

const data = [
  ["Month", "CTC", "Gross Salary", "Variation of CTC", "Total No of Employes"],
  ["Jan", 35000, 27000, 10000, 3],
  ["feb", 30000, 24000, 8000, 4],
  ["Mar", 50000, 37000, 7000, 5],
  ["May", 20000, 17000, 5000, 6],
  ["June", 20000, 17000, 5000, 5],
  ["July", 20000, 17000, 5000, 10],
  ["August", 20000, 17000, 5000, 7],
  ["Sep", 20000, 17000, 5000, 5],
  ["Nov", 20000, 17000, 5000, 5],
  ["Dec", 20000, 17000, 5000, 9]
];
const options = {
  chart: {
    title: "Nearby galaxies",
    legend: { position: "bottom" }
  },
  curveType: "function",
  series: {
    3: {
      axis: "test"
    }
  }
};
class App extends React.Component {
  render() {
    return (
      <div className="App">
        <Chart
          chartType="Bar"
          width={900}
          height="400px"
          data={data}
          options={options}
          legendToggle
        />
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

https://codesandbox.io/s/react-google-charts-columnchart-with-click-handler-4dywj


您可以改用 ColumnChart。使用此图表,您将能够自定义图例位置等

const options = {
  title: "Nearby galaxies",
  legend: { position: "bottom" },
  curveType: "function",
};

https://codesandbox.io/s/react-google-charts-columnchart-with-click-handler-wltpt

我不确定 series 道具。我没有在文档中看到它。

optionsColumnChart 不同。

参见示例中的 classicOptions https://developers.google.com/chart/interactive/docs/gallery/columnchart#dual-y-charts

因此当您将选项替换为

时它会起作用
const options = {
  width: 900,
  title: "Nearby galaxies",
  curveType: "function",
  seriesType: "bars",
  series: {
    3: { targetAxisIndex: 1 }
  },
  vAxes: {
    0: { title: "primary" },
    1: { title: "secondary" }
  },
  legend: { position: "bottom" }
};

https://codesandbox.io/s/react-google-charts-columnchart-with-click-handler-y6mo6