Skip to content

StatisticCard 指标卡 ​

面向后台仪表盘的指标卡,集中展示数值、趋势、状态、描述和可选的小型图表。

何时使用 ​

  • 在仪表盘或概览页展示销售额、访问量、转化率等关键指标。
  • 需要把数值与同比/环比、状态点、图表或页脚说明放在同一张卡片中时。
  • 只需要一个普通数字时用 Statistic 统计数值;需要通用内容容器时用 ProCard 高级卡片。

在线体验 ​

示例包含基础趋势、状态点、描述、指标组、图标、页脚以及左右放置的小型图表。

StatisticCard 指标卡 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。

go
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 为标题添加说明。

go
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 在分隔线下构建补充信息。页脚回调可以放普通文本、链接或操作。

go
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 等方式参与父级布局。

go
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。

go
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 处理导航或展开详情。

go
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 入门。

StatisticCard 指标卡

gallery 演示 StatisticCard 的具名 Render 函数,源码 page_pro2_card.go#L104。

加载中…

StatisticCard v3 布局与子部件

gallery 演示 StatisticCardAlign 的 Render 函数,源码 page_b4a2.go#L11。

加载中…

API ​

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

入口函数 ​

ui.StatisticCard ​

go
func StatisticCard(c *Context, title string) *StatisticCardW

