In this Sample Demo tutorial, you will be looking at How to Create a Drill Down Pie Chart in DOJO.
It uses a CSV file exported from a excel sheet and Read pro grammatically from DOJO code.
It is then Parsed to JSON so as to feed it to the Pie Chart.
It also cracks the Code of finding the item Clicked (Slice) in a pie chart to drill down to next level of data.
Index.html (which contains basic div for Mobile View and Charts)
Src.js file
It uses a CSV file exported from a excel sheet and Read pro grammatically from DOJO code.
It is then Parsed to JSON so as to feed it to the Pie Chart.
It also cracks the Code of finding the item Clicked (Slice) in a pie chart to drill down to next level of data.
Source: copythe link below and open in new Window
kiranatious.gofreeserve.com.0lx.org/dojo/drilldown/pieChart.zip
Index.html (which contains basic div for Mobile View and Charts)
- <!DOCTYPE HTML>
- <html lang="en">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <style>
- @import "../../dojo/resources/dojo.css";
- @import "../../dijit/themes/dijit.css";
- @import "../../dojox/charting/resources/Legend.css";
- body {
- background: #181818;
- }
- .dojoxLegendNode {
- background-color:#181818;
- border:1px solid #CCCCCC;
- margin:0;
- padding:0;
- }
- .dojoxLegendText {
- color: rgb(204, 204, 204);
- }
- h1, p {
- color: rgb(204, 204, 204);
- }
- </style>
- </head>
- <body class="claro">
- <div id="view1" data-dojo-type="dojox.mobile.View" >
- <h1 id="headerId" data-dojo-type="dojox.mobile.Heading" label="Drill Down Pie Chart">
- <div id="backButtonId" class="mblArrowButton" style="width: 67px;display: none" onclick="backFired()">
- </div>
- </h1>
- <div style="width: 100%; height: 100%" align="center">
- <div id="pieChart" style="width: 550px; height: 550px; text-align: center" >
- </div>
- <div id="legend">
- </div>
- </div>
- </div>
- </body>
- </html>
Src.js file
- var pieChart;
- var legend = null;
- var gObj = [];
- var objCount =0;
- var storeJson =[];
- var titleStack = [];
- require(["dojox/charting/Chart", "dojox/charting/plot2d/Pie",
- "dojox/charting/action2d/Tooltip",
- "dojox/charting/themes/Tom",
- "dojox/charting/widget/Legend", "dojo/ready","dojox/mobile/parser",
- "dojox/mobile/View", "dojox/mobile/Heading","dojox/mobile/deviceTheme",
- "dojox/mobile/Button","dojox/data/CsvStore","dojo/store/DataStore"],
- function(Chart, Pie, Tooltip, Tom, Legend, ready, parser){
- //programmatically reading CSV from external file
- var personStoreForGrid = new dojox.data.CsvStore({
- url:"salesdata_distributor.csv"
- });
- //storing to DataStore
- var objectStore = dojo.store.DataStore({
- store: personStoreForGrid
- });
- //On relustant Data load & Parsing CSV data into Json Format which can be fed into Pie chart
- objectStore.query().then(function(results){
- //alert(results.length);
- var parentrow=results[0].RowLabels;
- var primaryval = [];
- var secondaryval = [];
- var finalJson ;
- var nextVal=0;
- //var primaryjson;
- //var secondaryjson;
- //parseInt(a)+parseInt(b);
- var aggrsum = parseInt(results[0].SecondarySalesValue)+parseInt(results[0].PrimarySalesValue);
- finalJson={text: parentrow+'('+aggrsum+')',y: results[0].SecondarySalesValue};
- //alert("First Time Parent Row = "+parentrow)
- for(var i = 1; i < results.length; ++i) {
- if(results[i].RowLabels!==undefined && results[i].RowLabels!==parentrow){
- finalJson.child=[{text: "primary("+results[nextVal].PrimarySalesValue+")",y: results[nextVal].PrimarySalesValue,child:primaryval},{text: "secondary("+results[0].SecondarySalesValue+")",y: results[nextVal].SecondarySalesValue,child:secondaryval}];
- storeJson.push(finalJson);
- parentrow = results[i].RowLabels;
- //alert("Subsequently Parent Row = "+parentrow)
- finalJson=null;
- nextVal=i;
- var aggrsum = parseInt(results[i].SecondarySalesValue)+parseInt(results[i].PrimarySalesValue);
- finalJson={text: parentrow+'('+aggrsum+')',y: aggrsum};
- primaryval = [];
- secondaryval = [];
- }
- else
- {
- primaryval.push({text: results[i].ChildRowLabels+"("+results[i].PrimarySalesValue+")",y: results[i].PrimarySalesValue});
- secondaryval.push({text: results[i].ChildRowLabels+"("+results[i].SecondarySalesValue+")",y: results[i].SecondarySalesValue});
- }
- }
- finalJson.child=[{text: "primary("+results[nextVal].PrimarySalesValue+")",y: results[nextVal].PrimarySalesValue,child:primaryval},{text: "secondary("+results[nextVal].SecondarySalesValue+")",y: results[nextVal].SecondarySalesValue,child:secondaryval}];
- storeJson.push(finalJson);
- pieChart.updateSeries("Series A",storeJson);
- pieChart.render();
- legend.refresh();
- // alert(JSON.stringify(storeJson));
- //alert("json created");
- });
- ready(function(){
- titleStack[0] = "Drill Down Pie Chart";
- //var temp = dijit.byId("headerId");
- //.set("label", "titleStack[0]");
- //document.getElementById("headerId").setAttribute('label','yes') ;
- parser.parse();
- pieChart = new Chart("pieChart");
- pieChart.setTheme(Tom).addPlot("default", {
- type: "Pie",
- font: "normal normal 10pt Tahoma",
- fontColor: "#ccc",
- labelWiring: "#ccc",
- radius: 150,
- labelStyle: "columns",
- htmlLabels: true,
- startAngle: -10
- }).addSeries("Series A", storeJson);
- //Logic to handle Item Clicked on the Pie Chart for Drilling down to the Next Level
- var h = pieChart.connectToPlot("default", function(o){
- if(o.type == "onclick"){
- var test1 = storeJson;
- var childItem = o.run.data[o.index].child;
- if(childItem != undefined)
- {
- var str = (o.chart.series[0].data[o.index].text);
- titleStack[titleStack.length] = str;
- var temp = dijit.byId("headerId");
- temp.set("label", str);
- document.getElementById("backButtonId").style.display="";
- ++objCount;
- gObj[objCount] = childItem;
- drillDown(childItem);
- //document.getElementById("headerId").setAttribute('label', "str") ;
- }
- }
- });
- gObj[0] = storeJson;
- var anim_c = new Tooltip(pieChart, "default");
- pieChart.render();
- legend = new Legend({
- chart: pieChart,
- horizontal:true}, "legend");
- });
- });
- // Logic to Move Back to previous Level of Stack
- function backFired()
- {
- //alert(storeJson);
- if(objCount==1){
- document.getElementById("backButtonId").style.display="none";
- }
- if(objCount >= 0)
- {
- titleStack.pop();
- str = titleStack[titleStack.length - 1];
- var temp = dijit.byId("headerId");
- temp.set("label", str);
- gObj[objCount] = null;
- --objCount;
- var childItem = gObj[objCount];
- drillDown(childItem);
- }
- }
- //logic to Drill down on Click of the Item in a pie Chart
- function drillDown(item)
- {
- pieChart.updateSeries("Series A",item);
- pieChart.render();
- legend.refresh();
- }
Can I get a copy of the .csv file? The demo site doesn't respond and I'm not sure the format of the data to create the chart.
ReplyDeleteThanks,
Mark
Thanks A lot fot his efforts :)
ReplyDeleteMark, This is the CSV
Hi Mark, please find the CSV file in the below link
ReplyDeletehttp://kiranatious.gofreeserve.com.0lx.org/dojo/drilldown/salesdata_distributor.csv