echart详细

1、项目结构

 

js文件夹: 下载了ECharts之后,解压缩,如解压后的根目录是echarts-2.1.8,则到echarts-2.1.8\doc\example\www路径下,把里面的js文件夹直接复制粘贴到项目的 WebRoot根目录下即可

echarts.jsp: 在WebRoot根目录下新建echarts.jsp

echarts.jsp

 

[java]  view plain  copy
 
 print ?
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.     <title>ECharts实例</title>  
  12.   </head>  
  13.   <body>  
  14.     <!--Step:1 Prepare a dom for ECharts which (must) has size (width & hight)-->  
  15.     <!--Step:1 为ECharts准备一个具备大小(宽高)的Dom-->  
  16.     <div id="mainMap" style="height:500px;border:1px solid #ccc;padding:10px;"></div>  
  17.       
  18.     <!--Step:2 Import echarts.js-->  
  19.     <!--Step:2 引入echarts.js-->  
  20.     <script src="js/echarts.js"></script>  
  21.       
  22.     <script type="text/javascript">  
  23.     // Step:3 conifg ECharts's path, link to echarts.js from current page.  
  24.     // Step:3 为模块加载器配置echarts的路径,从当前页面链接到echarts.js,定义所需图表路径  
  25.     require.config({  
  26.         paths: {  
  27.             echarts: './js'  
  28.         }  
  29.     });  
  30.       
  31.     // Step:4 require echarts and use it in the callback.  
  32.     // Step:4 动态加载echarts然后在回调函数中开始使用,注意保持按需加载结构定义图表路径  
  33.     require(  
  34.         [  
  35.             //这里的'echarts'相当于'./js'  
  36.             'echarts',  
  37.             'echarts/chart/map'  
  38.         ],  
  39.         //创建ECharts图表方法  
  40.         function (ec) {  
  41.             // --- 地图 ---  
  42.                 //基于准备好的dom,初始化echart图表  
  43.             var myChart = ec.init(document.getElementById('mainMap'));  
  44.             //定义图表option  
  45.             var option = {  
  46.                 //标题,每个图表最多仅有一个标题控件,每个标题控件可设主副标题  
  47.                 title: {  
  48.                     //主标题文本,'\n'指定换行  
  49.                     text: 'iphone销量',  
  50.                     //副标题文本,'\n'指定换行  
  51.                     subtext: '纯属虚构',  
  52.                     //水平安放位置,默认为左侧,可选为:'center' | 'left' | 'right' | {number}(x坐标,单位px)  
  53.                     x: 'center',  
  54.                     //垂直安放位置,默认为全图顶端,可选为:'top' | 'bottom' | 'center' | {number}(y坐标,单位px)  
  55.                     y: 'top'  
  56.                 },  
  57.                 //图例,每个图表最多仅有一个图例  
  58.                 tooltip : {  
  59.                     //触发类型,默认('item')数据触发,可选为:'item' | 'axis'  
  60.                     trigger: 'item'  
  61.                 },  
  62.                 //图例,每个图表最多仅有一个图例  
  63.                 legend: {  
  64.                     //布局方式,默认为水平布局(即horizontal),可选为:'horizontal' | 'vertical'  
  65.                     orient: 'vertical',  
  66.                     //水平安放位置,默认为全图居中,可选为:'center' | 'left' | 'right' | {number}(x坐标,单位px)  
  67.                     x: 'left',  
  68.                     //垂直安放位置,默认为全图顶端,可选为:'top' | 'bottom' | 'center' | {number}(y坐标,单位px)  
  69.                     y: 'top',  
  70.                     //legend的data: 用于设置图例,data内的字符串数组需要与sereis数组内每一个series的name值对应  
  71.                     data:['iphone3','iphone4','iphone5']  
  72.                 },  
  73.                 //值域选择,每个图表最多仅有一个值域控件  
  74.                 dataRange: {  
  75.                     //布局方式,默认为垂直布局('vertical'),可选为:'horizontal' | 'vertical'  
  76.                     orient: 'vertical',  
  77.                     //指定的最小值,eg: 0,默认无(null),必须参数  
  78.                     min: 0,  
  79.                     //指定的最大值,eg: 100,默认无(null),必须参数  
  80.                     max: 2500,  
  81.                     //水平安放位置,默认为全图左对齐('left'),可选为:'center' | 'left' | 'right' | {number}(x坐标,单位px)  
  82.                     x: 'left',  
  83.                     //垂直安放位置,默认为全图底部('bottom'),可选为:'top' | 'bottom' | 'center' | {number}(y坐标,单位px)  
  84.                     y: 'bottom',  
  85.                     //值域文字显示,默认为数值文本  
  86.                     text: ['高','低'],             
  87.                     //是否启用值域漫游,默认为关闭(false),启用后无视splitNumber,值域显示为线性渐变  
  88.                     calculable: true  
  89.                 },  
  90.                 //工具箱,每个图表最多仅有一个工具箱  
  91.                 toolbox: {  
  92.                     //显示策略,可选为:true(显示) | false(隐藏),默认值为false  
  93.                     show: true,  
  94.                     //布局方式,默认为水平布局('horizontal'),可选为:'horizontal' | 'vertical'  
  95.                     orient: 'vertical',  
  96.                     //水平安放位置,默认为全图右对齐('right'),可选为:'center' | 'left' | 'right' | {number}(x坐标,单位px)  
  97.                     x: 'right',  
  98.                     //垂直安放位置,默认为全图顶端,可选为:'top' | 'bottom' | 'center' | {number}(y坐标,单位px)  
  99.                     y: 'center',  
  100.                     //启用功能,目前支持feature,工具箱自定义功能回调处理  
  101.                     feature: {  
  102.                         //辅助线标志  
  103.                         mark: {show: true},  
  104.                         //数据视图,打开数据视图,可设置更多属性,readOnly 默认数据视图为只读(即值为true),可指定readOnly为false打开编辑功能  
  105.                         dataView: {show: true, readOnly: false},  
  106.                         //restore,还原,复位原始图表  
  107.                         restore: {show: true},  
  108.                         //saveAsImage,保存图片(IE8-不支持),图片类型默认为'png'  
  109.                         saveAsImage: {show: true}  
  110.                     }  
  111.                 },  
  112.                 //缩放漫游组件,仅对地图有效  
  113.                 roamController: {  
  114.                     //显示策略,默认为显示(true),可选为:true(显示) | false(隐藏)。  
  115.                     show: true,  
  116.                     //水平安放位置,默认为左侧('letf'),可选为:'center' | 'left' | 'right' | {number}(x坐标,单位px)  
  117.                     x: 'right',  
  118.                     //垂直安放位置,默认为全图顶端,可选为:'top' | 'bottom' | 'center' | {number}(y坐标,单位px)  
  119.                     y: 'top',  
  120.                     //必须,默认无(null),指定漫游组件可控地图类型,如:{ china: true }  
  121.                     mapTypeControl: {  
  122.                         //指定地图类型  
  123.                         'china'true  
  124.                     }  
  125.                 },  
  126.                 //series(地图),驱动图表生成的数据内容数组,数组中每一项为一个系列的选项及数据  
  127.                 series : [  
  128.                     {  
  129.                         //系列名称,如果启用legend,该值将被legend.data索引相关  
  130.                         name: 'iphone3',  
  131.                         //图表类型,必要参数!如为空或不支持类型,则该系列数据不被显示  
  132.                         type: 'map',  
  133.                         //地图类型,支持world,china及全国34个省市自治区。省市自治区的mapType直接使用简体中文:如'广东'  
  134.                         mapType: 'china',  
  135.                         //是否开启滚轮缩放和拖拽漫游,默认为false(关闭),其他有效输入为true(开启),'scale'(仅开启滚轮缩放),'move'(仅开启拖拽漫游)  
  136.                         roam: 'scale',  
  137.                         //图形样式,可设置图表内图形的默认样式和强调样式(悬浮时样式)  
  138.                         itemStyle:{  
  139.                             //正常时的样式  
  140.                             normal:{  
  141.                                 //边框线宽,单位px  
  142.                                 borderWidth: 2,  
  143.                                 //边框颜色  
  144.                                 borderColor: 'lightgreen',  
  145.                                 //标签,饼图默认显示在外部,离饼图距离由labelLine.length决定,地图标签不可指定位置   
  146.                                 label: {show:true}  
  147.                             },  
  148.                             //鼠标经过时的样式  
  149.                              emphasis: {  
  150.                                 //边框线宽,单位px  
  151.                                 borderWidth:2,  
  152.                                 //边框颜色  
  153.                                 borderColor:'#fff',  
  154.                                 //鼠标经过时区域的颜色  
  155.                                 color: '#32cd32',  
  156.                                 //标签  
  157.                                 label: {  
  158.                                     show: true,  
  159.                                     //文本样式  
  160.                                     textStyle: {  
  161.                                         //文本字体颜色  
  162.                                         color: '#fff'  
  163.                                     }  
  164.                                 }  
  165.                             }  
  166.                         },  
  167.                         //当图表类型为地图时,需要说明每部分数据对应的省份,可设置选中状态,如 :{name: '北京',value: 1234,selected: true},  
  168.                         data:[  
  169.                             {name: '北京',value: Math.round(Math.random()*1000)},  
  170.                             {name: '天津',value: Math.round(Math.random()*1000)},  
  171.                             {name: '上海',value: Math.round(Math.random()*1000)},  
  172.                             {name: '重庆',value: Math.round(Math.random()*1000)},  
  173.                             {name: '河北',value: Math.round(Math.random()*1000)},  
  174.                             {name: '河南',value: Math.round(Math.random()*1000)},  
  175.                             {name: '云南',value: Math.round(Math.random()*1000)},  
  176.                             {name: '辽宁',value: Math.round(Math.random()*1000)},  
  177.                             {name: '黑龙江',value: Math.round(Math.random()*1000)},  
  178.                             {name: '湖南',value: Math.round(Math.random()*1000)},  
  179.                             {name: '安徽',value: Math.round(Math.random()*1000)},  
  180.                             {name: '山东',value: Math.round(Math.random()*1000)},  
  181.                             {name: '新疆',value: Math.round(Math.random()*1000)},  
  182.                             {name: '江苏',value: Math.round(Math.random()*1000)},  
  183.                             {name: '浙江',value: Math.round(Math.random()*1000)},  
  184.                             {name: '江西',value: Math.round(Math.random()*1000)},  
  185.                             {name: '湖北',value: Math.round(Math.random()*1000)},  
  186.                             {name: '广西',value: Math.round(Math.random()*1000)},  
  187.                             {name: '甘肃',value: Math.round(Math.random()*1000)},  
  188.                             {name: '山西',value: Math.round(Math.random()*1000)},  
  189.                             {name: '内蒙古',value: Math.round(Math.random()*1000)},  
  190.                             {name: '陕西',value: Math.round(Math.random()*1000)},  
  191.                             {name: '吉林',value: Math.round(Math.random()*1000)},  
  192.                             {name: '福建',value: Math.round(Math.random()*1000)},  
  193.                             {name: '贵州',value: Math.round(Math.random()*1000)},  
  194.                             {name: '广东',value: Math.round(Math.random()*1000)},  
  195.                             {name: '青海',value: Math.round(Math.random()*1000)},  
  196.                             {name: '西藏',value: Math.round(Math.random()*1000)},  
  197.                             {name: '四川',value: Math.round(Math.random()*1000)},  
  198.                             {name: '宁夏',value: Math.round(Math.random()*1000)},  
  199.                             {name: '海南',value: Math.round(Math.random()*1000)},  
  200.                             {name: '台湾',value: Math.round(Math.random()*1000)},  
  201.                             {name: '香港',value: Math.round(Math.random()*1000)},  
  202.                             {name: '澳门',value: Math.round(Math.random()*1000)}  
  203.                         ],  
  204.                     },  
  205.                     {  
  206.                         //系列名称,如果启用legend,该值将被legend.data索引相关  
  207.                         name: 'iphone4',  
  208.                         //图表类型,必要参数!如为空或不支持类型,则该系列数据不被显示  
  209.                         type: 'map',  
  210.                         //地图类型,支持world,china及全国34个省市自治区。省市自治区的mapType直接使用简体中文:如'广东'  
  211.                         mapType: 'china',  
  212.                         //是否开启滚轮缩放和拖拽漫游,默认为false(关闭),其他有效输入为true(开启),'scale'(仅开启滚轮缩放),'move'(仅开启拖拽漫游)  
  213.                         roam: 'scale',  
  214.                         //图形样式,可设置图表内图形的默认样式和强调样式(悬浮时样式)  
  215.                         itemStyle:{  
  216.                             //正常时的样式  
  217.                             normal:{  
  218.                                 //边框线宽,单位px  
  219.                                 borderWidth: 2,  
  220.                                 //边框颜色  
  221.                                 borderColor: 'lightgreen',  
  222.                                 //标签,饼图默认显示在外部,离饼图距离由labelLine.length决定,地图标签不可指定位置   
  223.                                 label: {show:true}  
  224.                             },  
  225.                             //鼠标经过时的样式  
  226.                              emphasis: {  
  227.                                 //边框线宽,单位px  
  228.                                 borderWidth:2,  
  229.                                 //边框颜色  
  230.                                 borderColor:'#fff',  
  231.                                 //鼠标经过时区域的颜色  
  232.                                 color: '#32cd32',  
  233.                                 //标签  
  234.                                 label: {  
  235.                                     show: true,  
  236.                                     //文本样式  
  237.                                     textStyle: {  
  238.                                         //文本字体颜色  
  239.                                         color: '#fff'  
  240.                                     }  
  241.                                 }  
  242.                             }  
  243.                         },  
  244.                         data:[  
  245.                             {name: '北京',value: Math.round(Math.random()*1000)},  
  246.                             {name: '天津',value: Math.round(Math.random()*1000)},  
  247.                             {name: '上海',value: Math.round(Math.random()*1000)},  
  248.                             {name: '重庆',value: Math.round(Math.random()*1000)},  
  249.                             {name: '河北',value: Math.round(Math.random()*1000)},  
  250.                             {name: '安徽',value: Math.round(Math.random()*1000)},  
  251.                             {name: '新疆',value: Math.round(Math.random()*1000)},  
  252.                             {name: '浙江',value: Math.round(Math.random()*1000)},  
  253.                             {name: '江西',value: Math.round(Math.random()*1000)},  
  254.                             {name: '山西',value: Math.round(Math.random()*1000)},  
  255.                             {name: '内蒙古',value: Math.round(Math.random()*1000)},  
  256.                             {name: '吉林',value: Math.round(Math.random()*1000)},  
  257.                             {name: '福建',value: Math.round(Math.random()*1000)},  
  258.                             {name: '广东',value: Math.round(Math.random()*1000)},  
  259.                             {name: '西藏',value: Math.round(Math.random()*1000)},  
  260.                             {name: '四川',value: Math.round(Math.random()*1000)},  
  261.                             {name: '宁夏',value: Math.round(Math.random()*1000)},  
  262.                             {name: '香港',value: Math.round(Math.random()*1000)},  
  263.                             {name: '澳门',value: Math.round(Math.random()*1000)}  
  264.                         ]  
  265.                     },  
  266.                     {  
  267.                         //系列名称,如果启用legend,该值将被legend.data索引相关  
  268.                         name: 'iphone5',  
  269.                         //图表类型,必要参数!如为空或不支持类型,则该系列数据不被显示  
  270.                         type: 'map',  
  271.                         //地图类型,支持world,china及全国34个省市自治区。省市自治区的mapType直接使用简体中文:如'广东'  
  272.                         mapType: 'china',  
  273.                         //是否开启滚轮缩放和拖拽漫游,默认为false(关闭),其他有效输入为true(开启),'scale'(仅开启滚轮缩放),'move'(仅开启拖拽漫游)  
  274.                         roam: 'scale',  
  275.                         //图形样式,可设置图表内图形的默认样式和强调样式(悬浮时样式)  
  276.                         itemStyle:{  
  277.                             //正常时的样式  
  278.                             normal:{  
  279.                                 //边框线宽,单位px  
  280.                                 borderWidth: 2,  
  281.                                 //边框颜色  
  282.                                 borderColor: 'lightgreen',  
  283.                                 //标签,饼图默认显示在外部,离饼图距离由labelLine.length决定,地图标签不可指定位置   
  284.                                 label: {show:true}  
  285.                             },  
  286.                             //鼠标经过时的样式  
  287.                              emphasis: {  
  288.                                 //边框线宽,单位px  
  289.                                 borderWidth:2,  
  290.                                 //边框颜色  
  291.                                 borderColor:'#fff',  
  292.                                 //鼠标经过时区域的颜色  
  293.                                 color: '#32cd32',  
  294.                                 //标签  
  295.                                 label: {  
  296.                                     show: true,  
  297.                                     //文本样式  
  298.                                     textStyle: {  
  299.                                         //文本字体颜色  
  300.                                         color: '#fff'  
  301.                                     }  
  302.                                 }  
  303.                             }  
  304.                         },  
  305.                         data:[  
  306.                             {name: '北京',value: Math.round(Math.random()*1000)},  
  307.                             {name: '天津',value: Math.round(Math.random()*1000)},  
  308.                             {name: '上海',value: Math.round(Math.random()*1000)},  
  309.                             {name: '广东',value: Math.round(Math.random()*1000)},  
  310.                             {name: '台湾',value: Math.round(Math.random()*1000)},  
  311.                             {name: '香港',value: Math.round(Math.random()*1000)},  
  312.                             {name: '澳门',value: Math.round(Math.random()*1000)}  
  313.                         ]  
  314.                     }  
  315.                 ]  
  316.             };  
  317.             //为echarts对象加载数据     
  318.             myChart.setOption(option);  
  319.         }  
  320.     );  
  321.     </script>  
  322.   </body>  
  323. </html>  

猜你喜欢

转载自zengshaotao.iteye.com/blog/2353093
今日推荐