百度echarts中地圖

ECharts主頁:  http://echarts.baidu.com/index.html

ECharts-2.1.8下載地址:  http://echarts.baidu.com/build/echarts-2.1.8.zip

ECharts官方實例:  http://echarts.baidu.com/doc/example.html

ECharts官方API文檔:  http://echarts.baidu.com/doc/doc.html

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>  
完成以上的步驟後,把項目發佈到服務器,在瀏覽器上訪問echarts.jsp(由於本實例還沒有跟後臺進行交互,只是先做一個前臺用法介紹,所以可以把echart.jsp直接修改成echarts.html文件,把它跟js文件夾放在同一級目錄下,然後直接用瀏覽器打開echarts.html),即可看到下面的效果


更多的圖表樣式設置,可以查看ECharts的官方API文檔

本文轉載於-------->>http://blog.csdn.net/wangcunhuazi/article/details/41621911

發佈了26 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章