Built with blockbuilder.org
forked from romsson's block: simple line chart from dataset
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
.line {
fill: none;
stroke: black;
stroke-width: 1.5px;
}
circle {
opacity: 0.1;
}
.value {
opacity : 0;
}
</style>
</head>
<body>
<script>
// Adapted from https://blockbuilder.org/romsson/855207ac5fb5c170de59b9c69ea56012
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500);
var width = 800;
var height = 400;
// How to parse (i.e. look for data) from the dataset
var parseDate = d3.timeParse("%Y-%m");
// How to display the parsed data
var displayDate = d3.timeFormat("%b %y");
// How to format the values
var displayValue = d3.format(",.0f");
// Ordinal scale
var x = d3.scaleTime()
.range([0, width]);
// Linear scale
var y = d3.scaleLinear()
.range([height, height - 200]);
var line = d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.value); });
var g = svg.append("g")
.attr("transform", "translate(50, 0)");
d3.json("dataset.json", function(error, data) {
if (error) throw error;
// Pre-processing
data.forEach(function(d) {
d.value = +d.value;
d["date"] = parseDate(d["date"]);
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
svg.selectAll(".label").data(data).enter()
.append("text")
.attr("class", "label")
.text(function(d, i) { return displayDate(d.date); })
.attr("y", 420)
.attr("x", function(d) { return x(d.date); })
.style("font-size", 12)
.style("font-family", "monospace");
// Display last value
g.selectAll(".value").data([data[data.length -1]]).enter()
.append("text")
.text(function(d, i) { return displayValue(d.value); })
.attr("class", "lastvalue")
.attr("y", function(d) { return y(d.value); })
.attr("x", width + 30)
.style("font-size", 20)
.style("font-family", "monospace");
g.selectAll(".line").data([data]).enter().append("path")
.attr("class", "line")
.attr("d", line);
g.selectAll("circle").data(data).enter().append("circle")
.attr("cx", function(d, i) { return x(d.date); })
.attr("cy", function(d) { return y(d.value); })
.attr("r", 10)
.attr("class", "dot")
.on("mouseover", function(d, i) {
d3.select(this).style("fill", "red").style("opacity", 1);
g.selectAll(".value").data(data).enter().append("text")
.text(function(d, i) { return displayValue(d.value); })
.attr("class", "value")
.attr("y", function(d) { return y(d.value) - 20; })
.attr("x", function(d) { return x(d.date);})
d3.selectAll("text.value")
.filter(function(e, j) {return i === j; })
.style("opacity", "1")
.style("font-size", 12)
.style("font-family", "monospace");
d3.selectAll("line")
.filter(function(e, j) {return i === j; })
.style("font-size", 12)
.style("opacity", "1");
})
.on("mouseout", function(d, i) {
d3.select(this).style("fill", "black").style("opacity", 0.1);
d3.selectAll("text.value")
.filter(function(e, j) {return i === j; })
.style("font-size", 12)
.style("opacity", "0");
d3.selectAll("line")
.filter(function(e, j) {return i === j; })
.style("font-size", 12)
.style("opacity", "0");
});
g.selectAll("dashedLine").data(data).enter().append("line")
.attr("x1", function(d, i) { return x(d.date);})
.attr("y1", function(d, i) { return y(d.value);})
.attr("x2", function(d, i) { return x(d.date);})
.attr("y2", height)
.style("stroke-dasharray", ("3, 3"))
.style("stroke", "black")
.style("opacity", 0);
});
</script>
</body>
https://d3js.org/d3.v4.min.js