StatisticCard starts a statistic card titled title (the statistic's label; use CardTitle for a card header). Chain options, then Show.

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

ui.StatisticCardGroup ​

go
func StatisticCardGroup(c *Context) *ProCardW

StatisticCardGroup builds StatisticCard.Group: a ProCard with zero body padding. Use Divider for explicit separators, or Split for automatic ones.

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

ui.ProStatistic ​

go
func ProStatistic(c *Context, title string) *ProStatisticW

ProStatistic 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 ​

go
func StatisticCardDivider(c *Context, orientation string) *StatisticCardDividerW

StatisticCardDivider builds StatisticCard.Divider. Empty orientation reads the Defaults key "orientation" and otherwise defaults to vertical.

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

ui.StatisticCardOperation ​

go
func StatisticCardOperation(c *Context, value string) *StatisticCardOperationW

StatisticCardOperation builds StatisticCard.Operation using heading-4 typography and Pro's marginLG block spacing.

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

StatisticCardW 方法 ​

ui.StatisticCardW:StatisticCardW builds a StatisticCard.

方法说明签名
BorderedBordered draws a border.Bordered() *StatisticCardW
CardCard returns the underlying ProCard (valid after Show for queries).Card() *ProCardW
CardTitleCardTitle shows a card header with title (the statistic keeps its own).CardTitle(s string) *StatisticCardW
ChartChart builds the chart area (a chart.Tiny*, any chart, or content).Chart(fn func()) *StatisticCardW
ChartPlaceChartPlace puts the chart below (ChartBottom, default), left or right of the statistic.ChartPlace(p string) *StatisticCardW
ClickedClicked reports a click on a hoverable card.Clicked() bool
ColSpanColSpan sizes the card in a parent ProCard / StatisticCardGroup row.ColSpan(span int) *StatisticCardW
DescDesc adds an inline statistic under the value ("Week 12% ▲"); trend is "up", "down" or "".Desc(title, value, trend string) *StatisticCardW
DescriptionDescription builds custom content under the value.Description(fn func()) *StatisticCardW
ExtraExtra builds the header actions.Extra(fn func()) *StatisticCardW
FooterFooter builds a footer below a divider.Footer(fn func()) *StatisticCardW
GhostGhost removes the card background and outer padding.Ghost() *StatisticCardW
HoverableHoverable lifts the card on hover.Hoverable() *StatisticCardW
IconIcon shows an icon left of the statistic.Icon(ic *icon.Icon) *StatisticCardW
LayoutLayout applies layout options to the card.Layout(o ...layout.Option) *StatisticCardW
LoadingLoading shows the card's loading placeholder.Loading(on bool) *StatisticCardW
PrefixPrefix sets text before the value (e.g. a currency sign).Prefix(s string) *StatisticCardW
ShowShow builds the card.Show() *StatisticCardW
StatisticLayoutStatisticLayout sets the statistic layout: vertical (default), horizontal (title and value at opposite ends), or inline (compact, secondary value).StatisticLayout(mode string) *StatisticCardW
StatusStatus shows a colored dot before the title ("success", "processing", "warning", "error", "default").Status(s string) *StatisticCardW
SuffixSuffix sets text after the value (e.g. a unit).Suffix(s string) *StatisticCardW
TooltipTooltip adds a question icon after the title showing tip on hover.Tooltip(tip string) *StatisticCardW
TrendTrend marks the value as rising ("up") or falling ("down").Trend(dir string) *StatisticCardW
TrendColorsTrendColors overrides the rising / falling colors (default: error red for up, success green for down, as in Pro).TrendColors(up, down theme.Color) *StatisticCardW
ValueValue sets a number formatted with thousands separators.Value(v float64, precision int) *StatisticCardW
ValueColorValueColor colors the value.ValueColor(col theme.Color) *StatisticCardW
ValueTextValueText sets the value as preformatted text.ValueText(s string) *StatisticCardW

ProCardW 方法 ​

ui.ProCardW:ProCardW builds a ProCard.

方法说明签名
ActionsActions 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
BodyBody builds the card with fn as its content.Body(fn func()) *ProCardW
BodyPaddingBodyPadding overrides the body padding.BodyPadding(in geom.Insets) *ProCardW
BorderedBordered draws a border around the card (the default; it overrides a "bordered": false ConfigProvider default).Bordered() *ProCardW
BorderlessBorderless removes the border (Pro v3 variant "borderless").Borderless() *ProCardW
BoxBox returns the card container (valid after Body).Box() *Box
BoxShadowBoxShadow gives the card the tertiary shadow and hides its border (Pro v3 boxShadow).BoxShadow(on bool) *ProCardW
CenteredCentered centres the body content on both axes.Centered() *ProCardW
ClickedClicked reports a click on a hoverable card.Clicked() bool
ColSpanColSpan sizes the card inside a parent ProCard row: span of 24 columns.ColSpan(span int) *ProCardW
ColWidthColWidth gives the card a fixed width inside a parent ProCard row.ColWidth(px float32) *ProCardW
CollapsedCollapsed binds the folded state (implies Collapsible).Collapsed(v *bool) *ProCardW
CollapsibleCollapsible lets a click on the header fold the body.Collapsible() *ProCardW
DefaultCollapsedDefaultCollapsed starts an uncontrolled collapsible card folded.DefaultCollapsed() *ProCardW
DirectionDirection sets how child ProCards are arranged: "row" (default) or "column".Direction(d string) *ProCardW
ExtraExtra builds the actions at the right of the header.Extra(fn func()) *ProCardW
GhostGhost removes the background and the body padding.Ghost() *ProCardW
GutterGutter sets the space between child ProCards.Gutter(px float32) *ProCardW
HeaderBorderedHeaderBordered draws a divider under the header.HeaderBordered() *ProCardW
HoverableHoverable lifts the card with a shadow on hover.Hoverable() *ProCardW
IDID returns the card's widget ID (valid after Body).ID() ID
IsCollapsedIsCollapsed reports whether the body is folded.IsCollapsed() bool
LayoutLayout applies layout options to the card.Layout(o ...layout.Option) *ProCardW
LoadingLoading shows the animated loading placeholder instead of the body.Loading(on bool) *ProCardW
SmallSmall uses the compact paddings.Small() *ProCardW
SplitSplit lays the child ProCards out on one surface separated by dividers (SplitVertical or SplitHorizontal).Split(dir string) *ProCardW
SubTitleSubTitle sets the secondary title shown after the title.SubTitle(s string) *ProCardW
TabChangedTabChanged reports a tab switch this frame.TabChanged() bool
TabExtraTabExtra builds content at the right of the tab bar.TabExtra(fn func()) *ProCardW
TabsTabs 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
TabsCardTabsCard uses card-style tabs.TabsCard() *ProCardW
ToggledToggled reports a collapse / expand by the user this frame.Toggled() bool
TooltipTooltip adds a question icon after the title showing tip on hover.Tooltip(tip string) *ProCardW
WrapWrap 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.

方法说明签名
DescriptionDescription builds description content.Description(fn func()) *ProStatisticW
IconIcon adds a leading icon.Icon(ic *icon.Icon) *ProStatisticW
PrefixPrefix adds text before the value.Prefix(s string) *ProStatisticW
ShowShow builds a standalone statistic.Show() *ProStatisticW
StatisticLayoutStatisticLayout sets vertical, horizontal or inline layout.StatisticLayout(s string) *ProStatisticW
StatusStatus adds a status dot.Status(s string) *ProStatisticW
SuffixSuffix adds text after the value.Suffix(s string) *ProStatisticW
TooltipTooltip adds a title tooltip.Tooltip(s string) *ProStatisticW
TrendTrend sets the trend direction.Trend(s string) *ProStatisticW
TrendColorsTrendColors overrides the trend colors.TrendColors(up, down theme.Color) *ProStatisticW
ValueValue formats a numeric value.Value(v float64, precision int) *ProStatisticW
ValueColorValueColor overrides the value color.ValueColor(col theme.Color) *ProStatisticW
ValueTextValueText sets a preformatted value.ValueText(s string) *ProStatisticW

StatisticCardDividerW 方法 ​

ui.StatisticCardDividerW:StatisticCardDividerW is an explicit Pro statistic divider.

方法说明签名
LayoutLayout applies layout options to the divider.Layout(opts ...layout.Option) *StatisticCardDividerW

StatisticCardOperationW 方法 ​

ui.StatisticCardOperationW:StatisticCardOperationW is an operator between statistics.

方法说明签名
LayoutLayout applies layout options to the operation.Layout(opts ...layout.Option) *StatisticCardOperationW

相关链接 ​

以 MIT 许可发布