Ody*_*3us 9 javascript linechart d3.js
我使用D3创建了一个小测试线图,但由于我对库很新,我不确定在图表中添加多行是最好的方法,目前我只有一行显示小提琴.
我想在图表上显示2行,但我不确定如何在没有复制粘贴代码的情况下实现这一点,我相信这将是非常低效的,因为我想根据用户选择定期更新/动画图形.
而不是这个,
var data = [12345,22345,32345,42345,52345,62345,72345,82345,92345,102345,112345,122345,132345,142345];
Run Code Online (Sandbox Code Playgroud)
我想展示这样的东西,
var data = [
[12345,42345,3234,22345,72345,62345,32345,92345,52345,22345], // line one
[1234,4234,3234,2234,7234,6234,3234,9234,5234,2234] // line two
];
Run Code Online (Sandbox Code Playgroud)
这可能吗?如果是这样,那么最好的方法是什么,以便我可以在需要时轻松更新/动画图形?
注意:我只是想学习并熟悉D3最佳实践和整个库.谢谢.
cmo*_*key 13
这是可能和合理的.在D3嵌套选择教程中有一个教程可以解决这个问题,该 教程 描述了数据的嵌套.
下面是我从你的小提琴中入侵以证明这一点的代码.
var data = [
[12345,42345,3234,22345,72345,62345,32345,92345,52345,22345],
[1234,4234,3234,2234,7234,6234,3234,9234,5234,2234]
];
var width = 625,
height = 350;
var x = d3.scale.linear()
.domain([0,data[0].length]) // Hack. Computing x-domain from 1st array
.range([0, width]);
var y = d3.scale.linear()
.domain([0,d3.max(data[0])]) // Hack. Computing y-domain from 1st array
.range([height, 0]);
var line = d3.svg.line()
.x(function(d,i) { return x(i); })
.y(function(d) { return y(d); });
var area = d3.svg.area()
.x(line.x())
.y1(line.y())
.y0(y(0));
var svg = d3.select("body").append("svg")
//.datum(data)
.attr("width", width)
.attr("height", height)
//.append("g");
var lines = svg.selectAll( "g" )
.data( data ); // The data here is two arrays
// for each array, create a 'g' line container
var aLineContainer = lines
.enter().append("g");
/*svg.append("path")
.attr("class", "area")
.attr("d", area);*/
aLineContainer.append("path")
.attr("class", "area")
.attr("d", area);
/*svg.append("path")
.attr("class", "line")
.attr("d", line);*/
aLineContainer.append("path")
.attr("class", "line")
.attr("d", line);
/*svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("cx", line.x())
.attr("cy", line.y())
.attr("r", 3.5);*/
// Access the nested data, which are values within the array here
aLineContainer.selectAll(".dot")
.data( function( d, i ) { return d; } ) // This is the nested data call
.enter()
.append("circle")
.attr("class", "dot")
.attr("cx", line.x())
.attr("cy", line.y())
.attr("r", 3.5);
?
Run Code Online (Sandbox Code Playgroud)
一个缺点是我计算了第一个数组的x和y轴的域,这是一个黑客,但与你的问题完全无关.

| 归档时间: |
|
| 查看次数: |
6542 次 |
| 最近记录: |