echars java帮助类使用操作实现

java代码:

package com.xk.cbs.sms.statisticAnalysis.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.alibaba.fastjson.JSON;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.axis.Axis;
import com.github.abel533.echarts.axis.AxisLabel;
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.Orient;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.code.X;
import com.github.abel533.echarts.data.PieData;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.Pie;
import com.xk.cbs.sms.statisticAnalysis.model.StatisticDataVo;
import com.xk.cbs.sms.statisticAnalysis.service.StatisticAnalysisService;
import com.xk.cbs.tms.classstudent.service.ClassStudentService;
import com.xk.cbs.tms.classtudent.po.dto.ClassStudent;
import com.xk.lang.redis.RedisKeyConstant;
import com.xk.lang.redis.RedisOperate;
import com.xk.lang.redis.ShardedJedisPoolService;


@Controller
@RequestMapping("sms/statisticAnalysis")
public class StatisticAnalysisController {

@Autowired
private StatisticAnalysisService statisticAnalysisService;

//数据字典接口
@Autowired
private ShardedJedisPoolService shardedJedisPoolService;

@Autowired
private ClassStudentService studentService;

//登陆用户接口
@Autowired
private RedisOperate redisOperate;

/**
* 初始化页面
* @return
*/
@RequestMapping("/init")
public String init(){
return "sms/statistic/student_statistics";
}

/**
* 根据条件统计学生数据
* @param model
* @param classify 查询字段
* @param scope 查询范围
* @param ways 展示形式
* @return
*/
@RequestMapping(path = "/data")
@ResponseBody
public String getDataByType(Model model,String classify,String scope,String ways){
String schoolNo = redisOperate.getUser().getUserNo();//学生编码
//参数
Map<String,Object> param = new HashMap<>();
param.put("schoolNo", schoolNo);
param.put(scope, scope);
List<ClassStudent> stuList = studentService.getStuInfoByParamList(param);
Map<String,Object> map = new HashMap<String,Object>();
//统计当前学校的班级的学生数据
map.put("classify", classify);
map.put("schoolNo", "'"+schoolNo+"'");
//获取统计数据源
List<StatisticDataVo> data = statisticAnalysisService.getDataByParam(map);
Option option = null;
if("bar".equals(ways)){
option =  getBarData(data,stuList,classify,scope,ways,schoolNo);
}else if("pie".equals(ways)){
option = getPieData(data,stuList,classify,scope,ways,schoolNo);
}else if("line".equals(ways)){
option = getLineData(data,stuList,classify,scope,ways,schoolNo);
}
return JSON.toJSONString(option);
}

/**
* <b>方法描述:</b>柱状图数据<br/>
* <b>参数描述:</b><br/>
@param data
@param stuList
@param classify 统计分类
@param scope 统计级别
@param ways 展示方式
@return<br/>
* @return Option<br/>
*/
@SuppressWarnings("rawtypes")
public Option getBarData(List<StatisticDataVo> data,List<ClassStudent> stuList,String classify,String scope,String ways,String schoolNo){
Option option = new Option();
option.title().text("学生数据统计").subtext("范围(学校)");
        option.tooltip().trigger(Trigger.axis);
        
        //头部数据
        List<String> legend = new ArrayList<>();
        for (StatisticDataVo obj : data) {
        legend.add(legendIsValida(obj.getFieldName(),classify));
}
        option.legend(legend.toArray());
        option.legend().x(X.left);//类目显示位置
        option.legend().y(120);//类目显示位置
        option.legend().orient(Orient.vertical);
        option.legend().padding(0);
        option.toolbox().show(true).feature(Tool.saveAsImage);
        option.calculable(true);
        //x轴数据
        List<String> xAxis = new ArrayList<>();
    for (ClassStudent cs : stuList) {
    if("school".equals(scope)){//学校
    xAxis.add("学校");
    }else if("gradeId".equals(scope)){//年级
    if(StringUtils.isNotEmpty(cs.getGradeName())){
    xAxis.add(cs.getGradeName());
    }else{
    xAxis.add("未分班数据");
    }
    }else if("classInfoId".equals(scope)){//班级
    if(StringUtils.isNotEmpty(cs.getClassName())){
    xAxis.add(cs.getGradeName()+cs.getClassName());
    }else{
    xAxis.add("未分班数据");
    }
    }
}
    //设置类目样式
    Axis axis = new CategoryAxis();
    axis.setType(AxisType.category);
    axis.data(xAxis.toArray());
    if(xAxis.size()>7){
    axis.setAxisLabel(new AxisLabel().interval(0).rotate(60));
    }
        option.setxAxis(Arrays.asList(axis));
        option.yAxis(new ValueAxis());
        //创建类目轴
        for (StatisticDataVo obj : data) {
        Bar bar = new Bar();
        bar.setName(legendIsValida(obj.getFieldName(),classify));
        Map<String,Object> param = new HashMap<>();
    param.put("schoolNo", schoolNo);
    param.put(scope, scope);
    param.put("classify", classify+"='"+obj.getFieldName()+"'");
    List<ClassStudent> result = studentService.getStuInfoByParamList(param);
    
    List<String> barData = new ArrayList<String>();
    for (int i=0;i<stuList.size();i++) {
    Integer compareVal = null;
    Integer compareVal2 = null;
    if("gradeId".equals(scope)){
    compareVal = stuList.get(i).getGradeId();
    }else if("classInfoId".equals(scope)){
    compareVal = stuList.get(i).getClassInfoId();
    }
    String amount = "";
    for (ClassStudent cs : result) {
    if("gradeId".equals(scope)){
    compareVal2 = cs.getGradeId();
        }else if("classInfoId".equals(scope)){
        compareVal2 = cs.getClassInfoId();
        }
    if(compareVal==null && compareVal2==null){
    amount = cs.getAmount();
    }else if(compareVal!=null && compareVal2!=null){
    if(compareVal.equals(compareVal2)){
    amount = cs.getAmount();
    }
    }
}
    if(amount!=""){
    barData.add(amount);
    }else{
    barData.add("0");
    }
        }
        bar.data(barData.toArray());
        option.series(bar);
}
        option.grid().x(170).x2(100).y2(130);
return option;
}
/**
* <b>方法描述:</b>饼状图数据<br/>
* <b>参数描述:</b><br/>
@param data
@param stuList
@param classify 统计分类
@param scope 统计级别
@param ways 展示方式
@return<br/>
* @return Option<br/>
*/
public Option getPieData(List<StatisticDataVo> data,List<ClassStudent> stuList,String classify,String scope,String ways,String schoolNo){
Option option = new Option();
option.title().text("学生数据统计").subtext("范围(学校)");
option.tooltip().trigger(Trigger.axis);

//头部数据
option.toolbox().show(true).feature(Tool.saveAsImage);
option.calculable(true);
//创建类目轴
Pie pie = new Pie("数据来源");
String showName = "学校";
for (StatisticDataVo obj : data) {

Map<String,Object> param = new HashMap<>();
    param.put("schoolNo", schoolNo);
    param.put(scope, scope);
    param.put("classify", classify+"='"+obj.getFieldName()+"'");
    List<ClassStudent> result = studentService.getStuInfoByParamList(param);
for (ClassStudent cs : result) {
if("gradeId".equals(scope)){
if(StringUtils.isNotEmpty(cs.getGradeName())){
showName = cs.getGradeName();
}else{
showName = "未分班数据";
}
}else if("classInfoId".equals(scope)){
if(StringUtils.isNotEmpty(cs.getGradeName())){
showName = cs.getGradeName()+cs.getClassName();
}else{
showName = "未分班数据";
}
}
pie.data(new PieData(showName+"("+legendIsValida(obj.getFieldName(),classify)+":"+cs.getAmount()+")",cs.getAmount())); 
}
option.series(pie);
}
return option;
}
/**
* <b>方法描述:</b>折线图数据<br/>
* <b>参数描述:</b><br/>
@param data
@param stuList
@param classify 统计分类
@param scope 统计级别
@param ways 展示方式
@return<br/>
* @return Option<br/>
*/
@SuppressWarnings("rawtypes")
public Option getLineData(List<StatisticDataVo> data,List<ClassStudent> stuList,String classify,String scope,String ways,String schoolNo){
Option option = new Option();
option.title().text("学生数据统计").subtext("范围(学校)");
option.tooltip().trigger(Trigger.axis);

//头部数据
List<String> legend = new ArrayList<>();
for (StatisticDataVo obj : data) {
legend.add(legendIsValida(obj.getFieldName(),classify));
}
option.legend(legend.toArray());
option.legend().x(X.left);//类目显示位置
   option.legend().y(120);//类目显示位置
   option.legend().orient(Orient.vertical);
   option.legend().padding(0);
option.toolbox().show(true).feature(Tool.saveAsImage);
option.calculable(true);
//x轴数据
List<String> xAxis = new ArrayList<>();
for (ClassStudent cs : stuList) {
if("school".equals(scope)){//学校
    xAxis.add("学校");
    }else if("gradeId".equals(scope)){//年级
    if(StringUtils.isNotEmpty(cs.getGradeName())){
    xAxis.add(cs.getGradeName());
    }else{
    xAxis.add("未分班数据");
    }
    }else if("classInfoId".equals(scope)){//班级
    if(StringUtils.isNotEmpty(cs.getClassName())){
    xAxis.add(cs.getGradeName()+cs.getClassName());
    }else{
    xAxis.add("未分班数据");
    }
    }
}
//设置类目样式
    Axis axis = new CategoryAxis();
    axis.setType(AxisType.category);
    axis.data(xAxis.toArray());
    if(xAxis.size()>7){
    axis.setAxisLabel(new AxisLabel().interval(0).rotate(60));
    }
        option.setxAxis(Arrays.asList(axis));
option.yAxis(new ValueAxis());
//创建类目轴
for (StatisticDataVo obj : data) {
Line bar = new Line();
bar.setName(legendIsValida(obj.getFieldName(),classify));
Map<String,Object> param = new HashMap<>();
param.put("schoolNo", schoolNo);
param.put(scope, scope);
param.put("classify", classify+"='"+obj.getFieldName()+"'");
List<ClassStudent> result = studentService.getStuInfoByParamList(param);

List<String> barData = new ArrayList<String>();
for (int i=0;i<stuList.size();i++) {
Integer compareVal = null;
Integer compareVal2 = null;
if("gradeId".equals(scope)){
compareVal = stuList.get(i).getGradeId();
}else if("classInfoId".equals(scope)){
compareVal = stuList.get(i).getClassInfoId();
}
String amount = "";
for (ClassStudent cs : result) {
if("gradeId".equals(scope)){
compareVal2 = cs.getGradeId();
}else if("classInfoId".equals(scope)){
compareVal2 = cs.getClassInfoId();
}
if(compareVal==null && compareVal2==null){
    amount = cs.getAmount();
    }else if(compareVal!=null && compareVal2!=null){
    if(compareVal.equals(compareVal2)){
    amount = cs.getAmount();
    }
    }
}
if(amount!=""){
barData.add(amount);
}else{
barData.add("0");
}
}
bar.data(barData.toArray());
option.series(bar);
}
option.grid().x(170).y2(150);
return option;
}


/**
* <b>方法描述:</b>读取统计类型数据源<br/>
* <b>参数描述:</b><br/>
@param model
@param classify
@return<br/>
* @return Model<br/>
*/
private Map<String, String> getCountType(String classify) {
//取得数据字典
// ShardedJedis shardedJedis = null;
Map<String, String> map = null;
try {
// shardedJedis = shardedJedisPoolService.getResource();
map = new HashMap<>();
if("sex".equals(classify)){
map = shardedJedisPoolService.hgetAll(RedisKeyConstant.SMS_SEX_KEY);
}else if("entranceWay".equals(classify)){
map = shardedJedisPoolService.hgetAll(RedisKeyConstant.SMS_WAYSOFENROLL_KEY);
}else if("source".equals(classify)){
map = shardedJedisPoolService.hgetAll(RedisKeyConstant.SMS_STUDENTSOURCE_KEY);
}else if("studyWay".equals(classify)){
map = shardedJedisPoolService.hgetAll(RedisKeyConstant.SMS_READMODE_KEY);
}else if("nation".equals(classify)){
map = shardedJedisPoolService.hgetAll(RedisKeyConstant.NATIONALITY_KEY);
}else if("bloodType".equals(classify)){
map = shardedJedisPoolService.hgetAll(RedisKeyConstant.SMS_BLOOD_KEY);
}else if("category".equals(classify)){
map = shardedJedisPoolService.hgetAll(RedisKeyConstant.SMS_CATEGORY_KEY);
}
} catch (Exception e) {
e.printStackTrace();
}/*finally {
if(shardedJedis!=null){
shardedJedis.close();
}
}*/
return map;
}

/**
* <b>方法描述:</b>legend数据校验<br/>
* <b>参数描述:</b><br/>
@param fieldName
@param classify
@return<br/>
* @return String<br/>
*/
public String legendIsValida(String fieldName,String classify){
Map<String,String> dataMap = getCountType(classify);
    if(StringUtils.isNotEmpty(fieldName)){
    String name = dataMap.get(fieldName);
    if(StringUtils.isNotEmpty(name)){
    return name;
    }else{
    return fieldName;
    }
    }else{
    return "其他";
    }
}

}



jsp代码:

<!---PC--首页右侧内容-学生统计管理-统计图  START--->
<div class="student_statistics_con">
<div id="statistics" style="width: 70%; height: 600px;"></div>
</div>
<!---PC--首页右侧内容-学生统计管理-统计图  END--->


<!---PC--首页右侧内容-查询功能区域  结尾--->
<script type="text/javascript" src="${statics}/js/ui/source/echarts-all.js"></script>
<script type="text/javascript">
//图表  
var psLineChar = echarts.init(document.getElementById('statistics'));  
//查询  
function loadDrugs() {  
    psLineChar.clear();  
    psLineChar.showLoading({text: '正在努力的读取数据中...'});  
    $.getJSON('${ctx}/sms/statisticAnalysis/data',$('#searchFrom').serialize(), function (data) {
        psLineChar.setOption(data, true);  
        psLineChar.hideLoading();  
    });  
}  
//载入图表  
loadDrugs();  
</script>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值