TinyLine 迷你图
在统计卡片等紧凑区域内展示没有坐标轴和图例的趋势线、面积、迷你柱或进度条。
何时使用
- 在 Statistic、ProCard 或摘要卡片中补充短期趋势。
- 需要用很小的空间表达方向,不需要读精确坐标时。
- 需要完整坐标、图例和多系列比较时改用 Line 折线图 或 Column 柱状图。
在线体验
Gallery 展示 TinyLine、TinyArea、TinyColumn、TinyProgress,以及空状态和加载态组合。
基本用法
TinyLine() 接收 []float64,不绘制坐标轴或图例;Smooth() 让曲线更平滑。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
var visits = []float64{264, 417, 438, 887, 309, 397, 550, 575}
func page(c *ui.Context) {
chart.TinyLine(c, visits).Smooth().Show()
}常见用法
面积和迷你柱
TinyArea() 填充趋势线下方区域,TinyColumn() 用紧凑柱形显示各点;它们都共享同一组数值输入。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func tinyVariants(c *ui.Context) {
vals := []float64{264, 417, 438, 887, 309, 397, 550, 575}
chart.TinyArea(c, vals).Smooth().Show()
chart.TinyColumn(c, vals).Show()
}Tooltip 标签
Names() 为每个点提供悬停标签,例如日期;标签数量可以少于数据点,未提供的点不显示名称。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func namedTinyLine(c *ui.Context) {
vals := []float64{12, 18, 15}
chart.TinyLine(c, vals).Names("周一", "周二", "周三").Show()
}进度和目标线
TinyProgress() 接收 0 到 1 的比例,可用 Target() 添加同一比例范围内的目标标记线。迷你折线和面积图上的 Target() 使用与数据相同的单位。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func tinyProgress(c *ui.Context) {
chart.TinyProgress(c, 0.78).Target(0.85).A11yLabel("完成率 78%,目标 85%").Show()
}颜色和状态
Color() 只覆盖当前迷你图的标记颜色;如果数据表示状态,仍要在卡片文字或图标中写出状态。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
"github.com/tangoui/tango/ui/theme"
)
func coloredTiny(c *ui.Context) {
chart.TinyLine(c, []float64{8, 10, 9, 13}).Color(theme.Hex("#1677ff")).NoTooltip().Show()
}主题与样式
Tiny 图表复用当前主题的首个系列色、轨道色和 tooltip;Color() 适合局部强调,Colors() 或 Style(func(*chart.Style) { ... }) 可用于更系统的单图覆盖。它没有坐标轴和图例,颜色不能承载唯一语义。
注意事项
- Tiny 图表刻意省略坐标轴,精确数值应放在卡片标题、统计值、Tooltip 或可展开的完整图表中。
TinyProgress()和Target()使用 0 到 1 的比例,不要传入 78 这样的百分数;超出范围的绘制会被限制,但业务值仍应在外部校验。- 即时模式下数组放在外部状态;更新时替换数组或更新状态后重绘,条件切换多个卡片时使用
Key()。 Names()的顺序必须与数值顺序一致;为动态数据设置A11yLabel(),并提供文字化趋势或状态说明。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
chart.TinyLine
func TinyLine(c *ui.Context, vals []float64) *TinyWTinyLine starts a sparkline.
返回 *TinyW,可链式调用的方法见 TinyW 方法。
chart.TinyArea
func TinyArea(c *ui.Context, vals []float64) *TinyWTinyArea starts a filled sparkline.
返回 *TinyW,可链式调用的方法见 TinyW 方法。
chart.TinyColumn
func TinyColumn(c *ui.Context, vals []float64) *TinyWTinyColumn starts a mini column chart.
返回 *TinyW,可链式调用的方法见 TinyW 方法。
chart.TinyProgress
func TinyProgress(c *ui.Context, ratio float64) *TinyWTinyProgress starts a thin progress bar (ratio 0..1).
返回 *TinyW,可链式调用的方法见 TinyW 方法。
TinyW 方法
chart.TinyW:TinyW builds a sparkline for statistic cards: no axes or legend, a compact tooltip on hover.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name of the chart (default: its title or the localized chart kind, e.g. "折线图"). (来自嵌入的 common[TinyW]) | A11yLabel(s string) *TinyW |
Animate | Animate enables (default) or disables entry and update transitions. (来自嵌入的 common[TinyW]) | Animate(on bool) *TinyW |
Color | Color sets the mark color (default: first palette color). | Color(col theme.Color) *TinyW |
Colors | Colors overrides the palette. (来自嵌入的 common[TinyW]) | Colors(cs ...theme.Color) *TinyW |
Format | Format sets the value formatter used by the value axis, tooltip and labels (default: thousands separators). (来自嵌入的 common[TinyW]) | Format(f Formatter) *TinyW |
Height | Height sets the chart height in logical pixels (the width follows the parent). (来自嵌入的 common[TinyW]) | Height(h float32) *TinyW |
Key | Key gives the chart a stable identity (legend and animation state) when charts are added or removed around it. (来自嵌入的 common[TinyW]) | Key(k any) *TinyW |
Label | Label shows data labels on the marks. (来自嵌入的 common[TinyW]) | Label() *TinyW |
Layout | Layout applies layout options to the chart box (Grow, Margin ...). (来自嵌入的 common[TinyW]) | Layout(o ...layout.Option) *TinyW |
Legend | Legend places (or with LegendNone hides) the legend. (来自嵌入的 common[TinyW]) | Legend(p LegendPos) *TinyW |
Loading | Loading shows a spinner over the chart (or in place of it without data). (来自嵌入的 common[TinyW]) | Loading(on bool) *TinyW |
Mirror | Mirror 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[TinyW]) | Mirror() *TinyW |
Names | Names labels the points in the tooltip (e.g. dates). | Names(names ...string) *TinyW |
NoTooltip | NoTooltip disables the hover tooltip and crosshair. (来自嵌入的 common[TinyW]) | NoTooltip() *TinyW |
Show | Show builds the chart. | Show() View |
Smooth | Smooth draws the line as a monotone curve. | Smooth() *TinyW |
Style | Style tweaks the resolved chart style for this chart only. (来自嵌入的 common[TinyW]) | Style(fn func(*Style)) *TinyW |
Target | Target draws a marker line at a target value (progress: ratio). | Target(v float64) *TinyW |