sumber : http://agusharyanto.net/wordpress/?p=1438
1. Buat project dengan nama “LearnMPChart” domain diisi dengan “agusharyanto.net” pada saat activity pilih “Empty Activity” klik Next terus sampai finish.
2. Edit Buid.Gradle (Module) tambahkan libray MPChart.
5 |
compile fileTree(dir: 'libs', include: ['*.jar']) |
6 |
testCompile 'junit:junit:4.12' |
7 |
compile 'com.android.support:appcompat-v7:23.2.1' |
8 |
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4' |
3.Edit activity_main lalu ketikan kode berikut
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
04 |
android:layout_width = "match_parent" |
05 |
android:layout_height = "match_parent" |
06 |
android:paddingBottom = "@dimen/activity_vertical_margin" |
07 |
android:paddingLeft = "@dimen/activity_horizontal_margin" |
08 |
android:paddingRight = "@dimen/activity_horizontal_margin" |
09 |
android:paddingTop = "@dimen/activity_vertical_margin" |
10 |
tools:context = "net.agusharyanto.learnmpchart.MainActivity" > |
11 |
< com.github.mikephil.charting.charts.BarChart |
12 |
android:id = "@+id/chart" |
13 |
android:layout_width = "match_parent" |
14 |
android:layout_height = "match_parent" /> |
4. Sekarang mari kita edit MainActivity.java lalu ketikan kode berikut
01 |
package net.agusharyanto.learnmpchart; |
02 |
import android.graphics.Color; |
03 |
import android.os.Bundle; |
04 |
import android.support.v7.app.AppCompatActivity; |
05 |
import com.github.mikephil.charting.charts.BarChart; |
06 |
import com.github.mikephil.charting.data.BarData; |
07 |
import com.github.mikephil.charting.data.BarDataSet; |
08 |
import com.github.mikephil.charting.data.BarEntry; |
09 |
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; |
10 |
import java.util.ArrayList; |
11 |
public class MainActivity extends AppCompatActivity { |
13 |
protected void onCreate(Bundle savedInstanceState) { |
14 |
super .onCreate(savedInstanceState); |
15 |
setContentView(R.layout.activity_main); |
16 |
BarChart chart = (BarChart) findViewById(R.id.chart); |
17 |
BarData data = new BarData(getXAxisValues(), getDataSet()); |
20 |
chart.setDescription( "My Chart" ); |
21 |
chart.animateXY( 2000 , 2000 ); |
24 |
private ArrayList<IBarDataSet> getDataSet() { |
25 |
ArrayList<IBarDataSet> dataSets = null ; |
26 |
ArrayList<BarEntry> valueSet1 = new ArrayList<>(); |
27 |
BarEntry v1e1 = new BarEntry( 110 .000f, 0 ); |
29 |
BarEntry v1e2 = new BarEntry( 40 .000f, 1 ); |
31 |
BarEntry v1e3 = new BarEntry( 60 .000f, 2 ); |
33 |
BarEntry v1e4 = new BarEntry( 30 .000f, 3 ); |
35 |
BarEntry v1e5 = new BarEntry( 90 .000f, 4 ); |
37 |
BarEntry v1e6 = new BarEntry( 100 .000f, 5 ); |
39 |
ArrayList<BarEntry> valueSet2 = new ArrayList<>(); |
40 |
BarEntry v2e1 = new BarEntry( 150 .000f, 0 ); |
42 |
BarEntry v2e2 = new BarEntry( 90 .000f, 1 ); |
44 |
BarEntry v2e3 = new BarEntry( 120 .000f, 2 ); |
46 |
BarEntry v2e4 = new BarEntry( 60 .000f, 3 ); |
48 |
BarEntry v2e5 = new BarEntry( 20 .000f, 4 ); |
50 |
BarEntry v2e6 = new BarEntry( 80 .000f, 5 ); |
52 |
BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Bakso" ); |
53 |
barDataSet1.setColor(Color.rgb( 0 , 155 , 0 )); |
54 |
BarDataSet barDataSet2 = new BarDataSet(valueSet2, "Mie Ayam" ); |
56 |
barDataSet2.setColor(Color.rgb( 193 , 37 , 82 )); |
57 |
dataSets = new ArrayList<>(); |
58 |
dataSets.add(barDataSet1); |
59 |
dataSets.add(barDataSet2); |
62 |
private ArrayList<String> getXAxisValues() { |
63 |
ArrayList<String> xAxis = new ArrayList<>(); |
6. Sekarang mari kita Run aplikasinya. Maka akan tampil seperti ini
Sudah terbuktikan mudah membuat grafik atau chart di android.
Kalau diaplikasi inikan datanya hardcode, coba kalian buat yang datanya dari database sepert sqlite atau MySQL
Salam Hangat Developer Android
Komentar Terbaru