EchartConver.java
8.12 KB
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
package com.bsth.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.AxisType;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.feature.Feature;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.K;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.Pie;
/**
* list数据转换成echart option
*
* @author HP
*
*/
public class EchartConver {
String xAxisName = ""; //xAxis字段名
String legendName = "";//legenName字段名
String chartTitle = ""; //图表名称
String chartType = ""; //图类型
Option option = new Option();
public EchartConver(Map<String, Object> map){
this.xAxisName = map.get("xAxisName")==null?"":map.get("xAxisName").toString().split(":")[0];
this.legendName = map.get("legendName")==null?"":map.get("legendName").toString().split(":")[0];
this.chartTitle = map.get("chartTitle")==null?"":map.get("chartTitle").toString();
this.chartType = map.get("chartType")==null?"":map.get("chartType").toString();
option = new Option();
}
public Option getOption(List dataList) {
Feature f = new Feature();
List xAxisList = getXAxisList(dataList);
List legendList = getLegendList(dataList);
chartType = chartType.toLowerCase();
ValueAxis valuexAxis = new ValueAxis();
Object [] legendV = null;
try{
if(dataList!=null){
//添加series数据
option = getSeriesValue(xAxisList,legendList,dataList);
//增加多图显示
option = getMagic();
//显示提示框
option.tooltip().show(true);
//legend内容
if(!chartType.equals("pie")){
// if(legendList.size()>0){
// legendV =legendList.toArray();
// option.legend(legendV);
// }
//x轴内容
valuexAxis.setData(xAxisList);
valuexAxis.setType(AxisType.category);
option.xAxis(valuexAxis);
//y轴内容
CategoryAxis yaxis = new CategoryAxis();
yaxis.setType(AxisType.value);
option.yAxis(yaxis);
}else{
legendV =xAxisList.toArray();
option.legend().x("left");
option.tooltip().formatter("{a} <br/>{b} : {c} ({d}%)");
option.legend(legendV);
option.legend().orient(Orient.vertical).x("left");
option.title().text(chartTitle).x("center");
}
/*Series series = (Series) option.series();
ItemStyle itemStyle = new ItemStyle();
Normal normal = new Normal();
Label label = new Label();
label.show(true);
normal.label(label);
itemStyle.normal(normal);
series.itemStyle(itemStyle);
option.series(series);*/
//工具栏内容
option.toolbox().show(true);
option.toolbox().feature(f.restore.show(true));
}
}catch (Exception e) {
e.printStackTrace();
}
return option;
}
/**
* 获取xAxis的值
*/
public List getXAxisList(List dataList) {
List list = getData(dataList,1,"xAxis");
return list;
}
/**
* 获取legend的值
*/
public List getLegendList(List dataList) {
List list = getData(dataList,3,"legend");
return list;
}
/**
* 获取seriesList的值
*/
public Option getSeriesValue(List xAxisList,List legendList,List dataList) {
List list = new ArrayList();
if(dataList!=null){
if(legendList.size()>0){//多数据的处理
for(int i=0;i<legendList.size();i++){
String legendName = legendList.get(i).toString();
List seriesList = new ArrayList();
for(int j=0;j<xAxisList.size();j++){
String xValue = xAxisList.get(j).toString();
Map map = new HashMap();
String value = "";
for(int k=0;k<dataList.size();k++){
map = (Map)dataList.get(k);
if(map.get("xAxis").equals(xValue)&&map.get("legend").equals(legendName)){
value = map.get("series").toString();
break;
}
}
seriesList.add(value.equals("")?"0":value);
}
getSeries(seriesList,chartType,legendName);
}
}else{//单数据的处理,单数据中pie图形组合不一样要分开
if(chartType.equals("pie")){
getPieSeries(dataList);
}else{
List seriesList = new ArrayList();
for(int j=0;j<xAxisList.size();j++){
String xValue = xAxisList.get(j).toString();
Map map = new HashMap();
String value = "";
for(int k=0;k<dataList.size();k++){
map = (Map)dataList.get(k);
if(map.get("xAxis").equals(xValue)){
value = map.get("series").toString();
break;
}
}
seriesList.add(value.equals("")?"0":value);
}
getSeries(seriesList,chartType,"");
}
}
}
return option;
}
public List getData(List data,int seriesNum,String dataLine){ //数据list,数据序列号,x轴或legend,序列号比如第一列、第二列,1:xAxis,2:series,3:legend
List list = new ArrayList();
List dataList = data;
String tempStr = ",";
for(int i=0;i<dataList.size();i++){
Map map = (Map)dataList.get(i);
if(map.size()>=seriesNum||dataLine.equals("legend")){
Object valueStr = map.get(dataLine);
if(valueStr!=null){
if(!tempStr.contains(","+valueStr+",")){
list.add(valueStr);
tempStr+=valueStr+",";
}
}
}else{
list = null;
break;
}
}
return list;
}
/**
*
*/
public Option getSeries(List seriesList, String chartType,String chartName){
if (chartType.equals("bar")) {//柱状图
Bar bar = new Bar();
bar.name(chartName).data(seriesList.toArray());
option.series(bar);
}
if (chartType.equals("line")) {//折线图
Line line = new Line();
line.name(chartName).data(seriesList.toArray());
option.series(line);
}
if (chartType.equals("k")) {//K线图
K k = new K();
k.name(chartName).data(seriesList.toString());
option.series(k);
}
if (chartType.equals("scatter")) {//散点图
}
if (chartType.equals("radar")) {//雷达图
}
if (chartType.equals("chord")) {//和弦图
}
if (chartType.equals("force")) {//力导布局图
}
if (chartType.equals("gauge")) {//仪表盘
}
if (chartType.equals("funnel")) {//漏斗图
}
if (chartType.equals("lsland")) {//孤岛
}
if (chartType.equals("map")) {//地图,暂时不考虑
}
return option;
}
public Option getPieSeries(List dataList){
List newList = new ArrayList();
for(int i=0;i<dataList.size();i++){
Map map = (Map)dataList.get(i);
Map newMap = new HashMap();
newMap.put("value", map.get("series"));
newMap.put("name", map.get("xAxis"));
newList.add(newMap);
}
Pie pie = new Pie();
pie.name("").data(newList.toArray());
option.series(pie);
return option;
}
public Option getMagic(){
Feature f = new Feature();
if (chartType.equals("bar")) {//柱状图
option.toolbox().feature(f.magicType.type((new Object[] { Magic.bar, Magic.line,Magic.stack,Magic.tiled })));
}
if (chartType.equals("line")) {//折线图
option.toolbox().feature(f.magicType.type((new Object[] { Magic.line, Magic.bar,Magic.stack})));
}
if (chartType.equals("pie")) {//饼图
option.toolbox().feature(f.magicType.type((new Object[] { Magic.pie, Magic.funnel })));
}
if (chartType.equals("k")) {//K线图
option.toolbox().feature(f.magicType.type((new Object[] { Magic.line, Magic.bar })));
}
if (chartType.equals("scatter")) {//散点图
}
if (chartType.equals("radar")) {//雷达图
}
if (chartType.equals("chord")) {//和弦图
}
if (chartType.equals("force")) {//力导布局图
}
if (chartType.equals("gauge")) {//仪表盘
}
if (chartType.equals("funnel")) {//漏斗图
}
if (chartType.equals("lsland")) {//孤岛
}
if (chartType.equals("map")) {//地图,暂时不考虑
}
return option;
}
}