Jun 17, 2014

Using chart in android 2 SMOOTHIE CHART




This is second post for using chart in android.

First chart is 'Google chart'. click to my post.

This post introduce 'Smoothie chart'.

Smoothie chart is realtime chart.

So if you want show realtime data, using this chart.

Smoothie chart page is here!!

You will get library file!!



Click this button and all select text and copy.


And make this file  project/assets/www/smoothie.js in eclipse and paste text.

And then make html file, example index2.html my case.

This is html code.
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <script type="text/javascript" src="smoothie.js"></script>
  5.     <script type="text/javascript">
  6.     var arr;
  7.    
  8.     var line = new TimeSeries();
  9.    
  10.    
  11.     function setMessage(msg){
  12.         arr=1;
  13.         filter=msg.split('|');
  14.         arr*=filter[1];
  15.         document.getElementById("title").innerHTML=filter[0];   //input name
  16.    
  17.     }
  18.    
  19.     function createTimeline(msg) {
  20.         var chart = new SmoothieChart({
  21.             millisPerPixel:50,
  22.             grid:{fillStyle:'#ffffff'},
  23.             labels:{fillStyle:'#000000',precision:1},
  24.             timestampFormatter:SmoothieChart.timeFormatter
  25.             });
  26.        
  27.        // Add a random value to each line every second
  28.         setInterval(function() {
  29.           line.append(new Date().getTime(), arr);   //input data
  30.         }1000);
  31.         // Add to SmoothieChart
  32.         chart.addTimeSeries(line,{
  33.             strokeStyle:'rgb(0, 255, 0)',
  34.             lineWidth:3,
  35.             fillStyle:'rgba(0,0,0,0.30)'});//config line
  36.        
  37.        
  38.         chart.streamTo(document.getElementById("chart")1000)//draw
  39.       }
  40.     </script>
  41.   </head>
  42.   <body onload="createTimeline()">
  43.     <p>Show Chart!!</p>
  44.     <p id="title"></p>
  45.     <canvas id="chart" width="500" height="200"></canvas>
  46.   </body>
  47. </html>
So. simple!!

And Java code is similar to google chart.

This is java code.
  1.  private void init() {
  2.         String url = "file:///android_asset/www/index2.html"//smoothie chart
  3.                    String sendChart="volt|222.1"//data
  4.         chart=(WebView)findViewById(R.id.chart);
  5.         chart.getSettings().setJavaScriptEnabled(true);
  6.         chart.loadUrl(url);
  7.         chart.loadUrl("javascript:setMessage('" + sendChart + "')");
  8.  }
Data is simple than google chart.

Don't need an array data!!

Look at smoothie Chart!



No comments:

Post a Comment