In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces Android MPAndroidChart open source chart library how to achieve pie chart, the article is very detailed, has a certain reference value, interested friends must read it!
MPAndroidChart is an open source chart library based on Android. MPAndroidChart can not only draw all kinds of statistical charts on Android devices, but also drag and zoom the charts. MPAndroidChart also has common chart types: line chart, pie chart, bar chart, and scatter chart.
The following pie charts are mainly implemented:
1. Download the latest mpandroidchartlibrary-2-0-8.jar package from the address above, and then copy it to the libs of the project
two。 Define the 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; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); mChart = (PieChart) findViewById (R.id.spread_pie_chart); PieData mPieData = getPieData (4100); showChart (mChart, mPieData);} private void showChart (PieChart pieChart, PieData pieData) {pieChart.setHoleColorTransparent (true); pieChart.setHoleRadius (60f); / / Radius pieChart.setTransparentCircleRadius (64f) / / translucent circle / / pieChart.setHoleRadius (0) / / solid circle pieChart.setDescription ("test pie chart"); / / mChart.setDrawYValues (true); pieChart.setDrawCenterText (true); / / words pieChart.setDrawHoleEnabled (true) can be added in the middle of the pie chart; 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 / / display percentage values pieChart.setUsePercentValues (true); / / display 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 / / sets the data pieChart.setData (pieData); / / undo all highlights / / pieChart.highlightValues (null); / / pieChart.invalidate (); Legend mLegend = pieChart.getLegend (); / / sets the scale image mLegend.setPosition (LegendPosition.RIGHT_OF_CHART); / / the rightmost display / / mLegend.setForm (LegendForm.LINE); / / sets the shape of the scale chart. The default is square mLegend.setXEntrySpace (7f). MLegend.setYEntrySpace (5f); pieChart.animateXY (1000, 1000); / / animate / / mChart.spin (2000, 0360);} / * @ param count is divided into several parts * @ param range * / private PieData getPieData (int count, float range) {ArrayList xValues = new ArrayList (); / / xVals is used to represent the content for (int I = 0; I) on each piece
< count; i++) { xValues.add("Quarterly" + (i + 1)); //饼块上显示成Quarterly1, Quarterly2, Quarterly3, Quarterly4 } ArrayList yValues = new ArrayList(); //yVals用来表示封装每个饼块的实际数据 // 饼图数据 /** * 将一个饼形图分成四部分, 四部分的数值比例为14:14:34:38 * 所以 14代表的百分比就是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)); //y轴的集合 PieDataSet pieDataSet = new PieDataSet(yValues, "Quarterly Revenue 2014"/*显示在比例图上*/); pieDataSet.setSliceSpace(0f); //设置个饼状图之间的距离 ArrayList colors = new ArrayList(); // 饼图颜色 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); // 选中态多出的长度 PieData pieData = new PieData(xValues, pieDataSet); return pieData; } } 效果图如下:Mainly some basic attributes and API calls, specific API has what kind of effect and function, can only rely on their own to try. Later, we will continue to introduce other types of MPAndroidChart charts.
The above is all the contents of the article "how to realize the pie chart of Android MPAndroidChart open source chart library". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.