riku
2020-08-21 0328eab9276e57487eb2cfff31a945a01dd8c73a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package cn.flightfeather.thirdapp.util;
 
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
 
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.ViewPortHandler;
 
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
public class ChartGenerator {
 
 
    //<editor-fold desc="快速设置一个饼图的参数">
    public static void quickSetupChart(PieChart pieChart) {
        //饼图样式设置***************************************************************************************************
        pieChart.setUsePercentValues(true);
        pieChart.getDescription().setEnabled(false);
        pieChart.setExtraOffsets(5, 10, 5, 5);
//        pieChart.setExtraOffsets(20.f, 0.f, 20.f, 0.f);
 
        pieChart.setDragDecelerationFrictionCoef(0.95f);
 
//        pieChart.setCenterTextTypeface(mTfLight);
        pieChart.setCenterText(generateCenterSpannableText());
 
        pieChart.setDrawHoleEnabled(true);
        pieChart.setHoleColor(Color.WHITE);
 
        pieChart.setTransparentCircleColor(Color.WHITE);
        pieChart.setTransparentCircleAlpha(110);
 
        pieChart.setHoleRadius(58f);
        pieChart.setTransparentCircleRadius(61f);
 
        pieChart.setDrawCenterText(true);
 
        pieChart.setRotationAngle(0);
        // enable rotation of the chart by touch
        pieChart.setRotationEnabled(true);
        pieChart.setHighlightPerTapEnabled(true);
 
        //图例设置
        Legend l = pieChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(true);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(7f);
        l.setYOffset(10f);
        l.setEnabled(false);
 
        // entry label styling
        pieChart.setEntryLabelColor(Color.WHITE);
//        pieChart.setEntryLabelTypeface(mTfRegular);
        pieChart.setEntryLabelTextSize(12f);
        pieChart.setDrawEntryLabels(true);
 
    }
 
    private static SpannableString generateCenterSpannableText() {
 
        SpannableString s = new SpannableString("问题统计图");
        s.setSpan(new RelativeSizeSpan(1.7f), 0, s.length(), 0);
        s.setSpan(new StyleSpan(Typeface.ITALIC), 0, s.length(), 0);
        s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), 0, s.length(), 0);
        return s;
    }
    //</editor-fold>
 
    //<editor-fold desc="快速将数据填充至饼图,并设置默认的颜色、动作等参数">
    public static void quickSetData(PieChart pieChart, HashMap<String, Float> dataMap, String label) {
        ArrayList<PieEntry> entries = new ArrayList<>();
        List<HashMap.Entry<String, Float>> list = new ArrayList<>(dataMap.entrySet());
        for (HashMap.Entry<String, Float> data :
                list) {
            entries.add(new PieEntry(data.getValue(), data.getKey()));
        }
 
        PieDataSet dataSet = new PieDataSet(entries, label);
        // add a lot of colors
        ArrayList<Integer> colors = new ArrayList<>();
 
        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);
 
        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);
 
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);
 
        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);
 
        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);
 
        colors.add(ColorTemplate.getHoloBlue());
 
        dataSet.setColors(colors);
        //value line
        dataSet.setValueLinePart1OffsetPercentage(80.f);
        dataSet.setValueLinePart1Length(0.2f);
        dataSet.setValueLinePart2Length(0.4f);
        dataSet.setDrawValues(true);
        //dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
