Echarts实现甘特图功能(完整实现代码)

<template>
  <div class="pwgh-card">
    <div class="chart" ref="chart"></div>
  </div>
</template>
<script>
import {
    mapActions, mapState, mapGetters } from 'vuex'
import {
    chunk, slice } from 'lodash'

import * as echarts from 'echarts'

export default {
   
  name: 'LinesCard',
  props: ['list', 'deptType'],
  data() {
   
    return {
   }
  },
  created() {
   },
  mounted() {
   
    this.initChart()
    this.refreshChart()
  },
  watch: {
   
    list() {
   
      this.initChart()
      this.refreshChart()
    },
    theme() {
   
      this.initChart()
      this.refreshChart()
    },
  },
  computed: {
   
    ...mapState(['theme']),
  },
  methods: {
   
    initChart() {
   
      echarts.registerTheme('light', {
   
        color: ['#19D4AE', '#D9D9D9'],
        title: {
   
          textStyle: {
   
            color: '#464646',
          },
        },
        legend: {
   
          textStyle: {
   
            color: '#4D4D4D',
          },
        },
        categoryAxis: {
   
          axisLabel: {
   
            show: true,
            color: '#4D4D4D',
          },
        },
        valueAxis: {
   
          axisLabel: {
   
            show: true,
            color: '#4D4D4D',
          },
        },
        line: {
   
          itemStyle: {
   
            color: '#5AB1EF',
          },
          areaStyle: {
   
            color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
              {
   
                offset: 0,
                color: 'rgba(128, 186, 231, 0)',
              },
              {
   
                offset: 1,
                color: 'rgba(90, 177, 239, 0.76)',
              },
            ]),
          },
        },
      })
      echarts.registerTheme('green', {
   
        color: ['#19D4AE', '#D9D9D9'],
        title: {
   
          textStyle: {
   
            color: '#fff',
          },
        },
        legend: {
   
          textStyle: {
   
            color: '#fff',
          },
        },
        categoryAxis: {
   
          axisLabel: {
   
            show: true,
            color: '#fff',
          },
        },
        valueAxis: {
   
          axisLabel: {
   
            show: true,
            color: '#fff',
          },
          nameTextStyle: {
   
            color: '#fff',
          },
        },
        bar: {
   
          itemStyle: {
   
            // color: 'rgba(118, 201, 85, 1)'
          },
          label:</
  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是使用ECharts实现甘特图的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ECharts甘特图示例</title> <!-- 引入ECharts库 --> <script src="https://cdn.jsdelivr.net/npm/echarts@5.2.1/dist/echarts.min.js"></script> </head> <body> <!-- 创建一个容器用于显示甘特图 --> <div id="gantt" style="width: 800px; height: 600px;"></div> <!-- 加载甘特图数据并绘制甘特图 --> <script> // 定义甘特图数据 var data = [ { name: '任务1', start: new Date(2022, 0, 1), end: new Date(2022, 0, 5), }, { name: '任务2', start: new Date(2022, 0, 6), end: new Date(2022, 0, 10), }, { name: '任务3', start: new Date(2022, 0, 11), end: new Date(2022, 0, 15), } ]; // 创建甘特图实例 var ganttChart = echarts.init(document.getElementById('gantt')); // 配置甘特图选项 var option = { tooltip: { formatter: function (params) { return params.name + '<br>' + '开始时间:' + params.start + '<br>' + '结束时间:' + params.end; } }, grid: { top: 50, left: 100, bottom: 50, right: 100, }, xAxis: { type: 'time', splitLine: { show: false } }, yAxis: { type: 'category', data: ['任务1', '任务2', '任务3'], splitLine: { show: false } }, series: [{ type: 'bar', data: data.map(function (item) { return { name: item.name, value: [item.start, item.end], itemStyle: { color: '#3A8DEE' } } }) }] }; // 使用甘特图选项绘制甘特图 ganttChart.setOption(option); </script> </body> </html> ``` 这个示例使用ECharts的bar类型系列来绘制甘特图,通过设置系列的data属性来传递甘特图数据,配置x轴和y轴的类型、刻度和分割线等属性,最后使用setOption方法将配置应用到甘特图实例中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值