我想将AnyChart库与我当前的 React、Redux 堆栈一起使用。有没有办法将 AnyCharts 包装在FauxDom 之类的东西中。如果您可以向我提供示例代码片段或指向执行此操作的库的说明,那就太好了。
问问题
877 次
1 回答
9
至于客户端 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 包装器组件中添加一个处理程序,您应该在其中将新数据从道具传递到图表并强制重绘。
Stephen Grider 制作了一个关于第三方组件集成的非常好的视频: https ://www.youtube.com/watch?v=GWVjMHDKSfU
我希望我对你有所帮助,至少在客户端渲染方面。
马泰奥·弗拉纳
于 2016-08-15T17:52:00.750 回答