vue3 甘特圖(二):gantt時間軸切換
1.固定時間軸縮放級別
gantt.config.scale_unit = "month"; //時間軸單位 gantt.config.step = 1;//單位數(shù) gantt.config.date_scale = "%Y年%M";//時間軸展現(xiàn)方式
2.動態(tài)改變時間軸縮放
點擊不同按鈕,切換時間軸狀態(tài)
2.1 配置時間軸參數(shù)
levels里可配置多個級別。
scales展示時間軸對應(yīng)多少行,下的format可自定展現(xiàn)方式,css屬性方法可判斷是否為軸,給周末添加上高亮屬性。
?文章來源:http://www.zghlxwxcb.cn/news/detail-695043.html
?
const zoomConfig = { levels: [ { name: 'day', scale_height: 60, scales: [{ unit: 'day', step: 1, format: '%d %M' }] }, { name: 'week', scale_height: 60, scales: [ { unit: 'week', step: 1, format: function (date) { let dateToStr = gantt.date.date_to_str('%m-%d') let endDate = gantt.date.add(date, -6, 'day') let weekNum = gantt.date.date_to_str('%W')(date) //第幾周 return dateToStr(endDate) + ' 至 ' + dateToStr(date) } }, { unit: 'day', step: 1, format: '%d', // + "周%D" css: function (date) { if (date.getDay() == 0 || date.getDay() == 6) { return 'day-item weekend weekend-border-bottom' } else { return 'day-item' } } } ] }, { name: 'month', scale_height: 60, min_column_width: 18, scales: [ { unit: 'month', format: '%Y-%m' }, { unit: 'day', step: 1, format: '%d', css: function (date) { if (date.getDay() == 0 || date.getDay() == 6) { return 'day-item weekend weekend-border-bottom' } else { return 'day-item' } } } ] }, { name: 'quarter', height: 60, min_column_width: 110, scales: [ { unit: 'quarter', step: 1, format: function (date) { let yearStr = new Date(date).getFullYear() + '年' let dateToStr = gantt.date.date_to_str('%M') let endDate = gantt.date.add(gantt.date.add(date, 3, 'month'), -1, 'day') return yearStr + dateToStr(date) + ' - ' + dateToStr(endDate) } }, { unit: 'week', step: 1, format: function (date) { let dateToStr = gantt.date.date_to_str('%m-%d') let endDate = gantt.date.add(date, 6, 'day') let weekNum = gantt.date.date_to_str('%W')(date) return dateToStr(date) + ' 至 ' + dateToStr(endDate) } } ] }, { name: 'year', scale_height: 50, min_column_width: 150, scales: [ { unit: 'year', step: 1, format: '%Y年' }, { unit: 'month', format: '%Y-%m' } ] } ] }
2.2 初始化時間軸配置
gantt.ext.zoom.init(zoomConfig) //配置初始化擴展 gantt.ext.zoom.setLevel('month') //切換到指定的縮放級別
2.3 改變時間軸
const changeTime = () => { gantt.ext.zoom.setLevel(data.timeState) }
2.4 周末或特殊日期高亮
.weekend { background: #ff9e2f; color: #fff; }
3. 完整示例代碼
<template> <section class="my-gantt"> <div class="time-box"> <el-radio-group v-model="data.timeState" @change="changeTime"> <el-radio-button v-for="(time, t_index) in data.timeList" :key="t_index" :label="time.code" size="default" border >{{ time.name }}</el-radio-button > </el-radio-group> </div> <div id="gantt_here" class="gantt-container"></div> </section> </template> <script setup> import { reactive, toRefs, onBeforeMount, onMounted, watchEffect, defineExpose } from 'vue' import { gantt } from 'dhtmlx-gantt' import 'dhtmlx-gantt/codebase/dhtmlxgantt.css' import demoData from './ganttData.json' const data = reactive({ timeList: [ // { // name: "周", // code: "week", // }, { name: '月', code: 'month' }, { name: '季', code: 'quarter' }, { name: '年', code: 'year' } ], timeState: 'month' }) const zoomConfig = { levels: [ { name: 'day', scale_height: 60, scales: [{ unit: 'day', step: 1, format: '%d %M' }] }, { name: 'week', scale_height: 60, scales: [ { unit: 'week', step: 1, format: function (date) { let dateToStr = gantt.date.date_to_str('%m-%d') let endDate = gantt.date.add(date, -6, 'day') let weekNum = gantt.date.date_to_str('%W')(date) //第幾周 return dateToStr(endDate) + ' 至 ' + dateToStr(date) } }, { unit: 'day', step: 1, format: '%d', // + "周%D" css: function (date) { if (date.getDay() == 0 || date.getDay() == 6) { return 'day-item weekend weekend-border-bottom' } else { return 'day-item' } } } ] }, { name: 'month', scale_height: 60, min_column_width: 18, scales: [ { unit: 'month', format: '%Y-%m' }, { unit: 'day', step: 1, format: '%d', css: function (date) { if (date.getDay() == 0 || date.getDay() == 6) { return 'day-item weekend weekend-border-bottom' } else { return 'day-item' } } } ] }, { name: 'quarter', height: 60, min_column_width: 110, scales: [ { unit: 'quarter', step: 1, format: function (date) { let yearStr = new Date(date).getFullYear() + '年' let dateToStr = gantt.date.date_to_str('%M') let endDate = gantt.date.add(gantt.date.add(date, 3, 'month'), -1, 'day') return yearStr + dateToStr(date) + ' - ' + dateToStr(endDate) } }, { unit: 'week', step: 1, format: function (date) { let dateToStr = gantt.date.date_to_str('%m-%d') let endDate = gantt.date.add(date, 6, 'day') let weekNum = gantt.date.date_to_str('%W')(date) return dateToStr(date) + ' 至 ' + dateToStr(endDate) } } ] }, { name: 'year', scale_height: 50, min_column_width: 150, scales: [ { unit: 'year', step: 1, format: '%Y年' }, { unit: 'month', format: '%Y-%m' } ] } ] } //初始化甘特圖 const initGantt = () => { gantt.config.grid_width = 350 gantt.config.add_column = false //添加符號 //時間軸圖表中,如果不設(shè)置,只有行邊框,區(qū)分上下的任務(wù),設(shè)置之后帶有列的邊框,整個時間軸變成格子狀。 gantt.config.autofit = false gantt.config.row_height = 60 gantt.config.bar_height = 34 // gantt.config.fit_tasks = true //自動延長時間刻度,以適應(yīng)所有顯示的任務(wù) gantt.config.auto_types = true //將包含子任務(wù)的任務(wù)轉(zhuǎn)換為項目,將沒有子任務(wù)的項目轉(zhuǎn)換回任務(wù) // gantt.config.xml_date = '%Y-%m-%d' //甘特圖時間數(shù)據(jù)格式 gantt.config.readonly = true //是否只讀 gantt.i18n.setLocale('cn') //設(shè)置語言 gantt.init('gantt_here') //初始化 gantt.parse(demoData) //填充數(shù)據(jù) gantt.ext.zoom.init(zoomConfig) //配置初始化擴展 gantt.ext.zoom.setLevel('month') //切換到指定的縮放級別 } const changeTime = () => { gantt.ext.zoom.setLevel(data.timeState) } onBeforeMount(() => {}) onMounted(() => { initGantt() }) watchEffect(() => {}) defineExpose({ ...toRefs(data) }) </script> <style scoped lang="scss"> .my-gantt { height: 800px; width: 100vw; .time-box { text-align: center; margin-bottom: 20px; } ::v-deep .gantt-container { width: 100%; height: 100%; .weekend { background: #ff9e2f; color: #fff; } } } </style>
4. 示例數(shù)據(jù)
見:vue3 甘特圖(一):選擇與初始化甘特圖 - 根號九九 - 博客園 (cnblogs.com)文章來源地址http://www.zghlxwxcb.cn/news/detail-695043.html
到了這里,關(guān)于vue3 甘特圖(二):甘特圖時間軸切換的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!