Open-source chart library MPAndroidChart introduction-pie chart & amp; line chart & amp; bar chart, chart mpandroidchart

Source: Internet
Author: User
Tags float range

Introduction to open-source chart library MPAndroidChart-pie chart & line chart & Column chart, and chart mpandroidchart

MPAndroidChart open-source chart library-pie chart

We will introduce an open-source icon library MPAndroidChart, which not only allows you to draw various statistical charts on Android devices, but also allows you to drag and scale charts, which are very flexible to use. MPAndroidChart also has common chart types: line chart, pie chart, column chart, and scatter chart.

Mpandroidchartlibrary. jar package:

Https://github.com/PhilJay/MPAndroidChart/releases

The following is a pie chart:

1. download the latest mpandroidchartlibrary-2-0-8.jar package from the address above and copy it to the libs of the project

2. Define an 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. legendPosition; importcom. github. mikephil. charting. data. entry; importcom. github. mikephil. charting. data. pieData; 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); // radius pieChart. setTransparentCircleRadius (64f); // translucent ring // pieChart. setHoleRadius (0) // solid circle pieChart. setDescription ("test pie chart"); // mChart. setDrawYValues (true); pieChart. setDrawCenterText (true); // you can add text pieChart to the pie chart. 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); // You can manually rotate it. // display percentage values pieChart. setUsePercentValues (true); // displayed as a percentage // 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 the data pieChart. setData (pieData); // undo all highlights // pieChart. highlightValues (null); // pieChart. invalidate (); Legend mLegend = pieChart. getLegend (); // you can specify the mLegend. setPosition (LegendPosition. RIGHT_OF_CHART); // the rightmost display // mLegend. setForm (LegendForm. LINE); // set the scale chart shape. The default value is square mLegend. setXEntrySpace (7f); mLegend. setYEntrySpace (5f); pieChart. animateXY (1000,100 0); // sets the animation // mChart. spin (2000, 0,360);}/*** @ param count is divided into several parts * @ param range */privatepieappsetpiedata (int count, float range) {ArrayList <String> xValues = new ArrayList <String> (); // xVals is used to represent the content on each pie block for (inti = 0; I <count; I ++) {xValues. add ("Quarterly" + (I + 1); // Quarterly1, Quarterly2, Quarterly3, quarterly4} ArrayList <Entry> yValues = new ArrayList <Entry> (); // yVals is used to encapsulate the actual data of each pie block // pie chart data/*** divide a pie chart into four parts, with a ratio of 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); // set PieDataSetpieDataSet = new PieDataSet (yValues, "Quarterly Revenue 2014"/* displayed on the scale chart */); pieDataSet. setSliceSpace (0f); // set the distance between pie charts. ArrayList <Integer> colors = new ArrayList <Integer> (); // the color of the 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 PieDatapieData = new PieData (xValues, pieDataSet); returnpieData ;}}

As follows:

Line chart of MPAndroidChart open source chart Library

1. copy the mpandroidchartlibrary-2-0-8.jar package to the project's libs

2. Define an 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. legendForm; importcom. github. mikephil. charting. data. entry; importcom. github. mikephil. charting. data. lineData; 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); // whether to add a border on the line chart // no description text lineChart. setDescription (""); // data description // This is displayed if no data exists, similar to the emtpyviewlineChart of listview. 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 & 0x70FFFFFF); // table color. Here, we set a transparency for the color. // enable touch gestures lineChart. setTouchEnabled (true); // you can set whether to touch or enable scaling and dragging lineChart. setDragEnabled (true); // whether lineChart can be dragged. setScaleEnabled (true); // whether scaling is allowed // if disabled, scaling can be done on x-and y-axis separately lineChart. setPinchZoom (false); // lineChart. setBackgroundColor (color); // set the background // add data lineChart. setData (lineData); // set data // get the legend (only possible after setting data) Legend mLegend = lineChart. getLegend (); // set the ratio 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); // animation to be executed immediately, X axis}/*** generate a data * @ param count indicates the number of coordinate points in the chart * @ param range is used to generate a random number within the 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, xValues is displayed by default using the numeric subscript. add ("" + I);} // ArrayList of Y axis data <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 // 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 lineDataSet parameter. setLineWidth (1.75f); // lineDataSet. setCircleSize (3f); // display the circular lineDataSet. setColor (Color. WHITE); // display color lineDataSet. setCircleColor (Color. WHITE); // lineDataSet in the circular color. setHighLightColor (Color. WHITE); // The ArrayList color of the highlighted line <LineDataSet> lineDataSets = new ArrayList <LineDataSet> (); lineDataSets. add (lineDataSet); // add the datasets // create a data object with the datasets LineDatalineData = new LineData (xValues, lineDataSets); returnlineData ;}}

As follows:

MPAndroidChart column chart of Open Source chart Library

1. copy the mpandroidchartlibrary-2-0-8.jar package to the project's libs

2. Define an 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. 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; @ Override protected 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); // whether to add the border barChart on the line chart. setDescription (""); // data description // This is displayed if no data exists, similar to the EmptyView barChart of ListView. setNoDataTextDescription ("You need to provide data for the chart. "); barChart. setDrawGridBackground (false); // whether to display the table color barChart. setGridBackgroundColor (Color. WHITE & 0x70FFFFFF); // table color. Here, we set a barChart for the color. setTouchEnabled (true); // you can specify whether to touch barChart. setDragEnabled (true); // whether barChart can be dragged. setScaleEnabled (true); // whether barChart can be scaled. setPinchZoom (false); // barChart. setBackgroundColor (); // sets the background barChart. setDrawBarShadow (true); barChart. setData (barData); // set the data Legend mLegend = barChart. getLegend (); // set the proportion icon to show 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); // animation to be executed immediately, X axis} privateBarDatagetBarData (int count, float range) {ArrayList <String> xValues = new ArrayList <String> (); for (inti = 0; I <count; I ++) {xValues. add ("quarter" + (I + 1) + "quarter");} ArrayList <BarEntry> yValues = new ArrayList <BarEntry> (); for (inti = 0; I <count; I ++) {float value = (float) (Math. random () * random number less than range/* 100 */) + 3; yValues. add (new BarEntry (value, I);} // 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 ;}}

As follows:

 

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.