这里写自定义目录标题
vue3使用vis.js插件中的vis-timeline时间轴
1. npm下载相关插件
npm install -S vis-linetime
npm install -S vis-data
npm install -S moment
2. 新建组件timeline.vue,在组件中导入模块
const moment = require('moment')
import { DataSet } from 'vis-data/peer'
import { Timeline } from 'vis-timeline/peer'
import 'vis-timeline/styles/vis-timeline-graph2d.css'
import { ref,onBeforeUnmount } from 'vue'
3. 创建模板
<template>
<div ref="visualization" class="visGantt"></div>
我是时间轴
</template>
4. 创建时间轴配置及获取元素
const timeline = ref(null)
const visualization = ref(null)
5. 数据
const ganntData = ref(
[
{
norad: '11',
name: 'test',
trackTimeWindows: [
{
deviceId: '1',
norad: '11',
content: 'item 1',
timeWindows: [
{
content: 'item 1',
startTime: '2021-12-27 00:08:52.078',
topTime: '2021-12-27 01:16:33.890',
stopTime: '2021-12-27 02:25:39.348'
},
]
},
{
deviceId: '2',
norad: '11',
content: 'item 2',
timeWindows: [
{
content: 'item 2',
startTime: '2021-12-27 01:08:52.078',
topTime: '2021-12-27 01:16:33.890',
stopTime: '2021-12-27 04:25:39.348'
},
]
},
{
deviceId: '3',
norad: '11',
content: 'item 3',
timeWindows: [
{
content: 'item 3',
startTime: '2021-12-27 03:08:37.247',
topTime: '2021-12-27 03:16:30.791',
stopTime: '2021-12-27 05:25:29.077'
},
]
},
{
deviceId: '4',
norad: '11',
content: 'item 4',
timeWindows: [
{
content: 'item 4',
startTime: '2021-12-27 08:10:24.156',
topTime: '2021-12-27 01:18:41.379',
stopTime: '2021-12-27 12:28:32.674'
},
]
},
{
deviceId: '5',
norad: '11',
content: 'item 5',
timeWindows: [
{
content: 'item 5',
startTime: '2021-12-27 05:09:12.876',
topTime: '2021-12-27 01:17:37.260',
stopTime: '2021-12-27 07:27:14.041'
},
]
},
{
deviceId: '6',
norad: '11',
content: 'item 6',
timeWindows: [
{
content: 'item 6',
startTime: '2021-12-27 12:06:14.969',
topTime: '2021-12-27 01:13:31.700',
stopTime: '2021-12-27 14:21:58.220'
},
]
},
{
deviceId: '7',
norad: '11',
content: 'item 7',
timeWindows: [
{
content: 'item 7',
startTime: '2021-12-27 12:11:47.926',
topTime: '2021-12-27 01:19:33.909',
stopTime: '2021-12-27 15:29:01.687'
},
]
},
{
deviceId: '8',
norad: '11',
content: 'item 8',
timeWindows: [
{
content: 'item 8',
startTime: '2021-12-27 18:23:58.104',
topTime: '2021-12-27 01:27:59.422',
stopTime: '2021-12-27 23:32:00.740'
},
]
},
{
deviceId: '9',
norad: '11',
content: 'item 9',
timeWindows: [
{
content: 'item 9',
startTime: '2021-12-27 13:23:58.104',
topTime: '2021-12-27 01:27:59.422',
stopTime: '2021-12-27 16:32:00.740'
},
]
}
]
}
]
)
6. 对数据进行处理
const computedData = () => {
const trackTimeWindows = []
const timeWindows = []
ganntData.value[0].trackTimeWindows.forEach(
(trackTimeWindow, trackTimeWindowIndex) => {
trackTimeWindows.push({
content: trackTimeWindow.content,
id: `${trackTimeWindow.deviceId}-${trackTimeWindowIndex}-trackTimeWindows`,
value: trackTimeWindowIndex + 1,
className: `visGantt-item${trackTimeWindowIndex % 10}`
})
trackTimeWindow.timeWindows.forEach((timeWindow, timeWindowIndex) => {
timeWindows.push({
content: trackTimeWindow.content,
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}-${timeWindowIndex}-timeWindows`,
deviceId: trackTimeWindow.deviceId
})
})
}
)
return {
trackTimeWindows,
timeWindows
}
}
7. 初始化配置时间轴
const initChart = () => {
const { timeWindows, trackTimeWindows } = computedData()
const groups = new DataSet(trackTimeWindows)
const items = new DataSet(timeWindows)
const container = visualization.value
const options = {
groupOrder: function(a, b) {
return a.value - b.value
},
groupOrderSwap: function(a, b, groups) {
const v = a.value
a.value = b.value
b.value = v
},
// height: '300px', // 高度
verticalScroll: true, // 竖向滚动
orientation: 'top', // 时间轴位置
editable: false,
groupEditable: false,
min: new Date('2021-12-27 00:00'), // 最小可见范围
max: new Date('2021-12-27 23:59'), // 最大可见范围
tooltip: {
followMouse: true,
overflowMethod: 'cap',
template: (originalItemData, parsedItemData) => {
return `<div>
<p
<span>设备名称:</span>
<span>${originalItemData.content}</span>
</p><br/>
<p>
<span>开始时间:</span>
<span>${originalItemData.startTime}</span>
</p><br/>
<p>
<span>结束时间:</span>
<span>${originalItemData.stopTime}</span>
</p>
</div>`
}
},
// zoomMin: 1728000000,
// zoomMax: 315360000000,
locale: moment.locale('zh-cn') // 时间轴国际化
}
timeline.value = new Timeline(container)
timeline.value.setOptions(options)
timeline.value.setGroups(groups)
timeline.value.setItems(items)
}
8. 在生命周期中使用
onMounted(() => {
initChart()
})
9. 销毁
onBeforeUnmount(() => {
timeline.value.destroy()
timeline.value = null
})
时间轴完整代码
template
<template>
<div ref="visualization" class="visGantt"></div>
我是时间轴
</template>
ts代码
<script setup lang="ts">
const moment = require('moment')
import { DataSet } from 'vis-data/peer'
import { Timeline } from 'vis-timeline/peer'
import 'vis-timeline/styles/vis-timeline-graph2d.css'
import { ref,onBeforeUnmount } from 'vue'
const ganntData = ref(
[
{
norad: '11',
name: 'test',
trackTimeWindows: [
{
deviceId: '1',
norad: '11',
content: 'item 1',
timeWindows: [
{
content: 'item 1',
startTime: '2021-12-27 00:08:52.078',
topTime: '2021-12-27 01:16:33.890',
stopTime: '2021-12-27 02:25:39.348'
},
]
},
{
deviceId: '2',
norad: '11',
content: 'item 2',
timeWindows: [
{
content: 'item 2',
startTime: '2021-12-27 01:08:52.078',
topTime: '2021-12-27 01:16:33.890',
stopTime: '2021-12-27 04:25:39.348'
},
]
},
{
deviceId: '3',
norad: '11',
content: 'item 3',
timeWindows: [
{
content: 'item 3',
startTime: '2021-12-27 03:08:37.247',
topTime: '2021-12-27 03:16:30.791',
stopTime: '2021-12-27 05:25:29.077'
},
]
},
{
deviceId: '4',
norad: '11',
content: 'item 4',
timeWindows: [
{
content: 'item 4',
startTime: '2021-12-27 08:10:24.156',
topTime: '2021-12-27 01:18:41.379',
stopTime: '2021-12-27 12:28:32.674'
},
]
},
{
deviceId: '5',
norad: '11',
content: 'item 5',
timeWindows: [
{
content: 'item 5',
startTime: '2021-12-27 05:09:12.876',
topTime: '2021-12-27 01:17:37.260',
stopTime: '2021-12-27 07:27:14.041'
},
]
},
{
deviceId: '6',
norad: '11',
content: 'item 6',
timeWindows: [
{
content: 'item 6',
startTime: '2021-12-27 12:06:14.969',
topTime: '2021-12-27 01:13:31.700',
stopTime: '2021-12-27 14:21:58.220'
},
]
},
{
deviceId: '7',
norad: '11',
content: 'item 7',
timeWindows: [
{
content: 'item 7',
startTime: '2021-12-27 12:11:47.926',
topTime: '2021-12-27 01:19:33.909',
stopTime: '2021-12-27 15:29:01.687'
},
]
},
{
deviceId: '8',
norad: '11',
content: 'item 8',
timeWindows: [
{
content: 'item 8',
startTime: '2021-12-27 18:23:58.104',
topTime: '2021-12-27 01:27:59.422',
stopTime: '2021-12-27 23:32:00.740'
},
]
},
{
deviceId: '9',
norad: '11',
content: 'item 9',
timeWindows: [
{
content: 'item 9',
startTime: '2021-12-27 13:23:58.104',
topTime: '2021-12-27 01:27:59.422',
stopTime: '2021-12-27 16:32:00.740'
},
]
}
]
}
]
)
const timeline = ref(null)
const visualization = ref(null)
const computedData = () => {
const trackTimeWindows = []
const timeWindows = []
ganntData.value[0].trackTimeWindows.forEach(
(trackTimeWindow, trackTimeWindowIndex) => {
trackTimeWindows.push({
content: trackTimeWindow.content,
id: `${trackTimeWindow.deviceId}-${trackTimeWindowIndex}-trackTimeWindows`,
value: trackTimeWindowIndex + 1,
className: `visGantt-item${trackTimeWindowIndex % 10}`
})
trackTimeWindow.timeWindows.forEach((timeWindow, timeWindowIndex) => {
timeWindows.push({
content: trackTimeWindow.content,
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}-${timeWindowIndex}-timeWindows`,
deviceId: trackTimeWindow.deviceId
})
})
}
)
return {
trackTimeWindows,
timeWindows
}
}
const initChart = () => {
const { timeWindows, trackTimeWindows } = computedData()
const groups = new DataSet(trackTimeWindows)
const items = new DataSet(timeWindows)
const container = visualization.value
const options = {
groupOrder: function(a, b) {
return a.value - b.value
},
groupOrderSwap: function(a, b, groups) {
const v = a.value
a.value = b.value
b.value = v
},
// height: '300px', // 高度
verticalScroll: true, // 竖向滚动
orientation: 'top', // 时间轴位置
editable: false,
groupEditable: false,
min: new Date('2021-12-27 00:00'), // 最小可见范围
max: new Date('2021-12-27 23:59'), // 最大可见范围
tooltip: {
followMouse: true,
overflowMethod: 'cap',
template: (originalItemData, parsedItemData) => {
return `<div>
<p
<span>设备名称:</span>
<span>${originalItemData.content}</span>
</p><br/>
<p>
<span>开始时间:</span>
<span>${originalItemData.startTime}</span>
</p><br/>
<p>
<span>结束时间:</span>
<span>${originalItemData.stopTime}</span>
</p>
</div>`
}
},
// zoomMin: 1728000000,
// zoomMax: 315360000000,
locale: moment.locale('zh-cn') // 时间轴国际化
}
timeline.value = new Timeline(container)
timeline.value.setOptions(options)
timeline.value.setGroups(groups)
timeline.value.setItems(items)
}
onBeforeUnmount(() => {
timeline.value.destroy()
timeline.value = null
})
onMounted(() => {
initChart()
})
</script>
最后效果
图片:
借鉴:https://www.jb51.net/article/275541.htm