Skip to content

Instantly share code, notes, and snippets.

@Qizly
Last active November 5, 2019 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Qizly/8f6ba236b79d9bb03a80 to your computer and use it in GitHub Desktop.
Save Qizly/8f6ba236b79d9bb03a80 to your computer and use it in GitHub Desktop.
Line Chart with Tooltip 1
date likes
10/1/2015 30303
10/2/2015 30452
10/3/2015 30568
10/4/2015 30598
10/5/2015 30798
10/6/2015 31002
10/7/2015 31376
10/8/2015 32661
10/9/2015 32875
10/10/2015 33088
10/11/2015 33500
10/12/2015 33612
10/13/2015 33813
10/14/2015 34138
10/15/2015 34256
10/16/2015 35013
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.overlay {
fill: none;
pointer-events: all;
}
.focus circle {
fill: steelblue;
}
.focus text {
font-size: 14px;
}
.tooltip {
fill: white;
stroke: #000;
}
.tooltip-date, .tooltip-likes {
font-weight: bold;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = { top: 30, right: 120, bottom: 30, left: 50 },
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
tooltip = { width: 100, height: 100, x: 10, y: -30 };
var parseDate = d3.time.format("%m/%e/%Y").parse,
bisectDate = d3.bisector(function(d) { return d.date; }).left,
formatValue = d3.format(","),
dateFormatter = d3.time.format("%m/%d/%y");
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(dateFormatter);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format("s"))
var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.likes); });
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 + ")");
d3.tsv("data.tsv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.date = parseDate(d.date);
d.likes = +d.likes;
});
data.sort(function(a, b) {
return a.date - b.date;
});
x.domain([data[0].date, data[data.length - 1].date]);
y.domain(d3.extent(data, function(d) { return d.likes; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Number of Likes");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
var focus = svg.append("g")
.attr("class", "focus")
.style("display", "none");
focus.append("circle")
.attr("r", 5);
focus.append("rect")
.attr("class", "tooltip")
.attr("width", 100)
.attr("height", 50)
.attr("x", 10)
.attr("y", -22)
.attr("rx", 4)
.attr("ry", 4);
focus.append("text")
.attr("class", "tooltip-date")
.attr("x", 18)
.attr("y", -2);
focus.append("text")
.attr("x", 18)
.attr("y", 18)
.text("Likes:");
focus.append("text")
.attr("class", "tooltip-likes")
.attr("x", 60)
.attr("y", 18);
svg.append("rect")
.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", mousemove);
function mousemove() {
var x0 = x.invert(d3.mouse(this)[0]),
i = bisectDate(data, x0, 1),
d0 = data[i - 1],
d1 = data[i],
d = x0 - d0.date > d1.date - x0 ? d1 : d0;
focus.attr("transform", "translate(" + x(d.date) + "," + y(d.likes) + ")");
focus.select(".tooltip-date").text(dateFormatter(d.date));
focus.select(".tooltip-likes").text(formatValue(d.likes));
}
});
</script>
@prostko
Copy link

prostko commented Nov 5, 2019

Are there any dependencies other than //d3js.org/d3.v3.min.js? I'm not able to get this to work when i try to put it into my project..

Its failing with Uncaught TypeError: Cannot read property 'sourceEvent' of null in the mousemove, specifically d3.mouse(this).

Any help is much appreciated

edit: nevermind, the issue was on my end. Your code is great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment