Last active
October 13, 2017 13:02
-
-
Save BastiTee/812b6d13f1e01b02cdcf1444f56505d1 to your computer and use it in GitHub Desktop.
An easy-to-use reusable tooltip plugin for d3v4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
An easy-to-use reusable tooltip plugin for d3v4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 | |
height: 500 | |
border: no |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/BastiTee/d3-cooltip Version 0.1.0. Copyright 2017 Basti Tee. | |
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("d3")):"function"==typeof define&&define.amd?define(["exports","d3"],e):e(t.d3=t.d3||{},t.d3)}(this,function(t,e){"use strict";var n=function(){function t(t){t.each(function(){var t,l,d,c=e.select(this),f=this.ownerSVGElement,s=f.getBBox(),g=!1,p=function(){var n=e.mouse(f),r=l.node().getBBox(),i=n[0],a=n[1]-r.height;i=i-r.width/2<0?r.width/2:i,i=i+r.width/2>s.width?s.width-r.width/2:i,a=a-o<0?o:a,a=a+r.height-o>s.height?s.height-r.height+o:a,t.attr("transform","translate("+i+","+a+")")},x=function(){g&&(g=!1,t.remove())},y=function(c){if(!g){g=!0,t=e.select(f).append("g").style("pointer-events","none"),l=t.append("rect"),d=t.append("text").attr("class","cooltip-text");var s=""+u(c),p=s.split("\n");for(var x in p)d.append("tspan").style("text-anchor","middle").style("dominant-baseline","hanging").style("fill",r).attr("x",0).attr("dy",function(){return 0==x?0:a}).text(p[x]);var y=d.node().getBBox();l.attr("class","cooltip-box").attr("rx",h).attr("ry",h).attr("width",y.width+2*o).attr("height",y.height+2*o).attr("x",-y.width/2-o).attr("y",-o).attr("opacity",n).style("fill",i)}};c.on("mouseover",y),c.on("mouseout",x),c.on("mousemove",p)})}var n=.8,o=5,r="white",i="black",a=20,h=5,u=function(){return(new Date).toDateString()+"\n"+String(Math.floor(9e8*Math.random()))};return t.opacity=function(e){return arguments.length?(n=e,t):n},t.padding=function(e){return arguments.length?(o=e,t):o},t.color=function(e){return arguments.length?(r=e,t):r},t.fill=function(e){return arguments.length?(i=e,t):i},t.lineHeight=function(e){return arguments.length?(a=e,t):a},t.roundCorners=function(e){return arguments.length?(h=e,t):h},t.selector=function(e){return arguments.length?(u=e,t):u},t};t.cooltip=n,Object.defineProperty(t,"__esModule",{value:!0})}); |
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
value | |
red | |
blue | |
green |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>d3-cooltip demo</title> | |
<script src="https://d3js.org/d3.v4.js"></script> | |
<script src="d3-cooltip.0.1.0.min.js"></script> | |
<style> | |
@import url('https://fonts.googleapis.com/css?family=Saira+Extra+Condensed'); | |
.cooltip-box { | |
/* The box around the cooltip text */ | |
stroke: orange; | |
stroke-width: 2; | |
} | |
.cooltip-text { | |
/* The cooltip text */ | |
font-family: 'Saira Extra Condensed', sans-serif; | |
font-size: 150%; | |
} | |
</style> | |
</head> | |
<body> | |
<svg id="my-svg"></svg> | |
</body> | |
<script> | |
d3.csv("data.csv", function(error, data) { | |
// setup basic canvas | |
var widHei = 500 | |
var dim = widHei / 3 | |
var svg = d3.select("#my-svg") | |
.attr("width", widHei) | |
.attr("height", widHei) | |
// add a grey background | |
svg.append("rect") | |
.attr("width", widHei) | |
.attr("height", widHei) | |
.attr("fill", "lightgrey") | |
// create some rectangles from data | |
svg.selectAll(".rectangles") | |
.data(data).enter().append("rect") | |
.attr("class", "rectangles") | |
.attr("width", dim * 1.5) | |
.attr("height", dim * 1.5) | |
.attr("x", function(d, i) { | |
return (dim / 2) * 1.5 * i | |
}) | |
.attr("y", function(d, i) { | |
return (dim / 2) * 1.5 * i | |
}) | |
.attr("fill", function(d) { | |
return d["value"] | |
}) | |
// write a callback to define what the cooltip should display | |
var cooltipText = function(d) { | |
return "This rectangle is\ncolored " + d["value"] | |
} | |
// create the cooltip and configure it | |
var cooltip = d3.cooltip() | |
.opacity(0.7) | |
.padding(10) | |
.color("lightgrey") | |
.fill("black") | |
.roundCorners(10) | |
.lineHeight(25) | |
.selector(cooltipText) | |
// call it for each rectangle | |
svg.selectAll(".rectangles").call(cooltip) | |
}) | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment