首页 > Android > 工具 > 正文

MPAndroidChart-PieChart(饼状图)

2021-03-01 工具 2662 ℃ 0 评论

官方文档地址:https://weeklycoding.com/mpandroidchart-documentation/

Git代码地址:https://github.com/PhilJay/MPAndroidChart

// 设置 pieChart 图表基本属性
pieChart.setUsePercentValues(false); //使用百分比显示
pieChart.getDescription().setEnabled(false); //设置pieChart图表的描述取消右下角描述
pieChart.setBackgroundColor(Color.YELLOW); //设置pieChart图表背景色
pieChart.setExtraOffsets(5, 10, 60, 10); //设置pieChart图表上下左右的偏移,类似于外边距
pieChart.setDragDecelerationFrictionCoef(0.95f);//设置pieChart图表转动阻力摩擦系数[0,1]
pieChart.setRotationAngle(0); //设置pieChart图表起始角度
pieChart.setRotationEnabled(true); //设置pieChart图表是否可以手动旋转
pieChart.setHighlightPerTapEnabled(true); //设置piecahrt图表点击Item高亮是否可用
pieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);// 设置pieChart图表展示动画效果

// 设置 pieChart 图表Item文本属性
pieChart.setDrawEntryLabels(true); //设置pieChart是否只显示饼图上百分比不显示文字(true:下面属性才有效果)
pieChart.setEntryLabelColor(Color.WHITE); //设置pieChart图表文本字体颜色
pieChart.setEntryLabelTypeface(mTfRegular); //设置pieChart图表文本字体样式
pieChart.setEntryLabelTextSize(10f); //设置pieChart图表文本字体大小


// 设置 pieChart 内部圆环属性
pieChart.setDrawHoleEnabled(false);	//  是否显示中间的洞
pieChart.setDrawHoleEnabled(true); //是否显示PieChart内部圆环(true:下面属性才有意义)
pieChart.setHoleRadius(28f); //设置PieChart内部圆的半径(这里设置28.0f)
pieChart.setTransparentCircleRadius(31f); //设置PieChart内部透明圆的半径(这里设置31.0f)
pieChart.setTransparentCircleColor(Color.BLACK);//设置PieChart内部透明圆与内部圆间距(31f-28f)填充颜色
pieChart.setTransparentCircleAlpha(50); //设置PieChart内部透明圆与内部圆间距(31f-28f)透明度[0~255]数值越小越透明
pieChart.setHoleColor(Color.WHITE); //设置PieChart内部圆的颜色
pieChart.setDrawCenterText(true); //是否绘制PieChart内部中心文本(true:下面属性才有意义)
pieChart.setCenterTextTypeface(Typeface.DEFAULT); //设置PieChart内部圆文字的字体样式
pieChart.setCenterText(“Test”); //设置PieChart内部圆文字的内容
pieChart.setCenterTextSize(10f); //设置PieChart内部圆文字的大小
pieChart.setCenterTextColor(Color.RED); //设置PieChart内部圆文字的颜色
pieChart.setCenterTextSizePixels(36);  //中间文字的大小px
pieChart.setCenterTextRadiusPercent(1f);
pieChart.setCenterTextOffset(0, 0); //中间文字的偏移量
pieChart.animateX(1500,Easing.EasingOption.EaseInOutQuad);//数据显示动画


// 获取pieCahrt图列
Legend l = pieChart.getLegend();
l.setEnabled(true); //是否启用图列(true:下面属性才有意义)
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); //顶部
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);//右对其
l.setOrientation(Legend.LegendOrientation.VERTICAL);//设置图例水平显示
l.setForm(Legend.LegendForm.DEFAULT); //设置图例的形状
l.setFormSize(10); //设置图例的大小
l.setFormToTextSpace(10f); //设置每个图例实体中标签和形状之间的间距
l.setDrawInside(false);
l.setWordWrapEnabled(true); //设置图列换行(注意使用影响性能,仅适用legend位于图表下面)
l.setXEntrySpace(10f); //设置图例实体之间延X轴的间距(setOrientation = HORIZONTAL有效)
l.setYEntrySpace(8f); //设置图例实体之间延Y轴的间距(setOrientation = VERTICAL 有效)
l.setYOffset(0f); //设置比例块Y轴偏移量
l.setTextSize(14f); //设置图例标签文本的大小
l.setTextColor(Color.parseColor("#ff9933"));//设置图例标签文本的颜色

