Highcharts 测量图(Gauge Chart)配置

Highcharts 提供了一个专门用于显示度量值(如仪表盘、表盘等)的图表类型——测量图(Gauge Chart)。测量图常用于显示单一数值的进度或状态,通常用于显示速度、温度、进度条等。

1. 引入 Highcharts Gauge 模块

为了使用 Highcharts 的测量图功能,需要引入 highcharts-gauge.js 模块。你可以通过以下方式引入它:

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/gauge.js"></script>

2. 创建基础测量图

以下是一个基本的测量图示例,显示一个指针,指示数值范围在 0 到 100 之间,当前数值为 60。

Highcharts.chart('container', {
    chart: {
        type: 'gauge'  // 图表类型为测量图
    },
    title: {
        text: '速度计'
    },
    pane: {
        startAngle: -150,  // 设置仪表盘起始角度
        endAngle: 150,  // 设置仪表盘结束角度
        background: [{
            backgroundColor: '#EEE',  // 仪表盘背景颜色
            borderWidth: 0,
            outerRadius: '109%'
        }, {
            backgroundColor: '#FFF',
            borderWidth: 1,
            outerRadius: '107%'
        }, {
            backgroundColor: '#DDD',
            borderWidth: 0,
            outerRadius: '105%'
        }]
    },
    yAxis: {
        min: 0,  // 最小值
        max: 100,  // 最大值
        tickPosition: 'inside',  // 刻度位置
        tickLength: 10,
        tickWidth: 2,
        labels: {
            distance: -25  // 标签的距离
        },
        plotBands: [{  // 定义颜色区间
            from: 0,
            to: 50,
            color: '#55BF3B'  // 绿色
        }, {
            from: 50,
            to: 75,
            color: '#DDDF0D'  // 黄色
        }, {
            from: 75,
            to: 100,
            color: '#DF5353'  // 红色
        }]
    },
    series: [{
        name: '速度',
        data: [60],  // 当前数值
        tooltip: {
            valueSuffix: ' km/h'
        }
    }]
});

3. 测量图的多个值和仪表盘

如果你想在同一个仪表盘上显示多个值,可以通过多个系列(series)来实现。例如,显示速度、油量、温度等。

Highcharts.chart('container', {
    chart: {
        type: 'gauge'  // 图表类型为测量图
    },
    title: {
        text: '仪表盘'
    },
    pane: {
        startAngle: -150,
        endAngle: 150,
        background: [{
            backgroundColor: '#EEE',
            borderWidth: 0,
            outerRadius: '109%'
        }]
    },
    yAxis: [{
        min: 0,
        max: 100,
        tickPosition: 'inside',
        tickLength: 10,
        tickWidth: 2,
        labels: {
            distance: -25
        },
        plotBands: [{
            from: 0,
            to: 50,
            color: '#55BF3B'
        }, {
            from: 50,
            to: 75,
            color: '#DDDF0D'
        }, {
            from: 75,
            to: 100,
            color: '#DF5353'
        }]
    }, {
        min: 0,
        max: 100,
        tickPosition: 'inside',
        tickLength: 10,
        tickWidth: 2,
        labels: {
            distance: -25
        },
        opposite: true,  // 使用对立轴
        plotBands: [{
            from: 0,
            to: 30,
            color: '#55BF3B'
        }, {
            from: 30,
            to: 70,
            color: '#DDDF0D'
        }, {
            from: 70,
            to: 100,
            color: '#DF5353'
        }]
    }],
    series: [{
        name: '速度',
        data: [60],  // 当前数值
        tooltip: {
            valueSuffix: ' km/h'
        }
    }, {
        name: '油量',
        data: [30],  // 当前油量
        tooltip: {
            valueSuffix: ' %'
        }
    }]
});

4. 动态更新测量图

Highcharts 测量图也支持动态更新数据,你可以使用 setInterval()setTimeout() 来定期更新测量图的数据,例如实时显示温度或速度的变化。

var chart = Highcharts.chart('container', {
    chart: {
        type: 'gauge'
    },
    title: {
        text: '实时速度'
    },
    pane: {
        startAngle: -150,
        endAngle: 150,
        background: [{
            backgroundColor: '#EEE',
            borderWidth: 0,
            outerRadius: '109%'
        }]
    },
    yAxis: {
        min: 0,
        max: 100,
        tickPosition: 'inside',
        tickLength: 10,
        tickWidth: 2,
        labels: {
            distance: -25
        },
        plotBands: [{
            from: 0,
            to: 50,
            color: '#55BF3B'
        }, {
            from: 50,
            to: 75,
            color: '#DDDF0D'
        }, {
            from: 75,
            to: 100,
            color: '#DF5353'
        }]
    },
    series: [{
        name: '速度',
        data: [50],  // 初始值
        tooltip: {
            valueSuffix: ' km/h'
        }
    }]
});

// 动态更新数据
setInterval(function () {
    var point = chart.series[0].points[0];
    var newValue = Math.floor(Math.random() * 100);  // 随机生成新值
    point.update(newValue);
}, 1000);  // 每秒更新一次

5. 配置选项说明

  • pane:配置仪表盘的外观,控制起始角度、结束角度和背景色等。
  • yAxis:定义仪表盘的刻度、范围以及颜色带(plotBands)。
    • minmax:设置数值的最小值和最大值。
    • plotBands:定义颜色区间,帮助用户区分不同的数值区间(如绿色表示正常范围,黄色表示警告,红色表示危险)。
  • series:定义数据系列,每个系列对应一个仪表盘值。data 数组中的数据可以是一个数值,表示当前仪表盘指针的位置。
    • name:每个系列的名称。
    • tooltip:定义鼠标悬停时显示的提示信息。

6. 总结

Highcharts 测量图是一种直观的方式来展示进度、状态或实时数据。它非常适合用于仪表盘、速度计、温度计等场景。通过 gauge 图表类型,您可以轻松地创建漂亮的测量图,并通过 yAxispane 自定义图表的外观。

  • 主要特点:实时更新数据、颜色区间、刻度自定义。
  • 适用场景:速度计、温度计、电池电量、生产进度等。

如果你有任何问题或需要更多帮助,随时告诉我!