Senin, 23 Juli 2018

Grafik pada ANdroid




Pada Manifest tambahkan:
 <activity android:name="org.achartengine.GraphicalActivity" />


Kode java
package in.wptrafficanalyzer.achartenginelinechart;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class Grafik extends Activity {
int[] x = { 1,2,3,4,5,6,7,8,9,10,11,12 };
double[] bb = { 3,4,5,6,7,8,9,10,11,11,12,13};
double[] tb = {45, 50, 55, 60, 65, 66, 67, 70,71,73,75,80 };
int jd=0;
dbHelperTimbang helper=null;

String id,nama,ortu,tgllahir,jk,ket;
private String[] mMonth = new String[] {
"Jan", "Feb" , "Mar", "Apr", "May", "Jun",
"Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"
};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grafik);
        Intent i = this.getIntent();
        id=i.getStringExtra("id");
        nama=i.getStringExtra("nama");
        ortu=i.getStringExtra("ortu");
        tgllahir=i.getStringExtra("tgllahir");
        jk=i.getStringExtra("jk");
        ket=i.getStringExtra("ket");
        helper=new dbHelperTimbang(this);
     
        Cursor c=helper.getAll(id);
jd=c.getCount();
        c.moveToFirst();
bb=new double[jd];
tb=new double[jd];
mMonth=new String[jd];

for(int h=0;h<jd;h++){
String sa=helper.getberatbadan(c);
String du=helper.gettinggibadan(c);
String ti=helper.gettgltimbang(c);
Log.v("OK",h+"="+sa+"#"+du+"#"+ti+"#"+jd);
bb[h]=Double.parseDouble(sa);
tb[h]=Double.parseDouble(du);
mMonth[h]=ti;
try{c.moveToNext();}
catch(Exception ee){}
}
try{c.close(); }
catch(Exception ee){}
 
     
       openChart();
    }
 
    private void openChart(){
    XYSeries bbSeries = new XYSeries("berat");
    XYSeries tbSeries = new XYSeries("tinggi");
    for(int i=0;i<x.length;i++){
    Log.v("OKR",i+"="+x[i]+"#"+ bb[i]+"#"+tb[i]+"#"+x.length);
    bbSeries.add(x[i], bb[i]);
    tbSeries.add(x[i],tb[i]);
    }
   
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    dataset.addSeries(bbSeries);
    dataset.addSeries(tbSeries);   
   
    XYSeriesRenderer bbRenderer = new XYSeriesRenderer();
    bbRenderer.setColor(Color.WHITE);
    bbRenderer.setPointStyle(PointStyle.CIRCLE);
    bbRenderer.setFillPoints(true);
    bbRenderer.setLineWidth(2);
    bbRenderer.setDisplayChartValues(true);
   
    XYSeriesRenderer tbRenderer = new XYSeriesRenderer();
    tbRenderer.setColor(Color.YELLOW);
    tbRenderer.setPointStyle(PointStyle.CIRCLE);
    tbRenderer.setFillPoints(true);
    tbRenderer.setLineWidth(2);
    tbRenderer.setDisplayChartValues(true);
   
   
    XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
    multiRenderer.setXLabels(0);
    multiRenderer.setChartTitle("Gizi Balita "+nama);
    multiRenderer.setXTitle("Periode");
    multiRenderer.setYTitle("BB/TB");
    multiRenderer.setZoomButtonsVisible(true);   
    int by=x.length;
    if(jd<by){by=jd;}
    for(int i=0;i<jd;i++){//x.length
    String bul=mMonth[i];
    String[]ar=bul.split("-");//Y-m-d
    String bl=ar[1];
    multiRenderer.addXTextLabel(i+1, ar[1]);   
    }   
   
    multiRenderer.addSeriesRenderer(bbRenderer);
    multiRenderer.addSeriesRenderer(tbRenderer);
   
    Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer);
    startActivity(intent);
   
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.grafik, menu);
        return true;
    }
}

kode xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background = "@drawable/bg4" >

</RelativeLayout>

download library

Tidak ada komentar:

Posting Komentar