使用ECharts實(shí)現(xiàn)狀態(tài)區(qū)間圖
需求背景
假如有如下圖所示的圖表需要顯示多個(gè)網(wǎng)口在一段時(shí)間內(nèi)的多個(gè)狀態(tài):y軸用于展示各網(wǎng)口,x軸用于展示時(shí)間(分鐘),使用條形圖的不同顏色來(lái)表示不同時(shí)間區(qū)間段的狀態(tài),使用深藍(lán)、淺藍(lán)、橙色、紅色分別代表正常、繁忙、故障、離線四種狀態(tài)。以WAN0為例,圖中則表示了0~10分鐘為正常,10~25分鐘為繁忙,25~45分鐘為故障,45~60分鐘為離線。
根據(jù)此圖,很容易想到可以用條形圖試試。但后來(lái)發(fā)現(xiàn),如果用堆疊條形圖,則每種狀態(tài)在每一個(gè)網(wǎng)口對(duì)應(yīng)的圖形中只能出現(xiàn)一次,這不能實(shí)現(xiàn)需求。于是繼續(xù)查閱echart官網(wǎng)示例,終于在自定義類(lèi)型圖表中找到了一個(gè)相似的示例,地址
通過(guò)研究示例代碼并進(jìn)行一番改造,終于實(shí)現(xiàn)了上述需求。
在實(shí)現(xiàn)的過(guò)程中遇到了一個(gè)小問(wèn)題,那就是使用自定義圖表實(shí)現(xiàn)chart之后,圖例不好處理。通過(guò)查看條形圖的示例,找到了一種顯示圖例的方法,那就是使用空的條形圖來(lái)顯示圖例,因?yàn)樵?strong>series里面配置了條形圖并配置name后,echart會(huì)自動(dòng)根據(jù)name的值去legend的配置中匹配對(duì)應(yīng)的圖例名字并顯示。
完整代碼如下,保存于本地之后再自己去echart官網(wǎng)下載庫(kù)文件(完整版)之后即可運(yùn)行:
<!doctype html> <html> <head> <meta charset="utf-8"> </head> <body> <div id="chart-box" style="width:400px;height:300px;border:1px solid black;"></div> <script src="./echarts.min.js"></script> <script> // 初始化echart var chart = echarts.init(document.getElementById('chart-box')); // 各狀態(tài)的顏色 var colors = ['#2f4554', '#61a0a8', '#d48265', '#c23531']; // 四種狀態(tài) var state = ['正常', '繁忙', '故障', '離線']; // echart配置 var opt = { color: colors, tooltip: { formatter: function (params) { return params.name + ':' + params.value[1] + '~' + params.value[2]; } }, legend: { data: state, bottom: '1%', selectedMode: false, // 圖例設(shè)為不可點(diǎn)擊 textStyle: { color: '#000' } }, grid: { left: '3%', right: '3%', top: '1%', bottom: '10%', containLabel: true }, xAxis: { min: 0 // x軸零刻度對(duì)應(yīng)的實(shí)際值 }, yAxis: { data: ['WAN0', 'WAN1'] }, series: [ // 用空bar來(lái)顯示四個(gè)圖例 {name: state[0], type: 'bar', data: []}, {name: state[1], type: 'bar', data: []}, {name: state[2], type: 'bar', data: []}, {name: state[3], type: 'bar', data: []}, { type: 'custom', renderItem: function (params, api) { var categoryIndex = api.value(0); var start = api.coord([api.value(1), categoryIndex]); var end = api.coord([api.value(2), categoryIndex]); var height = 24; return { type: 'rect', shape: echarts.graphic.clipRectByRect({ x: start[0], y: start[1] - height / 2, width: end[0] - start[0], height: height }, { x: params.coordSys.x, y: params.coordSys.y, width: params.coordSys.width, height: params.coordSys.height }), style: api.style() }; }, encode: { x: [1, 2], y: 0 }, data: [ { itemStyle: { normal: { color: colors[0] } }, name: '正常', value: [0, 0, 10] }, { itemStyle: { normal: { color: colors[1] } }, name: '繁忙', value: [0, 10, 25] }, { itemStyle: { normal: { color: colors[2] } }, name: '故障', value: [0, 25, 45] }, { itemStyle: { normal: { color: colors[3] } }, name: '離線', value: [0, 45, 60] }, { itemStyle: { normal: { color: colors[0] } }, name: '正常', value: [1, 0, 15] }, { itemStyle: { normal: { color: colors[1] } }, name: '繁忙', value: [1, 15, 20] }, { itemStyle: { normal: { color: colors[2] } }, name: '故障', value: [1, 20, 35] }, { itemStyle: { normal: { color: colors[3] } }, name: '離線', value: [1, 35, 40] }, { itemStyle: { normal: { color: colors[0] } }, name: '正常', value: [1, 40, 45] }, { itemStyle: { normal: { color: colors[3] } }, name: '離線', value: [1, 45, 60] } ] } ] }; chart.setOption(opt); </script> </body> </html>
對(duì)于自定義圖表的data字段里數(shù)據(jù)項(xiàng):
{ itemStyle: { normal: { color: colors[0] } }, name: '正常', value: [0, 0, 10] }
- itemStyle: 所渲染的矩形的樣式
- name: 該矩形的狀態(tài)名
- value: 第0項(xiàng)代表類(lèi)別標(biāo)識(shí),例如0就代表WAN0的,1就是WAN1的;第1和第2項(xiàng)代表該矩形區(qū)域?qū)?yīng)的x坐標(biāo)范圍開(kāi)始于0,結(jié)束于1。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript 設(shè)計(jì)模式之享元模式原理與應(yīng)用詳解
這篇文章主要介紹了javascript 設(shè)計(jì)模式之享元模式,結(jié)合實(shí)例形式詳細(xì)分析了javascript 設(shè)計(jì)模式之享元模式相關(guān)概念、原理、應(yīng)用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04JavaScript 解析數(shù)學(xué)表達(dá)式的過(guò)程詳解
這篇文章主要介紹了JavaScript 解析數(shù)學(xué)表達(dá)式的過(guò)程詳解,本文以一個(gè)的解題思路,來(lái)分享如何解決問(wèn)題,解決的過(guò)程,可以作為解決工作中一般問(wèn)題的通用思路,對(duì)js解析表達(dá)式相關(guān)知識(shí)感興趣的朋友一起看看吧2022-06-06JavaScript 數(shù)組運(yùn)用實(shí)現(xiàn)代碼
復(fù)習(xí)一下JS中數(shù)組的運(yùn)用。學(xué)習(xí)js數(shù)組的朋友可以參考下。2010-04-04JavaScript用JSONP跨域請(qǐng)求數(shù)據(jù)實(shí)例詳解
Javascript跨域訪問(wèn)是web開(kāi)發(fā)者經(jīng)常遇到的問(wèn)題,什么是跨域,就是一個(gè)域上加載的腳本獲取或操作另一個(gè)域上的文檔屬性。下面這篇文章主要介紹了JavaScript用JSONP跨域請(qǐng)求數(shù)據(jù)的方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-01-01深入了解JavaScript中l(wèi)et/var/function的變量提升
這篇文章主要介紹了深入了解JavaScript中l(wèi)et/var/function的變量提升,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07JS提示:Uncaught SyntaxError:Unexpected token ) 錯(cuò)誤的解決方法
這篇文章主要介紹了JS提示:Uncaught SyntaxError:Unexpected token ) 錯(cuò)誤的解決方法,結(jié)合實(shí)例形式分析了javascript提示此類(lèi)異常的常見(jiàn)原因與相關(guān)解決方法,需要的朋友可以參考下2016-08-08百度Popup.js彈出框進(jìn)化版 拖拽小框架發(fā)布 兼容IE6/7/8,Firefox,Chrome
百度空間的彈出窗口和拖拽效果(也就是popup.js),代碼精簡(jiǎn),效果也很好,我們可以在很多大型網(wǎng)站上見(jiàn)到這種效果,在我的項(xiàng)目中也使用了該js。2010-04-04