分组使用D3网格问题和控制台错误的条形图 [英] grouped Bar chart using D3 grid issue and console errors
问题描述
我使用D3创建了一个分组的条形图。我写了代码,但条形图没有正确显示,我无法弄清楚这个问题的原因。请参阅此帖中附加的代码。
I am creating a grouped bar chart using D3. I have written code but bar charts are not showing properly and I am unable to figure out the reason for the this issue. Please see the code attached in this post.
var margin = {
top: 20,
right: 30,
bottom: 30,
left: 40
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var z = d3.scale.category20c();
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S.%LZ");
var data = [{
"data": [
[
"2016-01-21T01:20:00.000Z",
1.41818181818182
],
[
"2016-01-21T02:28:00.000Z",
1.90661764705882
],
[
"2016-01-21T03:36:00.000Z",
1.66764705882353
],
[
"2016-01-21T04:44:00.000Z",
1.51691176470588
],
[
"2016-01-21T05:52:00.000Z",
1.40955882352941
],
[
"2016-01-21T07:00:00.000Z",
1.46323529411765
],
[
"2016-01-21T08:08:00.000Z",
1.48308823529412
],
[
"2016-01-21T09:16:00.000Z",
1.89384615384615
]
],
"label": "a"
}, {
"data": [
[
"2016-01-21T01:20:00.000Z",
4.98701298701299
],
[
"2016-01-21T02:28:00.000Z",
5.0
],
[
"2016-01-21T03:36:00.000Z",
4.94852941176471
],
[
"2016-01-21T04:44:00.000Z",
4.91176470588235
],
[
"2016-01-21T05:52:00.000Z",
4.81617647058824
],
[
"2016-01-21T07:00:00.000Z",
5.0
],
[
"2016-01-21T08:08:00.000Z",
4.94117647058824
],
[
"2016-01-21T09:16:00.000Z",
4.96969696969697
]
],
"label": "b"
}];
var x = d3.scale.ordinal().rangeRoundBands([0, width], .9);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x).tickSize(0)
.orient("bottom").innerTickSize(-height).outerTickSize(0)
.tickFormat(d3.time.format("%H:%M"));
var yAxis = d3.svg.axis()
.scale(y)
.orient("left").innerTickSize(-width).outerTickSize(0);
var ary = [];
data.forEach(function(d) {
ary.push(d.data);
});
x.domain(ary[0].map(function(d) {
return parseDate.parse(d[0]);
}));
y.domain([0, d3.max(d3.merge(ary), function(d) {
console.log(d.y0 + d.y); //This is NaN = Not a number
//return d.y0 + d.y;
return d[1]; //returns grid lines if that is what you want
})]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
var layer = svg.selectAll(".layer")
.data(data)
.enter().append("g")
.attr("class", "layer")
.style("fill", function(d, i) {
return z(i);
});
layer.selectAll("rect")
.data(function(d) {
return d.data;
})
.enter().append("rect")
.attr("x", function(d) {
return x(parseDate.parse(d[0]));
})
.attr("y", function(d) {
console.log(d[1]);
return y(d[1]); //Note this is returning data
})
.attr("height", function(d) {
return y(d[1]); //Note this is returning data
})
.attr("width", x.rangeBand() - 1);
text.inner-circle {
font-weight: 400;
font-size: 12px;
text-transform: uppercase;
}
text.inner-text {
font-weight: 400;
font-size: 36px;
font-family: 'Metric Regular', 'Metric';
text-align: center;
font-style: normal;
text-transform: uppercase;
}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 2;
shape-rendering: crispEdges;
}
.grid .tick {
stroke: lightgrey;
stroke-opacity: 0.7;
shape-rendering: crispEdges;
}
.grid path {
stroke-width: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
请帮助我弄清楚我做错了什么。 Plnkr.co
Please help me in figuring out what I am doing wrong. Plnkr.co
推荐答案
让你的网格显示。
y.domain([0, d3.max(d3.merge(ary), function(d) {
console.log(d.y0 + d.y); //This is NaN = Not a number
//return d.y0 + d.y;
return d[1]; //returns grid lines if that is what you want
})]);
也代码的这部分:
layer.selectAll("rect")
.data(function(d) {
return d.data;
})
.enter().append("rect")
.attr("x", function(d) {
return x(parseDate.parse(d[0]));
})
.attr("y", function(d) {
console.log(d[1]);
return y(d[1]); //Note this is returning data
})
.attr("height", function(d) {
return y(d[1]); //Note this is returning data
})
.attr("width", x.rangeBand() - 1);
通过上述修复代码,我看到了你的rect,而不是错误。但是,你有一些CSS修复要寻址,因为你的酒吧离开x轴。对于数据分组,还有相当多的代码缺失。
With the above fixes to your code, I am seeing your rects and not errors. However, you have some css fixes to address as your bars are off of the x axes. There is also quite a bit of code missing for the grouping of your data.
以下是示例的组合的条形图。在看这个例子,我看到你有一个方法来区分x0和x1(第一和第二组)。你需要将它添加到代码中以获取下一组矩形。
Here is an example of a grouped bar chart. In looking at this example I see that you do have a way of distinguishing an x0 and x1 (first and second group). You will need to add this into your code to get to the next group of rectangles.
这篇关于分组使用D3网格问题和控制台错误的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!