【发布时间】:2013-09-24 11:43:08
【问题描述】:
我需要使用 d3.js 为 3 个城市 3 年降雨量的一些分类数据制作散点图:
Year1 Year2 Year3
NY CA TX NY CA TX NY CA TX
17 20 15 23 10 11 14 15 17
16 18 12 15 21 22 20 18 19
13 22 16 17 25 18 17 25 18
19 18 13 16 21 20 22 15 16
(注意:这里的数据和情节不匹配,只是为了解释我的查询) 我对 d3.js 完全陌生。我尝试了一些关于简单散点图和条形图的教程,但我不知道如何显示像这样的分类图。
如果能帮助我入门,我们将不胜感激。
[编辑] 我重新排列了 tsv 文件中的数据,如下所示:
year state rain
1 NY 17
1 NY 16
1 NY 13
1 CA 20
1 TX 15
2 NY 23
3 CA 10
3 TX 14
3 NY 13
好像有问题。我得到“意外的值 NaN 解析 cx 属性”。和 cy 一样。知道如何解决吗?
var newArray = new Array();
// draw the scatterplot
svg.selectAll("dot") // provides a suitable grouping for the svg elements that will be added
.data(data) // associates the range of data to the group of elements
.enter().append("circle") // adds a circle for each data point
.attr("r", 5) // with a radius of 3.5 pixels
.attr("cx", function (d) {
newArray = data.filter( function (el) {
return el.category == "NY";
});
return xScale(newArray.rain);
}) // at an appropriate x coordinate
.attr("cy", function (d) {
return yScale(newArray.year);
}) // and an appropriate y coordinate
.style("fill", color(9));
【问题讨论】:
-
NVD3 scatterplot 可能是一个不错的起点。
-
@LarsKotthoff :我只能使用 d3。我更新了问题。你能看看吗?
-
NVD3 基于 D3。除非您向我们展示完整的代码,否则我无法真正帮助您。
-
@user1340852 你找到了一些方法吗?
标签: javascript d3.js scatter-plot