I am in the process of learning charting with the d3 library, I'm not developing for a web page but am using a web viewer within a Filemaker database. I have the following code, which plots a pie chart with d3.min.js library. The pie chart plots fine using the data array shown, however I am trying to plot the pie chart using the array of objects also shown. I can't seem to get things formatted correctly for an array of objects dataset. What would I need to change to format correctly?
Secondly where would I add the onclick attribute,
.attr("onclick", function(d, i){
return "location.href='" + script + "¶m=" + d.value[0] + "'";
});
so I can run a Filemaker script when I click on a pie slice? Is this the correct location and syntax to grab the x object value, currently I have it under var arcs?
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Pie layout, pie chart</title>
<script type="text/javascript" src="d3.js"></script>
<style type="text/css">
body {
background-color: white; //color of chart background
}
svg {
background-color: white; //color of smaller rectangle behind pit
}
text {
font-family: sans-serif;
font-size: 12px;
fill: white;
}
</style>
</head>
<body>
<script type="text/javascript">
var script = encodeURI('fmp://$/{{FileName}}?script=BarClick');
var w = 300;
var h = 300;
var dataset = [ {x: 1,y: 5}, {x: 2,y: 10}, {x: 3,y: 20}, {x: 4,y: 45}, {x: 5,y: 6}, {x: 6,y: 25} ];
//var dataset = [ 5, 10, 20, 45, 6, 25 ];
var outerRadius = w / 2;
var innerRadius = 0;
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var pie = d3.layout.pie();
var color = d3.scale.category20();
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
var arcs = svg.selectAll("g.arc")
.data(pie(dataset))
.enter()
.append("g")
.attr("class", "arc")
.attr("transform", "translate(" + outerRadius + "," + outerRadius + ")")
.attr("onclick", function(d, i){
return "location.href='" + script + "¶m=" + d.value[0] + "'";
});
arcs.append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", arc);
arcs.append("text")
.attr("transform", function(d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function(d) {
return d.value;
});
</script>
</body>
</html>