Last active
October 20, 2017 06:35
-
-
Save nanu146/f48ffc5ec10270f55c9e1fb3da8b38f0 to your computer and use it in GitHub Desktop.
Grouped Bar Chart with Line using d3.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.bar { | |
fill: steelblue; | |
} | |
.x.axis path { | |
display: none; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
function getTextWidth(text, fontSize, fontName) { | |
c = document.createElement("canvas"); | |
ctx = c.getContext("2d"); | |
ctx.font = fontSize + ' ' + fontName; | |
return ctx.measureText(text).width; | |
} | |
function DataSegregator(array, on) { | |
var SegData; | |
OrdinalPositionHolder = { | |
valueOf: function () { | |
thisObject = this; | |
keys = Object.keys(thisObject); | |
keys.splice(keys.indexOf("valueOf"), 1); | |
keys.splice(keys.indexOf("keys"), 1); | |
return keys.length == 0 ? -1 : d3.max(keys, function (d) { return thisObject[d] }) | |
} | |
, keys: function () { | |
keys = Object.keys(thisObject); | |
keys.splice(keys.indexOf("valueOf"), 1); | |
keys.splice(keys.indexOf("keys"), 1); | |
return keys; | |
} | |
} | |
array[0].map(function (d) { return d[on] }).forEach(function (b) { | |
value = OrdinalPositionHolder.valueOf(); | |
OrdinalPositionHolder[b] = OrdinalPositionHolder > -1 ? ++value : 0; | |
}) | |
SegData = OrdinalPositionHolder.keys().map(function () { | |
return []; | |
}); | |
array.forEach(function (d) { | |
d.forEach(function (b) { | |
SegData[OrdinalPositionHolder[b[on]]].push(b); | |
}) | |
}); | |
return SegData; | |
} | |
Data = [ | |
{ Date: "2016-06-14", Categories: [{ Name: "Category1", Value: 368 }, { Name: "Category2", Value: 321 }, { Name: "Category3", Value: 524 }], LineCategory: [{ Name: "Line1", Value: 69 }, { Name: "Line2", Value: 63 }] }, | |
{ Date: "2016-06-15", Categories: [{ Name: "Category1", Value: 521 }, { Name: "Category2", Value: 123 }, { Name: "Category3", Value: 653 }], LineCategory: [{ Name: "Line1", Value: 89 }, { Name: "Line2", Value: 96 }] }, | |
{ Date: "2016-06-17", Categories: [{ Name: "Category1", Value: 368 }, { Name: "Category2", Value: 236 }, { Name: "Category3", Value: 537 }], LineCategory: [{ Name: "Line1", Value: 63 }, { Name: "Line2", Value: 35 }] }, | |
{ Date: "2016-06-18", Categories: [{ Name: "Category1", Value: 423 }, { Name: "Category2", Value: 330 }, { Name: "Category3", Value: 689 }], LineCategory: [{ Name: "Line1", Value: 86 }, { Name: "Line2", Value: 45 }] }, | |
{ Date: "2016-06-19", Categories: [{ Name: "Category1", Value: 601 }, { Name: "Category2", Value: 423 }, { Name: "Category3", Value: 490 }], LineCategory: [{ Name: "Line1", Value: 65 }, { Name: "Line2", Value: 63 }] }, | |
{ Date: "2016-06-20", Categories: [{ Name: "Category1", Value: 412 }, { Name: "Category2", Value: 461 }, { Name: "Category3", Value: 321 }], LineCategory: [{ Name: "Line1", Value: 75 }, { Name: "Line2", Value: 85 }] } | |
] | |
var margin = { top: 20, right: 30, bottom: 60, left: 40 }, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var textWidthHolder = 0; | |
/// Adding Date in LineCategory | |
Data.forEach(function (d) { | |
d.LineCategory.forEach(function (b) { | |
b.Date = d.Date; | |
}) | |
}); | |
var Categories = new Array(); | |
// Extension method declaration | |
Categories.pro | |
var Data; | |
var ageNames; | |
var x0 = d3.scale.ordinal() | |
.rangeRoundBands([0, width], .1); | |
var XLine = d3.scale.ordinal() | |
.rangeRoundPoints([0, width], .1); | |
var x1 = d3.scale.ordinal(); | |
var y = d3.scale.linear() | |
.range([height, 0]); | |
var YLine = d3.scale.linear().range([height, 0]) | |
.domain([0, d3.max(Data, function (d) { return d3.max(d.LineCategory, function (b) { return b.Value }) })]); | |
var color = d3.scale.ordinal() | |
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); | |
var line = d3.svg.line().x(function (d) { | |
return x0(d.Date) + x0.rangeBand() / 2; | |
}).y(function (d) { return YLine(d.Value) }); | |
var xAxis = d3.svg.axis() | |
.scale(x0) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
.tickFormat(d3.format(".2s")); | |
var YLeftAxis = d3.svg.axis().scale(YLine).orient("right").tickFormat(d3.format(".2s")); | |
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 + ")"); | |
// Bar Data categories | |
Data.forEach(function (d) { | |
d.Categories.forEach(function (b) { | |
if (Categories.findIndex(function (c) { return c.Name===b.Name}) == -1) { | |
b.Type = "bar"; | |
console.log(JSON.stringify(b)) | |
Categories.push(b) | |
} | |
}) | |
}); | |
// Line Data categories | |
Data.forEach(function (d) { | |
d.LineCategory.forEach(function (b) { | |
if (Categories.findIndex(function (c) { return c.Name === b.Name }) == -1) { | |
b.Type = "line"; | |
console.log(JSON.stringify(b)) | |
Categories.push(b) | |
} | |
}) | |
}); | |
// Processing Line data | |
lineData = DataSegregator(Data.map(function (d) { return d.LineCategory }), "Name"); | |
// Line Coloring | |
LineColor = d3.scale.ordinal(); | |
LineColor.domain(Categories.filter(function (d) { return d.Type == "line" }).map(function (d) { return d.Name })); | |
LineColor.range(["#d40606", "#06bf00", "#98bdc5", "#671919", "#0b172b"]) | |
x0.domain(Data.map(function (d) { return d.Date; })); | |
XLine.domain(Data.map(function (d) { return d.Date; })); | |
x1.domain(Categories.filter(function (d) { return d.Type == "bar" }).map(function (d) { return d.Name})).rangeRoundBands([0, x0.rangeBand()]); | |
y.domain([0, d3.max(Data, function (d) { return d3.max(d.Categories, function (d) { return d.Value; }); })]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(" + (width) + ",0)") | |
.call(YLeftAxis) | |
.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", -10) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("Percent"); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("Population"); | |
var state = svg.selectAll(".state") | |
.data(Data) | |
.enter().append("g") | |
.attr("class", "state") | |
.attr("transform", function (d) { return "translate(" + x0(d.Date) + ",0)"; }); | |
state.selectAll("rect") | |
.data(function (d) { return d.Categories; }) | |
.enter().append("rect") | |
.attr("width", x1.rangeBand()) | |
.attr("x", function (d) { return x1(d.Name); }) | |
.attr("y", function (d) { return y(d.Value); }) | |
//.attr("height", function (d) { return height - y(d.Value); }) | |
.style("fill", function (d) { return color(d.Name); }) | |
.transition().delay(500).attrTween("height", function (d) { | |
var i = d3.interpolate(0, height - y(d.Value)); | |
return function (t) | |
{ | |
return i(t); | |
} | |
}); | |
// drawaing lines | |
svg.selectAll(".lines").data(lineData).enter().append("g").attr("class", "line") | |
.each(function (d) { | |
Name=d[0].Name | |
d3.select(this).append("path").attr("d", function (b) { return line(b) }).style({ "stroke-width": "2px", "fill": "none" }).style("stroke", LineColor(Name)).transition().duration(1500); | |
}) | |
// Legends | |
var LegendHolder = svg.append("g").attr("class", "legendHolder"); | |
var legend = LegendHolder.selectAll(".legend") | |
.data(Categories.map(function (d) { return {"Name":d.Name,"Type":d.Type}})) | |
.enter().append("g") | |
.attr("class", "legend") | |
.attr("transform", function (d, i) { return "translate(0," +( height+ margin.bottom/2 )+ ")"; }) | |
.each(function (d,i) { | |
// Legend Symbols | |
d3.select(this).append("rect") | |
.attr("width", function () { return 18 }) | |
.attr("x", function (b) { | |
left = (i+1) * 15 + i * 18 + i * 5 + textWidthHolder; | |
return left; | |
}) | |
.attr("y", function (b) { return b.Type == 'bar'?0:7}) | |
.attr("height", function (b) { return b.Type== 'bar'? 18:5 }) | |
.style("fill", function (b) { return b.Type == 'bar' ? color(d.Name) : LineColor(d.Name) }); | |
// Legend Text | |
d3.select(this).append("text") | |
.attr("x", function (b) { | |
left = (i+1) * 15 + (i+1) * 18 + (i + 1) * 5 + textWidthHolder; | |
return left; | |
}) | |
.attr("y", 9) | |
.attr("dy", ".35em") | |
.style("text-anchor", "start") | |
.text(d.Name); | |
textWidthHolder += getTextWidth(d.Name, "10px", "calibri"); | |
}); | |
// Legend Placing | |
d3.select(".legendHolder").attr("transform", function (d) { | |
thisWidth = d3.select(this).node().getBBox().width; | |
return "translate(" + ((width) / 2 - thisWidth / 2) + ",0)"; | |
}) | |
</script> | |
</body> |
Done
https://we.tl/qokvAsUNfw
Alernative download:
https://mega.nz/#!iRcFnLRI!dZ8WJK1QhuxDx6ZVeGBbFd130sn-Ppop9bjJyaAQ9dQ
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello
I like your example and was migrating to the new version of d3.js to version 4, so that it was updated.
And I've encountered a series of problems that I've been solving, changing the line 216 of your code, from .attr ("width", x1.rangeBand ()) to .attr ("width", x1.bandwidth )).
The problem has been that it has skipped another error and not followed, I have commented for example line 223 of my .style file ("fill", function (d) {return color (d.Name);}) so that it will start to see something.
If you could help me advance this update, I upload the file
https://we.tl/JwA4FccLoE