Skip to content

Instantly share code, notes, and snippets.

@ibtimoussi
Created November 13, 2017 19:41
Show Gist options
  • Save ibtimoussi/116d4baa5632e98285a2d7e6291daff3 to your computer and use it in GitHub Desktop.
Save ibtimoussi/116d4baa5632e98285a2d7e6291daff3 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": 30, "date": "2016-08"},
{"name": "G", "value": 40, "date": "2016-09"},
{"name": "G", "value": 60, "date": "2016-10"},
{"name": "G", "value": 30, "date": "2016-11"},
{"name": "G", "value": 10, "date": "2016-12"}
]
<!DOCTYPE html>
<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;
}
div.tooltip {
position: absolute;
text-align: center;
width: 60px;
height: 28px;
padding: 2px;
font: 12px sans-serif;
background: none;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
.hover-line {
stroke: red;
stroke-width: 2px;
stroke-dasharray: 3,3;
}
}
</style>
</head>
<body>
<script>
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500);
var width = 800;
var height = 400;
// See formats http://bl.ocks.org/zanarmstrong/raw/05c1e95bf7aa16c4768e/
// Data sample {"name": "G", "value": 10, "date": "2016-07"}
var parseDate = d3.timeParse("%Y-%m");
var displayDate = d3.timeFormat("%b %y");
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 div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
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", 10)
.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", "value")
.attr("y", function(d) { return y(d.value); })
.attr("x", width + 10)
.style("font-size", 20)
.style("font-family", "monospace");
g.selectAll(".line").data([data]).enter().append("path")
.attr("class", "line")
.attr("d", line);
///
g.append("line")
.attr("class", "x-hover-line hover-line")
.attr("y1", 0)
.attr("y2", height);
g.append("line")
.attr("class", "y-hover-line hover-line")
.attr("x1", width)
.attr("x2", width);
///
g.selectAll("rect")
.attr("transform", "translate(50, 0)")
.attr("class", "overlay")
.attr("width", width)
.attr("height", height)
.on("mouseover", function() { focus.style("display", null); })
.on("mouseout", function() { focus.style("display", "none"); })
.on("mousemove", function (){
g.select(".x-hover-line").attr("y2", height - y(d.value));
g.select(".y-hover-line").attr("x2", width + width);
});
g.selectAll('circle')
.data(data)
.enter().append('circle')
.attr('cx', function(d) {
return x(d.date);
})
.attr('cy', function(d) {
return y(d.value);
})
.attr('r', 10)
.attr('fill',"#d9d0e6")
.on("mouseover", function(d) {
d3.select(this).style("fill", "red")
.style("font-size", 20)
.style("font-size", 24)
div.transition()
.duration(200)
.style("opacity", .9);
div .html(d.value)
.style("left", (d3.event.pageX-20) + "px")
.style("top", (d3.event.pageY - 40) + "px");
})
.on("mouseout", function(d) {
d3.select(this).transition().duration(500).style("fill", "#d9d0e6")
// Should be using Ids instead of values
.transition().duration(500)
.style("font-size", 12)
div.transition()
.duration(500)
.style("opacity", 0);
});
});
// function mouse ()
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment