原文地址: http://www.codedata.cn/hackne...html
在这个博客中,咱们将看到如何使用ReactJS和D3JS绘制简单的折线图。react
若是您对ReactJS不熟悉,请查看 官方ReactJS网页。您还能够经过步骤视频系列查看咱们的 Learn ReactJS。git
D3.js 是一个Javascript库,用于建立交互式动态可视化。github
让咱们一步一步看看如何将ReactJS与D3JS集成来绘制一些交互式可视化图。ajax
咱们将使用 的jsfiddle例如 从 ReactJS文档 开始。分叉JSFiddle的例子,你应该很好去。swift
咱们将使用 Cloudflare CDN的D3.js。将D3.js添加为外部资源,以下图所示,并将如下URL做为外部资源输入。数组
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.12/d3.js
[图片上传失败...(image-90660b-1519689857580)]dom
如今让咱们尝试使用D3.js绘制折线图。ide
咱们来建立一个Line
为所提供的数据点呈现线路径的组件。svg
const Line = React.createClass({ propTypes: { path: React.PropTypes.string.isRequired, stroke: React.PropTypes.string, fill: React.PropTypes.string, strokeWidth: React.PropTypes.number }, getDefaultProps() { return { stroke: 'blue', fill: 'none', strokeWidth: 3 }; }, render() { let { path, stroke, fill, strokeWidth } = this.props; return ( <path d={path} fill={fill} stroke={stroke} strokeWidth={strokeWidth} /> ); } });
在上面的代码中, Line
组件呈现 SVG路径。路径数据d
是使用D3路径函数生成的 。
让咱们建立另外一个组件DataSeries
,它将Line
为每一个提供的数据系列提供组件。这产生path
基于xScale
与yScale
用于绘制线图表生成。
const DataSeries = React.createClass({ propTypes: { colors: React.PropTypes.func, data: React.PropTypes.object, interpolationType: React.PropTypes.string, xScale: React.PropTypes.func, yScale: React.PropTypes.func }, getDefaultProps() { return { data: [], interpolationType: 'cardinal', colors: d3.scale.category10() }; }, render() { let { data, colors, xScale, yScale, interpolationType } = this.props; let line = d3.svg.line() .interpolate(interpolationType) .x((d) => { return xScale(d.x); }) .y((d) => { return yScale(d.y); }); let lines = data.points.map((series, id) => { return ( <Line path={line(series)} stroke={colors(id)} key={id} /> ); }); return ( <g> <g>{lines}</g> </g> ); } });
在上面的代码中, d3.svg.line 建立了一个新的行生成器,它将输入视为一个由两个元素组成的数组。
如今咱们将建立LineChart
部件,将计算xScale
,yScale
基于对数据和将使得DataSeries
经过传递xScale
,yScale
,data
(输入x,y值),宽度,高度的图表。
const LineChart = React.createClass({ propTypes: { width: React.PropTypes.number, height: React.PropTypes.number, data: React.PropTypes.object.isRequired }, getDefaultProps(){ return { width: 600, height: 300 } }, render() { let { width, height, data } = this.props; let xScale = d3.scale.ordinal() .domain(data.xValues) .rangePoints([0, width]); let yScale = d3.scale.linear() .range([height, 10]) .domain([data.yMin, data.yMax]); return ( <svg width={width} height={height}> <DataSeries xScale={xScale} yScale={yScale} data={data} width={width} height={height} /> </svg> ); } });
这里 d3.scale.ordinal 构造一个能够具备离散域的序数标度,而 d3.scale.linear则 构造一个 线性定量标度。
你能够在这里了解更多关于D3定量标度的知识 。
如今咱们须要LineDataSeries
用数据调用组件。
let data = { points: [ [ { x: 0, y: 20 }, { x: 1, y: 30 }, { x: 2, y: 10 }, { x: 3, y: 5 }, { x: 4, y: 8 }, { x: 5, y: 15 }, { x: 6, y: 10 } ] , [ { x: 0, y: 8 }, { x: 1, y: 5 }, { x: 2, y: 20 }, { x: 3, y: 12 }, { x: 4, y: 4 }, { x: 5, y: 6 }, { x: 6, y: 2 } ] , [ { x: 0, y: 0 }, { x: 1, y: 5 }, { x: 2, y: 8 }, { x: 3, y: 2 }, { x: 4, y: 6 }, { x: 5, y: 4 }, { x: 6, y: 2 } ] ], xValues: [0,1,2,3,4,5,6], yMin: 0, yMax: 30 }; ReactDOM.render( <LineChart data={data} width={600} height={300} />, document.getElementById('container') );
带有id的元素container
将被呈现的内容替换LineChart
。
若是咱们如今看看输出结果,咱们会看到曲线图如何绘制。
[图片上传失败...(image-645bfb-1519689857580)]
为了以模块化的方式构建复杂的可视化,咱们能够根据它们的优势和缺点,使用下面提到的开源库之一。
这里有两个流行的开源ReactJS + D3.JS项目。
优势
缺点
优势
缺点
下面是在帖子中构建的JSFiddle的最终工做示例。