Vue echarts畫甘特圖流程詳細講解
vue項目中添加echarts,只需要增加echarts依賴,然后在main.js中引入echarts就可以使用了。
1、npm install echarts --save
2、修改main.js
import * as echarts from 'echarts' Vue.prototype.$echarts = echarts
3、具體頁面使用:
<template> <div class="about"> <h1>This is echarts page</h1> <div id="myechart" style="height:500px;width:1000px;" ></div> </div> </template> <script> export default{ name:'MyEchart', mounted(){ this.drawEchart() }, methods:{ drawEchart(){ let myechart = this.$echarts.init(document.getElementById("myechart")) myechart.setOption({ title:{text:"gant"}, xAxis:{ type:'value' }, yAxis:{ type:'category', data:["pro1","pro2","pro3","pro4","pro5","pro6"] }, series:[ { type:'bar', data:[10,20,30,46,78,22] } ] }) } } } </script>
展示效果:
甘特圖在這個圖形的基礎上還需要增加數(shù)據(jù),形成一個不斷迭代的效果。
{ type:'bar', name:'base', //stack:'Total', data:[10,20,30,46,78,22] }, { type:'bar', name:'data2', //stack:'Total', data:[20,20,30,20,10,20] }
如果不做設置,效果如下所示:
兩組數(shù)據(jù)都從原始位置開始,我們想要的結(jié)果是疊加,這里只需要增加一個參數(shù)stack:'',指定一個相同的名稱,效果如下:
我們?nèi)绻幌胝故镜谝欢危徽故镜诙?,這時候可以設置第一個數(shù)據(jù)集對應的樣式:
itemStyle{ borderColor:'transparent', color:'transparent' }
展示效果:
我們最終需要的效果如下所示:
從上面的示例,我們可以這樣來實現(xiàn), 把兩組數(shù)據(jù)看作一組,每一組第一個數(shù)據(jù)集展示隱藏,這里如果是項目進度圖,我們可以把開始時間、結(jié)束時間作為一組,結(jié)束時間與開始時間中間這一段時間就是持續(xù)時間,這個時間在這里是一個增量,就是第二個數(shù)據(jù)集是展示在第一個數(shù)據(jù)集的基礎上,它不能再直接使用結(jié)束時間,而是使用間隔時間。
數(shù)據(jù)集:
series:[ { type:'bar', name:'base', stack:'Total', data:[86,41,119,46], itemStyle:{ borderColor:'transparent', color:'transparent' } }, { type:'bar', name:'data2', stack:'Total', data:[100,100,100,100] }, { type:'bar', name:'data3', stack:'Total', data:[75,67,64,52], itemStyle:{ borderColor:'transparent', color:'transparent' } }, { type:'bar', name:'data4', stack:'Total', data:[100,100,100,100] }, { type:'bar', name:'data5', stack:'Total', data:[44,90,154,84], itemStyle:{ borderColor:'transparent', color:'transparent' } }, { type:'bar', name:'data6', stack:'Total', data:[100,100,100,100] }, { type:'bar', name:'data7', stack:'Total', data:[46,183,'-',188], itemStyle:{ borderColor:'transparent', color:'transparent' } }, { type:'bar', name:'data8', stack:'Total', data:[100,100,'-',100] }, { type:'bar', name:'data9', stack:'Total', data:[40,'-','-','-'], itemStyle:{ borderColor:'transparent', color:'transparent' } }, { type:'bar', name:'data8', stack:'Total', data:[{value:100,itemStyle:{normal:{color:'purple'}}},'-','-','-'] } ]
最終的一個效果:
這個里面,對應每一個數(shù)據(jù)項都可以設置顯示顏色,具體操作就是把data:[]數(shù)組變更為:
data:[ { value:100, itemStyle: { normal: {color:'purple'} } }, '-', '-', '-']
原來的數(shù)據(jù)項,變?yōu)橐粋€對象,對象的值value對應原來的數(shù)字值,樣式則使用itemStyle屬性設置。
甘特圖繪制需要注意的地方:
1、數(shù)據(jù)顯示疊加,使用屬性stack。
2、隱藏開始時間,設置顯示透明即可。結(jié)束時間點不能直接設置結(jié)束時間,這是一個增量,需要設置時間間隔。
3、每一個數(shù)據(jù)項展示,可以單獨設置樣式,比如顏色,可以把data數(shù)組做修改,原來單一的數(shù)字值,修改為對象,包含value,itemStyle等屬性。
到此這篇關(guān)于Vue echarts畫甘特圖流程詳細講解的文章就介紹到這了,更多相關(guān)Vue echarts畫甘特圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項目element UI input框掃碼槍掃描過快出現(xiàn)數(shù)據(jù)丟失問題及解決方案
這篇文章主要介紹了vue項目element UI input框掃碼槍掃描過快出現(xiàn)數(shù)據(jù)丟失問題,輸入框要掉兩個接口,根據(jù)第一個驗證接口返回的code,彈不同的框,點擊彈框確認再掉第二個接口,需要的朋友可以參考下2022-12-12