MPAAndroidChart Open Source Chart Library Pie Chart
I will introduce you to an icon open source library MPAAndroidChart. It can not only draw various statistical charts on Android devices, but also drag and zoom the charts, which is very flexible to use. MPAndroidChart also has common chart types: line charts, pie charts, bar charts and scatter charts.
mpandroidchartlibrary.jar package download address:
https://github.com/PhilJay/MPAndroidChart/releases
The following mainly implements the following pie chart:
1. Download the latest mpandroidchartlibrary-2-0-8.jar package from the above address and copy it into the project's libs
2. Define the xml file
3. The main Java logic code is as follows.
importjava.util.ArrayList; importcom.github.mikephil.charting.charts.PieChart; importcom.github.mikephil.charting.components.Legend; importcom.github.mikephil.charting.components.Legend; importcom.github.mikephil.charting.data.Entry; importcom.github.mikephil.charting.data.PieDataSet; import android.support.v7.app.ActionBarActivity; importandroid.graphics.Color; importandroid.os.Bundle; importandroid.util.DisplayMetrics; public class MainActivity extends ActionBarActivity { privatePieChartmChart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mChart = (PieChart) findViewById(R.id.spread_pie_chart); PieDatamPieData = getPieData(4, 100); showChart(mChart, mPieData); } private void showChart(PieChartpieChart, PieDatapieData) { pieChart.setHoleColorTransparent(true); pieChart.setHoleRadius(60f); //RadiuspieChart.setTransparentCircleRadius(64f); // Translucent Circle//pieChart.setHoleRadius(0) //Solid circle pieChart.setDescription("Test Pie Chart"); // mChart.setDrawYValues(true); pieChart.setDrawCenterText(true); //The text can be added in the middle of the pie chart pieChart.setDrawHoleEnabled(true); pieChart.setRotationAngle(90); // Initial rotation angle// draws the corresponding description value into the slice // mChart.setDrawXValues(true); // enable rotation of the chart by touch pieChart.setRotationEnabled(true); // Can be rotated manually// display percentage values pieChart.setUsePercentValues(true); // Display percentages// mChart.setUnit(" ); // Display percentages// mChart.setUnit(" ); // mChart.setDrawUnitsInChart(true); // add a selection listener // mChart.setOnChartValueSelectedListener(this); // mChart.setTouchEnabled(false); // mChart.setOnAnimationListener(this); pieChart.setCenterText("Quarterly Revenue"); // text in the middle of the pie chart//Set data pieChart.setData(pieData); // undo all highlights // pieChart.highlightValues(null); // pieChart.invalidate(); Legend mLegend = pieChart.getLegend(); //Set the scale graph mLegend.setPosition(LegendPosition.RIGHT_OF_CHART); //Show on the far right// mLegend.setForm(LegendForm.LINE); //Set the shape of the scale graph, the default is square mLegend.setXEntrySpace(7f); mLegend.setYEntrySpace(5f); pieChart.animateXY(1000, 1000); //Set the animation// mChart.spin(2000, 0, 360); } /** * * @param count Divided into several parts* @param range */ privatePieDatagetPieData(int count, float range) { ArrayList<String>xValues = new ArrayList<String>(); //xVals is used to represent the content on each pie for (inti = 0; i< count; i++) { xValues.add("Quarterly" + (i + 1)); //The display on the pie is Quarterly1, Quarterly2, Quarterly3, Quarterly4 } ArrayList<Entry>yValues = new ArrayList<Entry>(); //yVals is used to represent the actual data encapsulating each pie block// Pie chart data/** * Divide a pie chart into four parts, and the numerical ratio of the four parts is 14:14:34:38 * So the percentage represented by 14 is 14% */ float quarterly1 = 14; float quarterly2 = 14; float quarterly3 = 34; float quarterly4 = 38; yValues.add(new Entry(quarterly1, 0)); yValues.add(new Entry(quarterly2, 1)); yValues.add(new Entry(quarterly3, 2)); yValues.add(new Entry(quarterly4, 3)); //The collection of the y-axis PieDataSetpieDataSet = new PieDataSet(yValues, "Quarterly Revenue 2014"/*Show on the scale chart*/); pieDataSet.setSliceSpace(0f); //Set the distance between pie charts ArrayList<Integer> colors = new ArrayList<Integer>(); //Pie chart colors.add(Color.rgb(205, 205, 205)); colors.add(Color.rgb(114, 188, 223)); colors.add(Color.rgb(255, 123, 124)); colors.add(Color.rgb(57, 135, 200)); pieDataSet.setColors(colors); DisplayMetrics metrics = getResources().getDisplayMetrics(); floatpx = 5 * (metrics.densityDpi / 160f); pieDataSet.setSelectionShift(px); // The length of the selected state PieDatapieData = new PieData(xValues, pieDataSet); returnpieData; } } The renderings are as follows:
MPAAndroidChart Open Source Chart Library Line Chart
1. Copy the mandroidchartlibrary-2-0-8.jar package into the project's libs
2. Define the xml file
3. The main Java logic code is as follows.
packagecom.example.mpandroidlinechart; importjava.util.ArrayList; importcom.github.mikephil.charting.charts.LineChart; importcom.github.mikephil.charting.components.Legend; importcom.github.mikephil.charting.components.Legend; importcom.github.mikephil.charting.components.LegendForm; importcom.github.mikephil.charting.data.Entry; importcom.github.mikephil.charting.data.LineDataSet; importcom.github.mikephil.charting.data.LineDataSet; import android.support.v7.app.ActionBarActivity; importandroid.graphics.Color; importandroid.os.Bundle; public class MainActivity extends ActionBarActivity { privateLineChartmLineChart; // private Typeface mTf; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLineChart = (LineChart) findViewById(R.id.spread_line_chart); // mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Bold.ttf"); LineDatamLineData = getLineData(36, 100); showChart(mLineChart, mLineData, Color.rgb(114, 188, 223)); } // Set the display style private void showChart(LineChartlineChart, LineDatalineData, int color) { lineChart.setDrawBorders(false); //Where to add borders to the line chart// no description text lineChart.setDescription("");// Data description// If there is no data, this will be displayed, similar to listview emtpyviewlineChart.setNoDataTextDescription("You need to provide data for the chart."); // enable / disable grid background lineChart.setDrawGridBackground(false); // Whether to display the table color lineChart.setGridBackgroundColor(Color.WHITE& 0x70FFFFF); // The color of the table, here is to set a transparency for the color// enable touch gears lineChart.setTouchEnabled(true); // Set whether it can be touched// enable scaling and dragging lineChart.setDragEnabled(true); // Can you drag and drop lineChart.setScaleEnabled(true); // Can you scale// if disabled, scaling can be done on x- and y-axis separately lineChart.setPinchZoom(false); // lineChart.setBackgroundColor(color); // Set background// add data lineChart.setData(lineData); // Set data// get the legend (only possible after setting data) Legend mLegend = lineChart.getLegend(); // Set the scale icon, which is the value of the group of y // modify the legend ...// mLegend.setPosition(LegendPosition.LEFT_OF_CHART); mLegend.setForm(LegendForm.CIRCLE);// Style mLegend.setFormSize(6f);// Font mLegend.setTextColor(Color.WHITE);// Color // mLegend.setTypeface(mTf);// Font lineChart.animateX(2500); // Immediate execution of animation, x-axis} /** * Generate a data* @param count Indicates how many coordinate points there are in the chart* @param range Used to generate random numbers within range* @return */ privateLineDatagetLineData(int count, float range) { ArrayList<String>xValues = new ArrayList<String>(); for (inti = 0; i< count; i++) { // Data displayed on the x-axis, by default, numerical subscript is used to display xValues.add("" + i); } // y-axis data ArrayList<Entry>yValues = new ArrayList<Entry>(); for (inti = 0; i< count; i++) { float value = (float) (Math.random() * range) + 3; yValues.add(new Entry(value, i)); } // create a dataset and give it a type // y-axis data set LineDataSetlineDataSet = new LineDataSet(yValues, "test line chart" /*displayed on the scale chart*/); // mLineDataSet.setFillAlpha(110); // mLineDataSet.setFillColor(Color.RED); //Use the y-axis set to set the parameter lineDataSet.setLineWidth(1.75f); // Linewidth lineDataSet.setCircleSize(3f); // Display the circle size lineDataSet.setColor(Color.WHITE); // Display the color lineDataSet.setCircleColor(Color.WHITE); // The color of the circle lineDataSet.setHighLightColor(Color.WHITE); // The color of the highlighted line ArrayList<LineDataSet>lineDataSets = new ArrayList<LineDataSet>(); lineDataSets.add(lineDataSet); // add the datasets // create a data object with the datasets LineDatalineData = new LineData(xValues, lineDataSets); returnlineData; } } The renderings are as follows:
MPAAndroidChart Open Source Chart Library Bar Chart
1. Copy the mandroidchartlibrary-2-0-8.jar package into the project's libs
2. Define the xml file
3. The main Java logic code is as follows.
packagecom.jackie.mpandoidbarchart;importjava.util.ArrayList;importcom.github.mikephil.charting.charts.BarChart;importcom.github.mikephil.charting.charts.LineChart;importcom.github.mikephil.charting.components.Legend;importcom.github.mikephil.charting.components.Legend Form;importcom.github.mikephil.charting.components.XAxis;importcom.github.mikephil.charting.components.XAxis.XAxisPosition;importcom.github.mikephil.charting.data.BarData;importcom.github.mikephil.charting.data.BarDataSet;importcom.github.mikephil.charting.data.BarEntry;import android.support.v7.app.ActionBarActivity;importandroid.graphics.Color;importandroid.os.Bundle;public class MainActivity extends ActionBarActivity {privateBarChartmBarChart;privateBarDatamBarData;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mBarChart = (BarChart) findViewById(R.id.spread_bar_chart);mBarData = getBarData(4, 100); showBarChart(mBarChart, mBarData);}private void showBarChart(BarChartbarChart, BarDatabarData) {barChart.setDrawBorders(false); ///Where to add borders on line chart barChart.setDescription("");// Data Description// If there is no data, this will be displayed, similar to ListView's EmptyViewbarChart.setNoDataTextDescription("You need to provide data for the chart."); barChart.setDrawGridBackground(false); // Whether to display the table color barChart.setGridBackgroundColor(Color.WHITE& 0x70FFFFF); // The color of the table, here is to set a transparency for the color barChart.setTouchEnabled(true); // Set whether barChart.setDragEnabled(true); // Whether barChart.setScaleEnabled(true); // Whether barChart.setPinchZoom(false); // // barChart.setBackgroundColor(); // Set background barChart.setDrawBarShadow(true);barChart.setData(barData); // Set data Legend mLegend = barChart.getLegend(); // Set scale icon mLegend.setForm(LegendForm.CIRCLE); // Style mLegend.setFormSize(6f); // Font mLegend.setTextColor(Color.BLACK); // Color // X-axis setting // XAxisxAxis = barChart.getXAxis(); // xAxis.setPosition(XAxisPosition.BOTTOM);barChart.animateX(2500); // Immediate execution of animation, x-axis}privateBarDatagetBarData(int count, float range) {ArrayList<String>xValues = new ArrayList<String>();for (inti = 0; i< count; i++) {xValues.add("Th" + (i + 1) + "Quarter");}ArrayList<BarEntry>yValues = new ArrayList<BarEntry>(); for (inti = 0; i< count; i++) { float value = (float) (Math.random() * Random number within range/*100*/) + 3;yValues.add(new BarEntry(value, i)); }// Dataset of the y-axis BarDataSetbarDataSet = new BarDataSet(yValues, "test pie chart"); barDataSet.setColor(Color.rgb(114, 188, 223));ArrayList<BarDataSet>barDataSets = new ArrayList<BarDataSet>(); barDataSets.add(barDataSet); // add the datasets BarDatabarData = new BarData(xValues, barDataSets);returnbarData;}}packagecom.jackie.mpandoidbarchart;importjava.util.ArrayList;importcom.github.mikephil.charting.charts.BarChart;importcom.github.mikephil.charting.charts.LineChart;importcom.github.mikephil.charting.components.Legend;importcom.github.mikephil.charting.components.Legend;importcom.github.mikephil.charting.components. .Legend.LegendForm;importcom.github.mikephil.charting.components.XAxis;importcom.github.mikephil.charting.components.XAxis.XAxisPosition;importcom.github.mikephil.charting.data.BarData;importcom.github.mikephil.charting.data.BarDataSet;importcom.github.mikephil.charting.data.BarEntry;import android.support.v7.app.ActionBarActivity;importandroid.graphics.Color;importandroid.os.Bundle;public class MainActivity extends ActionBarActivity {privateBarChartmBarChart;privateBarDatamBarData;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mBarChart = (BarChart) findViewById(R.id.spread_bar_chart);mBarData = getBarData(4, 100); showBarChart(mBarChart, mBarData);}private void showBarChart(BarChartbarChart, BarDatabarData) {barChart.setDrawBorders(false); ///Where to add borders on line chart barChart.setDescription("");// Data Description// If there is no data, this will be displayed, similar to ListView's EmptyViewbarChart.setNoDataTextDescription("You need to provide data for the chart."); barChart.setDrawGridBackground(false); // Whether to display the table color barChart.setGridBackgroundColor(Color.WHITE& 0x70FFFFF); // The color of the table, here is to set a transparency for the color barChart.setTouchEnabled(true); // Set whether barChart.setDragEnabled(true); // Whether barChart.setScaleEnabled(true); // Whether barChart.setPinchZoom(false); // // barChart.setBackgroundColor(); // Set background barChart.setDrawBarShadow(true);barChart.setData(barData); // Set data Legend mLegend = barChart.getLegend(); // Set scale icon mLegend.setForm(LegendForm.CIRCLE); // Style mLegend.setFormSize(6f); // Font mLegend.setTextColor(Color.BLACK); // Color // X-axis setting // XAxisxAxis = barChart.getXAxis(); // xAxis.setPosition(XAxisPosition.BOTTOM);barChart.animateX(2500); // Immediate execution of animation, x-axis}privateBarDatagetBarData(int count, float range) {ArrayList<String>xValues = new ArrayList<String>();for (inti = 0; i< count; i++) {xValues.add("Th" + (i + 1) + "Quarter");}ArrayList<BarEntry>yValues = new ArrayList<BarEntry>(); for (inti = 0; i< count; i++) { float value = (float) (Math.random() * Random number within range/*100*/) + 3;yValues.add(new BarEntry(value, i)); }// Dataset of the y-axis BarDataSetbarDataSet = new BarDataSet(yValues, "test pie chart"); barDataSet.setColor(Color.rgb(114, 188, 223));ArrayList<BarDataSet>barDataSets = new ArrayList<BarDataSet>(); barDataSets.add(barDataSet); // add the datasets BarDatabarData = new BarData(xValues, barDataSets);returnbarData;}} The renderings are as follows:
The above is the introduction to the use of the MPAAndroidChart open source chart library, which is related to the pie chart, line chart and bar chart. I hope it will be helpful to everyone.