IT-KIMI_SHI\SINOIT.KIMI
2018-12-07 50eb1d766c470dc6ff927199eaee934f972a8b70
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
/*************************************************************************
 * Copyright (C) Unpublished JiuDaoTech Software, Inc. All rights reserved.
 * JiuDaoTech Software, Inc., Confidential and Proprietary.
 * <p>
 * This software is subject to copyright protection
 * under the laws of the Public of China and other countries.
 * <p>
 * Unless otherwise explicitly stated, this software is provided
 * by JiuDaoTech "AS IS".
 *************************************************************************/
package service.chart.pie.echarts;
 
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.series.Series;
import model.chart.ChartBuilderParams;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import service.chart.ChartOption;
import service.chart.ChartsUtil;
import service.system.helper.DataSetProvider;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * 标准饼图实现
 *
 * @author CSJ
 */
@Service
public class Pie implements ChartOption {
 
    @Resource
    private DataSetProvider dataSetProvider;
 
    @Resource
    private ChartsUtil chartsUtil;
 
    @Override
    public Option transform(final ChartBuilderParams chartBuilderParams) throws Exception {
 
        List<Map<String, Object>> dataSet = dataSetProvider.prepareDataSet(chartBuilderParams);
 
        dataSet = chartsUtil.dataGroupBy(chartBuilderParams,dataSet,"pie");
        chartsUtil.dataFilter(dataSet,chartBuilderParams,"pie");
        // 拆分数据, 结构化
        Option option = new Option();
        // backgroundColor
        option.backgroundColor("white");
        // title
        option.title("标题", "副标题");
        // tooltip
        option.tooltip().trigger(Trigger.item).formatter("{b}: {c} <br/>占比: {d}%");
        // legend
        Collection<String> legendData = CollectionUtils.collect(dataSet, new Transformer<Map<String, Object>, String>() {
            @Override
            public String transform(Map<String, Object> input) {
                Object obj = input.get(chartBuilderParams.getBuilderModel().getMark().getColor());
                if (obj != null && StringUtils.isNoneEmpty(obj.toString())) {
                    return String.valueOf(obj);
                }
                return "NULL";
            }
        }, new HashSet<String>());
        option.legend().data().addAll(legendData);
        // series
        Series series = new com.github.abel533.echarts.series.Pie();
        Collection<Object> seriesData = CollectionUtils.collect(dataSet, new Transformer<Map<String, Object>, Object>() {
            @Override
            public Object transform(Map<String, Object> input) {
                Object v = input.get(chartBuilderParams.getBuilderModel().getMark().getAngle());
                Object k = input.get(chartBuilderParams.getBuilderModel().getMark().getColor());
                Map<String, Object> transformData = new HashMap<>();
                transformData.put("name", k);
                transformData.put("value", v);
                return transformData;
            }
        });
        series.data().addAll(seriesData);
        option.series(series);
 
        return option;
    }
}