打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Echarts  java 后台交互

jsp页面

[html] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  

  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

  3. <html>  

  4. <head>  

  5. <title>line</title>  

  6. <script type="text/javascript" src="${pageContext.request.contextPath}/js/echarts2.0/esl.js"></script>  

  7. <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>  

  8. </head>  

  9. <body>  

  10.     <!-- 为ECharts准备一个具备大小(宽高)的Dom -->  

  11.         <div id="main" style="height:400px"></div>  

  12.     <script type="text/javascript" language="javascript">  

  13.         var myChart;  

  14.         var eCharts;  

  15.         require.config({  

  16.             paths : {  

  17.                 'echarts' : '${pageContext.request.contextPath}/js/echarts2.0/echarts' ,  

  18.                 'echarts/chart/line' : '${pageContext.request.contextPath}/js/echarts2.0/echarts' //需要的组件  

  19.             }  

  20.         });  

  21.         require(  

  22.             [ 'echarts',   

  23.               'echarts/chart/line'  

  24.             ], DrawEChart //异步加载的回调函数绘制图表  

  25.         );  

  26.         //创建ECharts图表方法  

  27.         function DrawEChart(ec) {  

  28.             eCharts = ec;  

  29.             myChart = eCharts.init(document.getElementById('main'));  

  30.             myChart.showLoading({  

  31.                 text : "图表数据正在努力加载..."  

  32.             });  

  33.             //定义图表options  

  34.             var options = {  

  35.                 title : {  

  36.                     text : "未来一周气温变化",  

  37.                     subtext : "纯属虚构",  

  38.                     sublink : "http://www.baidu.com"  

  39.                 },  

  40.                 tooltip : {  

  41.                     trigger : 'axis'  

  42.                 },  

  43.                 legend : {  

  44.                     data : [ "最高气温" ]  

  45.                 },  

  46.                 toolbox : {  

  47.                     show : true,  

  48.                     feature : {  

  49.                         mark : {  

  50.                             show : true  

  51.                         },  

  52.                         dataView : {  

  53.                             show : true,  

  54.                             readOnly : false  

  55.                         },  

  56.                         magicType : {  

  57.                             show : true,  

  58.                             type : [ 'line', 'bar' ]  

  59.                         },  

  60.                         restore : {  

  61.                             show : true  

  62.                         },  

  63.                         saveAsImage : {  

  64.                             show : true  

  65.                         }  

  66.                     }  

  67.                 },  

  68.                 calculable : true,  

  69.                 xAxis : [ {  

  70.                     type : 'category',  

  71.                     boundaryGap : false,  

  72.                     data : [ '1', '2', '3', '4', '5', '6', '7' ]  

  73.                 } ],  

  74.                 yAxis : [ {  

  75.                     type : 'value',  

  76.                     axisLabel : {  

  77.                         formatter : '{value} °C'  

  78.                     },  

  79.                     splitArea : {  

  80.                         show : true  

  81.                     }  

  82.                 } ],  

  83.                 grid : {  

  84.                     width : '90%'  

  85.                 },  

  86.                 series : [ {  

  87.                     name : '最高气温',  

  88.                     type : 'line',  

  89.                     data : [ 11, 22, 33, 44, 55, 33, 44 ],//必须是Integer类型的,String计算平均值会出错  

  90.                     markPoint : {  

  91.                         data : [ {  

  92.                             type : 'max',  

  93.                             name : '最大值'  

  94.                         }, {  

  95.                             type : 'min',  

  96.                             name : '最小值'  

  97.                         } ]  

  98.                     },  

  99.                     markLine : {  

  100.                         data : [ {  

  101.                             type : 'average',  

  102.                             name : '平均值'  

  103.                         } ]  

  104.                     }  

  105.                 } ]  

  106.             };  

  107.             myChart.setOption(options); //先把可选项注入myChart中  

  108.             myChart.hideLoading();  

  109.             getChartData();//aja后台交互   

  110.         }  

  111.     </script>  

  112.     <script type="text/javascript">  

  113.         function getChartData() {  

  114.             //获得图表的options对象  

  115.             var options = myChart.getOption();  

  116.             //通过Ajax获取数据  

  117.             $.ajax({  

  118.                 type : "post",  

  119.                 async : false, //同步执行  

  120.                 url : "${pageContext.request.contextPath}/echarts/line_data",  

  121.                 data : {},  

  122.                 dataType : "json", //返回数据形式为json  

  123.                 success : function(result) {  

  124.                     if (result) {  

  125.                         options.legend.data = result.legend;  

  126.                         options.xAxis[0].data = result.category;  

  127.                         options.series[0].data = result.series[0].data;  

  128.                         myChart.hideLoading();  

  129.                         myChart.setOption(options);  

  130.                     }  

  131.                 },  

  132.                 error : function(errorMsg) {  

  133.                     alert("不好意思,大爷,图表请求数据失败啦!");  

  134.                     myChart.hideLoading();  

  135.                 }  

  136.             });  

  137.         }  

  138.     </script>  

  139. </body>  

  140. </html>  