//        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
 
        //
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.BLACK);
//        data.setValueTypeface(mTfLight);
        if (!entries.isEmpty()) {//数据全为空时,不显示
            pieChart.setData(data);
        } else {
            pieChart.setData(null);
        }
        pieChart.setNoDataText("暂无数据");
 
        // undo all highlights
        pieChart.highlightValues(null);
 
        pieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
    }
    //</editor-fold>
 
    //<editor-fold desc="快速设置一个水平直方图的参数">
    public static void quickSetupChart(HorizontalBarChart mChart, char type) {
//        mChart.setOnChartValueSelectedListener(this);//数据点击事件
        // mChart.setHighlightEnabled(false);
 
        mChart.setScaleEnabled(false);
        mChart.fitScreen();
        mChart.setDrawBarShadow(false);
 
 
        mChart.getDescription().setEnabled(false);
 
        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        mChart.setMaxVisibleValueCount(60);
 
        // scaling can now only be done on x- and y-axis separately
        mChart.setPinchZoom(false);
 
        // draw shadows for each bar that show the maximum value
        // mChart.setDrawBarShadow(true);
 
        mChart.setDrawGridBackground(false);
 
        mChart.setFocusable(false);
 
        XAxis xl = mChart.getXAxis();
        xl.setPosition(XAxis.XAxisPosition.BOTTOM);
        xl.setTextSize(9f);
//        xl.setTypeface(mTfLight);//字体
        xl.setDrawAxisLine(true);
        xl.setDrawGridLines(true);
        xl.setGranularity(1f);//坐标间隔
 
        YAxis yl = mChart.getAxisLeft();
//        yl.setTypeface(mTfLight);
        yl.setDrawAxisLine(true);
        yl.setDrawGridLines(false);
        yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
 
        yl.setEnabled(false);
 
        YAxis yr = mChart.getAxisRight();
//        yr.setTypeface(mTfLight);
        yr.setDrawAxisLine(true);
        yr.setDrawGridLines(false);
        yr.setAxisMinimum(0f); // this replaces setStartAtZero(true)
//        yr.setInverted(true);
        yr.setEnabled(false);
 
        mChart.setFitBars(true);//x轴坐标显示对应每个bar
        mChart.animateY(2500);
 
        // setting data
 
        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setFormSize(8f);
        l.setXEntrySpace(4f);
 
 
 
        switch (type) {
            case 'a':
                mChart.setDrawValueAboveBar(true);
                yl.setInverted(true);
                xl.setPosition(XAxis.XAxisPosition.TOP_INSIDE);
                xl.setEnabled(true);
                break;
            case 'b':
                mChart.setDrawValueAboveBar(true);
                yl.setInverted(false);
                xl.setPosition(XAxis.XAxisPosition.TOP_INSIDE);
                xl.setEnabled(true);
                break;
            default:
                break;
        }
    }
    //</editor-fold>
 
    //<editor-fold desc="快速将数据填充至水平直方图,并设置默认的颜色、动作等参数">
    private static float spaceForBar = 1f;
 
    private static float barWidth = 0.4f;
    private static float barSpace = 0.05f;
    private static float groupSpace =0.1f;
 
    public static void quickSetData(HorizontalBarChart mChart, List<CommonData> datas, char type) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        ArrayList<CommonData.ChartData> chartData1 = datas.get(0).getChartDatas();
 
        //坐标轴
        XAxis xAxis = mChart.getXAxis();
//        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//        xAxis.setDrawGridLines(false);
////        xAxis.setLabelCount(2, true);//进入时的标签个数
//        xAxis.setCenterAxisLabels(true);//标签处于图表中间
//        xAxis.setAxisMinimum(0f);
        xAxis.setValueFormatter(new XFormatter(chartData1));
//
//        YAxis rightAxis = mChart.getAxisRight();
//        rightAxis.setEnabled(false);
////        rightAxis.setDrawGridLines(false);
////        rightAxis.setDrawAxisLine(false);
////        rightAxis.setDrawLabels(false);
//
        YAxis leftAxis = mChart.getAxisLeft();
