How can i made a dot chart from d3.js template 'Reusable Charts' [duplicate]

荒凉一梦 提交于 2019-12-13 01:29:58

问题


I'm attempting to use the 'Reusable Charts' approach described by @mbostock, everything is fine when using a line (<path>). However, when I try to use dots (<circle>), only one element is added to the chart and it's not well formed (wrong cx and cy).

My attempted code is between lines 50-55 of the fiddle. I tried to append a circle to my svg for each data but it is not added.


回答1:


The first problem is the d3.svg.line generator takes an array as input, but a circle element only needs one value of this array. So first, you have to get the data binding right.

Next, you need to have the scales right. cx and cy can use the X and Y scale accessor functions. The radius also need a scale, because your xSpeed contains negative numbers and a radius can't be negative. So here is the fixed version: http://jsfiddle.net/christopheviau/2DDuH/6/

var speedScale = d3.scale.linear().domain(d3.extent(data.map(function(d, i){return d.xSpeed;}))).range([2, 10]);
            gEnter.selectAll('circle.dot')
                .data(function(d, i){return d})
                .enter().append("circle")
                .attr("class", "dot")
                .attr("cx", X)
                .attr("cy", Y)
                .attr("r", function(d, i){return speedScale(d.xSpeed)});


来源:https://stackoverflow.com/questions/16491586/how-can-i-made-a-dot-chart-from-d3-js-template-reusable-charts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!