Skip to content

Statistic 统计数值 ​

突出展示金额、数量、增长值以及倒计时或计时器。

何时使用 ​

  • 仪表盘、概览卡片和详情页中强调关键指标。
  • 需要实时截止时间用 Countdown 或 StatisticTimer;静态数值用 Statistic。
  • 趋势图和多维比较不应塞进单个统计数值,应使用图表。

在线体验 ​

Statistic 统计数值 / Countdown 倒计时 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import "github.com/tangoui/tango/ui"

func page(c *ui.Context) {
    ui.Statistic(c, "活跃用户", 12345, 0).Suffix(" 人")
}

常见用法 ​

前后缀和颜色 ​

go
func money(c *ui.Context) {
    ui.Statistic(c, "收入", 1280.5, 2).Prefix("¥").Color(c.Theme.Success.Base)
}

倒计时 ​

go
import "time"

func deadline(c *ui.Context) {
    ui.Countdown(c, "活动剩余", time.Now().Add(2*time.Hour)).Format("HH:mm:ss")
}

正向计时 ​

go
func elapsed(c *ui.Context) {
    ui.StatisticTimer(c, "运行时长", ui.TimerCountup, time.Now().Add(-5*time.Minute)).Format("HH:mm:ss")
}

主题与样式 ​

Statistic 令牌控制标题、数值、前后缀和加载态颜色;ValueSize 可调整数值字号。倒计时支持 Format、Prefix、Suffix、Color 和 OnChange。

注意事项 ​

倒计时目标时间应稳定保存,不能在每帧重新计算。定时器结束可读取 Finished;高频更新时只在 OnChange 中做轻量工作。颜色需配合标题或数值传达语义。

示例代码 ​

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

Statistic 统计数值 / Countdown 倒计时

gallery 演示 Statistic 的 Render 函数,源码 page_data_misc.go#L113。

加载中…

Statistic 分隔符与精度

gallery 演示 StatisticAlign 的 Render 函数,源码 page_b2g.go#L102。

加载中…

API ​

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

入口函数 ​

ui.Statistic ​

go
func Statistic(c *Context, title string, value float64, precision int) *StatisticW

Statistic adds a statistic display. The value is shown with precision decimals, cut (not rounded) like antd and Element Plus; a negative precision shows the decimals the value has. NaN and ±Inf are shown as "NaN" and "Infinity".

ConfigProvider.Defaults("Statistic", ...) keys: "groupSeparator", "decimalSeparator" (string).

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

ui.Countdown ​

go
func Countdown(c *Context, title string, deadline time.Time) *CountdownW

Countdown adds a countdown to deadline. Format tokens: D, HH, mm, ss, SSS (default "HH:mm:ss"); text in [brackets] is literal.

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

StatisticW 方法 ​

ui.StatisticW:StatisticW shows a labelled number.

方法说明签名
ColorColor(col theme.Color) *StatisticW
CountUpCountUp animates the displayed value from 0 to v (precision digits) over the slow duration ×4 the first time the statistic appears.CountUp(v float64, precision int) *StatisticW
DecimalSeparatorDecimalSeparator sets the decimal point (default ".").DecimalSeparator(sep string) *StatisticW
FormatterFormatter formats the value with fn instead (antd / Element formatter).Formatter(fn func(v float64) string) *StatisticW
GroupSeparatorGroupSeparator sets the thousands separator (default ",").GroupSeparator(sep string) *StatisticW
LayoutLayout applies layout options.Layout(o ...layout.Option) *StatisticW
LoadingLoading replaces the value with a skeleton bar.Loading(on bool) *StatisticW
PrefixPrefix(p string) *StatisticW
PrefixIconPrefixIcon(ic *icon.Icon) *StatisticW
SuffixSuffix(p string) *StatisticW
ValueValue(v string) *StatisticW
ValueSizeValueSize sets the value font size (line height follows).ValueSize(px float32) *StatisticW

CountdownW 方法 ​

ui.CountdownW:CountdownW counts down to a deadline (Statistic.Countdown / el-countdown) or, made by StatisticTimer with TimerCountup, counts up from a start time (Ant Statistic.Timer).

方法说明签名
ColorColor sets the value color.Color(col theme.Color) *CountdownW
FinishedFinished reports (once) that the deadline passed this frame.Finished() bool
FormatFormat sets the display format (see Countdown).Format(f string) *CountdownW
OnChangeOnChange runs fn with the time left (counting up: the time elapsed) whenever the displayed value changes, e.g. every second with the default format.OnChange(fn func(d time.Duration)) *CountdownW
PrefixPrefix / Suffix add text around the value.Prefix(s string) *CountdownW
SuffixSuffix(s string) *CountdownW

相关链接 ​

以 MIT 许可发布