【问题标题】:Dynamically update react-google-chart type动态更新 react-google-chart 类型
【发布时间】:2018-08-08 21:37:32
【问题描述】:

我想使用选择框来更改 react-google-chart 的图表类型。代码如下所示:

const GoogleGraph = ({chartType, handleChartTypeChange}) => (
        <section>
            <Selector
                chartType={chartType}
                handleChartTypeChange={handleChartTypeChange}
            />
            <Chart
                chartType={chartType}
                data={data}
                options={options}
                graphID={chartType}
                width="100%"
                height="10em"
                chartEvents={chartEvents}
            />
        </section>
    );

更改了“选择框”时,我将获得我期望的数据,就像我在调试器中看到的那样,ChartType Prop已从“ bar”更改为“线”,但是在渲染完成后,图表仍然是条形图。我没有收到错误消息。

是否可以动态更新chartType?

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

如果所有其他方法都失败了,您还可以将chartType 用作key,这将使React 丢弃当前组件并在chartType 更改时创建一个全新的组件。

const GoogleGraph = ({ chartType, handleChartTypeChange }) => (
  <section>
    <Selector
      chartType={chartType}
      handleChartTypeChange={handleChartTypeChange}
    />
    <Chart
      key={chartType}
      chartType={chartType}
      data={data}
      options={options}
      graphID={chartType}
      width="100%"
      height="10em"
      chartEvents={chartEvents}
    />
  </section>
);

【讨论】:

    【解决方案2】:

    更新为通用 HTML Select

    const GoogleGraph = ({ chartType, handleChartTypeChange, data, options }) => (
    	<section>
    		<select value={chartType} onChange={handleChartTypeChange}>
    			<option value="PieChart">PieChart</option>
    			<option value="BarChart">BarChart</option>
    		</select>
    		<Chart
    			key={chartType}
    			chartType={chartType}
    			width={'80vw'}
    			height={'80vh'}
    			loader={<div>Loading Chart</div>}
    			data={data}
    			options={options}
    			graphID={chartType}
    		/>
    	</section>
    );

    【讨论】:

      猜你喜欢
      • 2010-12-15
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 2021-01-20
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      相关资源
      最近更新 更多