amcharts Slice Subdivision
HTML
<script src="http://www.amcharts.com/lib/amcharts.js" type="text/javascript"></script>
<div id="chartdiv" style="width: 100%; height: 362px;"></div>
JavaScript
var chart;
var legend;
var selected;
var types = [{
type: "Fossil Energy",
percent: 70,
color: "#ff9e01",
subs: [
{ type: "Oil", percent: 15 },
{ type: "Coal", percent: 35 },
{ type: "Nuclear", percent: 20 }
]},
{
type: "Green Energy",
percent: 30,
color: "#b0de09",
subs: [
{ type: "Hydro", percent: 15 },
{ type: "Wind", percent: 10 },
{ type: "Other", percent: 5 }
]}
];
function generateChartData () {
var chartData = [];
for (var i = 0; i < types.length; i++) {
if (i == selected) {
for (var x = 0; x < types[i].subs.length; x++) {
chartData.push({
type: types[i].subs[x].type,
percent: types[i].subs[x].percent,
color: types[i].color,
pulled: true
});
}
}
else {
chartData.push({
type: types[i].type,
percent: types[i].percent,
color: types[i].color,
id: i
});
}
}
return chartData;
}
AmCharts.ready(function() {
// PIE CHART
chart = new AmCharts.AmPieChart();
chart.dataProvider = generateChartData();
chart.titleField = "type";
chart.valueField = "percent";
chart.outlineColor = "#FFFFFF";
chart.outlineAlpha = 0.8;
chart.outlineThickness = 2;
chart.colorField = "color";
chart.pulledField = "pulled";
chart.depth3D = 15;
chart.angle = 30;
// ADD TITLE
chart.addTitle("Click a slice to see the details");
// AN EVENT TO HANDLE SLICE CLICKS
chart.addListener("clickSlice", function (event) {
if (event.dataItem.dataContext.id != undefined) {
selected = event.dataItem.dataContext.id;
}
else {
selected = undefined;
}
chart.dataProvider = generateChartData();
...