Last active
October 15, 2020 19:48
-
-
Save basilesimon/37f41582212254ded2ae31ae0aca0b61 to your computer and use it in GitHub Desktop.
Vertical line chart with labels
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
[ | |
{"year" : "2007", "value": -10}, | |
{"year" : "2008", "value": 34}, | |
{"year" : "2009", "value": 10}, | |
{"year" : "2010", "value": -60}, | |
{"year" : "2011", "value": -40}, | |
{"year" : "2012", "value": 20}, | |
{"year" : "2013", "value": 45}, | |
{"year" : "2014", "value": 50}, | |
{"year" : "2015", "value": 60}, | |
{"year" : "2016", "value": 40} | |
] |
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 { | |
background-color: #fff; | |
} | |
.axis { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: lightgrey; | |
stroke-width: 0px; | |
shape-rendering: crispEdges; | |
} | |
.line { | |
fill: none; | |
stroke: #0074D9; | |
stroke-width: 3px; | |
} | |
.label { | |
background-color: #fff; | |
font-family: sans-serif; | |
font-size: 12px; | |
} | |
.zero { | |
stroke: lightgrey; | |
stroke-width: 1px; | |
} | |
</style> | |
<body> | |
<svg width="400" height="500"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="script.js"></script> | |
</body> | |
</html> |
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
var svg = d3.select("svg"), | |
margin = {top: 20, right: 60, bottom: 30, left: 40}, | |
width = +svg.attr("width") - margin.left - margin.right, | |
height = +svg.attr("height") - margin.top - margin.bottom; | |
var parseTime = d3.timeParse("%Y") | |
bisectDate = d3.bisector(function(d) { return d.year; }).left; | |
var x = d3.scaleLinear().range([0, width]); | |
var y = d3.scaleTime().range([height, 0]); | |
var line = d3.line() | |
.x(function(d) { return x(d.value); }) | |
.y(function(d) { return y(d.year); }) | |
.curve(d3.curveStepAfter); | |
var g = svg.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
d3.json("data.json", function(error, data) { | |
if (error) throw error; | |
data.forEach(function(d) { | |
d.year = parseTime(d.year); | |
d.value = +d.value; | |
}); | |
y.domain(d3.extent(data, function(d) { return d.year; }).reverse()); | |
x.domain([d3.min(data, function(d) { return d.value; }), d3.max(data, function(d) { return d.value; }) * 1.005]); | |
g.append("g") | |
.attr("class", "axis axis--x") | |
.attr("transform", "translate(0," + height + ")"); | |
g.append("g") | |
.attr("class", "axis axis--y") | |
.call(d3.axisLeft(y)) | |
.append("text") | |
.attr("class", "axis-title") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.attr("fill", "#5D6971") | |
.text("lol"); | |
g.append("line") | |
.attr("class", "zero") | |
.attr("stroke-dasharray", "5,5") | |
.attr("x1", x(0)) | |
.attr("y1", 0) | |
.attr("x2", x(0)) | |
.attr("y2", height) | |
.attr("transform", "translate(30,0)"); | |
g.append("path") | |
.datum(data) | |
.attr("class", "line") | |
.attr("transform", "translate(30,0)") | |
.attr("d", line); | |
g.append("g") | |
.attr("class", "rects") | |
.selectAll("text") | |
.data(data) | |
.enter() | |
.append("rect") | |
.attr("x", function (d) { return x(d.value); }) | |
.attr("y", function (d) { return y(d.year); }) | |
.attr("width", 35) | |
.attr("height", 20) | |
.attr("transform", "translate(15,-15)") | |
.style("fill", "#fff"); | |
g.append("g") | |
.attr("class", "labels") | |
.selectAll("text") | |
.data(data) | |
.enter() | |
.append("text") | |
.attr("class", "label") | |
.attr("x", function(d) { return x(d.value); }) | |
.attr("y", function(d) { return y(d.year); }) | |
.attr("text-anchor", "middle") | |
.attr("transform", "translate(30,0)") | |
.text(function(d) { | |
return d.value + "m"; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment