| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<title>d3.tip.js - Tooltips for D3</title> |
| 5 |
<meta charset="utf-8" /> |
| 6 |
<title>Example styles</title> |
| 7 |
<script type="text/javascript" src="../bower_components/d3/d3.js"></script> |
| 8 |
<script type="text/javascript" src="../index.js"></script> |
| 9 |
<link rel="stylesheet" href="example-styles.css"> |
| 10 |
<style type="text/css"> |
| 11 |
body { |
| 12 |
padding: 40px; |
| 13 |
font-family: "Helvetica Neue", Helvetica, sans-serif; |
| 14 |
font-size: 12px; |
| 15 |
width: 600px; |
| 16 |
} |
| 17 |
|
| 18 |
svg.n, svg.s { |
| 19 |
margin-left: 200px; |
| 20 |
margin-right: 200px; |
| 21 |
} |
| 22 |
svg.e { |
| 23 |
margin-left: 200px; |
| 24 |
} |
| 25 |
|
| 26 |
circle { |
| 27 |
fill: #ccc; |
| 28 |
fill-opacity: 0.6; |
| 29 |
stroke: #bbb; |
| 30 |
stroke-width: 1px; |
| 31 |
} |
| 32 |
|
| 33 |
circle:hover { |
| 34 |
fill: #bbb; |
| 35 |
stroke: #999; |
| 36 |
} |
| 37 |
|
| 38 |
text { |
| 39 |
text-anchor: middle; |
| 40 |
} |
| 41 |
</style> |
| 42 |
</head> |
| 43 |
<body> |
| 44 |
<script type="text/javascript"> |
| 45 |
var data = [], |
| 46 |
random = d3.randomNormal(5), |
| 47 |
random2 = d3.randomIrwinHall(1) |
| 48 |
for(var i = 0; i < 25; i++) data.push(random(i)) |
| 49 |
|
| 50 |
var w = 200, |
| 51 |
h = 200, |
| 52 |
b = 20, |
| 53 |
r = 10, |
| 54 |
x = d3.scaleLinear().domain([0, data.length - 1]).range([r, w - r]), |
| 55 |
y = d3.scaleLinear().domain([0, d3.max(data)]).range([h, 0]) |
| 56 |
|
| 57 |
var directions = ['n', 'w', 'e', 's']; |
| 58 |
directions.forEach(function (direction) { |
| 59 |
var tip = d3.tip() |
| 60 |
.attr('class', 'd3-tip') |
| 61 |
.html(function(d) { return d.toFixed(2) }) |
| 62 |
.direction(direction) |
| 63 |
.offset(function () { |
| 64 |
if(direction=='n') { return [-10,0] } |
| 65 |
else if(direction=='s') { return [10,0] } |
| 66 |
else if(direction=='e') { return [0,10] } |
| 67 |
else if(direction=='w') { return [0,-10] } |
| 68 |
}) |
| 69 |
|
| 70 |
var vis = d3.select(document.body) |
| 71 |
.append('svg') |
| 72 |
.attr('class', direction) |
| 73 |
.attr('width', w) |
| 74 |
.attr('height', h) |
| 75 |
.append('g') |
| 76 |
.attr('transform', 'translate('+b+','+b+')') |
| 77 |
.call(tip) |
| 78 |
|
| 79 |
vis.append('text') |
| 80 |
.attr('class', 'direction') |
| 81 |
.attr('x', w/2) |
| 82 |
.attr('y', -b) |
| 83 |
.attr('dy', '1em') |
| 84 |
.text('direction: ' + direction) |
| 85 |
|
| 86 |
vis.selectAll('circle') |
| 87 |
.data(data) |
| 88 |
.enter().append('circle') |
| 89 |
.attr('r', function(d, i) { return random2(i) * 10 }) |
| 90 |
.attr('cx', function(d, i) { return x(i) }) |
| 91 |
.attr('cy', y) |
| 92 |
.on('mouseover', tip.show) |
| 93 |
.on('mouseout', tip.hide) |
| 94 |
}) |
| 95 |
</script> |
| 96 |
</body> |
| 97 |
</html> |