EchartConver.java 8.12 KB
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;
	}
}