Alg*_*kas 3 javascript d3.js reactjs anychart redux
我想将AnyChart库与我当前的React,Redux堆栈一起使用.有没有办法将AnyCharts包装在像FauxDom这样的东西中.如果您可以向我提供示例代码段或方向指向那样做的库,那就太好了.
至于客户端React渲染,肯定可以使用包装在React组件中的AnyChart.
您可以编写一个包装AnyChart组件,以这种方式接受数据数组和标题作为道具(饼图包装器的示例):
import React, { Component } from 'react';
class AnyChart extends Component {
  constructor(props) {
    super(props);
  }
  // Important, otherwise the re-render
  // will destroy your chart
  shouldComponentUpdate() {
    return false;
  }
  componentDidMount() {
    // Get data from the props
    let data = this.props.data;
    let title = this.props.title;
    // Let's draw the chart
    anychart.onDocumentReady(function() {
      let chart = anychart.pie(data);
      chart.container('chart');
      chart.title(title);
      chart.draw();
    });
  }
  render() {
    return (
      <div id="chart" style={{height: '400px'}}/>
    );
  }
}
export default AnyChart;
然后,您可以使用另一个反应组件中的此组件.例如,从功能组件:
import React from 'react';
import AnyChart from './AnyChart';
const AnyChartTest = (props) => {
  const data = [
    ['React', 5200],
    ['ES6', 2820],
    ['Redux', 2650],
    ['Redux Ducks', 670]
  ];
  return (
    <div>
      <h1>AnyChart Test</h1>
      <AnyChart data={data} title="Technology Adoption" />
    </div>
  );
};
export default AnyChartTest;
如果您不需要使用道具中的新数据动态更新图表,则此方法很有效.如果是这种情况,您应该ComponentWillReceiveProps在AnyChart包装器组件中添加一个处理程序,您应该将新数据从props传递到图表并强制重绘.
Stephen Grider制作了关于第三方组件集成的非常好的视频:https: //www.youtube.com/watch?v = GWWjMHDKSfU
我希望我帮助过你,至少对于客户端渲染.
Matteo Frana
| 归档时间: | 
 | 
| 查看次数: | 590 次 | 
| 最近记录: |