【备忘录】Java后台和Echart图表数据交互

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/java_faep/article/details/81668737
package com.epoint.PingBiao_Common.spc.bim.domain;

import java.util.ArrayList;
import java.util.List;

/**
 * 存放Echart数据
 *  [一句话功能简述]
 *  [功能详细描述]
 * @作者 Faep
 * @version [版本号, 2018年8月13日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class EchartData
{
    public List<String> legend = new ArrayList<String>();//数据分组  
    public List<String> category = new ArrayList<String>();//横坐标  
    public List<Series> series = new ArrayList<Series>();//纵坐标  


    public EchartData(List<String> legendList, List<String> categoryList, List<Series> seriesList) {  
        super();  
        this.legend = legendList;  
        this.category = categoryList;  
        this.series = seriesList;  
    }  

}
package com.epoint.PingBiao_Common.spc.bim.domain;

import java.util.List;
/**
 * 存放Echart数据
 *  [一句话功能简述]
 *  [功能详细描述]
 * @作者 Faep
 * @version [版本号, 2018年8月14日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class Series
{
    public String name;  

    public String type;  

    public List<Double> data;  
    public Series( String name, String type, List<Double> data) {  
        super();  
        this.name = name;  
        this.type = type;  
        this.data = data;  
    }  
}
package com.epoint.PingBiao_Common.spc.bim.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RestController;

import com.epoint.PingBiao_Common.spc.bim.domain.EchartData;
import com.epoint.PingBiao_Common.spc.bim.domain.Series;
import com.epoint.ZtbCommon.ZtbCommonDao;
import com.epoint.basic.controller.BaseController;
import com.epoint.core.dto.base.TreeNode;
import com.epoint.core.dto.model.TreeModel;
import com.epoint.core.grammar.Record;
import com.epoint.database.config.JdbcConstantValue;
import com.epoint.database.jdbc.connection.DataSourceConfig;

/**
 * 
 *  资金趋势显示后台action
 *  界面:spcplancapitaltrend.html
 *  脚本:spcplancapitaltrend.js
 *  [功能详细描述]
 * @作者 Faep
 * @version [版本号, 2018年8月14日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@RestController("spcplancosttrendaction")
@Scope("request")
public class SpcPlanCostTrendAction extends BaseController
{
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    /**
     * 获取人材机树
     */
    private TreeModel treeModel;

    /**
     * 获取叠加复选框状态
     */
    private Boolean djstatus;

    /**
     * 树结点code
     */
    private String treecode;
    
    /**
     * 构件ID
     */
    @SuppressWarnings("unused")
    private String instanceguids;

    /**
     * 初始化
     * 已验证+1
     */
    @Override
    public void pageLoad() {
//        // 获取url传递过来的instanceguids
//        instanceguids = getRequestParameter("instanceguids");
//        // 通过构件集合字符串获取对应的条件sql
//        if (StringUtils.isNotBlank(instanceguids)) {
//            if (instanceguids.contains(",")) {
//                String[] lst = instanceguids.split(",");
//                instanceguids = "'" + StringUtil.join(lst, "','") + "'";
//            }
//            else if (!instanceguids.equals("")) {
//                instanceguids = "'" + instanceguids + "'";
//            }
//        }
    }
    
    private ZtbCommonDao getSQLiteDao(){
        String path = "E:\\待办\\BIM\\代码demo\\111.bc-jgk";
        DataSourceConfig config = new DataSourceConfig(path, JdbcConstantValue.SQLLite);
        ZtbCommonDao dao = ZtbCommonDao.getInstance(config);
        
        return dao;
    }

    /**
     * 
     *  [获取人材机树]
     *  [功能详细描述]
     *  @return    
     * @exception/throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    @SuppressWarnings("serial")
    public TreeModel getTree() {
        if (treeModel == null) {
            treeModel = new TreeModel()
            {
                @Override
                public List<TreeNode> fetch(TreeNode treeNode) {
                    List<TreeNode> list = new ArrayList<TreeNode>();
                    TreeNode root = new TreeNode();
                    root.setText("全部");
                    root.setId("0");
                    root.setPid("-1");
                    list.add(root);
                    root.setExpanded(true);//展开下一层节点
                    TreeNode node1 = new TreeNode();
                    node1.setText("人工");
                    node1.setId("1");
                    node1.setPid("0");
                    node1.setLeaf(true);
                    list.add(node1);
                    TreeNode node2 = new TreeNode();
                    node2.setText("材料");
                    node2.setId("2");
                    node2.setPid("0");
                    list.add(node2);
                    TreeNode node21 = new TreeNode();
                    node21.setText("黄沙");
                    node21.setId("21");
                    node21.setPid("2");
                    node21.setLeaf(true);
                    list.add(node21);
                    TreeNode node22 = new TreeNode();
                    node22.setText("水泥");
                    node22.setId("22");
                    node22.setPid("2");
                    node22.setLeaf(true);
                    list.add(node22);
                    TreeNode node23 = new TreeNode();
                    node23.setText("混凝土");
                    node23.setId("23");
                    node23.setPid("2");
                    node23.setLeaf(true);
                    list.add(node23);
                    TreeNode node3 = new TreeNode();
                    node3.setText("机械");
                    node3.setId("3");
                    node3.setPid("0");
                    node3.setLeaf(true);
                    list.add(node3);
                    return list;
                }
            };
        }
        return treeModel;
    }

    /**
     * 
     *  根据选择人材机时显示图表数据
     *  [功能详细描述]
     *  @param clbhjsonstr
     *  材料编号的json字符串
     *  @param unitguid
     *  工程guid
     *  @param isMonth
     *  显示每月资金
     *  @return    
     * @exception/throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public EchartData lineData() {
        List<String> legend = new ArrayList<String>();//纵坐标数据展示
        List<Series> series = new ArrayList<Series>();//纵坐标数据展示
        List<String> serisData = new ArrayList<String>();//横坐标数据展示
        List<String> monthList = dateList();//获取x轴的月份列表
        List<Double> jihuaList = new ArrayList<Double>();//计划
        List<Double> shijiList = new ArrayList<Double>();//实际
        List<Record> jihuaRecord = dataList(treecode);//getAllFyjh();
        List<Record> shijiRecord = dataList(treecode);//getAllFysj();
        for (int i = 0; i < monthList.size(); i++) {
            serisData.add(monthList.get(i));
            boolean flagJh = false;
            for (Record record : jihuaRecord) {
                if (record.getStr("realend").equals(monthList.get(i))) {
                    switch (treecode) {
                        case "0":
                            jihuaList.add(record.getDouble("zfy"));
                            break;
                        default:
                            jihuaList.add(record.getDouble("totalprice"));
                            break;
                    }
                    flagJh = true;
                    break;
                }
            }
            if (!flagJh) {
                jihuaList.add(0.0);
            }
            boolean flagSj = false;
            for (Record record : shijiRecord) {
                if (record.getStr("realend").equals(monthList.get(i))) {
                    switch (treecode) {
                        case "0":
                            shijiList.add(record.getDouble("zfy"));
                            break;
                        default:
                            shijiList.add(record.getDouble("totalprice"));
                            break;
                    }
                    flagSj = true;
                    break;
                }
            }
            if (!flagSj) {
                shijiList.add(0.0);
            }
        }
        jihuaList = getData(djstatus, jihuaList);
        shijiList = getData(djstatus, shijiList);
        
        //赋值series变量的数据
        series.addAll(getSeriesByData(jihuaList, shijiList));
        
        //赋值legen变量的数据
        legend.add("计划日期");
        legend.add("实际日期");
        
        EchartData data = new EchartData(legend, monthList, series);
        
        return data;
    }
    
    /**
     * 获取日期月份
     *  [一句话功能简述]
     *  [功能详细描述]
     *  @return    
     * @exception/throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private List<String> dateList(){
        ZtbCommonDao dao = getSQLiteDao();
        String sql = "select realend from ComponentTime";
        List<String> dateList = dao.findList(sql, String.class);
        dao.close();
        dao = null;
        return dateList;
    }
    
    /**
     * 获取人材机数据
     *  [一句话功能简述]
     *  [功能详细描述]
     *  @return    
     * @exception/throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private List<Record> dataList(String treecode){
        ZtbCommonDao dao = getSQLiteDao();
        String where = "";
        switch (treecode) {
            case "0":
                return getAllPriceCost();
            case "1":
                where = "1.0";
                break;
            case "2":
                where = "2.0";
                break;
            case "3":
                where = "3.0";
                break;
            default:
                break;
        }
        
        String sql = "select a.realend,b.componentid,sum(c.price) as totalprice from componenttimeinstance b LEFT JOIN "
                + "componenttime a on a.id = b.componenttimeid LEFT JOIN zaojiarcj c where b.componentid = c.instanceid "
                + "and c.rcjlb='" + where + "' GROUP BY a.realend";
        
        List<Record> dateList = dao.findList(sql, Record.class);
        dao.close();
        dao = null;
        return dateList;
    }
    
    /**
     * 获取总价
     *  [一句话功能简述]
     *  [功能详细描述]
     *  @return    
     * @exception/throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private List<Record> getAllPriceCost(){
        ZtbCommonDao dao = getSQLiteDao();
        String sql = "select realend,cost*10000.0 as zfy from ComponentTime";
        List<Record> list = dao.findList(sql, Record.class);
        dao.close();
        dao = null;
        return list;
    }
    
    
    /**
     * 
     *  [选择材料时显示图表]
     *  [功能详细描述]
     *  @return    
     * @exception/throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public EchartData lineDataClbh() {
        List<String> legend = new ArrayList<String>();//纵坐标数据展示
        List<Series> series = new ArrayList<Series>();//纵坐标数据展示
        List<String> serisData = new ArrayList<String>();//横坐标数据展示
        //获取x轴的月份列表
        List<String> monthList = dateList();
        List<Double> jihuaList = new ArrayList<Double>();
        List<Double> shijiList = new ArrayList<Double>();
        List<Record> jihuaRecord = new ArrayList<Record>();
        List<Record> shijiRecord = new ArrayList<Record>();
        
        jihuaRecord = dataCLList(treecode);
        shijiRecord = dataCLList(treecode);//TODO 目前二者一样,如何区别
        
        for (int i = 0; i < monthList.size(); i++) {
            serisData.add(monthList.get(i));
            boolean flagJh = false;
            for (Record record : jihuaRecord) {
                if (record.getStr("realend").equals(monthList.get(i))) {
                    jihuaList.add(record.getDouble("totalprice"));
                    flagJh = true;
                    break;
                }
            }
            if (!flagJh) {
                jihuaList.add(0.0);
            }
            boolean flagSj = false;
            for (Record record : shijiRecord) {
                if (record.getStr("realend").equals(monthList.get(i))) {//TODO
                    shijiList.add(record.getDouble("totalprice"));
                    flagSj = true;
                    break;
                }
            }
            if (!flagSj) {
                shijiList.add(0.0);
            }
        }
        jihuaList = getData(djstatus, jihuaList);
        shijiList = getData(djstatus, shijiList);
        
        //赋值series变量的数据
        series.addAll(getSeriesByData(jihuaList, shijiList));
        
        //赋值legen变量的数据
        legend.add("计划日期");
        legend.add("实际日期");
        
        EchartData data = new EchartData(legend, monthList, series);
        
        return data;
    }
    
    /**
     * 获取材料细则数据
     *  [一句话功能简述]
     *  [功能详细描述]
     *  @return    
     * @exception/throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private List<Record> dataCLList(String treecode){
        ZtbCommonDao dao = getSQLiteDao();
        String where = "";
        switch (treecode) {
            case "21":
                where = "'%黄沙%'";
                break;
            case "22":
                where = "'%水泥%'";
                break;
            case "23":
                where = "'%混凝土%'";
                break;
            default:
                break;
        }
        
        String sql = "select a.realend,b.componentid,sum(c.price) as totalprice from componenttimeinstance b LEFT JOIN "
                + "componenttime a on a.id = b.componenttimeid LEFT JOIN zaojiarcj c where b.componentid = c.instanceid "
                + "and c.rcjlb='2' and c.name like " + where + " GROUP BY a.realend";
        
        List<Record> dateList = dao.findList(sql, Record.class);
        dao.close();
        dao = null;
        return dateList;
    }

    /**
     * 
     *  根据list数据按照月份是否累计得到新的集合数据
     *  [功能详细描述]
     *  @param isMonth
     *  @param list
     *  @return    
     * @exception/throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private List<Double> getData(boolean isMonth, List<Double> list) {
        //判断是否按月统计(true表示按月统计,false表示按月累计)
        if (!isMonth) {
            return list;
        }
        else {
            List<Double> lstResult = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                lstResult.add(getListLJ(list, i));
            }
            return lstResult;
        }
    }
    
    /**
     * 获取月份累计预算
     */
    private double getListLJ(List<Double> list, int index){
        double val = 0;
        for (int i = 0; i < index; i++) {
            val += list.get(i);
        }
        return val;
    }

    /**
     * 
     *  根据list数据获取Series数据
     *  [功能详细描述]
     *  @param jhlist
     *  @param sjlist
     *  @return    
     * @exception/throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private List<Series> getSeriesByData(List<Double> jhlist, List<Double> sjlist) {
        List<Series> series = new ArrayList<Series>();

        series.add(new Series("计划成本", "line", jhlist));
        series.add(new Series("实际成本", "line", sjlist));
        return series;
    }

    public Boolean getDjstatus() {
        return djstatus;
    }

    public void setDjstatus(Boolean djstatus) {
        this.djstatus = djstatus;
    }

    public String getTreecode() {
        return treecode;
    }

    public void setTreecode(String treecode) {
        this.treecode = treecode;
    }

}
<!-- page/costtrend/spcplancosttrend.html -->
<!DOCTYPE html>
<html>
<head>
<title>计划成本趋势</title>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="../../../../../fui/js/cssboot.js"></script>
<style type="text/css">
.mini-checkbox {
    font-size: 9pt;
    line-height: 26px;
    position: relative;
    font-family: Microsoft Yahei;
    margin-right: 15px;
}
</style>
</head>
<body>
	<!-- 必须有,加载时的loading效果 -->
	<div class="page-loading"></div>
	<!-- 左侧条件区域 -->
	<div class="fui-left">
		<div role="body">
			<ul id="rcj-tree" class="mini-tree" action="getTree" style="width:100%;height:100%;"></ul>
			<input class="mini-hidden" id="treecode" bind="treecode" />
		</div>
	</div>
	<!-- 右侧图表区域 -->
	<div class="fui-right">
		<div class="fui-toolbar">
			<div id="dj-checkbox" bind="djstatus" text="按日期累计" class="mini-checkbox"  onvaluechanged="onDjValueChanged"></div>
		</div>

		<div class="fui-content" style="padding-top: 10px;">
			<div id="main" style="width: 85%; height: 95%; margin-left: 6%"></div>
		</div>
	</div>
	<script src="../../../../../fui/js/jsboot.js"></script>
	<script src="./js/spcplancosttrend.js"></script>
	
</body>
</html>
/*
 * 成本趋势脚本(costtrend/spcplancosttrend.js)
 * 界面:spcplancosttrend.html
 */
