记录---vue3如何封装一个基础甘特图?保姆级教程
🧑💻 写在开头
点赞 + 收藏 === 学会🤣🤣🤣
vue3封装一个基础甘特图
- 只支持简单展示功能
- 代码通俗易懂
效果图:
主要计算
- 计算出整体的日期范围
- 根据每项开始时间和结束时间计算出每一项所占的长度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // 基础甘特图封装 import { ref } from 'vue' import dayjs from 'dayjs' const data = [ { order: 'AWM69963320-1' , start: '2023-01-01' , end: '2023-01-15' , progress: 50 }, { order: 'AWM69963320-1' , start: '2023-01-18' , end: '2023-02-15' , progress: 30 }, { order: 'AWM69963320-1' , start: '2023-01-01' , end: '2023-01-20' , progress: 20 }, { order: 'AWM69963320-1' , start: '2023-02-18' , end: '2023-02-25' , progress: 10 }, ] // 日期数组 const dates = ref<string[]>([]) const itemWidth = 30 const init = () => { const earliestStart = data.reduce((min, item) => dayjs(item.start).isBefore(dayjs(min)) ? item.start : min, data[0].start) const latestEnd = data.reduce((max, item) => dayjs(item.end).isAfter(dayjs(max)) ? item.end : max, data[0].end) let currentDate = dayjs(earliestStart) let endDate = dayjs(latestEnd).add(1, 'day' ) while (currentDate.isBefore(endDate)) { dates.value.push(currentDate. format ( 'YYYY-MM-DD' )); currentDate = currentDate.add(1, 'day' ); } } // 计算每个任务的起始位置和宽度 const calculatePositionAndWidth = (item: any) => { const startIndex = dates.value.findIndex(( date ) => date === item.start) const endIndex = dates.value.findIndex(( date ) => date === item.end) const position = startIndex * itemWidth const width = (endIndex - startIndex + 1) * itemWidth console.log(width, position); return { position, width } } init() |
代码还是非常少的,计算出日期存储到数组中用于渲染头部和每项长度的计算。
- itemWidth: 头部每个日期的宽度(可以根据实际需求自行调整,我这只需要展示日,所以设定的比较短。)
- 渲染每条数据时都调用函数 calculatePositionAndWidth 计算出元素的偏移量和宽度。
- 偏移量:
起始日期在数组中的位置 x 每个日期的宽度(itemWidth)
- 实际宽度:
(结束日期在数组中的位置 - 起始日期在数组中的位置 + 1)x 每个日期的宽度(itemWidth)
,宽度需要加一个单位,这很好理解吧,假设起始日期在0位置,结束日期在1位置,1 - 0 = 1,而实际占了两个位置,所以需要 + 1。 - 有了偏移量和宽度,剩下的就是布局了,完整代码如下。
完整代码
- css中:
--labelWidth: 150px;
- --labelWidth: 侧边栏的宽度,相当于 5 个日期长度,
这个长度布局会用到
。 - "{ width:
${(dates.length + 5) * itemWidth}px
}",给每一项都赋予宽度,这个 + 5 就是侧边栏的宽度。数据太多太长的,滚动的时候需要固定左侧栏目和顶部日期,利于阅读。如果不指定每一项宽度,可能会导致,底部的border只有一部分。 - 至此,一个简单的甘特图封装完成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | <template> <div class= "ganttChart" > <ul class= "itemList scrollBar" > <li class= "item header" :style= "{ width: `${(dates.length + 5) * itemWidth}px` }" > <div class= "label" >< /div > <div class= "dates" > <div class= "date" :style= "{ width: `${itemWidth}px` }" v - for = "date in dates" :key= "date" >{{ date . split ( '-' )[2] }}< /div > < /div > < /li > <li class= "item itemData" v - for = "item in data" :style= "{ width: `${(dates.length + 5) * itemWidth}px` }" > <div class= "label" >{{ item.order }}< /div > <a-tooltip color= 'white' > <template #title> <div style= "color: black;" > 排程数量:1000 < /div > <div style= "color: black;" > 完成数量:500 < /div > < /template > <div class= "speedOfProgress" :style= "{ left: `${calculatePositionAndWidth(item).position}px`, width: `${calculatePositionAndWidth(item).width}px` }" > <div class= "progress" :style= "{ width: `${item.progress}%` }" >< /div > {{ item.progress }} < /div > < /a-tooltip > < /li > < /ul > < /div > < /template > <script setup lang= "ts" > // 基础甘特图封装 import { ref } from 'vue' import dayjs from 'dayjs' const data = [ { order: 'AWM69963320-1' , start: '2023-01-01' , end: '2023-01-15' , progress: 50 }, { order: 'AWM69963320-1' , start: '2023-01-18' , end: '2023-02-15' , progress: 30 }, { order: 'AWM69963320-1' , start: '2023-01-01' , end: '2023-01-20' , progress: 20 }, { order: 'AWM69963320-1' , start: '2023-02-18' , end: '2023-02-25' , progress: 10 }, { order: 'AWM69963320-1' , start: '2023-01-01' , end: '2023-01-15' , progress: 30 }, { order: 'AWM69963320-1' , start: '2023-01-18' , end: '2023-02-15' , progress: 70 }, { order: 'AWM69963320-1' , start: '2023-01-01' , end: '2023-01-15' , progress: 50 }, { order: 'AWM69963320-1' , start: '2023-01-18' , end: '2023-02-15' , progress: 30 }, { order: 'AWM69963320-1' , start: '2023-01-01' , end: '2023-01-20' , progress: 20 }, { order: 'AWM69963320-1' , start: '2023-02-18' , end: '2023-02-25' , progress: 10 }, { order: 'AWM69963320-1' , start: '2023-01-01' , end: '2023-01-15' , progress: 30 }, { order: 'AWM69963320-1' , start: '2023-01-18' , end: '2023-02-15' , progress: 70 }, { order: 'AWM69963320-1' , start: '2023-01-01' , end: '2023-01-15' , progress: 50 }, { order: 'AWM69963320-1' , start: '2023-01-18' , end: '2023-02-15' , progress: 30 }, { order: 'AWM69963320-1' , start: '2023-01-01' , end: '2023-01-20' , progress: 20 }, { order: 'AWM69963320-1' , start: '2023-02-18' , end: '2023-02-25' , progress: 10 }, { order: 'AWM69963320-1' , start: '2023-01-01' , end: '2023-01-15' , progress: 30 }, { order: 'AWM69963320-1' , start: '2023-01-18' , end: '2023-02-15' , progress: 70 }, ] // 日期数组 const dates = ref<string[]>([]) const itemWidth = 30 const init = () => { const earliestStart = data.reduce((min, item) => dayjs(item.start).isBefore(dayjs(min)) ? item.start : min, data[0].start) const latestEnd = data.reduce((max, item) => dayjs(item.end).isAfter(dayjs(max)) ? item.end : max, data[0].end) let currentDate = dayjs(earliestStart) let endDate = dayjs(latestEnd).add(1, 'day' ) while (currentDate.isBefore(endDate)) { dates.value.push(currentDate. format ( 'YYYY-MM-DD' )); currentDate = currentDate.add(1, 'day' ); } } // 计算每个任务的起始位置和宽度 const calculatePositionAndWidth = (item: any) => { const startIndex = dates.value.findIndex(( date ) => date === item.start) const endIndex = dates.value.findIndex(( date ) => date === item.end) const position = startIndex * itemWidth const width = (endIndex - startIndex + 1) * itemWidth console.log(width, position); return { position, width } } init() < /script > <style scoped lang= "scss" > .ganttChart { --labelWidth: 150px; height: calc(100% - 35px); .itemList { height: 100%; position: relative; .header { position: sticky; top : 0; background-color: white; z-index: 10; } .item { display: grid; box-sizing: border-box; border-bottom: 1px solid #ccc; grid-template-columns: var(--labelWidth) 1fr; &:first-child { border: none; } .label { position: sticky; left: 0; background-color: white; z-index: 5; text-align: center; } .dates { display: flex; position: relative; background-color: white; . date { box-sizing: border-box; border: 1px solid #ccc; text-align: center; border-left: none; &:first-child { border-left: 1px solid #ccc; } } } .speedOfProgress { margin- top : 4px; height: 10px; position: relative; background-color: #b9d8fb; border-radius: 5px; overflow: hidden; cursor: pointer; .progress { position: absolute; height: 100%; background-color: #27f; } } } .itemData { padding: 5px 0; } } } < /style > |
补充滚动条样式
- scrollBar(滚动条样式) 是单独封装在sass文件中的,所以没在原文中显示,补充一下滚动条样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | // 滚动条 .scrollBar{ overflow-y: overlay; &::-webkit-scrollbar { width: 8px; height: 8px; } &::-webkit-scrollbar-track { background: transparent; } &::-webkit-scrollbar-thumb { background-color: #bfbfbf; border-radius: 5px; border: 1px solid #F1F1F1; box-shadow: inset 0 0 6px rgba(0, 0, 0, .3); } &::-webkit-scrollbar-thumb:hover { background-color: #A8A8A8; } &::-webkit-scrollbar-thumb:active { background-color: #787878; } /*边角,即两个滚动条的交汇处*/ &::-webkit-scrollbar-corner { background-color: #FFFFFF; } } |
本文转载于:https://juejin.cn/post/7458107546391117887
如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。
__EOF__
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek-R1本地部署如何选择适合你的版本?看这里
· 开源的 DeepSeek-R1「GitHub 热点速览」
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 揭秘 Sdcb Chats 如何解析 DeepSeek-R1 思维链
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
2023-01-13 记录--“非主流” 的纯前端性能优化
2022-01-13 记录:浏览器渲染过程
2021-01-13 ichartjs插件的使用