Vue+Echarts實(shí)現(xiàn)繪制多設(shè)備狀態(tài)甘特圖
Apache ECharts ECharts官網(wǎng),可在“快速上手”處查看詳細(xì)安裝方法
1.效果圖
可鼠標(biāo)滾輪圖表和拉動下方藍(lán)色的條條調(diào)節(jié)時間細(xì)節(jié)哦
(注:最后一個設(shè)備沒有數(shù)據(jù),所以不顯示任何矩形)
2.代碼
根據(jù)每個設(shè)備的不同的狀態(tài),和對應(yīng)狀態(tài)的持續(xù)時間渲染矩形
html部分
<div id="myechart" :style="{ float: 'left', width: '100%', height: '100%' }"></div>
js部分
// 甘特圖 數(shù)據(jù)處理及掛載函數(shù),可在獲取到數(shù)據(jù)或者數(shù)據(jù)更新時調(diào)用 drawEchart() { // this.newEqp為數(shù)據(jù)集,從后端獲取 var data = this.newEqp // 設(shè)定狀態(tài)對應(yīng)顯示的顏色 var types = [ { name: '輔料待料', color: '#07c160' }, { name: '下游待料', color: '#269abc' }, { name: '上游待料', color: '#edb217' }, { name: '其他停機(jī)', color: '#f68ba7' }, { name: '故障停機(jī)', color: '#ff3374' }, { name: '運(yùn)行', color: '#66E656' }, ]; var startTime; var datatemp = []; // 處理時間,x軸需要月、日、時、分 for (let i = 0; i < data.length; i++) { startTime = new Date(data[i].Last_Eqpt_Update_Time).getTime(); var typeItem = types.filter(a => a.name == data[i].Eqpt_State_Dsc)[0]; datatemp.push({ name: typeItem.name, value: [ parseInt(data[i].Index), new Date(data[i].Last_Eqpt_Update_Time).getTime(), new Date(data[i].Eqpt_Update_Time).getTime(), //data[i].RUNTIME_TIMESTAMP, //data[i].END_TIME_TIMESTAMP, data[i].Index, data[i].Eqpt_Dsc ], itemStyle: { normal: { color: typeItem.color } } }); } // console.log("data:", data); // console.log('datatemp', datatemp); // 處理各種狀態(tài)的起始和結(jié)束時間函數(shù) function renderItem(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 = api.size([0, 1])[1] * 0.6; var rectShape = 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 } ); // 返回矩形對象 return ( rectShape && { type: 'rect', transition: ['shape'], shape: rectShape, style: api.style() } ); } // 基于echarts的甘特圖 let myechart = this.$echarts.init(document.getElementById("myechart")) let myechart2 = { textStyle: { color: '#333', fontSize: '0.13rem' }, grid: { top: '5%', left: '8%', bottom: '16%', width: '90%' }, legend: { show: true, itemWidth: 10, itemHeight: 10, textStyle: { color: '#fff', fontSize: '0.12rem' }, data: types.map(function (type) { return type.name; }), }, tooltip: { show: true, textStyle: { fontSize: 10 }, axisPointer: { type: 'cross', crossStyle: { color: '#333' } }, formatter: function (params) { return params.value[4] + '\t' + params.name + '\t' + params.marker + (new Date(params.value[1])).getHours() + ':' + (new Date(params.value[1])).getMinutes() + '—' + (new Date(params.value[2])).getHours() + ':' + (new Date(params.value[2])).getMinutes(); } }, dataZoom: [ { type: 'inside', filterMode: 'weakFilter' }, { type: "slider", show: true, height: "6", bottom: "4%", labelFormatter: '', backgroundColor: "white", brushSelect: false, minValueSpan: 3600 * 24 * 1000 * 7, handleIcon: 'path://path://M100, 100m -75, 0a75,75 0 1,0 150,0a75,75 0 1,0 -150,0', handleSize: 15, handleColor: '#6bc5da', start: 0, end: 100, handleStyle: { borderCap: 'round', color: "#fff", shadowColor: 'rgba(0, 0, 0, 0.5)', shadowBlur: 1 }, textStyle: { color: "transparent" }, borderColor: 'transparent', backgroundColor: '#D7F4FF', dataBackground: { lineStyle: { width: 0 }, areaStyle: { color: 'transparent' } }, fillerColor: '#00EBFF' } ], xAxis: { type: 'time', //min: new Date(startTime).setHours(7, 0, 0, 0), //max: new Date(new Date(startTime).setDate(new Date(startTime).getDate() + 1)).setHours(7, 0, 0, 0), interval: 3600000, scale: true, axisLabel: { formatter: function (val) { return new Date(val).toLocaleTimeString('en-US', { hour: '2-digit', minute: 'numeric', hour12: false }); } }, splitLine: { show: false }, axisLine: { show: false }, axisTick: { show: true, lineStyle: { color: '#333', width: 2 } }, axisLabel: { textStyle: { color: '#333', fontSize: '0.14rem' }, show: true } }, yAxis: { // y軸數(shù)據(jù),這里是設(shè)備編號 data: this.eqptId, scale: true, splitLine: { show: false }, axisLine: { show: false }, axisTick: { show: false }, axisLabel: { show: true, textStyle: { color: '#333', fontSize: '0.12rem', fontWeight: 'bolder', } } }, series: [ { type: 'custom', // 圖表矩形數(shù)據(jù) renderItem: renderItem, itemStyle: { opacity: 0.8 }, encode: { x: [0, 1], y: 0 }, data: datatemp, } ] } myechart.setOption(myechart2) }
3.注意事項
甘特圖圖表可能會不顯示,原因一般是在獲取到數(shù)據(jù)之前圖表就掛載上了,然后數(shù)據(jù)更新后并沒有更新圖表數(shù)據(jù)。這里本人的方法是在獲取到數(shù)據(jù)的后面調(diào)用掛載圖表的函數(shù),當(dāng)然這肯定不是最好的方法。
// 獲取圖表數(shù)據(jù) getCharts().then((res)=>{ ...... ...... // 判斷獲取到數(shù)據(jù)后調(diào)用處理數(shù)據(jù)及掛載圖表的函數(shù)this.drawEchart() if(res.length !== 0){ this.drawEchart() } })
以上就是Vue+Echarts實(shí)現(xiàn)繪制多設(shè)備狀態(tài)甘特圖的詳細(xì)內(nèi)容,更多關(guān)于Vue Echarts甘特圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在vue中實(shí)現(xiàn)某一些路由頁面隱藏導(dǎo)航欄的功能操作
這篇文章主要介紹了在vue中實(shí)現(xiàn)某一些路由頁面隱藏導(dǎo)航欄的功能操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09自定義input組件如何實(shí)現(xiàn)拖拽文件上傳
這篇文章主要介紹了自定義input組件如何實(shí)現(xiàn)拖拽文件上傳問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03vue+iview實(shí)現(xiàn)手機(jī)號分段輸入框
這篇文章主要為大家詳細(xì)介紹了vue+iview實(shí)現(xiàn)手機(jī)號分段輸入框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03