1、安装 highcharts
| npm install highcharts --save |
2、页面引用
| |
| import Highcharts from 'highcharts/highcharts-gantt.src.js' |
| |
| import factory from 'highcharts/modules/draggable-points.js' |
| factory(Highcharts) |
3、封装组件
| |
| |
| |
| |
| |
| |
| |
| |
| <template> |
| <div class="BaseNewGantt"> |
| <div id="container"></div> |
| </div> |
| </template> |
| |
| <script> |
| import Highcharts from 'highcharts/highcharts-gantt.src.js' |
| import factory from 'highcharts/modules/draggable-points.js' |
| factory(Highcharts) |
| export default { |
| name: 'BaseNewGantt', |
| data() { |
| return { |
| isShow: true, |
| tableConfig: [], |
| } |
| }, |
| props: { |
| |
| ganttConfig: { |
| type: Object, |
| default: () => { |
| return { |
| data: [], |
| columnsConfig: [], |
| } |
| }, |
| }, |
| }, |
| mounted() {}, |
| methods: { |
| |
| init() { |
| Highcharts.setOptions({ |
| global: { |
| useUTC: false, |
| }, |
| lang: { |
| noData: '暂无数据', |
| months: [ |
| '一月', |
| '二月', |
| '三月', |
| '四月', |
| '五月', |
| '六月', |
| '七月', |
| '八月', |
| '九月', |
| '十月', |
| '十一月', |
| '十二月', |
| ], |
| shortMonths: [ |
| '一月', |
| '二月', |
| '三月', |
| '四月', |
| '五月', |
| '六月', |
| '七月', |
| '八月', |
| '九月', |
| '十月', |
| '十一月', |
| '十二月', |
| ], |
| weekdays: ['日', '一', '二', '三', '四', '五', '六'], |
| buttons: [ |
| { type: 'month', count: 1, text: '月' }, |
| { type: 'month', count: 3, text: '季度' }, |
| { type: 'month', count: 6, text: '半年' }, |
| { type: 'ytd', text: 'YTD' }, |
| { type: 'year', count: 1, text: '年' }, |
| { type: 'all', text: '所有' }, |
| ], |
| rangeSelectorZoom: '范围', |
| }, |
| }) |
| |
| this.getGantt() |
| }, |
| getGantt() { |
| const _this = this |
| const day = 1000 * 60 * 60 * 24 |
| const map = Highcharts.map |
| |
| let series = this.ganttConfig.data |
| |
| this.columnsConfig = [] |
| if (this.ganttConfig.tableConfig.length > 0) { |
| this.ganttConfig.tableConfig.forEach((item) => { |
| let obj = { |
| title: { |
| text: item.title, |
| }, |
| categories: map(series, function (s) { |
| return s[item.labels] |
| }), |
| } |
| this.columnsConfig.push(obj) |
| }) |
| } |
| Highcharts.ganttChart('container', { |
| plotOptions: { |
| series: { |
| animation: true, |
| dragDrop: { |
| draggableX: true, |
| draggableY: false, |
| dragPrecisionX: day, |
| }, |
| dataLabels: { |
| enabled: true, |
| format: '{point.title}', |
| style: { |
| cursor: 'default', |
| opacity: _this.isShow ? 0 : 1, |
| pointerEvents: 'none', |
| }, |
| }, |
| allowPointSelect: true, |
| point: { |
| events: { |
| dragStart: _this.onDragStart, |
| drag: _this.onDrag, |
| drop: _this.onDrop, |
| select: _this.onHandleSelect, |
| }, |
| }, |
| }, |
| }, |
| yAxis: { |
| type: 'category', |
| grid: { |
| enabled: true, |
| borderColor: 'rgba(0,0,0,0.3)', |
| borderWidth: 1, |
| columns: _this.columnsConfig, |
| }, |
| }, |
| xAxis: [ |
| { |
| currentDateIndicator: true, |
| grid: { |
| borderWidth: 1, |
| cellHeight: 0, |
| }, |
| labels: { |
| format: '{value:%d}日', |
| }, |
| }, |
| { |
| labels: { |
| format: '{value:%Y年-%m月}', |
| }, |
| }, |
| ], |
| |
| tooltip: { |
| formatter: function () { |
| return `<div> |
| ${this.point.title}<br/> |
| 开始时间: ${_this.$library.common.formatDate( |
| new Date(this.point.start), |
| 'YYYY-MM-DD', |
| )}<br/> |
| 结束时间: ${_this.$library.common.formatDate( |
| new Date(this.point.end), |
| 'YYYY-MM-DD', |
| )}<br/> |
| </div>` |
| }, |
| }, |
| navigator: { |
| enabled: true, |
| series: { |
| type: 'gantt', |
| pointPlacement: 0.5, |
| pointPadding: 0.25, |
| }, |
| yAxis: { |
| min: 0, |
| max: 6, |
| reversed: true, |
| categories: [], |
| }, |
| }, |
| series: series, |
| |
| scrollbar: { |
| enabled: true, |
| }, |
| |
| rangeSelector: { |
| enabled: true, |
| selected: 0, |
| buttons: [ |
| { type: 'month', count: 1, text: '月' }, |
| { type: 'month', count: 3, text: '季度' }, |
| { type: 'month', count: 6, text: '半年' }, |
| { type: 'ytd', text: 'YTD' }, |
| { type: 'year', count: 1, text: '年' }, |
| { type: 'all', text: '所有' }, |
| ], |
| }, |
| |
| credits: { |
| enabled: false, |
| }, |
| }) |
| }, |
| |
| onDragStart(e) {}, |
| |
| onDrag(e) {}, |
| |
| onDrop(e) { |
| this.$emit('onDragStop', e.target.options) |
| }, |
| |
| onHandleSelect(e) { |
| this.$emit('onHandleSelect', e.target.options) |
| console.log(e) |
| }, |
| |
| onShowTitle() { |
| |
| this.isShow = !this.isShow |
| |
| this.getGantt() |
| }, |
| }, |
| } |
| </script> |
| |
| <style lang="less" scoped> |
| .BaseNewGantt { |
| height: calc(100% - 70px); |
| overflow: auto; |
| } |
| </style> |
| |
4、组件使用
| <base-gantt |
| ref="gantt" |
| :ganttConfig="ganttConfig" |
| @onDragStop="onDragStop" |
| @onHandleSelect="onHandleSelect"> |
| </base-gantt> |
data中配置
| |
| ganttConfig: { |
| |
| data: [], |
| // 左侧表格列配置 |
| tableConfig: [ |
| { |
| title: '设备名称', |
| labels: 'machineName', |
| }, |
| { |
| title: '设备编码', |
| labels: 'machineNo', |
| }, |
| ], |
| }, |
事件
| |
| onDragStop(option) {}, |
| |
| onHandleSelect(option) { |
| this.selectGanttRowData = option |
| }, |
5、实例

6、官网地址
https://www.hcharts.cn/
演示地址
https://www.hcharts.cn/demo/gantt
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!