//pieChart 选择监听
pieChart.setOnChartValueSelectedListener(this);

//设置MARKERVIEW
CustomMarkerView mv = new CustomMarkerView(this, new PercentFormatter());
mv.setChartView(pieChart);
pieChart.setMarker(mv);

//设置饼图的数据

ArrayList pieEntryList = new ArrayList();
ArrayList colors = new ArrayList();
colors.add(Color.parseColor("#f17548"));
colors.add(Color.parseColor("#FF9933"));
//饼图实体 PieEntry
PieEntry CashBalance = new PieEntry(70, “现金余额 1500”);
PieEntry ConsumptionBalance = new PieEntry(30, “消费余额 768”);
pieEntryList.add(CashBalance);
pieEntryList.add(ConsumptionBalance);
//饼状图数据集 PieDataSet
PieDataSet pieDataSet = new PieDataSet(pieEntryList, “资产总览”);
pieDataSet.setSliceSpace(3f); //设置饼状Item之间的间隙
pieDataSet.setSelectionShift(10f); //设置饼状Item被选中时变化的距离
pieDataSet.setColors(colors); //为DataSet中的数据匹配上颜色集(饼图Item颜色)
pieDataSet.setValueLinePart1Length(0.4f);//当值位置为外边线时,表示线的前半段长度。
pieDataSet.setValueLinePart2Length(0.4f);//当值位置为外边线时,表示线的后半段长度。		
pieDataSet.setValueLinePart1OffsetPercentage(80f);//当ValuePosits为OutsiDice时,指示偏移为切片大小的百分比
pieDataSet.setValueLineColor(Color.parseColor("#a1a1a1"));// 当值位置为外边线时,表示线的颜色。
pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);//设置Y值的位置是在圆内还是圆外
pieDataSet.setUsingSliceColorAsValueLineColor(false);	//设置Y轴描述线和填充区域的颜色一致
pieDataSet.setSelectionShift(5f);//设置饼状Item被选中时变化的距离
//最终数据 PieData
PieData pieData = new PieData(pieDataSet);
pieData.setDrawValues(true); //设置是否显示数据实体(百分比,true:以下属性才有意义)
pieData.setValueTextColor(Color.BLUE); //设置所有DataSet内数据实体(百分比)的文本颜色
pieData.setValueTextSize(12f); //设置所有DataSet内数据实体(百分比)的文本字体大小
pieData.setValueTypeface(mTfLight); //设置所有DataSet内数据实体(百分比)的文本字体样式
pieData.setValueFormatter(new PercentFormatter());//设置所有DataSet内数据实体(百分比)的文本字体格式
pieChart.setData(pieData);
pieChart.highlightValues(null);
pieChart.invalidate(); //将图表重绘以显示设置的属性和数据


其中highlightValues未设置
highlightValue(float x, int dataSetIndex, boolean callListener):
高亮显示在给定的DataSet中x坐标所对应的值。
设置dataSetIndex为-1会撤销所有高亮显示。
callListener决定了是否调用选择监听器。
highlightValue(Highlight high, boolean callListener):
高亮显示的值代表了给定的Highlight对象 。
提供null撤销所有高亮显示。
callListener决定了是否调用选择监听器。
highlightValues(Highlight[] highs):
高亮显示的值代表了给定的Highlight对象数组。
提供null或空数组撤消所有高亮显示。


属性整理网络部分原链接

https://blog.csdn.net/a136447572/article/details/106373434

https://blog.csdn.net/baidu_31956557/article/details/80930116

新手引导看以下链接

https://www.jianshu.com/p/9f60708b8c68

日历
«    2024年3月    »
123
45678910
11121314151617
18192021222324
25262728293031
标签列表
最近发表
友情链接