I'm working on a pie chart that has the following functionality:
- Section will be highlighted when "mouseover" occurred.
- Section will be zoomed in when "click" is performed.
I have written the following code that is able to do the first job (highlighting). Currently, I'm having a hard time to make the second functionality to work. I tried to select arcs/paths, and to transform its coordinate to zoom in. I also tried to adopt the idea from the following link:
http://bl.ocks.org/mbostock/2206590 But I didn't make it to work, Any ideas to solve the problem? Your suggestion is greatly appreciated!
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.arc path {
stroke: #fff;
}
#code_hierarchy_legend
{
height:100px;
font-size:1.4em;
text-align:center;
}
</style>
var width = 1200,
height = 700,
radius = Math.min(width, height) / 3;
var color = d3.scale.category20();
var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(0);
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d.count; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
d3.csv("data.csv", function(error, data) {
data.forEach(function(d) {
d.count = +d.count;
});
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc")
.on("mouseover", function(g, i){
svg.selectAll(".arc").filter(function(d, ii){return ii != i && ii != i;})
.transition(0)
.style("opacity", 0.05);
})
.on("mouseout", function(g, i){
svg.selectAll(".arc").filter(function(d, ii){return ii != i && ii != i;})
.transition(0)
.style("opacity", 1);
})
.on("click", clicked);
// append paths to 'g'
var path = g.append("path");
path.attr("d", arc)
.style("fill", function(d) { return color(d.data.category); });
g.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) { return d.data.category +", "+ d.data.count; });
// somehow the clicked() function does not work as I expected
function clicked(){
console.log("called");
var x, y, k;
x = 200;
y = 200;
k = 4;
g.selectAll("path").classed("active", true);
g.transition().duration(750)
.attr("transform", "translate("+width/2+", "+height/2+")scale("+k+")translate")
.style("stroke", 1.5 / k + "px")
}
</script>