Last active
May 28, 2019 13:58
-
-
Save ColinEberhardt/4676df62ce9cad924e8bc7d492fb602e to your computer and use it in GitHub Desktop.
d3fc grouped bar chart
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
license: mit |
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
State | Under 5 Years | 5 to 13 Years | 14 to 17 Years | 18 to 24 Years | 25 to 44 Years | 45 to 64 Years | 65 Years and Over | |
---|---|---|---|---|---|---|---|---|
CA | 2704659 | 4499890 | 2159981 | 3853788 | 10604510 | 8819342 | 4114496 | |
TX | 2027307 | 3277946 | 1420518 | 2454721 | 7017731 | 5656528 | 2472223 | |
NY | 1208495 | 2141490 | 1058031 | 1999120 | 5355235 | 5120254 | 2607672 | |
FL | 1140516 | 1938695 | 925060 | 1607297 | 4782119 | 4746856 | 3187797 | |
IL | 894368 | 1558919 | 725973 | 1311479 | 3596343 | 3239173 | 1575308 | |
PA | 737462 | 1345341 | 679201 | 1203944 | 3157759 | 3414001 | 1910571 |
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> | |
<!-- include polyfills for custom event, Symbol and Custom Elements --> | |
<script src="//unpkg.com/babel-polyfill@6.26.0/dist/polyfill.js"></script> | |
<script src="//unpkg.com/custom-event-polyfill@0.3.0/custom-event-polyfill.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/document-register-element/1.8.0/document-register-element.js"></script> | |
<!-- use babel so that we can use arrow functions and other goodness in this block! --> | |
<script src="//unpkg.com/babel-standalone@6/babel.min.js"></script> | |
<script src="//unpkg.com/d3@5.5.0"></script> | |
<script src="//unpkg.com/d3fc@14.0.41"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.18.0/d3-legend.min.js"></script> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
#chart { | |
width: 100%; | |
height: 500px; | |
position: relative; | |
} | |
#chart .legend { | |
position: absolute; | |
bottom: auto; | |
right: 0; | |
left: auto; | |
top: 0; | |
width: 170px; | |
} | |
.x-axis .domain { | |
display: none; | |
} | |
</style> | |
<body> | |
<div id="chart"></div> | |
<script type='text/babel'> | |
var color = d3.scaleOrdinal() | |
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); | |
var legend = d3.legendColor() | |
.orient('vertical') | |
.shapeWidth(20) | |
.scale(color); | |
var yExtent = fc.extentLinear() | |
.accessors([(d) => d.map((e) => e[1])]) | |
.include([0]); | |
d3.csv("data.csv").then(data => { | |
var group = fc.group() | |
.key("State"); | |
var series = group(data); | |
var ages = series.map(d => d.key); | |
color.domain(ages); | |
var groupedBar = fc.seriesSvgGrouped(fc.seriesSvgBar()) | |
.crossValue(d => d[0]) | |
.mainValue(d => d[1]) | |
.bandwidth (120) | |
.decorate((sel, data, index) => { | |
sel.enter() | |
.select('path') | |
.attr('fill', color(ages[index])); | |
}); | |
var chart = fc.chartCartesian( | |
d3.scalePoint(), | |
d3.scaleLinear() | |
) | |
.xDomain(data.map(d => d.State)) | |
.xPadding(0.5) | |
.yDomain(yExtent(series)) | |
.yTickFormat(d3.format(".2s")) | |
.yOrient('left') | |
.svgPlotArea(groupedBar) | |
.decorate((selection) => { | |
selection.enter() | |
.append('svg') | |
.attr('class', 'legend'); | |
selection.select('.legend') | |
.call(legend); | |
}); | |
d3.select('#chart') | |
.datum(series) | |
.call(chart); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment