简体   繁体   English

基本d3:水平条形图

[英]Basic d3: Horizontal Bar Chart

Having trouble with a simple bar chart here, can't find the problem. 在这里使用简单的条形图遇到麻烦,找不到问题。

See this fiddle http://jsfiddle.net/nn3yex69/ 看到这个小提琴http://jsfiddle.net/nn3yex69/

Notice two main problems: 注意两个主要问题:

1) The number of bars is not matching the number of elements in dataset . 1)条形数量与dataset的元素数量不匹配。

2) The widths of the largest rect s extend to the full size of the svg despite the desired padding in xScale . 2)最大的rect的宽度扩展到svg的整个大小,尽管在xScale需要padding

Would really appreciate any help here, I'm stumped. 我很感激这里的任何帮助。

1) You are selecting all rect elements in the SVG. 1)您正在选择SVG中的所有rect元素。 You already have one rect element: the background! 您已经有一个rect元素:背景! Try changing line 36 to svg.selectAll("rect.bar") . 尝试将第36行更改为svg.selectAll("rect.bar")

2) There's padding on the left and the right, so on line 17 use .range([padding,w - (2*padding)]); 2)左右都有填充,因此在第17行使用.range([padding,w - (2*padding)]);

You were appending a 'rect' and so on the selectAll('rect'), the first element from the dataset was being overridden by the attributes of the first appended 'rect'. 您要附加一个“ rect”,依此类推,例如selectAll('rect'),数据集中的第一个元素被第一个附加的“ rect”的属性覆盖。

Removing the following will do the trick: 删除以下内容将达到目的:

svg.append("rect")
    .attr("height", h)
    .attr("width", w)
    .attr("stroke", "black")
    .attr("fill", "#ccc")
    .attr("stroke-width", 2)  

Updated fiddle: http://jsfiddle.net/nn3yex69/1/ 更新的提琴: http : //jsfiddle.net/nn3yex69/1/

I would create a div and style with css to be the containing div to hold your bars. 我将使用css创建一个div和样式,使其成为容纳栏的包含div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM