StatisticCard 指标卡
面向后台仪表盘的指标卡,集中展示数值、趋势、状态、描述和可选的小型图表。
何时使用
- 在仪表盘或概览页展示销售额、访问量、转化率等关键指标。
- 需要把数值与同比/环比、状态点、图表或页脚说明放在同一张卡片中时。
- 只需要一个普通数字时用 Statistic 统计数值;需要通用内容容器时用 ProCard 高级卡片。
在线体验
示例包含基础趋势、状态点、描述、指标组、图标、页脚以及左右放置的小型图表。
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import "github.com/tangoui/tango/ui"
func page(c *ui.Context) {
ui.StatisticCard(c, "总销售额").Value(126560, 0).Prefix("¥").Show()
}Value(value, precision) 按精度格式化数字并添加千位分隔符,Prefix 和 Suffix 分别放在数字前后。链式设置完成后调用 Show() 构建卡片;title 是指标标题,不是卡片头部标题。
常见用法
以下片段省略了 import。
趋势、状态与说明
Trend 接受 up 或 down,Desc 添加指标下方的比较项,Status 在标题前显示状态点,Tooltip 为标题添加说明。
func trendCard(c *ui.Context) {
ui.StatisticCard(c, "运营活动效果").Tooltip("近 30 天活动带来的转化效果").
Value(78, 0).Suffix("%").Trend("up").
Desc("周同比", "12%", "up").Desc("日同比", "11%", "down").
Status("processing").Show()
}图标、颜色与页脚
Icon 在指标左侧放置图标,ValueColor 修改数值颜色,Footer 在分隔线下构建补充信息。页脚回调可以放普通文本、链接或操作。
func decoratedCard(c *ui.Context) {
ui.StatisticCard(c, "支付金额").Icon(icon.Shop).Value(2176, 0).Prefix("¥").
ValueColor(c.Theme.Primary.Base).
Footer(func() {
ui.Text(c, "今日累计 · 12:00 更新").Tertiary()
}).Show()
}指标组
StatisticCardGroup 创建带分隔线的 ProCard,直接在 Body 中放多个指标卡。单张卡可以用 ColSpan、Layout 等方式参与父级布局。
func metricGroup(c *ui.Context) {
ui.StatisticCardGroup(c).Body(func() {
ui.StatisticCard(c, "今日 UV").Value(79, 0).Desc("较昨日", "8.04%", "up").Show()
ui.StatisticCard(c, "订单数").Value(3701928, 0).Status("success").Show()
ui.StatisticCard(c, "转化率").Value(6.8, 1).Suffix("%").Show()
})
}接入小型图表
Chart 接受一个构建任意内容的回调。StatisticCard 不直接依赖 ui/chart,因此图表包需要由页面显式导入;ChartPlace 可使用 ChartBottom、ChartLeft 或 ChartRight。
func chartCard(c *ui.Context) {
values := []float64{264, 417, 438, 887, 309, 397, 550}
ui.StatisticCard(c, "访问量").Value(8846, 0).
ChartPlace(ui.ChartRight).
Chart(func() { chart.TinyArea(c, values).Smooth().Show() }).
Layout(layout.W(420)).Show()
}加载和可点击卡片
Loading(true) 显示卡片加载占位;Hoverable 让卡片具有悬浮反馈,点击后通过 Clicked 处理导航或展开详情。
var loadingMetric = true
func interactiveCard(c *ui.Context) {
card := ui.StatisticCard(c, "待处理任务").Value(24, 0).
Loading(loadingMetric).Hoverable().Show()
if card.Clicked() {
ui.Text(c, "打开任务列表").Tertiary()
}
}主题与样式
StatisticCard 使用 ProCard 和 theme.Components.Statistic 的指标令牌:标题字号、数值字号、行高和数值颜色由当前主题决定。Ant Design 使用轻量的指标标题与醒目数字;Element Plus 采用 Element 卡片边框;shadcn 使用更重的数值字重和卡片表面。
ConfigProvider.Defaults("StatisticCard", ...) 支持 "bordered"、"hoverable" 和 "chartPlacement"(bottom、left、right)。实例上的 Bordered、Hoverable 和 ChartPlace 优先。
注意事项
- 调用顺序:
Chart、Footer、CardTitle、Layout等配置在Show前调用;Clicked在Show返回后读取。 - 状态放在哪:指标值、请求加载状态、趋势方向和当前选中卡片由页面状态持有;不要在每帧计算中随机生成指标。
- 图表性能:卡片内图表适合小型趋势概览;数据量很大时先在应用层聚合,避免每张卡都重复计算全量数据。
- 无障碍:标题、数值和趋势会作为卡片内容呈现;图表回调仍应提供可读的文字摘要或配套说明。可点击卡片要有明确标题和操作结果。
- RTL:前缀、数值、后缀和左右图表布局遵循当前 RTL 方向。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.StatisticCard
func StatisticCard(c *Context, title string) *StatisticCardWStatisticCard starts a statistic card titled title (the statistic's label; use CardTitle for a card header). Chain options, then Show.
返回 *StatisticCardW,可链式调用的方法见 StatisticCardW 方法。
ui.StatisticCardGroup
func StatisticCardGroup(c *Context) *ProCardWStatisticCardGroup builds StatisticCard.Group: a ProCard with zero body padding. Use Divider for explicit separators, or Split for automatic ones.
返回 *ProCardW,可链式调用的方法见 ProCardW 方法。
ui.ProStatistic
func ProStatistic(c *Context, title string) *ProStatisticWProStatistic starts a compact Pro statistic. ui.Statistic is the antd statistic; this entry adds Pro layout, descriptions, status, tip and trend.
返回 *ProStatisticW,可链式调用的方法见 ProStatisticW 方法。
ui.StatisticCardDivider
func StatisticCardDivider(c *Context, orientation string) *StatisticCardDividerWStatisticCardDivider builds StatisticCard.Divider. Empty orientation reads the Defaults key "orientation" and otherwise defaults to vertical.
返回 *StatisticCardDividerW,可链式调用的方法见 StatisticCardDividerW 方法。
ui.StatisticCardOperation
func StatisticCardOperation(c *Context, value string) *StatisticCardOperationWStatisticCardOperation builds StatisticCard.Operation using heading-4 typography and Pro's marginLG block spacing.
返回 *StatisticCardOperationW,可链式调用的方法见 StatisticCardOperationW 方法。
StatisticCardW 方法
ui.StatisticCardW:StatisticCardW builds a StatisticCard.
| 方法 | 说明 | 签名 |
|---|---|---|
Bordered | Bordered draws a border. | Bordered() *StatisticCardW |
Card | Card returns the underlying ProCard (valid after Show for queries). | Card() *ProCardW |
CardTitle | CardTitle shows a card header with title (the statistic keeps its own). | CardTitle(s string) *StatisticCardW |
Chart | Chart builds the chart area (a chart.Tiny*, any chart, or content). | Chart(fn func()) *StatisticCardW |
ChartPlace | ChartPlace puts the chart below (ChartBottom, default), left or right of the statistic. | ChartPlace(p string) *StatisticCardW |
Clicked | Clicked reports a click on a hoverable card. | Clicked() bool |
ColSpan | ColSpan sizes the card in a parent ProCard / StatisticCardGroup row. | ColSpan(span int) *StatisticCardW |
Desc | Desc adds an inline statistic under the value ("Week 12% ▲"); trend is "up", "down" or "". | Desc(title, value, trend string) *StatisticCardW |
Description | Description builds custom content under the value. | Description(fn func()) *StatisticCardW |
Extra | Extra builds the header actions. | Extra(fn func()) *StatisticCardW |
Footer | Footer builds a footer below a divider. | Footer(fn func()) *StatisticCardW |
Ghost | Ghost removes the card background and outer padding. | Ghost() *StatisticCardW |
Hoverable | Hoverable lifts the card on hover. | Hoverable() *StatisticCardW |
Icon | Icon shows an icon left of the statistic. | Icon(ic *icon.Icon) *StatisticCardW |
Layout | Layout applies layout options to the card. | Layout(o ...layout.Option) *StatisticCardW |
Loading | Loading shows the card's loading placeholder. | Loading(on bool) *StatisticCardW |
Prefix | Prefix sets text before the value (e.g. a currency sign). | Prefix(s string) *StatisticCardW |
Show | Show builds the card. | Show() *StatisticCardW |
StatisticLayout | StatisticLayout sets the statistic layout: vertical (default), horizontal (title and value at opposite ends), or inline (compact, secondary value). | StatisticLayout(mode string) *StatisticCardW |
Status | Status shows a colored dot before the title ("success", "processing", "warning", "error", "default"). | Status(s string) *StatisticCardW |
Suffix | Suffix sets text after the value (e.g. a unit). | Suffix(s string) *StatisticCardW |
Tooltip | Tooltip adds a question icon after the title showing tip on hover. | Tooltip(tip string) *StatisticCardW |
Trend | Trend marks the value as rising ("up") or falling ("down"). | Trend(dir string) *StatisticCardW |
TrendColors | TrendColors overrides the rising / falling colors (default: error red for up, success green for down, as in Pro). | TrendColors(up, down theme.Color) *StatisticCardW |
Value | Value sets a number formatted with thousands separators. | Value(v float64, precision int) *StatisticCardW |
ValueColor | ValueColor colors the value. | ValueColor(col theme.Color) *StatisticCardW |
ValueText | ValueText sets the value as preformatted text. | ValueText(s string) *StatisticCardW |
ProCardW 方法
ui.ProCardW:ProCardW builds a ProCard.
| 方法 | 说明 | 签名 |
|---|---|---|
Actions | Actions adds the bottom action bar (Ant ProCard actions): each fn builds one equal-width slot, separated by dividers (CardAction makes an icon action). | Actions(fns ...func()) *ProCardW |
Body | Body builds the card with fn as its content. | Body(fn func()) *ProCardW |
BodyPadding | BodyPadding overrides the body padding. | BodyPadding(in geom.Insets) *ProCardW |
Bordered | Bordered draws a border around the card (the default; it overrides a "bordered": false ConfigProvider default). | Bordered() *ProCardW |
Borderless | Borderless removes the border (Pro v3 variant "borderless"). | Borderless() *ProCardW |
Box | Box returns the card container (valid after Body). | Box() *Box |
BoxShadow | BoxShadow gives the card the tertiary shadow and hides its border (Pro v3 boxShadow). | BoxShadow(on bool) *ProCardW |
Centered | Centered centres the body content on both axes. | Centered() *ProCardW |
Clicked | Clicked reports a click on a hoverable card. | Clicked() bool |
ColSpan | ColSpan sizes the card inside a parent ProCard row: span of 24 columns. | ColSpan(span int) *ProCardW |
ColWidth | ColWidth gives the card a fixed width inside a parent ProCard row. | ColWidth(px float32) *ProCardW |
Collapsed | Collapsed binds the folded state (implies Collapsible). | Collapsed(v *bool) *ProCardW |
Collapsible | Collapsible lets a click on the header fold the body. | Collapsible() *ProCardW |
DefaultCollapsed | DefaultCollapsed starts an uncontrolled collapsible card folded. | DefaultCollapsed() *ProCardW |
Direction | Direction sets how child ProCards are arranged: "row" (default) or "column". | Direction(d string) *ProCardW |
Extra | Extra builds the actions at the right of the header. | Extra(fn func()) *ProCardW |
Ghost | Ghost removes the background and the body padding. | Ghost() *ProCardW |
Gutter | Gutter sets the space between child ProCards. | Gutter(px float32) *ProCardW |
HeaderBordered | HeaderBordered draws a divider under the header. | HeaderBordered() *ProCardW |
Hoverable | Hoverable lifts the card with a shadow on hover. | Hoverable() *ProCardW |
ID | ID returns the card's widget ID (valid after Body). | ID() ID |
IsCollapsed | IsCollapsed reports whether the body is folded. | IsCollapsed() bool |
Layout | Layout applies layout options to the card. | Layout(o ...layout.Option) *ProCardW |
Loading | Loading shows the animated loading placeholder instead of the body. | Loading(on bool) *ProCardW |
Small | Small uses the compact paddings. | Small() *ProCardW |
Split | Split lays the child ProCards out on one surface separated by dividers (SplitVertical or SplitHorizontal). | Split(dir string) *ProCardW |
SubTitle | SubTitle sets the secondary title shown after the title. | SubTitle(s string) *ProCardW |
TabChanged | TabChanged reports a tab switch this frame. | TabChanged() bool |
TabExtra | TabExtra builds content at the right of the tab bar. | TabExtra(fn func()) *ProCardW |
Tabs | Tabs turns the card into a tab card bound to active; the active tab's Body is shown as the card body. | Tabs(active *string, tabs []ProCardTab) *ProCardW |
TabsCard | TabsCard uses card-style tabs. | TabsCard() *ProCardW |
Toggled | Toggled reports a collapse / expand by the user this frame. | Toggled() bool |
Tooltip | Tooltip adds a question icon after the title showing tip on hover. | Tooltip(tip string) *ProCardW |
Wrap | Wrap lets child ProCards wrap onto new lines. | Wrap() *ProCardW |
ProStatisticW 方法
ui.ProStatisticW:ProStatisticW is the standalone Pro statistic (StatisticCard.Statistic). It shares StatisticCard's statistic renderer without adding a card surface.
| 方法 | 说明 | 签名 |
|---|---|---|
Description | Description builds description content. | Description(fn func()) *ProStatisticW |
Icon | Icon adds a leading icon. | Icon(ic *icon.Icon) *ProStatisticW |
Prefix | Prefix adds text before the value. | Prefix(s string) *ProStatisticW |
Show | Show builds a standalone statistic. | Show() *ProStatisticW |
StatisticLayout | StatisticLayout sets vertical, horizontal or inline layout. | StatisticLayout(s string) *ProStatisticW |
Status | Status adds a status dot. | Status(s string) *ProStatisticW |
Suffix | Suffix adds text after the value. | Suffix(s string) *ProStatisticW |
Tooltip | Tooltip adds a title tooltip. | Tooltip(s string) *ProStatisticW |
Trend | Trend sets the trend direction. | Trend(s string) *ProStatisticW |
TrendColors | TrendColors overrides the trend colors. | TrendColors(up, down theme.Color) *ProStatisticW |
Value | Value formats a numeric value. | Value(v float64, precision int) *ProStatisticW |
ValueColor | ValueColor overrides the value color. | ValueColor(col theme.Color) *ProStatisticW |
ValueText | ValueText sets a preformatted value. | ValueText(s string) *ProStatisticW |
StatisticCardDividerW 方法
ui.StatisticCardDividerW:StatisticCardDividerW is an explicit Pro statistic divider.
| 方法 | 说明 | 签名 |
|---|---|---|
Layout | Layout applies layout options to the divider. | Layout(opts ...layout.Option) *StatisticCardDividerW |
StatisticCardOperationW 方法
ui.StatisticCardOperationW:StatisticCardOperationW is an operator between statistics.
| 方法 | 说明 | 签名 |
|---|---|---|
Layout | Layout applies layout options to the operation. | Layout(opts ...layout.Option) *StatisticCardOperationW |