Using chart in android is easy.
There are 2-type method.
First method is using java library like jfreechart.
Second method is using web library like google chart.
This post is using second method.
using google chart is simple!!
You just visit this page and select chart type.
You can see HTML code & API.
You just copy&paste.
Look at my code.
- function setMessage(msg){
- arr=msg;
- google.setOnLoadCallback(drawChart);
- }
- function drawChart() {
- filt=arr.split("|"); //filtering
- filt2=filt[0].split(","); //filtering
- title[0][0]=filt2[0]; //x
- title[0][1]=filt2[1]; //title
- data=filt[1].split("/");//data filter
- arrRange = data[0].split(",").length;
- for(i=0;i<data.length;i++){
- tempdata=data[i].split(",");
- chartdata[i][0]=tempdata[0];
- chartdata[i][1]*=tempdata[1];
- }
- showchart=title.concat(chartdata);
- var data = google.visualization.arrayToDataTable(showchart);
- var options = {
- curveType: 'function',
- title: showchart[0][1]
- };
- var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
- chart.draw(data, options);
- }
Android sending value to function 'setMessage()'.
And setMessage() call drawChart().
'drawChart()' drawing the chart.
Sure that android using webview and javascrpit.
Need a this java code.
- private void init() {
- String url = "file:///android_asset/www/index.html";
- //google chart
- chart=(WebView)findViewById(R.id.chart);
- chart.getSettings().setJavaScriptEnabled(true);
- chart.loadUrl(url);
- chart.loadUrl("javascript:setMessage('" + sendChart + "')");
- }
'chart.loadUrl("javascript:setMessage('" + sendChart + "')");' is sending msg to html.
This is google chart!!
So simple and beauty.
No comments:
Post a Comment