Reputation: 369
I'm attempting to build an error chart using D3 that looks like this:
The issue I'm having is that maximum error square (the square at the top) isn't showing up. Here's what I have so far:
var m = { top: 10, right: 10, bottom: 10, left: 10 }
var h = 400 - m.top - m.bottom;
var w = 500 - m.left - m.right;
var r = 5;
var data = [
{ x: 0, y: 12, yMin: 10, yMax: 20 },
{ x: 1, y: 25, yMin: 17, yMax: 30 },
{ x: 2, y: 15, yMin: 12, yMax: 17 },
{ x: 3, y: 10, yMin: 5, yMax: 14 },
{ x: 4, y: 5, yMin: 1, yMax: 6 }
]
var xScale = d3.scale.linear()
.domain([0, d3.max(data, function(d) { return d.x } )])
.range([0, w]);
var yScale = d3.scale.linear()
.domain([0, d3.max(data, function(d) { return d.yMax } )])
.range([h, 0]);
var vis = d3.select('body').append('svg')
.attr({
height : h + m.top + m.bottom,
width : w + m.left + m.right,
})
.append('g')
.attr({
transform : 'translate(' + m.left + ',' + m.top + ')'
})
var scatterPlot = vis.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr({
x : function(d) { return xScale(d.x) - r; },
y : function(d) { return yScale(d.y); },
width : r * 2,
height : r
});
var errorMin = vis.selectAll('circle')
.data(data)
.enter()
.append('circle')
.attr({
cx : function(d) { return xScale(d.x); },
cy : function(d) { return yScale(d.yMin); },
r : r
});
var errorMax = vis.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr({
x : function(d) { return xScale(d.x) - r; },
y : function(d) { return yScale(d.yMax); },
width : r * 2,
height : r * 2
});
And here is a working example: http://jsfiddle.net/du37ep71/2/
I am pretty sure that when I do var errorMax = vis.selectAll('square')
at the end of my code it's telling D3 to select the existing squares rather than appending new ones, and I'm sure there's a way to just append new squares, I've just been unable to figure out how.
Upvotes: 0
Views: 63
Reputation: 16989
.append('square')
=> .append('rect')
Pre-defined SVG shapes that can be appended
<rect>
<circle>
<ellipse>
<line>
<polyline>
<polygon>
<path>
var errorMax = vis.selectAll('square')
.data(data)
.enter()
.append('rect')
.attr({
x : function(d) { return xScale(d.x) - r; },
y : function(d) { return yScale(d.yMax); },
width : r * 2,
height : r * 2
});
Upvotes: 2