controller


[java] view plaincopy
  1. package com.ffcs.wlan.controller;  

  2. import java.util.ArrayList;  

  3. import java.util.Arrays;  

  4. import java.util.List;  

  5. import org.slf4j.Logger;  

  6. import org.slf4j.LoggerFactory;  

  7. import org.springframework.stereotype.Controller;  

  8. import org.springframework.web.bind.annotation.RequestMapping;  

  9. import org.springframework.web.bind.annotation.ResponseBody;  

  10. import com.ffcs.wlan.model.EchartData;  

  11. import com.ffcs.wlan.model.Series;  

  12. @Controller  

  13. @RequestMapping("/echarts")  

  14. public class EntityController {  

  15.     private static final Logger logger = LoggerFactory.getLogger(EntityController.class);  

  16.     @RequestMapping("/line_data")  

  17.     @ResponseBody  

  18.     public EchartData lineData() {  

  19.         logger.info("lineData....");  

  20.         List<String> legend = new ArrayList<String>(Arrays.asList(new String[]{"最高气温"}));//数据分组  

  21.         List<String> category = new ArrayList<String>(Arrays.asList(new String []{"周一","周二","周三","周四","周五","周六","周日"}));//横坐标  

  22.         List<Series> series = new ArrayList<Series>();//纵坐标  

  23.         series.add(new Series("最高气温", "line",   

  24.                         new ArrayList<Integer>(Arrays.asList(  

  25.                                 21,23,28,26,21,33,44))));  

  26.         EchartData data=new EchartData(legend, category, series);  

  27.         return data;  

  28.     }  

  29.     @RequestMapping("/line_page")  

  30.     public String linePage() {  

  31.         logger.info("linePage....");  

  32.         return "report/line";  

  33.     }  

  34. }  


Echarts 类


[java] view plaincopy
  1. package com.ffcs.wlan.model;  

  2. import java.util.ArrayList;  

  3. import java.util.List;  

  4. public class EchartData {  

  5.     public List<String> legend = new ArrayList<String>();//数据分组  

  6.     public List<String> category = new ArrayList<String>();//横坐标  

  7.     public List<Series> series = new ArrayList<Series>();//纵坐标  

  8.     public EchartData(List<String> legendList, List<String> categoryList, List<Series> seriesList) {  

  9.         super();  

  10.         this.legend = legendList;  

  11.         this.category = categoryList;  

  12.         this.series = seriesList;  

  13.     }  

  14. }  


Series 类


[java] view plaincopy
  1. package com.ffcs.wlan.model;  

  2. import java.util.List;  

  3. public class Series  {  

  4.     public String name;  

  5.     public String type;  

  6.     public List<Integer> data;//这里要用int 不能用String 不然前台显示不正常(特别是在做数学运算的时候)  

  7.     public Series( String name, String type, List<Integer> data) {  

  8.         super();  

  9.         this.name = name;  

  10.         this.type = type;  

  11.         this.data = data;  

  12.     }  

  13. }  



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
技术图文:如何利用C# + Echarts 绘制 Bar Simple?
ASP.NET+JQuery+.Ashx实现+百度Echarts 实现动态柱状图数据图形报表的统计
java List 转 String
java.util.ArrayList排序示例
java根据年份循环每一天的时间
树莓派:4. 树莓派搭建美观的物联网温度服务器
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服