This is an adaption of the same subject, but using JSON
Last active
June 22, 2017 15:25
-
-
Save brianbancroft/c79e3e32527f278e58758c394c38d5a8 to your computer and use it in GitHub Desktop.
D3 - Basic Barchart - uses modern function calls
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
[{ | |
"date": "Bob", | |
"edits": 32 | |
}, { | |
"date": "Robin", | |
"edits": 12 | |
}, { | |
"date": "Anne", | |
"edits": 24 | |
}, { | |
"date": "Mark", | |
"edits": 8 | |
}, { | |
"date": "Joe", | |
"edits": 19 | |
}, { | |
"date": "Eve", | |
"edits": 16 | |
}, { | |
"date": "Karen", | |
"edits": 12 | |
}, { | |
"date": "Kristy", | |
"edits": 31 | |
}, { | |
"date": "Chris", | |
"edits": 42 | |
}, { | |
"date": "Lisa", | |
"edits": 57 | |
}, { | |
"date": "Tom", | |
"edits": 23 | |
}, { | |
"date": "Stacey", | |
"edits": 14 | |
}, { | |
"date": "Charles", | |
"edits": 12 | |
}, { | |
"date": "Mary", | |
"edits": 2 | |
}] |
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> /* set the CSS */ | |
.bar { fill: steelblue; } | |
</style> | |
<body> | |
<!-- load the d3.js library --> | |
<script src="//d3js.org/d3.v4.min.js"></script> | |
<script> | |
// set the dimensions and margins of the graph | |
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
// set the ranges | |
var x = d3.scaleBand() | |
.range([0, width]) | |
.padding(0.1); | |
var y = d3.scaleLinear() | |
.range([height, 0]); | |
// append the svg object to the body of the page | |
// append a 'group' element to 'svg' | |
// moves the 'group' element to the top left margin | |
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 + ")"); | |
// get the data | |
d3.json("edits.json", (error, data) => { | |
if (error) throw error; | |
// format the data | |
data.forEach(d => d.edits = +d.edits); | |
// Scale the range of the data in the domains | |
x.domain(data.map(d => d.date )); | |
y.domain([0, d3.max(data, d => d.edits)]); | |
// append the rectangles for the bar chart | |
svg.selectAll(".bar") | |
.data(data) | |
.enter().append("rect") | |
.attr("class", "bar") | |
.attr("x", d => x(d.date)) | |
.attr("width", x.bandwidth()) | |
.attr("y", d => y(d.edits)) | |
.attr("height", d => height - y(d.edits)); | |
// add the x Axis | |
svg.append("g") | |
.attr("transform", "translate(0," + height + ")") | |
.call(d3.axisBottom(x)); | |
// add the y Axis | |
svg.append("g") | |
.call(d3.axisLeft(y)); | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment