Grouping of an ordinal scale not using the axis component
In response to: http://stackoverflow.com/questions/25913528/add-second-x-axis-to-scatterplot-to-display-group-information
Grouping of an ordinal scale not using the axis component
In response to: http://stackoverflow.com/questions/25913528/add-second-x-axis-to-scatterplot-to-display-group-information
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<style> | |
text { | |
font: 12px sans-serif; | |
} | |
.axis path, .axis line, g.chartGroup path { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
circle.point { | |
fill: steelblue; | |
stroke: black; | |
} | |
</style> | |
<script> | |
var margin = {top: 10, right: 10, bottom: 30, left: 30}, | |
width = 500 - margin.left - margin.right, | |
height = 300 - margin.top - margin.bottom, | |
offset = 7; | |
var chartData = [ | |
{name: "AB", group: "A", number: 0.5}, | |
{name: "ABC", group: "A", number: 10.0}, | |
{name: "BC", group: "B", number: 3.0}, | |
{name: "BCD", group: "B", number: 5.0}, | |
{name: "BCDE", group: "B", number: 0.3}, | |
{name: "CD", group: "C", number: 1.6}, | |
{name: "DE", group: "D", number: 1.5} | |
]; | |
var x = d3.scale.ordinal() | |
.domain(chartData.map(function(d) { return d.name; })) | |
.rangePoints([0, width]); | |
var chartGroups = []; | |
chartData.forEach(function(d, i, array) { | |
if (i === 0) d.first = true; else if (d.group !== array[i - 1].group) d.first = true; | |
if (i === array.length - 1) d.last = true; else if (d.group !== array[i + 1].group) d.last = true; | |
if (d.first) chartGroups.push({ | |
group: d.group, | |
start: i === 0 ? x(d.name) : ((x(d.name) + x(array[i - 1].name)) / 2) | |
}); | |
if (d.last) chartGroups[chartGroups.length - 1].end = | |
i === array.length - 1 ? x(d.name) : ((x(d.name) + x(array[i + 1].name)) / 2); | |
}); | |
var y = d3.scale.linear() | |
.domain([0, d3.max(chartData, function(d) { return d.number; })]) | |
.range([height, 0]), | |
xAxis = d3.svg.axis() | |
.scale(x) | |
.tickValues([]) | |
.outerTickSize(offset) | |
.orient("bottom"), | |
yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left"); | |
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 + ")"); | |
svg.selectAll("circle.point") | |
.data(chartData) | |
.enter().append("circle") | |
.attr({ | |
"class": "point", | |
cx: function(d) { return x(d.name); }, | |
cy: function(d) { return y(d.number); }, | |
r: 5 | |
}); | |
var groups = svg.selectAll("g.chartGroup") | |
.data(chartGroups) | |
.enter().append("g") | |
.attr("class", "chartGroup") | |
.attr("transform", "translate(" + 0 + "," + (height + offset) + ")"); | |
groups.append("text") | |
.attr({ | |
x: function(d) { return (d.start + d.end) / 2; }, | |
dy: "1em", | |
"text-anchor": "middle" | |
}) | |
.text(function(d) { return d.group; }); | |
groups.append("path") | |
.attr("d", function(d) { | |
var t = d3.select(this.parentNode).select("text").node().getBBox(), | |
ttop = [t.x + t.width / 2, t.y]; | |
console.log(d, t, ttop); | |
return "M" + d.start + ",0" + "V" + -offset; | |
}); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
</script> |