Skip to content

Instantly share code, notes, and snippets.

@gee-whiz
Created November 24, 2020 21:18
Show Gist options
  • Save gee-whiz/ef14b62df19f45264faede573bfdf963 to your computer and use it in GitHub Desktop.
Save gee-whiz/ef14b62df19f45264faede573bfdf963 to your computer and use it in GitHub Desktop.
5.4 bar chart | mouse events
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<!-- Google fonts Second Reference-->
<link href="https://fonts.googleapis.com/css2?family=Montserrat+Alternates:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Roboto:ital,wght@0,100;0,300;1,100;1,300&display=swap" rel="stylesheet">
<!-- Connecting with D3 library-->
<script src="https://d3js.org/d3.v4.min.js" charset="utf-8"></script>
<!-- Creating the headlines -->
<p id="h1">BAR CHART | MOUSE EVENTS </p>
<div id="link">by
<a href="https://slides.com/sandravizmad">SANDRA</a></div>
<style>
/*Defining text stylings*/
#h1 {
font-size:30px;
margin:30px 0px 0px 20px;
color:#f5fa91;
font-family:'Montserrat Alternates', sans-serif;
font-weight:300;
}
#link {
font-family:'Montserrat Alternates', sans-serif;
font-weight:200;
font-size:10px;
margin:5px 0px 0px 22px;
color:white;
}
a:link, a:visited, a:active {
text-decoration: none;
color:white;
border-bottom:1.5px dotted white;
}
#tooltip {
position: absolute;
width: 200px;
padding: 0px;
background-color: none;
pointer-events: none;
font-family:'Montserrat Alternates', sans-serif;
fill:#FC77AD;
font-size:12px;
font-weight:300;
line-height: 5px;
}
body {
background-color:#011227;
}
</style>
</head>
<body>
<script>
//Margin conventions
var m = {top:80, right:50, bottom:50, left:40}
w = 800 - m.left - m.right,
h = 600 - m.top - m.bottom;
//Container
var svg = d3.select("body")
.append("svg")
.attr("width", w + m.left + m.right)
.attr("height", h + m.top + m.bottom)
.append("g")
.attr("transform", `translate(${m.left}, ${m.top})`);
//Format
var formatNumber = d3.format(".2s");
//Data loading
d3.csv(
"https://gist.githubusercontent.com/sandravizmad/22191c2a376a41df870994faf6eab6eb/raw/dfcf4a93505254832e37f66e3328ff6e740908b0/OECD%2520Threatened%2520species:%2520plants%2520by%2520country%25202015",
prepare,
function(data) {
//Sort data by value
data.sort((b, a) => a.Value - b.Value);
//Y scale
var y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.Value)])
.range([h, 0]);
//X scale
var x = d3.scaleBand()
.domain(d3.range(data.length))
.rangeRound([0, w])
.paddingInner(0.05);
//Draw the bars
svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr("x",(d, i) => x(i))
.attr("width", x.bandwidth())
.attr("y",d => y(d.Value))
.attr("height",d => h - y(d.Value))
.attr("fill", "#8ff798")
.on("mouseover", function(d) {
//Get this bar's x/y values, then augment for the tooltip
var xPos = +(d3.select(this).attr("x")) + x.bandwidth() / 2;
var yPos = +(d3.select(this).attr("y")) - 5;
//Create the tooltip label
svg.append("text")
.attr("id", "tooltip")
.attr("x", xPos)
.attr("y", yPos)
.text(formatNumber(d.Value));
})
.on("mouseout", function() {
//Remove the tooltip
d3.select("#tooltip").remove();
});
});
//Make sure all variables are numbers
function prepare (d) {
d.Value = +d.Value;
return d;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment