我有n列数据,我想从D3散点图。它需要2列来制作一个绘图,因此n列将以相同代码的2次n置换结束,我想从中绘制一个绘图函数。问题是我不知道如何在D3时尚中设置变量。数据和代码片段的例子如下。我很欣赏任何提示。谢谢。特设D3功能
// Potentially, there are a lot more columns.
var data = [
{"col1": 34, "col2": 54, "col3": 345, "col4": 35, "col5": 52},
{"col1": 75, "col2": 98, "col3": 917, "col4": 03, "col5": 47},
{"col1": 63, "col2": 23, "col3": 236, "col4": 34, "col5": 78},
{"col1": 23, "col2": 38, "col3": 198, "col4": 12, "col5": 18},
{"col1": 57, "col2": 48, "col3": 274, "col4": 67, "col5": 39},
{"col1": 65, "col2": 12, "col3": 381, "col4": 27, "col5": 45}
];
// The code is long
// I just list here the parts that are involved
// How to replace FOO and BAR with a pair of parameters "col1", "col2", etc ...
var x = d3.scale.linear().range([0, width]),
y = d3.scale.linear().range([height, 0]);
x.domain(d3.extent(data, function(q) {return q.FOO;}));
y.domain(d3.extent(data, function(q) {return q.BAR;}));
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("r", 4)
.attr("cx", function(d) {return x(d.FOO);})
.attr("cy", function(d) {return y(d.BAR);})
.style('fill', 'blue');
编辑: 我参加了一个暗示从mgold,使功能与原始数据集重建的两列数据集,而不是绘制散点图的功能。它可能不够高雅,但可以工作。 :) 谢谢!
function get2Columns(foo, bar) {
var tempArr = [];
for (var i = 0; i < data.length; i++) {
tempArr.push({"FOO":data[i][foo],"BAR":data[i][bar]});
}
return tempArr;
}
data = get2Columns('col1', 'col2');
我把减少数据集,以2列数据集的提示。查看编辑。谢谢! – ngungo 2014-10-19 19:18:24
很高兴我能帮忙;感谢接受。 – mgold 2014-10-19 19:44:41