vue3使用vis繪制甘特圖制作timeline可拖動時間軸及時間軸中文化(推薦)
前言:參考文檔文章
一、實現(xiàn)效果:

二、安裝插件及依賴:
cnpm install -S vis-linetime cnpm install -S vis-data cnpm install -S moment
三、封裝組件:
下端時間軸單獨封裝成組件


1.html部分:
<template> <div class="visGantt" ref="visGanttDom"></div> </template>
2.引入依賴:
import { DataSet } from 'vis-data/peer'
import { dateFormat } from '@/util' //封裝的時間格式化函數(shù),如下所示
import { Timeline } from 'vis-timeline/peer'
import 'vis-timeline/styles/vis-timeline-graph2d.css'
const moment = require('moment')時間格式化函數(shù):
export function dateFormat(date, fmt) { //date是日期,fmt是格式
let o = {
'M+': date.getMonth() + 1, // 月份
'd+': date.getDate(), // 日
'H+': date.getHours(), // 小時
'h+': date.getHours(), // 小時
'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
S: date.getMilliseconds() // 毫秒
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
for (var k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
}
}
return fmt
}3.父組件傳入數(shù)據:
let props = defineProps({
ganntData: { // 初始傳入數(shù)據
type: Object,
default: () => {}
},
ganntHistoryData: { // 全部的歷史數(shù)據,為了實現(xiàn)撤銷上一步
type: Object,
default: () => {}
}
})4.js部分全部配置
配置項參考官方文檔,僅注釋解釋個別方法。
<script setup>
import { ref, defineProps, watch, nextTick, defineEmits } from 'vue'
import { DataSet } from 'vis-data/peer'
import { dateFormat } from '@/util'
import { Timeline } from 'vis-timeline/peer'
import 'vis-timeline/styles/vis-timeline-graph2d.css'
const moment = require('moment')
let props = defineProps({
ganntData: {
type: Object,
default: () => {}
},
ganntHistoryData: {
type: Object,
default: () => {}
}
})
let timeline = ref(null)
watch(
props.ganntData,
(newVal) => {
if (newVal && newVal[0].trackTimeWindows && newVal[0].trackTimeWindows.length > 0) {
nextTick(() => {
initChart()
checkTimeConflict()
})
}
},
{
immediate: true,
deep: true
}
)
const computedData = () =>{
const trackTimeWindows = []
const timeWindows = []
props.ganntData[0].trackTimeWindows.forEach(
(trackTimeWindow, trackTimeWindowIndex) => {
// 項目類別數(shù)組
trackTimeWindows.push({
content: trackTimeWindow.deviceId,
id: `${trackTimeWindow.deviceId}-${trackTimeWindowIndex}-trackTimeWindows`,
value: trackTimeWindowIndex + 1,
className: `visGantt-item${trackTimeWindowIndex % 10}`
})
// 項目時間數(shù)組
trackTimeWindow.timeWindows.forEach((timeWindow, timeWindowIndex) => {
timeWindows.push({
start: new Date(timeWindow.startTime),
startTime: timeWindow.startTime,
end: new Date(timeWindow.stopTime),
stopTime: timeWindow.stopTime,
topTime: timeWindow.topTime,
group: `${trackTimeWindow.deviceId}-${trackTimeWindowIndex}-trackTimeWindows`,
className: `visGantt-item${trackTimeWindowIndex % 10}`,
id: `${trackTimeWindow.deviceId}`,
deviceId: trackTimeWindow.deviceId
})
})
}
)
return {
trackTimeWindows,
timeWindows
}
}
const visGanttDom = ref(null)
let historyDataArray = ref([])
const emit = defineEmits()
// 選擇某個item
let onSelect = (properties) => {
emit('selectItem', properties.items[0])
}
const initChart = ()=> {
const { timeWindows, trackTimeWindows } = computedData()
const groups = new DataSet(trackTimeWindows)
const items = new DataSet(timeWindows)
let container = visGanttDom.value
if (container.firstChild) {
container.removeChild(container.firstChild)
}
// 甘特圖配置
const options = {
groupOrder: function(a, b) {
return a.value - b.value
},
groupOrderSwap: function(a, b, groups) {
var v = a.value
a.value = b.value
b.value = v
},
height: '23.8vh', // 高度
verticalScroll: false, // 豎向滾動
orientation: 'top', // 時間軸位置
margin: {
axis: 1,
item: {
horizontal: 0,
vertical: 20
}
},
editable: {
updateTime: true,
updateGroup: false
},
multiselect: true,
moment: function(date) {
return moment(date).utc('+08:00')
},
groupHeightMode: 'fixed',
// min: new Date(startTime.value), // 最小可見范圍
tooltip: {
followMouse: true,
overflowMethod: 'cap',
template: (originalItemData, parsedItemData) => {
// 鼠標hover時顯示樣式
return `<div>
<p>
<span>項目名稱:</span>
<span>${originalItemData.deviceId}</span>
</p><br/>
<p>
<span>開始時間:</span>
<span>${dateFormat(parsedItemData.start, 'yyyy-MM-dd')}</span>
</p><br/>
<span>結束時間:</span>
<span>${dateFormat(parsedItemData.end, 'yyyy-MM-dd')}</span>
</p>
</div>`
}
},
tooltipOnItemUpdateTime: {
template: (item) => {
// 鼠標拖動時顯示樣式
return `<div>
<span>開始時間:${dateFormat(item.start, 'yyyy-MM-dd')}</span>
<span>\n</span>
<span>結束時間:${dateFormat(item.end, 'yyyy-MM-dd')}</span>
</div>`
}
},
locale: moment.locale('zh-cn'), // 時間軸國際化
showCurrentTime: false,
selectable: true,
zoomMin: 1728000000,
zoomMax: 315360000000,
// showTooltips: false,
// autoResize: false,
snap: function(date, scale, step) {
var day = 60 * 60 * 1000 * 24
return Math.round(date / day) * day
},
// 移動時返回函數(shù)
onMove: function(item, callback) {
// 深拷貝一下,不能直接修改父組件數(shù)據
historyDataArray.value = JSON.parse(JSON.stringify(props.ganntHistoryData))
let historyData = []
// props.ganntHistoryData是全部的歷史數(shù)據,historyData 是取上一步的數(shù)據
historyData = JSON.parse(JSON.stringify(props.ganntHistoryData[props.ganntHistoryData.length - 1]))
// 更改一下格式
historyData[0].trackTimeWindows.forEach((eachItem)=>{
if (eachItem.deviceId === item.deviceId) {
if (!item.start || !item.end) {
return
}
eachItem.timeWindows[0].startTime = item.start
eachItem.timeWindows[0].stopTime = item.end
}
})
historyDataArray.value.push(historyData)
// 更新一下ganntHistoryData歷史數(shù)據
emit('update:ganntHistoryData', historyDataArray.value)
callback(item)
},
onMoving: function(item, callback) {
// 移動時間軸時不顯示tooltip提示框
let tooltipDom = document.getElementsByClassName('vis-tooltip')
tooltipDom[0].style.visibility = 'hidden'
callback(item)
}
}
timeline.value = new Timeline(container)
timeline.value.redraw()
timeline.value.setOptions(options)
timeline.value.setGroups(groups)
timeline.value.setItems(items)
timeline.value.on('select', onSelect)
}
</script>
四、父組件調用
1.引入子組件
<div v-loading="loading"> // loading是為了有個加載效果,為了美觀 <time-line :ganntData="ganntData" // 原始數(shù)據 v-model:ganntHistoryData="ganntHistoryData" // 歷史數(shù)據 @selectItem="timelineSelected" //選擇事件 > </time-line> </div>
import TimeLine from '@/components/modules/planControl/TimeLine'
2.初始數(shù)據
let props = defineProps({
// 因為這個父組件是通過點擊進來的,所以有傳入的數(shù)據,也可以直接賦值ganntData 數(shù)據,可以省略watch里面的轉格式
conflictList: {
type: Array,
default: null
}
})
const ganntData = reactive([
{
name: 'confilct',
trackTimeWindows: [
]
}
])
const ganntHistoryData = ref([])
// 傳入數(shù)據變化時為ganntData和ganntHistoryData賦值。
watch(
() => props.conflictList, (newValue) => {
ganntData[0].trackTimeWindows.length = 0
newValue.forEach(element => {
ganntData[0].trackTimeWindows.push({
deviceId: element.content,
timeWindows: [
{
startTime: element.startTime,
stopTime: element.stopTime
}
]
})
})
// 記錄操作歷史
ganntHistoryData.value.length = 0
ganntHistoryData.value.push(ganntData)
},
{
deep: true,
immediate: true
}
)
原數(shù)據(省略部分未使用參數(shù)):
[
{
"id": 1,
"content": "xxxxxxxxxxxxxx計劃1",
"time": "2023.08~10",
"startTime": "2023-08-09",
"stopTime": "2023-10-20"
},
{
"id": 2,
"content": "xxxxxxxxxxxxxx計劃2",
"time": "2023.09~11",
"startTime": "2023-09-09",
"stopTime": "2023-11-1"
},
{
"id": 3,
"content": "xxxxxxxxxxxxxx計劃3",
"time": "2023.08~10",
"startTime": "2023-08-20",
"stopTime": "2023-10-1"
}
]3.父組件按鈕及事件
僅展示原始圖、撤銷事件。
<div>
<div>
<el-button @click="reset()">原始圖</el-button>
<el-button @click="preNode()">撤銷</el-button>
<el-button>一鍵調整</el-button>
</div>
<div>
<el-button>取消</el-button>
<el-button>保存并退出</el-button>
</div>
</div>回歸原始圖事件:
大致思路:先把ganntData清空,將拿到的props.conflictList里的數(shù)據賦值給ganntData,再把ganntData的數(shù)據push進ganntHistoryData中
// showResetTip 是顯示一個“已回到初始狀態(tài)”的提示框,可以自己封裝或者使用組件,此處不展示
const showResetTip = ref(false)
const loading = ref(false)
const reset = () => {
// loading是加載效果
loading.value = true
ganntData[0].trackTimeWindows.length = 0
props.conflictList.forEach(element => {
ganntData[0].trackTimeWindows.push({
deviceId: element.content,
timeWindows: [
{
startTime: element.startTime,
stopTime: element.stopTime
}
]
})
})
showResetTip.value = true
ganntHistoryData.value.splice(0)
ganntHistoryData.value.push(ganntData)
setTimeout(() => {
showResetTip.value = false
loading.value = false
}, 1000)
}撤銷事件:
大致思路:拿到子組件返回的ganntHistoryData歷史數(shù)據數(shù)組,刪掉最后一組數(shù)據后:
如果歷史數(shù)據數(shù)組的長度<= 1,代表再撤銷就回到原始狀態(tài)了,那就直接調用reset()回到原始圖;
否則,將ganntHistoryData刪掉最后一組數(shù)據后的ganntHistoryDataClone最后一組值賦給ganntData,
const preNode = () => {
// loading是加載效果
loading.value = true
let ganntHistoryDataClone = []
ganntHistoryDataClone = JSON.parse(JSON.stringify(ganntHistoryData.value))
ganntHistoryDataClone.splice(ganntHistoryDataClone.length - 1, 1)
if (ganntHistoryDataClone.length <= 1) {
reset()
} else {
ganntData[0] = ganntHistoryDataClone[ganntHistoryDataClone.length - 1][0]
ganntHistoryData.value = JSON.parse(JSON.stringify(ganntHistoryDataClone))
}
setTimeout(() => {
loading.value = false
}, 1000)
}到此這篇關于vue3使用vis繪制甘特圖制作timeline可拖動時間軸,時間軸中文化的文章就介紹到這了,更多相關vue3用vis繪制甘特圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
uni-app中App與webview雙向實時通信詳細代碼示例
在移動應用開發(fā)中,uni-app是一個非常流行的框架,它允許開發(fā)者使用一套代碼庫構建多端應用,包括H5、小程序、App等,這篇文章主要給大家介紹了關于uni-app中App與webview雙向實時通信的相關資料,需要的朋友可以參考下2024-07-07
el-tree樹組件懶加載(后端上千條數(shù)據前端進行處理)
本文主要介紹了el-tree樹組件懶加載(后端上千條數(shù)據前端進行處理),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03

