Created
September 21, 2016 03:08
-
-
Save benznest/ff2d40946d51ed6a7c2b52e0fa886feb to your computer and use it in GitHub Desktop.
My test using MPAndroidChart library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout | |
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" | |
tools:context="com.benzneststudios.mychartapplication.MainActivity"> | |
<com.github.mikephil.charting.charts.BarChart | |
android:id="@+id/bar_chart" | |
android:layout_width="match_parent" | |
android:layout_height="300dp" | |
/> | |
<com.github.mikephil.charting.charts.PieChart | |
android:id="@+id/pie_chart" | |
android:layout_width="match_parent" | |
android:layout_height="300dp" | |
android:padding="16dp" | |
android:visibility="300dp"/> | |
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.benzneststudios.mychartapplication; | |
import android.os.Bundle; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import com.github.mikephil.charting.animation.Easing; | |
import com.github.mikephil.charting.charts.BarChart; | |
import com.github.mikephil.charting.charts.PieChart; | |
import com.github.mikephil.charting.components.AxisBase; | |
import com.github.mikephil.charting.components.XAxis; | |
import com.github.mikephil.charting.components.YAxis; | |
import com.github.mikephil.charting.data.BarData; | |
import com.github.mikephil.charting.data.BarDataSet; | |
import com.github.mikephil.charting.data.BarEntry; | |
import com.github.mikephil.charting.data.Entry; | |
import com.github.mikephil.charting.data.PieData; | |
import com.github.mikephil.charting.data.PieDataSet; | |
import com.github.mikephil.charting.data.PieEntry; | |
import com.github.mikephil.charting.formatter.AxisValueFormatter; | |
import com.github.mikephil.charting.formatter.PercentFormatter; | |
import com.github.mikephil.charting.highlight.Highlight; | |
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; | |
import com.github.mikephil.charting.listener.OnChartValueSelectedListener; | |
import com.github.mikephil.charting.utils.ColorTemplate; | |
import java.util.ArrayList; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
initBarChart(); | |
initPieChart(); | |
} | |
private void initBarChart() { | |
BarChart chart = (BarChart) findViewById(R.id.bar_chart); | |
final ArrayList<BarEntry> entries = new ArrayList<>(); | |
final ArrayList<Student> listStudent = Student.getSampleStudentData(30); | |
int index = 0; | |
for (Student student : listStudent) { | |
entries.add(new BarEntry(index, student.getScore(),student.getName())); | |
index++; | |
} | |
BarDataSet dataset = new BarDataSet(entries, "# expenditure"); | |
dataset.setValueTextSize(8); | |
dataset.setColors(ColorTemplate.VORDIPLOM_COLORS); // set the color | |
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>(); | |
dataSets.add(dataset); | |
BarData data = new BarData(dataSets); // initialize BarChart | |
chart.setData(data); | |
chart.setHorizontalScrollBarEnabled(true); | |
chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM); | |
chart.getXAxis().setLabelRotationAngle(90); | |
chart.setDescription(""); // set the description | |
// chart.animateY(3000); | |
// chart.animateX(5000); | |
chart.animateXY(2000,5000, Easing.EasingOption.EaseInBounce, Easing.EasingOption.EaseInElastic); | |
YAxis RightAxis = chart.getAxisRight(); | |
RightAxis.setEnabled(false); | |
// YAxis LeftAxis = chart.getAxisLeft(); | |
// LeftAxis.setEnabled(false); | |
final XAxis xAxis = chart.getXAxis(); | |
xAxis.setTextSize(12); | |
xAxis.setDrawLimitLinesBehindData(true); | |
//xAxis.setCenterAxisLabels(true); | |
xAxis.setDrawLabels(true); | |
xAxis.setValueFormatter(new AxisValueFormatter() { | |
@Override | |
public String getFormattedValue(float value, AxisBase axis) { | |
if (value < 0 || value >= listStudent.size()) { | |
return ""; | |
} | |
return "Android-"+listStudent.get((int) value).getName(); | |
} | |
@Override | |
public int getDecimalDigits() { | |
return 0; | |
} | |
}); | |
} | |
private void initPieChart() { | |
PieChart chart = (PieChart) findViewById(R.id.pie_chart); | |
ArrayList<Student> listStudent = Student.getSampleStudentData(4); | |
ArrayList<PieEntry> entries = new ArrayList<>(); | |
for (Student student : listStudent) { | |
entries.add(new PieEntry(student.getScore(), student.getName())); | |
} | |
PieDataSet dataset = new PieDataSet(entries, "student"); | |
dataset.setSelectionShift(10); | |
dataset.setValueTextSize(14); | |
dataset.setColors(ColorTemplate.PASTEL_COLORS); // set the color | |
// dataset.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); | |
dataset.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); | |
dataset.setValueLinePart1Length(0.5f); | |
dataset.setValueLinePart2Length(0.5f); | |
dataset.setValueFormatter(new PercentFormatter()); | |
PieData data = new PieData(dataset); // initialize Piedata | |
// data.setValueFormatter(new PercentFormatter()); | |
chart.setNoDataText("No data available."); | |
chart.setData(data); | |
chart.setHoleRadius(30); | |
chart.setTransparentCircleRadius(40); | |
chart.setDescription(""); // set the description | |
chart.animateY(3000); | |
// chart.setUsePercentValues(true); | |
chart.setEntryLabelTextSize(14); | |
chart.setEntryLabelColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimaryDark)); | |
chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() { | |
@Override | |
public void onValueSelected(Entry e, Highlight h) { | |
// when select. | |
} | |
@Override | |
public void onNothingSelected() { | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.benzneststudios.mychartapplication; | |
import java.util.ArrayList; | |
/** | |
* Created by benznest on 20-Sep-16. | |
*/ | |
public class Student { | |
float score; | |
String name; | |
public Student(String name,float score) { | |
this.score = score; | |
this.name = name; | |
} | |
public float getScore() { | |
return score; | |
} | |
public void setScore(float score) { | |
this.score = score; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public static ArrayList<Student> getSampleStudentData(int size) { | |
ArrayList<Student> student = new ArrayList<>(); | |
for (int i = 0; i < size; i++) { | |
student.add(new Student("Android v."+i, (float) Math.random() * 100)); | |
} | |
return student; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment