Skip to content

Instantly share code, notes, and snippets.

@dz1984
Forked from zbryikt/README.md
Last active August 29, 2015 14:20
Show Gist options
  • Save dz1984/6f8d953558e8532b1771 to your computer and use it in GitHub Desktop.
Save dz1984/6f8d953558e8532b1771 to your computer and use it in GitHub Desktop.
date weight label
1.0 4.0 台灣民主危機
1.0 1.2 立委張慶忠的座位
2.0 1.7 EventA
2.0 1.3 EventA
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
#tooltip {
position: absolute;
width: 200px;
height: auto;
padding: 10px;
background-color: white;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
-webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
-moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
pointer-events: none;
}
#tooltip.hidden {
display: none;
}
#tooltip p {
margin: 0;
font-family: sans-serif;
font-size: 16px;
line-height: 20px;
}
</style>
<body>
<div id="tooltip" class="hidden">
<p><span id="label">100</span></p>
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 20, left: 40},
width = 960 - margin.left - margin.right,
height = 650 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
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) {
data.forEach(function(d) {
d.date = +d.date;
d.weight = +d.weight;
d.delta = Math.random();
});
x.domain(d3.extent(data, function(d) { return d.date; })).nice();
y.domain(d3.extent(data, function(d) { return d.weight; })).nice();
var rx = d3.scale.linear().domain([1.0,5.0]).range([0,2*Math.PI]);
var ry = d3.scale.linear().domain([1.0,8.0]).range([50,300]);
cr = ry.ticks(7);
svg.selectAll("circle.line").data(cr)
.enter().append("circle").attr("class","line")
.attr({
cx: width / 2 + margin.left,
cy: height / 2 + margin.top,
r: function(d) { return ry(d); },
fill: "none",
stroke: "#999"
});
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 8)
.attr("cx", function(d) {
angle = rx((d.weight + d.delta));
radius = ry((d.date));
return Math.cos(angle) * radius + width / 2 + margin.left;
})
.attr("cy", function(d) {
angle = rx((d.weight + d.delta));
radius = ry((d.date));
return Math.sin(angle) * radius + height / 2 + margin.top;
})
.style("fill", function(d) { return 'white'; })
.on('mouseover', function(d) {
var xPosition = parseFloat(d3.select(this).attr("cx")+ margin.left);
var yPosition = parseFloat(d3.select(this).attr("cy")+ margin.top);
//Update the tooltip position and value
d3.select("#tooltip")
.style("left", xPosition + "px")
.style("top", yPosition + "px")
.select("#label")
.text(d.label);
//Show the tooltip
d3.select("#tooltip").classed("hidden", false);
})
.on('mouseout', function(d) {
d3.select("#tooltip").classed("hidden", true);
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment