MPAndroidChar--饼状图
16lz
2021-01-23
MPAndroidChart是一款基于Android的开源图表库,MPAndroidChart不仅可以在Android设备上绘制各种统计图表,而且可以对图表进行拖动和缩放操作,应用起来非常灵活。MPAndroidChart同样拥有常用的图表类型:线型图、饼图、柱状图和散点图。
GitHub地址:
https://github.com/PhilJay/MPAndroidChart
定义XML文件
<com.github.mikephil.charting.charts.PieChart android:id="@+id/chart1" android:layout_width="120dp" android:layout_height="120dp" android:layout_marginTop="16dp" android:layout_marginLeft="15dp" />
主要Java逻辑代码如下:
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(4, 100); showChart(mChart, mPieData); } private void showChart(PieChart pieChart, PieData pieData) { //显示成内圈颜色是否透明 pieChart.setHoleColorTransparent(true); //半径 pieChart.setHoleRadius(60f); // 半透明圈 pieChart.setTransparentCircleRadius(64f); //实心圆 //pieChart.setHoleRadius(0); //右下角名字 pieChart.setDescription("测试饼状图"); // mChart.setDrawYValues(true); //饼状图中间可以添加文字 pieChart.setDrawCenterText(true); //变成空心的饼图(有白色边) pieChart.setDrawHoleEnabled(true); // 初始旋转角度 pieChart.setRotationAngle(90); // draws the corresponding description value into the slice // mChart.setDrawXValues(true); // 可以手动旋转 pieChart.setRotationEnabled(true); //显示成百分比 pieChart.setUsePercentValues(true); // mChart.setUnit(" €"); // mChart.setDrawUnitsInChart(true); // add a selection listener // mChart.setOnChartValueSelectedListener(this); // mChart.setTouchEnabled(false); // mChart.setOnAnimationListener(this); pieChart.setCenterText("Quarterly Revenue"); //饼状图中间的文字 //设置数据 setData(pieData); //setData(总共几部分,传进来的char,每个部分的值的集合) setData(stoclist.size(), chart,bililist); // undo all highlights // pieChart.highlightValues(null); // pieChart.invalidate(); //设置比例图 Legend mLegend = pieChart.getLegend(); //最右边显示 mLegend.setPosition(LegendPosition.RIGHT_OF_CHART); //设置比例图的形状,默认是方形 // mLegend.setForm(LegendForm.LINE); mLegend.setXEntrySpace(7f); mLegend.setYEntrySpace(5f); //隐藏说明(比例圈) mLegend.setEnabled(false); pieChart.animateXY(1000, 1000); //设置动画 // mChart.spin(2000, 0, 360); } /** * * @param count 分成几部分 * @param range */ private void setData(int count, PieChart chart,List blist) { ArrayList yVals1 = new ArrayList(); ArrayList xVals = new ArrayList(); ArrayList colors = new ArrayList(); // IMPORTANT: In a PieChart, no values (Entry) should have the same // xIndex (even if from different DataSets), since no values can be // drawn above each other. if (new BigDecimal(pricecount.getText().toString()).compareTo(new BigDecimal(0))!=1 &&chart.equals(chart2)){ yVals1.add(new Entry(1.0f, 0)); xVals.add("stock"); colors.add(getResources().getColor(R.color.cC3)); } else { for (int i = 0; i < count ; i++) { //确定比例权重 yVals1.add(new Entry(blist.get(i), i)); } //往饼图上写的分类名字(介绍上也有这个名字) for (int i = 0; i < count ; i++) xVals.add(stoclist.get(i)); // add a lot of colors for (int c : colorlist) colors.add(c); } PieDataSet dataSet = new PieDataSet(yVals1, ""); //设置个饼状图之间的距离 dataSet.setSliceSpace(0f); dataSet.setSelectionShift(5f); // 饼图颜色 dataSet.setColors(colors); // 选中态多出的长度 //dataSet.setSelectionShift(0f); PieData data = new PieData(xVals, dataSet); data.setValueFormatter(new PercentFormatter()); data.setValueTextSize(9f); data.setValueTextColor(Color.TRANSPARENT); chart.setData(data); // undo all highlights chart.highlightValues(null); chart.invalidate(); }}
更多相关文章
- Android开发教程--设置ImageView图片的显示比例
- 简单的 Android 系统主要版本对比图表
- 【Android】自定义ImageView高度自适应,宽度成比例缩放
- android 图表引擎AChartEngine(线性图)
- Android之关于图表
- Android 图表
- iPhone、Android 社交 App 使用比例大調查(資訊圖表)
- Android-Charts,Android图形图表控件
- android 图表引擎AChartEngine(柱状图)