Statistic 统计数值
突出展示金额、数量、增长值以及倒计时或计时器。
何时使用
- 仪表盘、概览卡片和详情页中强调关键指标。
- 需要实时截止时间用
Countdown或StatisticTimer;静态数值用Statistic。 - 趋势图和多维比较不应塞进单个统计数值,应使用图表。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
func page(c *ui.Context) {
ui.Statistic(c, "活跃用户", 12345, 0).Suffix(" 人")
}常见用法
前后缀和颜色
func money(c *ui.Context) {
ui.Statistic(c, "收入", 1280.5, 2).Prefix("¥").Color(c.Theme.Success.Base)
}倒计时
import "time"
func deadline(c *ui.Context) {
ui.Countdown(c, "活动剩余", time.Now().Add(2*time.Hour)).Format("HH:mm:ss")
}正向计时
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 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Statistic
func Statistic(c *Context, title string, value float64, precision int) *StatisticWStatistic 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
func Countdown(c *Context, title string, deadline time.Time) *CountdownWCountdown 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.
| 方法 | 说明 | 签名 |
|---|---|---|
Color | Color(col theme.Color) *StatisticW | |
CountUp | CountUp 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 |
DecimalSeparator | DecimalSeparator sets the decimal point (default "."). | DecimalSeparator(sep string) *StatisticW |
Formatter | Formatter formats the value with fn instead (antd / Element formatter). | Formatter(fn func(v float64) string) *StatisticW |
GroupSeparator | GroupSeparator sets the thousands separator (default ","). | GroupSeparator(sep string) *StatisticW |
Layout | Layout applies layout options. | Layout(o ...layout.Option) *StatisticW |
Loading | Loading replaces the value with a skeleton bar. | Loading(on bool) *StatisticW |
Prefix | Prefix(p string) *StatisticW | |
PrefixIcon | PrefixIcon(ic *icon.Icon) *StatisticW | |
Suffix | Suffix(p string) *StatisticW | |
Value | Value(v string) *StatisticW | |
ValueSize | ValueSize 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).
| 方法 | 说明 | 签名 |
|---|---|---|
Color | Color sets the value color. | Color(col theme.Color) *CountdownW |
Finished | Finished reports (once) that the deadline passed this frame. | Finished() bool |
Format | Format sets the display format (see Countdown). | Format(f string) *CountdownW |
OnChange | OnChange 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 |
Prefix | Prefix / Suffix add text around the value. | Prefix(s string) *CountdownW |
Suffix | Suffix(s string) *CountdownW |