Skip to content

Instantly share code, notes, and snippets.

@Benjit87
Created December 6, 2012 08:30
Show Gist options
  • Save Benjit87/4222743 to your computer and use it in GitHub Desktop.
Save Benjit87/4222743 to your computer and use it in GitHub Desktop.
/* A Simple Java class just to write a google chart html when called by BASE SAS*/
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Chart {
HashMap<String,Integer> map = new HashMap<String,Integer>();
public Chart()
{
System.out.println("Chart obj created");
}
//write the html file
public void write()
{
System.out.println(map.size());
String html = "<html>\n";
html += "<head>\n";
html += "<script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script>\n";
html += "<script type=\"text/javascript\">\n";
html += "google.load('visualization', '1.0', {'packages':['corechart']});\n";
html += "google.setOnLoadCallback(drawChart);\n";
html += "function drawChart() {\n";
html += "var data = new google.visualization.DataTable();\n";
html += "data.addColumn('string', 'Topping');\n";
html += "data.addColumn('number', 'Slices');\n";
html += "data.addRows([\n";
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
html += "['" + pairs.getKey() +"',"+pairs.getValue()+ "]";
if (it.hasNext()) { html += ",\n"; } else { html +="\n";}
}
html += "]);\n";
html += "var options = {'title':'How Much Pizza I Ate Last Night','width':400,'height':300};\n";
html += "var chart = new google.visualization.PieChart(document.getElementById('chart_div'));\n";
html += "chart.draw(data, options);}\n";
html += "</script>\n";
html += "</head>\n";
html += "<body>\n";
html += "<div id=\"chart_div\"></div>\n";
html += "</body>\n";
html += "</html>";
FileWriter fstream;
try {
fstream = new FileWriter("C:\\Users\\Benji_Thian\\Desktop\\chart.html");
BufferedWriter out = new BufferedWriter(fstream);
out.write(html);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//map dataset to store the SAS Data
public void put(String id,String count)
{
map.put(id, Integer.parseInt(count.trim()));
System.out.println(id+"|"+count+"|"+map.size());
}
//For testing purposes
public static void main(String[] args)
{
Chart test = new Chart();
test.put("test", "1");
test.write();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment