将数据复制到 JavaScript D3 中的数组.js然后绘制散点图
copying data to an array in javascript d3.js and then drawing a scatterplot
我是javascript和d3的新手.js并试图理解散点图。我使用了这段代码,工作正常:
//Get the data
d3.tsv("graphdata.tsv", function (error, data) {
data.forEach(function (d) {
d.date = parseDate(d.date);
d.close = +d.close;
d.open = +d.open;
});
x.domain(d3.extent(data, function (d) { return d.date; }));
y.domain([0, d3.max(data, function (d) { return d.close; })]);
// draw the scatterplot
svg.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 8)
.attr("cx", function (d) { return x(d.date); })
.attr("cy", function (d) { return y(d.close); })
.style("fill", color(9));
但是当我将"data"的内容复制到"anArray",然后尝试在anArray中绘制数据时,我得到一个空的图形。
//Get the data
d3.tsv("graphdata.tsv", function (error, data) {
data.forEach(function (d) {
d.date = parseDate(d.date);
d.close = +d.close;
d.open = +d.open;
});
var anArray = new Array();
for (var i = 0; i < 26; i++)
anArray.push(data[i]);
x.domain(d3.extent(anArray, function (d) { return anArray.date; }));
y.domain([0, d3.max(anArray, function (d) { return anArray.close; })]);
// draw the scatterplot
svg.selectAll("dot")
.data(anArray)
.enter().append("circle")
.attr("r", 8)
.attr("cx", function (d) { return x(anArray.date); })
.attr("cy", function (d) { return y(anArray.close); })
.style("fill", color(9));
我想知道为什么会这样?如果我需要做这样的事情,还有别的方法吗?
自从我使用 d3 以来已经有一段时间了,但我认为问题就在这里:
x.domain(d3.extent(anArray, function (d) { return anArray.date; }));
y.domain([0, d3.max(anArray, function (d) { return anArray.close; })]);
和
.attr("cx", function (d) { return x(anArray.date); })
.attr("cy", function (d) { return y(anArray.close); })
您要求它返回anArray.date
并anArray.close
何时应该d.date
和d.close
。
尝试将其更改为:
x.domain(d3.extent(anArray, function (d) { return d.date; }));
和
.attr("cx", function (d) { return x(d.date); })
相关文章:
- JS数组-检查对象值是否重复
- 将JS数组附加到变量并显示它
- 正在尝试将JSON文件放入JS数组
- 删除JS数组中的最小数字
- 调用laravel{{action(Controller@method}}通过传递vue.js数组中的变量
- 如何将JS数组添加到Meteor自动表单条目中
- 了解JS数组和拼接
- JS数组称为“;name”;行为怪异
- 在js数组中不断循环并暂停
- 根据范围过滤角度.js数组
- 循环访问 JS 数组 + 数组属性
- 为什么当我在JS数组上调用.toString时得到“对象对象”
- Ember.js数组控制器计算属性的复选框
- 我的JS数组打印只显示结束值
- 如何在AngularJs中将json数组转换为js数组
- JS数组无理由地传递值更改
- 是否可以引用JS数组/对象中的另一个元素
- 将jQuery事件函数应用于新的Knockout.js数组元素
- 通过多列和多个值过滤angular js数组
- 在js数组中查找下一个和上一个键