阿里云主机折上折
  • 微信号
您当前的位置:网站首页 > 仪表盘(Gauge)实现

仪表盘(Gauge)实现

作者:陈川 阅读数:41542人阅读 分类: ECharts

仪表盘(Gauge)实现

仪表盘是一种常见的数据可视化组件,用于展示单个指标在特定范围内的当前值。ECharts提供了强大的仪表盘图表类型,支持多种自定义配置,能够满足不同场景下的数据展示需求。

基本仪表盘实现

ECharts中实现基础仪表盘需要配置series的type为'gauge'。以下是一个最简单的仪表盘示例:

option = {
    series: [{
        type: 'gauge',
        data: [{
            value: 50,
            name: '完成率'
        }]
    }]
};

这个基础配置会生成一个0-100的圆形仪表盘,指针指向50的位置。仪表盘默认包含三个部分:刻度盘、指针和数值显示。

仪表盘核心配置项

刻度盘配置

刻度盘是仪表盘的背景部分,可以通过axisLine、axisTick、axisLabel等配置项进行定制:

option = {
    series: [{
        type: 'gauge',
        axisLine: {
            lineStyle: {
                width: 30,
                color: [
                    [0.3, '#67e0e3'],
                    [0.7, '#37a2da'],
                    [1, '#fd666d']
                ]
            }
        },
        axisTick: {
            distance: -30,
            length: 8,
            lineStyle: {
                color: '#fff',
                width: 2
            }
        },
        axisLabel: {
            color: 'auto',
            distance: 40,
            fontSize: 20
        },
        data: [{value: 70}]
    }]
};

指针样式配置

指针样式可以通过pointer配置项进行调整:

series: [{
    type: 'gauge',
    pointer: {
        icon: 'path://M12.8,0.7l12,40.1H0.7L12.8,0.7z',
        length: '50%',
        width: 16,
        offsetCenter: [0, '-60%'],
        itemStyle: {
            color: 'auto'
        }
    },
    data: [{value: 70}]
}]

数值显示配置

仪表盘中心的数值显示可以通过detail配置项定制:

series: [{
    type: 'gauge',
    detail: {
        formatter: '{value}%',
        fontSize: 30,
        offsetCenter: [0, '40%'],
        color: 'auto'
    },
    data: [{value: 70}]
}]

多指针仪表盘

ECharts支持在一个仪表盘中显示多个指针,用于同时展示多个相关指标:

option = {
    series: [{
        type: 'gauge',
        radius: '80%',
        center: ['50%', '55%'],
        axisLine: {
            lineStyle: {
                width: 20
            }
        },
        pointer: {
            length: '60%'
        },
        data: [
            {value: 45, name: 'CPU'},
            {value: 65, name: '内存'},
            {value: 30, name: '磁盘'}
        ]
    }]
};

进度仪表盘

通过修改仪表盘的起始角度和结束角度,可以创建进度条式的仪表盘:

option = {
    series: [{
        type: 'gauge',
        startAngle: 90,
        endAngle: -270,
        pointer: {
            show: false
        },
        progress: {
            show: true,
            overlap: false,
            roundCap: true,
            clip: false,
            itemStyle: {
                color: {
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [{
                        offset: 0,
                        color: 'rgb(58,77,233)'
                    }, {
                        offset: 1,
                        color: 'rgb(187,75,255)'
                    }]
                }
            }
        },
        data: [{
            value: 75
        }]
    }]
};

仪表盘动画效果

ECharts为仪表盘提供了丰富的动画效果配置:

option = {
    series: [{
        type: 'gauge',
        animationDuration: 2000,
        animationEasing: 'elasticOut',
        animationDelay: 0,
        animationDurationUpdate: 1000,
        animationEasingUpdate: 'cubicInOut',
        data: [{
            value: 68
        }]
    }]
};

仪表盘事件处理

可以为仪表盘添加交互事件,增强用户体验:

myChart.on('click', function(params) {
    if(params.seriesType === 'gauge') {
        console.log('点击了仪表盘', params.dataIndex, params.data);
    }
});

myChart.on('mouseover', function(params) {
    if(params.seriesType === 'gauge') {
        console.log('鼠标悬停在仪表盘上', params.dataIndex, params.data);
    }
});

仪表盘与其它图表组合

仪表盘可以与其他图表类型组合使用,创建更丰富的数据看板:

