Skip to content

Instantly share code, notes, and snippets.

@RomyRatolojanahary
Last active November 17, 2016 20:08
Show Gist options
  • Save RomyRatolojanahary/906cc7a3f6d8eea3f0ae8ed84767114c to your computer and use it in GitHub Desktop.
Save RomyRatolojanahary/906cc7a3f6d8eea3f0ae8ed84767114c to your computer and use it in GitHub Desktop.
simple line chart from dataset
license: mit
[{"name": "A", "value": 10, "date": "2016-01"},
{"name": "B", "value": 30, "date": "2016-02"},
{"name": "C", "value": 20, "date": "2016-03"},
{"name": "D", "value": 40, "date": "2016-04"},
{"name": "E", "value": 50, "date": "2016-05"},
{"name": "F", "value": 20, "date": "2016-06"},
{"name": "G", "value": 10, "date": "2016-07"},
{"name": "H", "value": 15, "date": "2016-08"},
{"name": "I", "value": 38, "date": "2016-09"},
{"name": "J", "value": 60, "date": "2016-10"},
{"name": "K", "value": 28, "date": "2016-11"},
{"name": "L", "value": 19, "date": "2016-12"}
]
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.line {
fill: none;
stroke: black;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
var width = 800;
var height = 400;
// http://bl.ocks.org/zanarmstrong/raw/05c1e95bf7aa16c4768e/
var parseDate = d3.time.format("%Y-%m");
var displayDate = d3.time.format("%b %y");
var displayValue = d3.format(",.0f");
// Ordinal scale
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .5);
var y = d3.scale.linear()
.range([height, height - 200]);
var line = d3.svg.line()
.x(function(d) { return x(d.name); })
.y(function(d) { return y(d.value); });
var g = svg.append("g")
.attr("transform", "translate(20, 0)")
d3.json("dataset.json", function(data) {
// Pre-processing
data.forEach(function(d) {
d.value;// = +d.value;
d["date"] = parseDate.parse(d["date"]);
});
x.domain(data.map(function(d) { return d.name; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
svg.selectAll("text").data(data).enter()
.append("text")
.text(function(d, i) { return displayDate(d.date); })
.attr("y", 420)
.attr("x", function(d) { return x(d.name); })
.style("font-size", 10)
.style("font-family", "monospace")
.style("fill", "black");
//on écrit les valeurs en blanc pour les rendre invisibles
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)-10; })
.attr("x",function(d) { return x(d.name)-10;})
.style("font-size", 20)
.style("font-family", "monospace")
.style("fill", "white");
g.selectAll("path").data([data]).enter().append("path")
.attr("class", "line")
.attr("d", line);
// .style("stroke-dasharray", ("3, 3"));
g.selectAll("line").data(data).enter().append("line") // attach a line
.attr("class", "ligne")
.style("stroke", "white") // colour the line in white
.style("stroke-dasharray", ("3, 3")) // dashes
.attr("x1", function(d) {return x(d.name);}) // x position of the first end of the line
.attr("y1", function(d) {return y(d.value);}) // y position of the first end of the line
.attr("x2",function(d) {return x(d.name);} ) // x position of the second end of the line
.attr("y2",400 )
g.selectAll("dot")
.attr("id",function (d,i) {return "id_"+i;})
.data(data)
.enter().append("circle")
.attr("r", 8)
.attr("cx", function(d) { return x(d.name); })
.attr("cy", function(d) { return y(d.value); })
.style("fill", "grey")
.style("opacity", 0.5)
.on("mouseover", function(d) {
d3.select(this).style("fill", "red").style("opacity", 1);
d3.selectAll(".value").filter(function(e) {
return d === e;
}).style("fill", "black")
d3.selectAll(".ligne").filter(function(e) {
return d === e;
}).style("stroke", "black");
})
.on("mouseout", function(d) {
d3.select(this).style("fill","grey").style("opacity",0.5)
d3.selectAll(".value").style("fill","white")
d3.selectAll(".ligne").style("stroke","white");
})
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment