I have a .java class to read a mongodb collection and copy the contents into the .json file.
.json file is then provided to the chart (created using Fusion Charts as it only accepts data either in json format or in xml format) written in javascripts in html and then the chart is build on the basis of the data provided to the chart.
My requirement is to call a java class from java script so that it will get the data in .json file and chart would get created based on it.
How to do so?
public class Write {
//@SuppressWarnings("null")
public void connect() throws Exception{
MongoClient mongo = new MongoClient( "localhost" , 27017 );
DB db = mongo.getDB("Age");
DBCollection collection = db.getCollection("agegroup");
try{
File file = null;
String content = "{ "+
" \"chart\": { \n"+
" \"caption\" : \"Weekly Sales Summary\" ,\n"+
" \"xAxisName\" : \"Week\",\n" +
" \"yAxisName\" : \"Sales\",\n"+
" \"numberPrefix\" : \"$\" \n"+
"},\n"+
"\"data\":[\n";
// if file doesnt exists, then create it
file = new File("C:/Users/ila/Desktop/pie.json");
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
DBObject s;
String str;
String delimiter=",";
DBCursor cursorDoc = collection.find();
while (cursorDoc.hasNext()) {
s=cursorDoc.next();
str=s.toString();
String[] str1= str.split(delimiter);
bw.write("{\n");
bw.write(str1[1]+", \n");
bw.write(str1[2]+"\n");
if(cursorDoc.hasNext())
bw.write(",");
System.out.println(Arrays.toString(str1));
}
bw.write("]\n");
bw.write("}\n");
bw.close();
System.out.println("Done");
}catch(Exception e){
e.printStackTrace();
}
}
}
.html code
<!DOCTYPE html>
<html>
<head>
<title>Pie chart</title>
<script type="text/javascript" src="fusioncharts/fusioncharts.js"></script>
<script type="text/javascript" src="fusioncharts/fusioncharts.charts.js"></script>
<script type="text/javascript" src="fusioncharts/themes/fusioncharts.theme.zune.js"></script>
<script type="text/javascript">
FusionCharts.ready(function(){
var revenueChart = new FusionCharts({
//type: "pie2d",
type: "pie2d",
renderAt: "chart-container",
width: "430",
height: "450",
dataFormat: "json",
events: {
'slicingStart': function(evtObj, argObj){
var label = argObj.data.categoryLabel;
// alert(label);
var n= label.localeCompare("Teenage")
if(n==0)
{
alert("An alert Msg!!!Teenage");
// document.getElementById('iframe1').contentWindow.location.reload();
parent.frames['iframe2'].location.href = parent.frames['iframe2'].location.href
}
// document.getElementById("iframe_a").contentDocument.location.reload(true);
var m= label.localeCompare("Child")
if(m==0)
{
alert("An alert Msg!!!Child");
parent.frames['iframe2'].location.href = parent.frames['iframe2'].location.href
}
var l= label.localeCompare("Adult")
if(l==0)
{
alert("An alert Msg!!!Adult");
parent.frames['iframe2'].location.href = parent.frames['iframe2'].location.href
}
var p= label.localeCompare("Senior")
if(p==0)
{
alert("An alert Msg!!!Senior");
parent.frames['iframe2'].location.href = parent.frames['iframe2'].location.href
}
}
}
});
revenueChart.setJSONUrl("pie.json");
// revenueChart.setJSONData("C:/data/db/Age.json");
revenueChart.render("chart-container");
}
);
</script>
</head>
<body>
<div id="chart-container" > </div>
</body>
</html>