使用场景:
使用到圆饼图,环形图来展示某些比例的时候。
使用方法:
1 添加依赖 https://github.com/PhilJay/MPAndroidChart
2 复制代码
/** * 环形图用法 */ private void initChart() { //true PieChart内的值会被抽取成百分比 indexBiding.chart.setUsePercentValues(false); //返回负责保存所有相关信息的图表的描述对象,在图表右下角显示的描述文本(默认情况下) indexBiding.chart.getDescription().setEnabled(false); //设置额外的偏移量 indexBiding.chart.setExtraOffsets(5, 10, 5, 5); //*减速摩擦系数[0];1]区间,更高的值 表示速度会缓慢下降,例如,如果设置为0,则速度会减慢。将立即停止。1是无效值,将被转换为。自动* 0.999 f indexBiding.chart.setDragDecelerationFrictionCoef(0.95f); //设置在PieChart中心显示的文本字符串 indexBiding.chart.setCenterText(generateCenterSpannableText()); //将此设置为true,以使饼中心为空 indexBiding.chart.setDrawHoleEnabled(true); //为绘制在该图中心的孔设置颜色 indexBiding.chart.setHoleColor(Color.WHITE); //设置透明圈应有的颜色 indexBiding.chart.setTransparentCircleColor(Color.WHITE); //设置透明圈的透明度为0 =完全透明,255 =完全不透明,默认值为100。 indexBiding.chart.setTransparentCircleAlpha(254); //将该孔的半径设为百分比,最大半径(最大值=整个图的半径),默认值为50%。 indexBiding.chart.setHoleRadius(95f); //设置在孔旁边的透明圆的半径,在最大半径的百分比(最大值=半径)的情况下,整个图表),默认的55% ->意味着比中心孔大5% // indexBiding.chart.setTransparentCircleRadius(61f); //将此设置为true,以绘制显示在中心位置的文本,饼图 indexBiding.chart.setDrawCenterText(true); //设置一个偏移量,以使雷达图的旋转度。 indexBiding.chart.setRotationAngle(0); //将此设置为true,可以通过触摸启用图表的旋转/旋转,设置为false以禁用它。默认值:真正的 indexBiding.chart.setRotationEnabled(true); //将此设置为false,以防止通过tap手势突出显示值,仍然可以通过拖动或编程来突出显示值 indexBiding.chart.setHighlightPerTapEnabled(false); setData(4, 100); //在y轴上以指定的方式对图表的绘制进行动画处理,动画。如果调用animate(…),则不再调用。invalidate()是刷新图表所必需的 indexBiding.chart.animateY(1400, Easing.EasingOption.EaseInOutQuad); Legend l = indexBiding.chart.getLegend(); //不想展示每种颜色的说明,调用下面那句代码即可。 l.setEnabled(false); // l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); // l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); // l.setOrientation(Legend.LegendOrientation.VERTICAL); // //设置l的形状,默认是矩形 // l.setForm(LegendForm.LINE); // l.setDrawInside(false); // //设置距离饼图的距离,防止与饼图重合 // l.setXEntrySpace(2f); // l.setYEntrySpace(0f); // l.setYOffset(0f); //设置条目标签的颜色。 indexBiding.chart.setEntryLabelColor(Color.WHITE); //设置dp中条目标签的大小。默认值:13 dp indexBiding.chart.setEntryLabelTextSize(1f); //为图表设置一个选择监听器 indexBiding.chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() { @Override public void onValueSelected(Entry e, Highlight h) { } @Override public void onNothingSelected() { } }); } private void setData(int count, float range) { float mult = range; ArrayList<PieEntry> entries = new ArrayList<PieEntry>(); entries.add(new PieEntry(40, "优秀")); entries.add(new PieEntry(20, "满分")); entries.add(new PieEntry(30, "及格")); entries.add(new PieEntry(10, "不及格")); PieDataSet dataSet = new PieDataSet(entries, ""); dataSet.setDrawIcons(false); dataSet.setSliceSpace(3f); dataSet.setIconsOffset(new MPPointF(0, 0)); dataSet.setSelectionShift(5f); ArrayList<Integer> colors = new ArrayList<Integer>(); colors.add(getActivity().getResources().getColor(R.color.FF9BD2)); colors.add(getActivity().getResources().getColor(R.color.NTCFST)); colors.add(getActivity().getResources().getColor(R.color.B5E5)); colors.add(getActivity().getResources().getColor(R.color.E21918)); dataSet.setColors(colors); PieData data = new PieData(dataSet); data.setValueFormatter(new PercentFormatter()); //圆饼图中子体大小设置 data.setValueTextSize(0f); //圆饼中字体颜色设置 data.setValueTextColor(Color.WHITE); //为图表设置一个新的数据对象。数据对象包含所有值,和显示所需的信息。 indexBiding.chart.setData(data); //在给定的数据集中突出显示给定索引的值。提供null或空数组以撤消所有突出显示。这应该已经习惯了,此方法*不会*调用侦听器。 indexBiding.chart.highlightValues(null); //整个视图无效。如果视图是可见的, indexBiding.chart.invalidate(); } /** * 设置圆饼图内显示内容 * * @return */ private SpannableString generateCenterSpannableText() { SpannableString s = new SpannableString(""); return s; }
3 运行