当前位置: 代码迷 >> 综合 >> MPAndroidChart:v3.1.0 饼图工具类拿走
  详细解决方案

MPAndroidChart:v3.1.0 饼图工具类拿走

热度:64   发布时间:2024-01-25 05:01:03.0

MPAndroidChart:v3.1.0 饼图工具类拿走

废话不多说直接上代码,简单粗暴一gei窝里gei,gei
 * 饼图工具类*/
public class PieChartManagger {public PieChart pieChart;public PieChartManagger(PieChart pieChart,String text) {this.pieChart = pieChart;initPieChart( text);}//初始化private void initPieChart(String text) {//饼状图pieChart.setUsePercentValues(true);//设置value是否用显示百分数,默认为falsepieChart.getDescription().setEnabled(false);//设置描述pieChart.setExtraOffsets(5f, 10f, 5f, 5f);//设置饼状图距离上下左右的偏移量pieChart.setDragDecelerationFrictionCoef(0.95f);//设置阻尼系数,范围在[0,1]之间,越小饼状图转动越困难//设置中间文字pieChart.setDrawCenterText(true);//是否绘制中间的文字pieChart.setCenterText(text);//设置饼中间标题pieChart.setCenterTextSizePixels(66);pieChart.setCenterTextColor(Color.parseColor("#292F4C")); //中间问题的颜色pieChart.setCenterTextSize(15f);//中间文字的大小pxpieChart.setNoDataText("暂无数据");// 如果没有数据的时候,会显示这个,类似ListView的EmptyViewpieChart.setDrawHoleEnabled(true);//是否绘制饼状图中间的圆pieChart.setHoleColor(Color.WHITE);//饼状图中间的圆的绘制颜色pieChart.setTransparentCircleColor(Color.WHITE);//设置圆环的颜色pieChart.setTransparentCircleAlpha(110);//设置圆环的透明度[0,255]pieChart.setHoleRadius(68f);//饼状图中间的圆的半径大小pieChart.setTransparentCircleRadius(31f);//设置圆环的半径值pieChart.setRotationAngle(0f);//设置饼状图旋转的角度// 触摸旋转pieChart.setRotationEnabled(true);pieChart.setHighlightPerTapEnabled(true);//设置旋转的时候点中的tab是否高亮(默认为true)// 输入标签样式pieChart.setDrawEntryLabels(false);//设置是否绘制Label饼上的字体,只显示百分比pieChart.setEntryLabelColor(Color.WHITE);//设置绘制Label的颜色pieChart.setEntryLabelTextSize(13f);//设置绘制Label的字体大小pieChart.animateX(500);//设置每个tab比例块的显示位置(饼图外字体)Legend l = pieChart.getLegend();//设置比例块  饼图外数据的位置l.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER);l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);l.setOrientation(Legend.LegendOrientation.VERTICAL) ;//设置图例水平显示l.setDrawInside(false);l.setXEntrySpace(10f);//设置tab比例块之间X轴方向上的空白间距值(水平排列时)l.setYEntrySpace(10f);//设置tab比例块之间Y轴方向上的空白间距值(垂直排列时)l.setYOffset(12f);l.setFormSize(14f);//设置比例块大小l.setTextSize(14f);//设置比例块字体大小l.setForm(Legend.LegendForm.SQUARE);//设置比例块图标形状,默认为方块l.setTextColor(Color.parseColor("#8E92A8"));l.setEnabled(true);//设置是否启用比例块,默认启用l.setWordWrapEnabled(false);//设置比例块换行.../*** 饼子的样式(一块压一块)*/boolean toSet = !pieChart.isDrawRoundedSlicesEnabled() || !pieChart.isDrawHoleEnabled();pieChart.setDrawRoundedSlices(toSet);if (toSet && !pieChart.isDrawHoleEnabled()) {pieChart.setDrawHoleEnabled(true);}if (toSet && pieChart.isDrawSlicesUnderHoleEnabled()) {pieChart.setDrawSlicesUnderHole(false);}}/*** 显示圆环* @param* @param*/public void  showRingPieChart(List<PieEntry> entries, List<Integer> colors) {PieDataSet dataSet = new PieDataSet(entries, "");dataSet.setSliceSpace(0f);//饼子间距dataSet.setSelectionShift(9f);//点击某个饼子伸长dataSet.setColors(colors);PieData data = new PieData(dataSet);data.setValueFormatter(new PercentFormatter());data.setValueTextSize(11f);data.setValueTextColor(Color.WHITE);pieChart.setData(data);pieChart.highlightValues(null);//刷新pieChart.invalidate();}
}