document.write("<script language=javascript src='../../../../../js/echarts/echarts-all.js'></script>");
var unit = mini.get("unit-combobox");//获取工程下拉框对象
var mini_rcj_tree = mini.get('rcj-tree')//获取人材机树对象
var mini_treecode=mini.get('treecode');//获取treecode对应的隐藏域
epoint.initPage('spcplancosttrendaction',null,function(data){
	mini.get("dj-checkbox").setValue(true);
	mini_treecode.setValue('0');
	getChart();//默认展示第一个工程下全部的费用的图表展示
	getChartfunc(data);
	
});
function onunitchanged(){
	getChart();
}

/*
 * 通过点击左侧材料树,图表信息切换展示
 */
mini_rcj_tree.on('nodeclick', function(e) {
	mini_treecode.setValue(e.node.id);
	//epoint.alert(e.node.id);
	if(e.node.id.length == 1){
		getChart();
	}else{
		epoint.execute('lineDataClbh', '', [ ], getChartfunc);
	}

})
/*
 * 通过勾选叠加按钮,图表信息切换展示
 */
function onDjValueChanged(){
	if(mini_treecode.getValue().length == 1){
		getChart();		
	}else{
		epoint.execute('lineDataClbh', '', [ ], getChartfunc);
	}
}
/*
 * 根据界面人材机组选择,显示图表信息
 */