//        leftAxis.setDrawGridLines(false);
//        leftAxis.setDrawAxisLine(false);
//        leftAxis.setDrawLabels(false);
//        leftAxis.setDrawTopYLabelEntry(true);
//        leftAxis.setZeroLineColor(Color.BLACK);
//        leftAxis.setDrawZeroLine(true);
//        leftAxis.setAxisMinimum(0f);
//        leftAxis.setAxisMaximum(100f);
//        leftAxis.setValueFormatter(new YFormatter(chartData1));
//        leftAxis.setValueFormatter(new PercentFormatter());
//        LimitLine limitLine = new LimitLine(100f);
//        limitLine.setLineColor(Color.BLACK);
//        leftAxis.addLimitLine(limitLine);
 
        YAxis rightAxis = mChart.getAxisRight();
        rightAxis.setValueFormatter(new PercentFormatter());
 
        //Entry数据
        List<List<BarEntry>> allEntries = new ArrayList<>();
        for (CommonData d :
                datas) {
            List<BarEntry> entries = new ArrayList<>();
            ArrayList<CommonData.ChartData> chartDatas = d.getChartDatas();
            for (CommonData.ChartData c :
                    chartDatas) {
                entries.add(new BarEntry(c.xAxisValue * spaceForBar, (float)(Math.round(c.yAxisValue *1) / 1)));
            }
            allEntries.add(entries);
        }
 
 
        if (mChart.getData() != null &&
                mChart.getData().getDataSetCount() > 0) {
 
            for (int i = 0; i < allEntries.size(); i++) {
                BarDataSet set = (BarDataSet) mChart.getData().getDataSetByIndex(i);
                if (set != null) {
                    set.setValues(allEntries.get(i));
                }
            }
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            for (int i = 0; i < allEntries.size(); i++) {
                BarDataSet set = new BarDataSet(allEntries.get(i), datas.get(i).getLabel());
                set.setDrawIcons(false);
                set.setColor(getColor(i));
                set.setValueFormatter(new YFormatter(chartData1));
                dataSets.add(set);
            }
            BarData data = new BarData(dataSets);
            data.setValueTextSize(8f);
            data.setBarWidth(barWidth);
            mChart.setData(data);
            mChart.groupBars(0f, groupSpace, barSpace);
            //视图设置
            mChart.setVisibleXRangeMaximum(14f);
            mChart.setVisibleXRangeMinimum(8f);
            mChart.setFitBars(true);
 
        }
 
    }
    //</editor-fold>
 
    public static class CommonData {
 
        class ChartData {
            String xAxis;
            float xAxisValue;
            String yAxis;
            float yAxisValue;
 
            public ChartData(String xAxis, float xAxisValue, String yAxis, float yAxisValue) {
                this.xAxis = xAxis;
                this.xAxisValue = xAxisValue;
                this.yAxis = yAxis;
                this.yAxisValue = yAxisValue;
            }
        }
 
        private ArrayList<ChartData> chartDatas;
        private String label;
 
        public CommonData(String label) {
            chartDatas = new ArrayList<>();
            this.label = label;
        }
 
        public void add(String xAxis, float x, String yAxis, float y) {
            chartDatas.add(new ChartData(xAxis, x, yAxis, y));
        }
 
        public ArrayList<ChartData> getChartDatas() {
            return chartDatas;
        }
 
        public String getLabel() {
            return label;
        }
 
        public void setLabel(String label) {
            this.label = label;
        }
 
        public float getTotalYValueCount() {
            float count = 0;
            for (ChartData c : chartDatas) {
                count = count + c.yAxisValue;
            }
            return count;
        }
    }
 
    //<editor-fold desc="x、y轴坐标名称格式化">
    static class YFormatter implements IValueFormatter {
 
        private List<CommonData.ChartData> data;
 
        private DecimalFormat mFormat;
 
        public YFormatter(List<CommonData.ChartData> data) {
            this.data = data;
            mFormat = new DecimalFormat("###");
        }
 
        @Override
        public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
            return mFormat.format(value);
        }
    }
 
    static class XFormatter implements IAxisValueFormatter {
 
        private List<CommonData.ChartData> data;
 
        public XFormatter(List<CommonData.ChartData> data) {
            this.data = data;
        }
 
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return data.get((int) (value/spaceForBar)).xAxis;
        }
    }
    //</editor-fold>
 
    private static int getColor(int i) {
        int colors[] = new int[10];
        colors[0] = Color.argb(128, 255, 0, 0);
        colors[1] = Color.argb(128, 0, 255, 0);
        return colors[i];
    }
}