【echarts】甘特图

javascript 复制代码
const milestones = [
    { progress: 100, milestoneName: '阶段一', startDate: '2020-12-23', endDate: '2021-01-30' },
    { progress: 100, milestoneName: '阶段二', startDate: '2021-01-15', endDate: '2021-03-15' },
    { progress: 100, milestoneName: '阶段三', startDate: '2021-03-10', endDate: '2021-04-05' },
    { progress: 94, milestoneName: '阶段四', startDate: '2021-04-06', endDate: '2021-04-29' },
    { progress: 0, milestoneName: '阶段五', startDate: '2021-04-30', endDate: '2021-05-03' },
];

const colors = [
'#f19489',
'#bb8fce',
'#85c1e9',
'#76d7c3',
'#81e0aa',
'#f7dc6f',
'#e59865'
]

const categories = milestones.map(milestone => milestone.milestoneName);
const data = milestones.map((milestone, index) => {
    const startTime = new Date(milestone.startDate).getTime();
    const endTime = new Date(milestone.endDate).getTime();
    const duration = endTime - startTime;
    
    return {
        name: milestone.milestoneName,
        value: [
            index,         // category index
            startTime,     // start time
            endTime,       // end time
            duration       // duration in milliseconds
        ],
        itemStyle: {
            normal: {
                color: colors[index]
            }
        }
    };
});

function renderItem(params, api) {
    const categoryIndex = api.value(0);
    const start = api.coord([api.value(1), categoryIndex]);
    const end = api.coord([api.value(2), categoryIndex]);
    const height = api.size([0, 1])[1] * 0.6;

    const rectShape = echarts.graphic.clipRectByRect({
        x: start[0],
        y: start[1] - height / 2,
        width: end[0] - start[0],
        height: height
    }, {
        x: params.coordSys.x,
        y: params.coordSys.y,
        width: params.coordSys.width,
        height: params.coordSys.height
    });

    return rectShape && {
        type: 'rect',
        shape: rectShape,
        style: api.style()
    };
}

option = {
    tooltip: {
        formatter: function (params) {
            return params.marker + params.name + ': ' + (params.value[3] / 1000 / 60 / 60 / 24).toFixed(1) + ' days';  // converting ms to days
        }
    },
    dataZoom: [{
        type: 'slider',
        filterMode: 'weakFilter',
        showDataShadow: false,
        top: 400,
        height: 10,
        borderColor: 'transparent',
        backgroundColor: '#e2e2e2',
        handleIcon: 'M10.7,11.9H9.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4h1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7v-1.2h6.6z M13.3,22H6.7v-1.2h6.6z M13.3,19.6H6.7v-1.2h6.6z', // jshint ignore:line
        handleSize: 20,
        handleStyle: {
            shadowBlur: 6,
            shadowOffsetX: 1,
            shadowOffsetY: 2,
            shadowColor: '#aaa'
        },
        labelFormatter: ''
    }, {
        type: 'inside',
        filterMode: 'weakFilter'
    }],
    grid: {
        height: 300
    },
    xAxis: {
        type: 'time',
        axisLabel: {
            formatter: function (val) {
                return new Date(val).toLocaleDateString();
            }
        }
    },
    yAxis: {
        type: 'category',
        data: categories
    },
    series: [{
        type: 'custom',
        renderItem: renderItem,
        encode: {
            x: [1, 2],
            y: 0
        },
        data: data
    }]
};
相关推荐
几何心凉3 分钟前
如何使用 React Hooks 替代类组件的生命周期方法?
前端·javascript·react.js
小堃学编程10 分钟前
前端学习(1)—— 使用HTML编写一个简单的个人简历展示页面
前端·javascript·html
hnlucky1 小时前
通俗易懂版知识点:Keepalived + LVS + Web + NFS 高可用集群到底是干什么的?
linux·前端·学习·github·web·可用性测试·lvs
懒羊羊我小弟1 小时前
使用 ECharts GL 实现交互式 3D 饼图:技术解析与实践
前端·vue.js·3d·前端框架·echarts
前端小巷子1 小时前
CSS3 遮罩
前端·css·面试·css3
运维@小兵1 小时前
vue访问后端接口,实现用户注册
前端·javascript·vue.js
雨汨2 小时前
web:InfiniteScroll 无限滚动
前端·javascript·vue.js
Samuel-Gyx2 小时前
前端 CSS 样式书写与选择器 基础知识
前端·css
天天打码3 小时前
Rspack:字节跳动自研 Web 构建工具-基于 Rust打造高性能前端工具链
开发语言·前端·javascript·rust·开源
字节高级特工3 小时前
【C++】”如虎添翼“:模板初阶
java·c语言·前端·javascript·c++·学习·算法