function getChart() {
	epoint.execute('lineData', '', [ ], getChartfunc);
}

/*
 * 图表信息展示核心函数
 */
function getChartfunc(result) {
	var myChart = echarts.init(document.getElementById('main'));
	myChart.showLoading({
		text : "图表数据正在努力加载..."
	});
	
	// 定义图表options
	options = {
		title : {
			text : '计划成本趋势图'
		},
		tooltip : {
			trigger : 'axis',

		},
		legend : {
			data : []
		},
		grid : {
			left : '3%',
			right : '10%',
			bottom : '3%',
			containLabel : true
		},
		toolbox : {
			right : '35',
			itemSize : 20,
			itemGap : 20,
			feature : {
				saveAsImage : {
					title : '保存为图片'
				}
			},

		},
		xAxis : {
			type : 'category',
			boundaryGap : false,
			data : [],
			name:'日期'
		},
		yAxis : {
			type : 'value',
			axisLabel : {
				formatter : '{value} 元'
			},
			name:'金额'
		},
		series : []
	};
	

	// 先把可选项注入myChart中(时间轴)
	options.xAxis.data = result.category;
	
	// 获取当前月份的数据
	options.series = result.series;
	
	var namearray = new Array();
	if(options.series != 'undefined' && options.series != null){
		for (var i = 0; i < options.series.length; i++) {
			namearray[i] = options.series[i].name;
		}
		options.legend.data = namearray;
	}
	
	myChart.hideLoading();
	myChart.clear();
	myChart.setOption(options);
	// 随屏幕分辨率自适应
	window.onresize = myChart.resize;
}

猜你喜欢

转载自blog.csdn.net/java_faep/article/details/81668737
今日推荐