MPAndroidChart open-source chart Library (1): pie chart, mpandroidchart open-source

Source: Internet
Author: User
Tags float range

MPAndroidChart open-source chart Library (1): pie chart, mpandroidchart open-source

MPAndroidChart is an open-source chart Library Based on Android. MPAndroidChart not only allows you to draw various statistical charts on Android devices, but also allows you to drag and scale charts. It is very flexible to use. MPAndroidChart also has common chart types: line chart, pie chart, column chart, and scatter chart.

GitHub address:

Https://github.com/PhilJay/MPAndroidChart

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, and comments have been added.

Package com. jackie. mpandroidpiechart; import java. util. arrayList; import com. github. mikephil. charting. charts. pieChart; import com. github. mikephil. charting. components. legend; import com. github. mikephil. charting. components. legend. legendPosition; import com. github. mikephil. charting. data. entry; import com. github. mikephil. charting. data. pieData; import com. github. mikephil. charting. data. pieDataSet; import android. support. v7.app. actionBarActivity; import android. graphics. color; import android. OS. bundle; import android. util. displayMetrics; public class MainActivity extends ActionBarActivity {private PieChart mChart; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mChart = (PieChart) findViewById (R. id. spread_pie_chart); PieData mPieData = getPieData (4,100); showChart (mChart, mPieData);} private void showChart (PieChart pieChart, PieData pieData) {pieChart. setHoleColorTransparent (true); pieChart. setHoleRadius (60f); // radius pieChart. setTransparentCircleRadius (64f); // pieChart with a translucent ring. 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 touchpieChart. setRotationEnabled (true); // You can manually rotate it. // display percentage valuespieChart. 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 */private PieData getPieData (int count, float range) {ArrayList <String> xValues = new ArrayList <String> (); // xVals is used to represent the content on each pie block for (int I = 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 PieDataSet pieDataSet = 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 (); float px = 5 * (metrics. densityDpi/160f); pieDataSet. setSelectionShift (px); // The length of PieData pieData = new PieData (xValues, pieDataSet); return pieData ;}}

As follows:


It mainly involves some basic attributes and API calls. The specific effects and functions of each API can only be tried by yourself. Next, we will introduce other types of MPAndroidChart.

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.