D3js won't使用数据数组绘制圆
d3js won't draw circles using data array
我有一个d3js SVG绘图,我做的如下:
d是一个数组,包含像
这样的元素 var d=[{"x":370,"y":40,"label":"Label 1"},
{"x":370,"y":150,"label":"Label 2"} ];
和d3的代码是这样的
// here el is my HTML element and width/height are sizes
var svg=d3.select(el).append('svg')
svg.attr({width: width, height: height});
// this works just fine
svg.append("circle") // attach a circle
.attr("cx", 200) // position the x-center
.attr("cy", 100) // position the y-center
.attr("r", 50);
// now trying to plot that 'd' array
svg.data(d)
.enter().append("circle")
.attr("cy", function (d) {
return d.y
})
.attr("cx", function (d) {
return d.x
})
.attr("r", 10);
后一条指令不会生成SVG元素。也没有显示错误。"cy"/"cx"中的函数被调用为ok。
不幸的是,我的d3有点生锈了,但你似乎错过了一个selectAll
。试试下面的命令:
svg.selectAll("*").data(d)
.enter().append("circle")
.attr("cy", function (d) {
return d.y
})
.attr("cx", function (d) {
return d.x
})
.attr("r", 10);
查看这里的jsFiddle,看看它是如何工作的;据我所知,selectAll
是用来过滤掉任何可能已经存在的值。即使使用"*"
作为CSS选择器,也没有值匹配,因此使用d
中的所有项。
相关文章:
- 使用数据数组创建多个类似组件
- WebAudio API数据数组大小
- Jquery 循环一次或在数据数组中显示一次数据
- Cakephp 访问不在模型数据数组上的输入
- 使用来自 Ajax 响应的 JSON 数据数组
- Angular JS(离子)数据数组到$scope元素
- handontable:在不更改数据数组/对象的情况下隐藏某些列
- 如何使用for循环语句通过Ajax发送大数据数组
- 压缩 JS 中的数据数组
- 传递的参数D3提示回调是完整的数据数组
- 如何在数据数组的每次迭代中创建一个内容为“i+1”的新 HTML 元素
- 在 laravel 4 上使用循环将数据数组存储到单个数组中
- 为什么它说输入数据数组的格式不正确 jqchart.
- Javascript - 使用变量 RegExp 匹配数据数组中的多个关键字
- 尝试将数据数组设置为从视图调用函数后$scope
- 如何在 PHP 中创建 jqplot 数据数组
- 如何使用该数据数组从左向右移动画布
- 如何根据数字列从 Parse 中对数据数组进行排序
- 为什么 php json 响应不会将数据数组发送回 ajax jquery 请求
- 如何将内容添加到 JQuery Flot 图表数据数组工具提示