Skip to content

Gauge 仪表盘 ​

用弧形刻度展示一个处于范围内的当前值,适合完成率、健康度等单一指标。

何时使用 ​

  • 需要突出一个当前值及其上下限或完成比例时。
  • 需要用分段阈值表达正常、预警和危险区间时。
  • 需要同时比较多个指标时不要使用仪表盘,改用 Column 柱状图 或 Radar 雷达图。

在线体验 ​

Gallery 展示 0 到 1 的完成率仪表盘,以及 0 到 100 的分段健康度仪表盘。

Gauge 仪表盘与水波图 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

默认范围是 0 到 1,视觉和提示会按百分比格式显示;Title() 为数值添加语义标题。

go
package main

import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/chart"
)

func page(c *ui.Context) {
    chart.Gauge(c, 0.72).Title("完成率").Show()
}

常见用法 ​

自定义数值范围 ​

Range(lo, hi) 设置刻度范围;范围不是 0 到 1 时默认按普通数字格式显示。

go
package main

import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/chart"
)

func healthGauge(c *ui.Context) {
    chart.Gauge(c, 68).Range(0, 100).Title("健康度").Show()
}

分段阈值 ​

Bands() 接收按升序排列的上限和对应颜色。未提供颜色的段会使用主题分类色板。最后一段的上限取 Range 的最大值时,色带会覆盖整个刻度范围。

go
package main

import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/chart"
    "github.com/tangoui/tango/ui/theme"
)

func thresholdGauge(c *ui.Context) {
    chart.Gauge(c, 68).Range(0, 100).Title("健康度").Bands(
        []float64{40, 70, 100},
        theme.Hex("#ff4d4f"), theme.Hex("#faad14"), theme.Hex("#52c41a"),
    ).Show()
}

格式化、尺寸和无障碍 ​

Format() 控制刻度、中心值和提示的文本;Height() 调整绘图区,A11yLabel() 提供视觉标题之外的可访问名称。

go
package main

import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/chart"
)

func formattedGauge(c *ui.Context) {
    chart.Gauge(c, 6850).Range(0, 10000).Title("请求数").Format(chart.Thousands(0)).Height(280).A11yLabel("请求数当前为 6850").Show()
}

静态和加载状态 ​

悬停提示默认开启;静态大屏可用 NoTooltip(),异步值尚未到达时可用 Loading(true)。

go
package main

import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/chart"
)

func staticGauge(c *ui.Context) {
    chart.Gauge(c, 0.45).Title("容量").NoTooltip().Loading(false).Show()
}

主题与样式 ​

Gauge 使用主题的轨道、刻度、指针和分类色板。可用 Colors() 覆盖进度弧颜色,或用 Style(func(*chart.Style) { ... }) 调整单图令牌;状态色应和图例、文字或阈值标签一起使用,不要只依赖红黄绿。

注意事项 ​

  • 指针和中心数值按 Range() 限制到刻度范围内,悬停提示显示传入的原始值。
  • 即时模式下外部状态保存原始值,更新时下一个渲染帧会产生过渡;条件切换多个仪表盘时使用稳定 Key()。
  • 指针、颜色和刻度之外,用 Title() 和 A11yLabel() 清楚表达指标名称与单位;RTL 需要反向弧线时才调用 Mirror()。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Gauge 仪表盘与水波图

gallery 演示 ChartGauge 的 Render 函数,源码 page_chart_more.go#L135。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

chart.Gauge ​

go
func Gauge(c *ui.Context, value float64) *GaugeW

Gauge starts a gauge showing value on the range 0..1 (see Range).

chart.Gauge(c, 0.72).Title("完成率").Show()

返回 *GaugeW,可链式调用的方法见 GaugeW 方法。

GaugeW 方法 ​

chart.GaugeW:GaugeW builds a gauge (dial) chart.

方法说明签名
A11yLabelA11yLabel sets the accessible name of the chart (default: its title or the localized chart kind, e.g. "折线图"). (来自嵌入的 common[GaugeW])A11yLabel(s string) *GaugeW
AnimateAnimate enables (default) or disables entry and update transitions. (来自嵌入的 common[GaugeW])Animate(on bool) *GaugeW
BandsBands colors the track by thresholds (upper bounds in scale units, sorted) instead of a single progress arc.Bands(stops []float64, colors ...theme.Color) *GaugeW
ColorsColors overrides the palette. (来自嵌入的 common[GaugeW])Colors(cs ...theme.Color) *GaugeW
FormatFormat sets the value formatter used by the value axis, tooltip and labels (default: thousands separators). (来自嵌入的 common[GaugeW])Format(f Formatter) *GaugeW
HeightHeight sets the chart height in logical pixels (the width follows the parent). (来自嵌入的 common[GaugeW])Height(h float32) *GaugeW
KeyKey gives the chart a stable identity (legend and animation state) when charts are added or removed around it. (来自嵌入的 common[GaugeW])Key(k any) *GaugeW
LabelLabel shows data labels on the marks. (来自嵌入的 common[GaugeW])Label() *GaugeW
LayoutLayout applies layout options to the chart box (Grow, Margin ...). (来自嵌入的 common[GaugeW])Layout(o ...layout.Option) *GaugeW
LegendLegend places (or with LegendNone hides) the legend. (来自嵌入的 common[GaugeW])Legend(p LegendPos) *GaugeW
LoadingLoading shows a spinner over the chart (or in place of it without data). (来自嵌入的 common[GaugeW])Loading(on bool) *GaugeW
MirrorMirror draws the plot right to left: the x axis runs from the right, the value axis sits on the right, bars, lines, areas and points are mirrored (so categories read in reverse order), and pie, donut, radar and gauge start at the mirrored angle and sweep counter-clockwise. (来自嵌入的 common[GaugeW])Mirror() *GaugeW
NoTooltipNoTooltip disables the hover tooltip and crosshair. (来自嵌入的 common[GaugeW])NoTooltip() *GaugeW
RangeRange sets the scale bounds (default 0..1, shown as percentages).Range(lo, hi float64) *GaugeW
ShowShow builds the chart.Show() View
StyleStyle tweaks the resolved chart style for this chart only. (来自嵌入的 common[GaugeW])Style(fn func(*Style)) *GaugeW
TitleTitle sets the caption under the value.Title(s string) *GaugeW

相关链接 ​

以 MIT 许可发布