= =,那這周不是不用幹別的了。javascript
恰好最近在研究百度出的 ECharts ,搜了搜居然有矩形樹圖的模塊,真是感動啊眼淚都要出來了。立馬研究了一下,結果還能夠,能實現我要的效果。html
就在博客裏總結一下吧,但願對你有幫助。java
使此類結構更有效的可視化的方法已經有不少了,比較重要的一類就是樹。矩形式樹狀結構圖(Treemap)就是一種有效的實現層次結構可視化的圖表結構,簡稱矩形樹圖。數組
在矩形樹圖中,每一個節點都有名字和相應的大小。若是用矩形樹圖表示咱們熟悉的文件目錄列表,那麼葉子節點的大小就能表示各個文件的體積,父節點就能表示文件夾的體積也就是它包含的文件體積之和。echarts
經過矩形樹圖,咱們即可以很清晰地知道整個文件的全局層級結構。
dom
蘋果:6 香蕉:4 獼猴桃:4 梨:2 橙子:2 橘子:1 西瓜:1
能夠直觀看出,喜歡吃蘋果的人最多,喜歡吃香蕉的也很多。若是你願意,還能夠爲不一樣的數據塊設置對應的顏色,蘋果:紅,香蕉:黃,等等,更直觀一點啦。異步
看更嚴肅的矩形樹圖實例戳這裏~
async
<!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</title> </head> <body> <!-- 爲ECharts準備一個具有大小(寬高)的Dom --> <div id="main" style="height:400px"></div> <!-- ECharts單文件引入 --> <script src="http://echarts.baidu.com/build/dist/echarts.js"></script> </body>
咱們須要新建一個<script>
標籤中爲模塊加載器配置 ECharts 和所需圖表的路徑,這樣 ECharts 才能正確的找到模塊的存儲位置,正確加載須要的模塊。ui
<!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</title> </head> <body> <!-- 爲ECharts準備一個具有大小(寬高)的Dom --> <div id="main" style="height:400px"></div> <!-- ECharts單文件引入 --> <script src="http://echarts.baidu.com/build/dist/echarts.js"></script> <script type="text/javascript"> // 路徑配置 require.config({ paths: { echarts: 'http://echarts.baidu.com/build/dist' } }); </script> </body>
require( [ 'echarts', 'echarts/chart/treemap' // 加載treemap模塊,按需加載 ], function (ec) { ... // 在回調函數裏面寫具體配置代碼 } );
代碼實現了加載 echarts 模塊,和咱們實現矩陣樹圖要用的 treemap 模塊。
function(ec) { var myChart = ec.init(document.getElementById("main")); var option = { title : { text: 'A公司員工吃水果統計表', subtext: '多吃水果有益身體健康' }, tooltip : { trigger: 'item', formatter: "{b}: {c}" }, toolbox: { show : true, feature : { mark : {show: true}, dataView : {show: true, readOnly: false}, restore : {show: true}, saveAsImage : {show: true} } }, calculable : false, series : [ { name:'矩形圖', type:'treemap', itemStyle: { normal: { label: { show: true, formatter: "{b}" }, borderWidth: 1 }, emphasis: { label: { show: true } } }, data:[ { name: '蘋果', value: 6 }, { name: '香蕉', value: 4 }, { name: '獼猴桃', value: 4 }, { name: '梨', value: 2 }, { name: '橙子', value: 2 }, { name: '桔子', value: 1 }, { name: '西瓜', value: 1 } ] } ] }; myChart.setOption(option); }
有關 title,tooltip,toolbox 等組件在全部 ECharts 建表中是通用的(相關文檔請戳這裏)如今先說說若是須要使用矩形樹圖須要特別關注的事情。
ECharts 中規定使用 series 來設定驅動圖表生成的數據內容,它是一個數組格式,數組中每一項表明一個系列的特殊選項及數據。
在 series 中,首先是一些通用的配置項,如 name,type 等。這些比較簡單,你們都懂。和矩形樹圖相關的特殊配置項有兩個,如今分別介紹以下:
itemStyle: { normal: { ... }, emphasis: { ... } }
{string} name null 數據名稱 {Number} value null 數據值 {Object} itemStyle {} 參見 itemStyle ,權重最高
series : [ { name:'矩形圖', type:'treemap', itemStyle: { normal: { label: { show: true, formatter: "{b}: {c}" }, borderWidth: 1, borderColor: '#ccc' }, emphasis: { label: { show: true }, color: '#cc99cc', borderWidth: 3, borderColor: '#996699' } }, data:[ { name: '三星', value: 6, itemStyle: { normal: { label: { show: true, formatter : "{b}最多", textStyle: { color: '#ccc', fontSize: 16 } }, color: '#ccff99', borderWidth: 1 }, emphasis: { label: { show: true, formatter : "{b}: {c}", textStyle: { color: 'red', fontSize: 18 } }, color: '#cc9999', borderWidth: 3, borderColor: '#999999' } } }, { name: '小米', value: 4, itemStyle: { normal: { color: '#99ccff', } } }, { name: '蘋果', value: 4, itemStyle: { normal: { color: '#9999cc', } } }, { name: '魅族', value: 1, itemStyle: { normal: { color: '#99cccc', } } }, { name: '華爲', value: 2, itemStyle: { normal: { color: '#ccffcc', } } }, { name: '聯想', value: 2, itemStyle: { normal: { color: '#ccccff', } } }, { name: '中興', value: 1, itemStyle: { normal: { label: { show: true, formatter: "{b}: {c}", }, borderWidth: 3 }, emphasis: { label: { show: true }, color: '#cc9999', borderWidth: 3, borderColor: '#999999' } } } ] } ]
就是有一點用着不太爽,在官網實例部分能夠下載圖表到本地,以下圖:
原本挺好的功能,可是圖片右上角爲何還帶着那些編輯button啊,強迫症受不了。。
還有但願早日支持兩層的矩形樹圖啊~~