option = {
    grid: [
        {left: '5%', top: '10%', width: '45%', height: '80%'},
        {right: '5%', top: '10%', width: '45%', height: '80%'}
    ],
    series: [
        {
            type: 'gauge',
            gridIndex: 0,
            data: [{value: 75}]
        },
        {
            type: 'pie',
            gridIndex: 1,
            data: [
                {value: 335, name: '直接访问'},
                {value: 310, name: '邮件营销'},
                {value: 234, name: '联盟广告'}
            ]
        }
    ]
};

仪表盘高级应用

动态更新数据

仪表盘数据可以动态更新,实现实时监控效果:

setInterval(function() {
    option.series[0].data[0].value = (Math.random() * 100).toFixed(2) - 0;
    myChart.setOption(option, true);
}, 2000);

自定义仪表盘形状

通过修改配置项,可以创建不同形状的仪表盘:

option = {
    series: [{
        type: 'gauge',
        radius: '120%',
        startAngle: 180,
        endAngle: 0,
        min: 0,
        max: 100,
        splitNumber: 10,
        axisLine: {
            lineStyle: {
                width: 30,
                color: [
                    [0.3, '#67e0e3'],
                    [0.7, '#37a2da'],
                    [1, '#fd666d']
                ]
            }
        },
        pointer: {
            icon: 'path://M12.8,0.7l12,40.1H0.7L12.8,0.7z',
            length: '12%',
            width: 20,
            offsetCenter: [0, '-60%']
        },
        axisTick: {
            length: 12,
            lineStyle: {
                color: 'auto',
                width: 2
            }
        },
        splitLine: {
            length: 20,
            lineStyle: {
                color: 'auto',
                width: 5
            }
        },
        axisLabel: {
            color: '#464646',
            fontSize: 20,
            distance: -60
        },
        detail: {
            valueAnimation: true,
            formatter: '{value} km/h',
            color: 'inherit'
        },
        data: [{
            value: 70
        }]
    }]
};

仪表盘与地图结合

仪表盘可以与地图结合使用,展示区域指标:

option = {
    geo: {
        map: 'china',
        roam: true,
        emphasis: {
            itemStyle: {
                areaColor: '#f3b329'
            }
        }
    },
    series: [{
        type: 'gauge',
        coordinateSystem: 'geo',
        geoIndex: 0,
        center: ['50%', '50%'],
        radius: '20%',
        data: [{
            value: 65,
            name: '全国完成率'
        }]
    }]
};

仪表盘性能优化

对于需要展示大量仪表盘的场景,可以采用以下优化策略:

  1. 关闭不必要的动画效果
  2. 减少刻度数量
  3. 简化指针样式
  4. 使用canvas渲染代替SVG
option = {
    series: [{
        type: 'gauge',
        silent: true,
        animation: false,
        axisTick: {
            splitNumber: 5
        },
        data: [{
            value: 50
        }]
    }]
};

仪表盘主题适配

ECharts仪表盘支持主题切换,可以轻松适配不同风格的界面:

// 使用内置dark主题
echarts.registerTheme('dark', {
    backgroundColor: '#333',
    gauge: {
        axisLine: {
            lineStyle: {
                color: [[1, 'rgba(255,255,255,0.3)']]
            }
        },
        axisLabel: {
            color: '#fff'
        },
        detail: {
            color: '#fff'
        }
    }
});

// 应用主题
myChart.setOption({
    series: [{
        type: 'gauge',
        data: [{value: 50}]
    }]
}, 'dark');

仪表盘国际化

通过配置项可以实现仪表盘的国际化支持:

option = {
    series: [{
        type: 'gauge',
        detail: {
            formatter: function(value) {
                return i18n.t('gauge.value', {value: value});
            }
        },
        data: [{
            value: 75,
            name: i18n.t('gauge.completionRate')
        }]
    }]
};

仪表盘无障碍访问

为提升可访问性,可以为仪表盘添加ARIA标签:

option = {
    aria: {
        enabled: true,
        description: '当前完成率75%,处于良好水平'
    },
    series: [{
        type: 'gauge',
        data: [{
            value: 75,
            name: '完成率'
        }]
    }]
};

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

前端川

前端川,陈川的代码茶馆🍵,专治各种不服的Bug退散符💻,日常贩卖秃头警告级的开发心得🛠️,附赠一行代码笑十年的摸鱼宝典🐟,偶尔掉落咖啡杯里泡开的像素级浪漫☕。‌