|
| 1 | +package com.pengxh.androidx.lite.utils; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Color; |
| 5 | + |
| 6 | +import com.github.mikephil.charting.animation.Easing; |
| 7 | +import com.github.mikephil.charting.charts.BarChart; |
| 8 | +import com.github.mikephil.charting.charts.Chart; |
| 9 | +import com.github.mikephil.charting.charts.HorizontalBarChart; |
| 10 | +import com.github.mikephil.charting.charts.LineChart; |
| 11 | +import com.github.mikephil.charting.charts.PieChart; |
| 12 | +import com.github.mikephil.charting.components.Legend; |
| 13 | +import com.github.mikephil.charting.components.XAxis; |
| 14 | +import com.github.mikephil.charting.components.YAxis; |
| 15 | +import com.github.mikephil.charting.formatter.ValueFormatter; |
| 16 | +import com.pengxh.androidx.lite.R; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +/** |
| 21 | + * MPAndroidChart初始化类 |
| 22 | + */ |
| 23 | +public class ChartUtil { |
| 24 | + |
| 25 | + public static void initLineChart(Context context, LineChart chart) { |
| 26 | + chart.setNoDataText("无数据,无法渲染..."); |
| 27 | + chart.setNoDataTextColor(R.color.red); |
| 28 | + chart.getPaint(Chart.PAINT_INFO).setTextSize(DeviceSizeUtil.sp2px(context, 14f)); |
| 29 | + chart.setDrawGridBackground(false); |
| 30 | + chart.setDrawBorders(false); |
| 31 | + chart.animateY(1200, Easing.EaseInOutQuad); |
| 32 | + //设置样式 |
| 33 | + YAxis rightAxis = chart.getAxisRight(); |
| 34 | + //设置图表右边的y轴禁用 |
| 35 | + rightAxis.setEnabled(false); |
| 36 | + YAxis leftAxis = chart.getAxisLeft(); |
| 37 | + leftAxis.setAxisMinimum(0f); |
| 38 | + chart.setScaleXEnabled(true); //X轴可缩放 |
| 39 | + chart.setScaleYEnabled(false); //Y轴不可缩放 |
| 40 | + //设置x轴 |
| 41 | + XAxis xAxis = chart.getXAxis(); |
| 42 | + xAxis.setTextColor(ColorUtil.convertColor(context, R.color.lib_text_color)); |
| 43 | + xAxis.setTextSize(DeviceSizeUtil.sp2px(context, 10f)); |
| 44 | + xAxis.setLabelCount(7, true); |
| 45 | + xAxis.setDrawLabels(true); //绘制标签 指x轴上的对应数值 |
| 46 | + xAxis.setDrawAxisLine(true); //是否绘制轴线 |
| 47 | + xAxis.setDrawGridLines(false); //设置x轴上每个点对应的线 |
| 48 | + xAxis.setGranularity(1f); //禁止放大后x轴标签重绘 |
| 49 | + xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); |
| 50 | + chart.setExtraBottomOffset(5f); //解决X轴显示不完全问题 |
| 51 | + //去掉描述 |
| 52 | + chart.getDescription().setEnabled(false); |
| 53 | + //设置图例 |
| 54 | + setChartLegend(chart.getLegend()); |
| 55 | + } |
| 56 | + |
| 57 | + public static void initPieChart(Context context, PieChart chart) { |
| 58 | + chart.setNoDataText("无数据,无法渲染..."); |
| 59 | + chart.setNoDataTextColor(R.color.red); |
| 60 | + chart.getPaint(Chart.PAINT_INFO).setTextSize(DeviceSizeUtil.sp2px(context, 14f)); |
| 61 | + chart.setUsePercentValues(false); //百分比数字显示 |
| 62 | + chart.getDescription().setEnabled(false); |
| 63 | + chart.setDragDecelerationFrictionCoef(0.95f); //图表转动阻力摩擦系数[0,1] |
| 64 | + chart.setBackgroundColor(Color.WHITE); //设置图表背景色 |
| 65 | + chart.setRotationAngle(0f); |
| 66 | + chart.setRotationEnabled(false); |
| 67 | + chart.setHighlightPerTapEnabled(true); |
| 68 | + chart.animateY(1200, Easing.EaseInOutQuad); // 设置图表展示动画效果 |
| 69 | + chart.setDrawEntryLabels(false); //不显示分类标签 |
| 70 | + chart.setDrawHoleEnabled(false); //圆环显示 |
| 71 | + chart.setDrawCenterText(false); //圆环中心文字 |
| 72 | + chart.setEntryLabelColor(R.color.blue); //图表文本字体颜色 |
| 73 | + chart.setEntryLabelTextSize(12f); |
| 74 | + //设置图表上下左右的偏移,类似于外边距,可以控制饼图大小 |
| 75 | + chart.setExtraOffsets(7.5f, 2.5f, 7.5f, 2.5f); |
| 76 | + //设置图例位置 |
| 77 | + setChartLegend(chart.getLegend()); |
| 78 | + } |
| 79 | + |
| 80 | + public static void initBarChart(Context context, BarChart chart, List<String> barLabels) { |
| 81 | + chart.setNoDataText("无数据,无法渲染..."); |
| 82 | + chart.setNoDataTextColor(R.color.red); |
| 83 | + if (barLabels.isEmpty()) { |
| 84 | + chart.clearValues(); |
| 85 | + return; |
| 86 | + } |
| 87 | + chart.getPaint(Chart.PAINT_INFO).setTextSize(DeviceSizeUtil.sp2px(context, 14f)); |
| 88 | + chart.animateY(1200, Easing.EaseInOutQuad); |
| 89 | + chart.setDrawGridBackground(false); |
| 90 | + chart.setDrawBorders(false); |
| 91 | + chart.setScaleEnabled(false); |
| 92 | + //去掉描述 |
| 93 | + chart.getDescription().setEnabled(false); |
| 94 | + //去掉图例 |
| 95 | + chart.getLegend().setEnabled(false); |
| 96 | + XAxis xAxis = chart.getXAxis(); |
| 97 | + xAxis.setTextColor(ColorUtil.convertColor(context, R.color.lib_text_color)); |
| 98 | + xAxis.setDrawLabels(true); //绘制标签 指x轴上的对应数值 |
| 99 | + xAxis.setLabelCount(barLabels.size()); // 设置x轴上的标签个数 |
| 100 | + xAxis.setValueFormatter(new ValueFormatter() { |
| 101 | + @Override |
| 102 | + public String getFormattedValue(float value) { |
| 103 | + return barLabels.get((int) value); |
| 104 | + } |
| 105 | + }); |
| 106 | + xAxis.setDrawAxisLine(true); //是否绘制轴线 |
| 107 | + xAxis.setDrawGridLines(false); //设置x轴上每个点对应的线 |
| 108 | + xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); |
| 109 | + xAxis.setLabelRotationAngle(-45); //X轴标签斜45度 |
| 110 | + chart.setExtraRightOffset(5f); //解决X轴显示不完全问题 |
| 111 | + //设置样式 |
| 112 | + YAxis rightAxis = chart.getAxisRight(); |
| 113 | + //设置图表右边的y轴禁用 |
| 114 | + rightAxis.setEnabled(false); |
| 115 | + YAxis leftAxis = chart.getAxisLeft(); |
| 116 | + leftAxis.setAxisMinimum(0f); |
| 117 | + } |
| 118 | + |
| 119 | + public static void initHorizontalBarChart(Context context, HorizontalBarChart chart, List<String> barLabels) { |
| 120 | + chart.setNoDataText("无数据,无法渲染..."); |
| 121 | + chart.setNoDataTextColor(R.color.red); |
| 122 | + if (barLabels.isEmpty()) { |
| 123 | + chart.clearValues(); |
| 124 | + return; |
| 125 | + } |
| 126 | + chart.getPaint(Chart.PAINT_INFO).setTextSize(DeviceSizeUtil.sp2px(context, 14f)); |
| 127 | + chart.animateY(1200, Easing.EaseInOutQuad); |
| 128 | + chart.setDrawGridBackground(false); |
| 129 | + chart.setDrawBorders(false); |
| 130 | + chart.setScaleEnabled(false); |
| 131 | + //去掉描述 |
| 132 | + chart.getDescription().setEnabled(false); |
| 133 | + //去掉图例 |
| 134 | + chart.getLegend().setEnabled(false); |
| 135 | + XAxis xAxis = chart.getXAxis(); |
| 136 | + xAxis.setTextColor(ColorUtil.convertColor(context, R.color.lib_text_color)); |
| 137 | + xAxis.setDrawLabels(true); //绘制标签 指x轴上的对应数值 |
| 138 | + xAxis.setLabelCount(barLabels.size()); // 设置x轴上的标签个数 |
| 139 | + xAxis.setValueFormatter(new ValueFormatter() { |
| 140 | + @Override |
| 141 | + public String getFormattedValue(float value) { |
| 142 | + return barLabels.get((int) value); |
| 143 | + } |
| 144 | + }); |
| 145 | + xAxis.setDrawAxisLine(true); //是否绘制轴线 |
| 146 | + xAxis.setDrawGridLines(false); //设置x轴上每个点对应的线 |
| 147 | + xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); |
| 148 | + chart.setExtraRightOffset(5f); //解决X轴显示不完全问题 |
| 149 | + //设置样式 |
| 150 | + YAxis rightAxis = chart.getAxisRight(); |
| 151 | + //设置图表右边的y轴禁用 |
| 152 | + rightAxis.setEnabled(false); |
| 153 | + YAxis leftAxis = chart.getAxisLeft(); |
| 154 | + leftAxis.setAxisMinimum(0f); |
| 155 | + } |
| 156 | + |
| 157 | + private static void setChartLegend(Legend legend) { |
| 158 | + legend.setOrientation(Legend.LegendOrientation.HORIZONTAL); |
| 159 | + legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM); |
| 160 | + legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER); |
| 161 | + //图例是否自动换行 |
| 162 | + legend.setWordWrapEnabled(true); |
| 163 | + } |
| 164 | +} |
0 commit comments