Echarts甘特图详解

一、Echarts甘特图

Echarts是一个基于Javascript的开源可视化库,支持多种图表类型,其中甘特图是一个常用的图表类型。甘特图可以直观展示项目进度、时间安排等信息。

下面是一个简单的甘特图示例:

    
        var data = [{
            name: '任务一',
            value: [new Date('2021/08/01'), new Date('2021/08/05')]
        }, {
            name: '任务二',
            value: [new Date('2021/08/06'), new Date('2021/08/10')]
        }]
        
        option = {
            title: {
                text: '甘特图示例'
            },
            tooltip: {},
            legend: {
                data: ['任务']
            },
            xAxis: {
                type: 'time',
                splitLine: {
                    show: false
                }
            },
            yAxis: {
                type: 'category',
                data: ['任务一', '任务二']
            },
            series: [{
                type: 'bar',
                name: '任务',
                stack: '甘特图',
                label: {
                    show: true,
                    position: 'insideLeft'
                },
                itemStyle: {
                    borderColor: 'white'
                },
                barWidth: 20,
                data: data.map(function (item) {
                    return {
                        name: item.name,
                        value: [item.value[0], item.value[1] - item.value[0]]
                    }
                }),
                tooltip: {
                    formatter: function (params) {
                        return params.name + ': ' + params.value[0].toLocaleDateString() + '-' + params.value[1].toLocaleDateString();
                    }
                }
            }]
        };
        
        var myChart = echarts.init(document.getElementById('main'));
        myChart.setOption(option);
    

以上代码中,x轴表示时间轴,y轴表示任务名称,柱形图表示任务所占用的时间段。图例为任务,对应的任务分别为任务一和任务二。

二、Echarts甘特图X轴动态

Echarts甘特图默认情况下X轴刻度是固定的,不能动态改变。但是,在实际开发中,我们经常需要动态根据时间范围调整X轴刻度。

下面给出一个动态改变X轴刻度的示例:

    
        var startTime = new Date('2021/08/01');
        var endTime = new Date('2021/08/10');

        var option = {
            title: {
                text: '动态刻度示例'
            },
            tooltip: {},
            legend: {
                data: ['任务']
            },
            xAxis: {
                type: 'time',
                min: startTime,
                max: endTime,
                splitLine: {
                    show: false
                }
            },
            yAxis: {
                type: 'category',
                data: ['任务一', '任务二']
            },
            series: [{
                type: 'bar',
                name: '任务',
                stack: '甘特图',
                label: {
                    show: true,
                    position: 'insideLeft'
                },
                itemStyle: {
                    borderColor: 'white'
                },
                barWidth: 20,
                data: [{
                    name: '任务一',
                    value: [new Date('2021/08/01'), new Date('2021/08/05')]
                }, {
                    name: '任务二',
                    value: [new Date('2021/08/06'), new Date('2021/08/10')]
                }],
                tooltip: {
                    formatter: function (params) {
                        return params.name + ': ' + params.value[0].toLocaleDateString() + '-' + params.value[1].toLocaleDateString();
                    }
                }
            }]
        };

        var myChart = echarts.init(document.getElementById('main'));

        setInterval(function () {
            startTime.setDate(startTime.getDate() + 1);
            endTime.setDate(endTime.getDate() + 1);

            myChart.setOption({
                xAxis: {
                    min: startTime,
                    max: endTime
                }
            });
        }, 1000);
        
        myChart.setOption(option);        
    

以上代码中,我们通过setInterval函数模拟了每秒更新一次时间范围,并通过setOption方法动态改变X轴刻度,实现了动态变化的效果。

三、Echarts设置甘特图

Echarts甘特图支持多种设置,例如背景颜色、轴线样式、字体样式等。

