[英]Display x axis properly on stacked bar chart
Hey so I'm having difficulty with the positioning of my stacked bar chart. 嘿,我在放置堆叠条形图时遇到困难。 It's showing up, I'm just having difficulty declaring it's x axis.
它正在显示,我只是很难宣布它是x轴。 Here is the jsfiddle: http://jsfiddle.net/E2HST/
这是jsfiddle: http : //jsfiddle.net/E2HST/
var xTimeScale = d3.time.scale().
domain([new Date(data[0].date), d3.time.day.offset(new Date(data[data.length - 1].date), 1)])
.range([0, width]);
is obviously part of the problem, I pulled code and have unfortunately fallen into the trap of not fully understanding it. 这显然是问题的一部分,我拉了代码,不幸地陷入了无法完全理解它的陷阱。
var bars = svg.selectAll(".bar")
.data(data).enter()
.append("g")
.attr("class","bar")
.attr("transform", function(d){
return "translate("+xTimeScale(d.date)+",0)"
})
I've tried swapping in d.year for d.date seeing as there is no more d.date but it's throwing up errors. 我已经尝试在d.year中将d.date交换为d.date,因为没有更多的d.date,但是它抛出了错误。 Any help would be greatly appreciated.
任何帮助将不胜感激。
The simple answer is that the objects in your data
array do not have a date
key, so your xTimeScale
domain is undefined. 简单的答案是
data
数组中的对象没有date
键,因此xTimeScale
域是未定义的。 An easy way to correct this would be to create this key for each data item: 纠正此问题的简单方法是为每个数据项创建此密钥:
data.forEach( function(d) { d.date = new Date(d.year, 0, 1); });
Which creates the date as January 1st of the year
variable. 它将日期创建为
year
1月1 year
。 This basically solves your x-axis problem. 这基本上可以解决您的x轴问题。
I would, however, suggest that your data would be better suited to using an ordinal scale , since you only have yearly data. 但是,我建议您的数据更适合使用序数刻度 ,因为您只有年度数据。
A couple of other small things: 其他一些小问题:
.axis
elements, to improve readability .axis
元素添加CSS样式,以提高可读性 An updated fiddle with these slight changes is at http://jsfiddle.net/E2HST/1/ 这些细微变化的更新小提琴位于http://jsfiddle.net/E2HST/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.