Jun 17, 2014

Using chart in android 1 GOOGLE CHART



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.

  1.       function setMessage(msg){
  2.          
  3.           arr=msg;
  4.           google.setOnLoadCallback(drawChart);
  5.       }
  6.      
  7.       function drawChart() {
  8.          
  9.           filt=arr.split("|");  //filtering
  10.           filt2=filt[0].split(",")//filtering
  11.          
  12.           title[0][0]=filt2[0]//x
  13.           title[0][1]=filt2[1]//title
  14.          
  15.           data=filt[1].split("/");//data filter
  16.          
  17.           arrRange = data[0].split(",").length;
  18.      
  19.      
  20.          for(i=0;i<data.length;i++){
  21.             tempdata=data[i].split(",");
  22.             chartdata[i][0]=tempdata[0];
  23.             chartdata[i][1]*=tempdata[1];
  24.             }
  25.          
  26.         showchart=title.concat(chartdata);
  27.          
  28.         var data = google.visualization.arrayToDataTable(showchart);
  29.         var options = {
  30.           curveType: 'function',
  31.          
  32.           title: showchart[0][1]
  33.          
  34.        
  35.         };
  36.         var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
  37.         chart.draw(data, options);
  38.        
  39.       }
function setMessage(msg) is receive value function.

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.

  1.  private void init() {
  2.         String url = "file:///android_asset/www/index.html";
  3.         //google chart
  4.                    String sendChart="volt|10:01:00,222.1,10:01:01,223.0,10:01:02,222.8,10:01:03,222.2;  //data

  5.         chart=(WebView)findViewById(R.id.chart);
  6.         chart.getSettings().setJavaScriptEnabled(true);
  7.         chart.loadUrl(url);
  8.         chart.loadUrl("javascript:setMessage('" + sendChart + "')");
  9.  }
chart is webview.

'chart.loadUrl("javascript:setMessage('" + sendChart + "')");' is sending msg to html.

This is google chart!!




So simple and beauty.




No comments:

Post a Comment