下面给出一个设置甘特图的示例:

    
        option = {
            backgroundColor: '#F9FAFB',
            title: {
                text: 'Echarts甘特图设置',
                left: 'center'
            },
            tooltip: {},
            legend: {
                data: ['任务']
            },
            xAxis: {
                type: 'time',
                splitLine: {
                    lineStyle: {
                        type: 'dotted'
                    }
                },
                axisLine: {
                    lineStyle: {
                        color: '#065279'
                    }
                },
                axisLabel: {
                    color: '#097054'
                }
            },
            yAxis: {
                type: 'category',
                data: ['任务一', '任务二'],
                axisLine: {
                    lineStyle: {
                        color: '#065279'
                    }
                },
                axisLabel: {
                    color: '#097054'
                }
            },
            series: [{
                type: 'bar',
                name: '任务',
                stack: '甘特图',
                label: {
                    show: true,
                    position: 'insideLeft'
                },
                itemStyle: {
                    borderColor: 'white'
                },
                barWidth: 20,
                data: [{
                    name: '任务一',
                    value: [new Date('2021/08/01'), new Date('2021/08/05')]
                }, {
                    name: '任务二',
                    value: [new Date('2021/08/06'), new Date('2021/08/10')]
                }],
                tooltip: {
                    formatter: function (params) {
                        return params.name + ': ' + params.value[0].toLocaleDateString() + '-' + params.value[1].toLocaleDateString();
                    }
                }
            }]
        };
        
        var myChart = echarts.init(document.getElementById('main'));
        
        myChart.setOption(option);
    

以上代码中,我们给出了设置背景颜色、轴线样式、字体样式等内容的示例,可以根据需求自行调整。

四、Echarts图例

Echarts甘特图支持设置图例,用于表示每个数据的意义。

下面给出一个设置图例的示例:

    
        option = {
            title: {
                text: 'Echarts甘特图图例',
                left: 'center'
            },
            tooltip: {},
            legend: {
                data: [{
                    name: '任务一',
                    icon: 'circle'
                }, {
                    name: '任务二',
                    icon: 'circle'
                }],
                orient: 'vertical',
                left: 'right'
            },
            xAxis: {
                type: 'time',
                splitLine: {
                    lineStyle: {
                        type: 'dotted'
                    }
                },
                axisLine: {
                    lineStyle: {
                        color: '#065279'
                    }
                },
                axisLabel: {
                    color: '#097054'
                }
            },
            yAxis: {
                type: 'category',
                data: ['任务一', '任务二'],
                axisLine: {
                    lineStyle: {
                        color: '#065279'
                    }
                },
                axisLabel: {
                    color: '#097054'
                }
            },
            series: [{
                type: 'bar',
                name: '任务一',
                stack: '甘特图',
                label: {
                    show: true,
                    position: 'insideLeft'
                },
                itemStyle: {
                    borderColor: 'white'
                },
                barWidth: 20,
                data: [{
                    name: '任务一',
                    value: [new Date('2021/08/01'), new Date('2021/08/05')]
                }],
                tooltip: {
                    formatter: function (params) {
                        return params.name + ': ' + params.value[0].toLocaleDateString() + '-' + params.value[1].toLocaleDateString();
                    }
                }
            }, {
                type: 'bar',
                name: '任务二',
                stack: '甘特图',
                label: {
                    show: true,
                    position: 'insideLeft'
                },
                itemStyle: {
                    borderColor: 'white'
                },
                barWidth: 20,
                data: [{
                    name: '任务二',
                    value: [new Date('2021/08/06'), new Date('2021/08/10')]
                }],
                tooltip: {
                    formatter: function (params) {
                        return params.name + ': ' + params.value[0].toLocaleDateString() + '-' + params.value[1].toLocaleDateString();
                    }
                }
            }]
        };
        
        var myChart = echarts.init(document.getElementById('main'));
        
        myChart.setOption(option);
    

以上代码中,我们对图例进行了自定义设置,可以根据需求进行调整。

五、Echarts折线图

Echarts甘特图除了可以用柱形图来展示任务所占用的时间段,还可以用折线图展示任务进度情况。

下面给出一个Echarts折线图的示例:

    
        option = {
            title: {
                text: 'Echarts折线图示例',
                left: 'center'
            },
            legend: {
                data: ['任务一', '完成进度']
            },
            tooltip: {},
            xAxis: {
                type: 'time',
                splitLine: {
                    lineStyle: {
                        type: 'dotted'
                    }
                },
                axisLine: {
                    lineStyle: {
                        color: '#065279'
                    }
                },
                axisLabel: {
                    color: '#097054'
                }
            },
            yAxis: [{
                type: 'category',
                data: ['任务一'],
                axisLine: {
                    lineStyle: {
                        color: '#065279'
                    }
                },
                axisLabel: {
                    color: '#097054'
                }
            }, {
                type: 'value',
                name: '完成进度',
                position: 'right',
                axisLine: {
                    lineStyle: {
                        color: '#065279'
                    }
                },
                axisLabel: {
                    color: '#097054'
                }
            }],
            series: [{
                type: 'line',
                name: '任务一',
                data: [{
                    name: '任务一',
                    value: [new Date('2021/08/01'), new Date('2021/08/10')]
                }],
                lineStyle: {
                    color: '#eac736'
                },
                itemStyle: {
                    color: '#eac736'
                },
                areaStyle: {
                    color: '#FFFBE6'
                },
                tooltip: {
                    formatter: function (params) {
                        return params.name + ': ' + params.value[0].toLocaleDateString() + '-' + params.value[1].toLocaleDateString();
                    }
                }
            }, {
                type: 'line',
                name: '完成进度',
                data: [{
                    name: '完成进度',
                    value: [new Date('2021/08/01'), 0.2],
                    yAxisIndex: 1
                }, {
                    name: '完成进度',
                    value: [new Date('2021/08/05'), 0.6],
                    yAxisIndex: 1
                }, {
                    name: '完成进度',
                    value: [new Date('2021/08/10'), 1],
                    yAxisIndex: 1
                }],
                lineStyle: {
                    color: '#2c82c9'
                },
                itemStyle: {
                    color: '#2c82c9'
                },
                tooltip: {
                    formatter: '{b}
{a}: {c}' } }] }; var myChart = echarts.init(document.getElementById('main')); myChart.setOption(option);

以上代码中,我们通过添加完成进度的y轴来展示折线图,同时给折线和区域添加了颜色,可以更加清晰直观地表示任务完成情况。

六、Echarts数据可视化

Echarts甘特图不仅支持展示静态图表,还支持动态展示数据的变化。

下面给出一个Echarts数据可视化的示例:

    
option = {
title: {
text: 'Echarts数据可视化示例',
left: 'center'
},
legend: {
data: ['任务']
},
tooltip: {},
xAxis: {
type: 'time',
splitLine: {
lineStyle: {
type: 'dotted'
}
},
axisLine: {
lineStyle: {
color: '#065279'

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/190845.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-11-30 09:07
下一篇 2024-11-30 09:07

相关推荐

  • Echarts 地图 Label 增加背景图

    本文将从多个方面对 Echarts 地图 Label 增加背景图进行详细的阐述。 一、背景图的作用 为 Echarts 地图添加背景图可以使 Label 更加直观、美观,提升视觉效…

    编程 2025-04-29
  • Python甘特图排期

    本篇文章将从多个方面对Python甘特图排期进行详细阐述。 一、甘特图排期是什么? 甘特图排期是一种展示项目进度的工具,通过横轴表示时间,纵轴表示任务,通过柱状图来展示任务的完成情…

    编程 2025-04-27
  • ECharts地图轮播

    本文将从插件基础、数据准备及处理、地图呈现、轮播控制等方面,对ECharts地图轮播进行详细阐述。 一、插件基础 ECharts官方提供了一个名为“echarts-gl”的插件,它…

    编程 2025-04-27
  • Linux sync详解

    一、sync概述 sync是Linux中一个非常重要的命令,它可以将文件系统缓存中的内容,强制写入磁盘中。在执行sync之前,所有的文件系统更新将不会立即写入磁盘,而是先缓存在内存…

    编程 2025-04-25
  • 神经网络代码详解

    神经网络作为一种人工智能技术,被广泛应用于语音识别、图像识别、自然语言处理等领域。而神经网络的模型编写,离不开代码。本文将从多个方面详细阐述神经网络模型编写的代码技术。 一、神经网…

    编程 2025-04-25
  • Linux修改文件名命令详解

    在Linux系统中,修改文件名是一个很常见的操作。Linux提供了多种方式来修改文件名,这篇文章将介绍Linux修改文件名的详细操作。 一、mv命令 mv命令是Linux下的常用命…

    编程 2025-04-25
  • 详解eclipse设置

    一、安装与基础设置 1、下载eclipse并进行安装。 2、打开eclipse,选择对应的工作空间路径。 File -> Switch Workspace -> [选择…

    编程 2025-04-25
  • git config user.name的详解

    一、为什么要使用git config user.name? git是一个非常流行的分布式版本控制系统,很多程序员都会用到它。在使用git commit提交代码时,需要记录commi…

    编程 2025-04-25
  • nginx与apache应用开发详解

    一、概述 nginx和apache都是常见的web服务器。nginx是一个高性能的反向代理web服务器,将负载均衡和缓存集成在了一起,可以动静分离。apache是一个可扩展的web…

    编程 2025-04-25
  • Python输入输出详解

    一、文件读写 Python中文件的读写操作是必不可少的基本技能之一。读写文件分别使用open()函数中的’r’和’w’参数,读取文件…

    编程 2025-04-25

发表回复

登录后才能评论