Line 折线图
用连续的线展示一个或多个指标随分类或时间变化的趋势。
何时使用
- 比较月度、日度等有顺序数据的变化趋势。
- 需要同时观察多个系列,并通过图例切换系列时。
- 只有一个离散数值时不要使用折线图,改用 Statistic 统计数值;少量阶段进度可用 Steps 步骤条。
在线体验
Gallery 包含基础、平滑、阶梯、面积、堆叠面积和大数据降采样场景,可切换 Ant Design、Element Plus 及暗色变体。
基本用法
下面示例把行数据映射为横轴 month、数值轴 value,并用 kind 拆分系列;Show() 返回可读取悬停状态的 chart.View。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
var trendRows = []chart.Row{
{"month": "1月", "kind": "本期", "value": 120},
{"month": "2月", "kind": "本期", "value": 168},
{"month": "1月", "kind": "上期", "value": 96},
{"month": "2月", "kind": "上期", "value": 142},
}
func page(c *ui.Context) {
chart.Line(c, trendRows).X("month").Y("value").Series("kind").Show()
}常见用法
平滑曲线和阶梯线
Smooth() 使用平滑曲线,Step() 使用阶梯线;两者都保留同样的字段映射和悬停提示。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func smoothAndStep(c *ui.Context) {
rows := []chart.Row{{"month": "1月", "value": 12}, {"month": "2月", "value": 18}, {"month": "3月", "value": 15}}
chart.Line(c, rows).X("month").Y("value").Smooth().Show()
chart.Line(c, rows).X("month").Y("value").Step().Show()
}面积和堆叠面积
用 Area() 填充线下区域;多个系列需要叠加时同时调用 Series() 和 Stack()。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func areaTrend(c *ui.Context) {
rows := []chart.Row{
{"month": "1月", "channel": "直接访问", "value": 32},
{"month": "1月", "channel": "搜索引擎", "value": 58},
{"month": "2月", "channel": "直接访问", "value": 40},
{"month": "2月", "channel": "搜索引擎", "value": 64},
}
chart.Area(c, rows).X("month").Y("value").Series("channel").Stack().Smooth().Show()
}时间轴和格式化
X() 可以指向 time.Time 字段,组件会使用时间轴;Format() 同时影响数值轴和悬停值。
package main
import (
"time"
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func timeSeries(c *ui.Context) {
rows := []chart.Row{
{"date": time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), "pv": 3200},
{"date": time.Date(2025, 1, 2, 0, 0, 0, 0, time.UTC), "pv": 4100},
}
chart.Line(c, rows).X("date").Y("pv").Format(chart.Thousands(0)).Height(280).Show()
}固定坐标范围和数据点
YMin() / YMax() 固定数值轴边界。Points() 在绘图区有足够空间时为绘制的数据点加上标记。NoTooltip() 隐藏悬停十字线和提示框。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func fixedTrend(c *ui.Context) {
rows := []chart.Row{{"month": "1月", "value": 72}, {"month": "2月", "value": 86}, {"month": "3月", "value": 78}}
chart.Line(c, rows).X("month").Y("value").YMin(0).YMax(100).Points().Show()
}主题与样式
图表样式从当前 c.Theme 派生:Ant Design 使用 G2 风格,Element Plus 使用 ECharts 风格,shadcn 使用对应的中性表面和调色板。单个图表可用 Colors(theme.Hex(...)) 覆盖系列色,或用 Style(func(*chart.Style) { ... }) 调整该图表的令牌;优先保持同一页面的系列颜色顺序稳定。
注意事项
- 即时模式下数据放在页面函数外的稳定变量中;数据替换后下一帧会重绘。条件渲染中增删图表时,用
Key()保持图例和动画状态对应同一张图。 - 多系列图表保留图例,不要只用颜色表达系列含义;可用
A11yLabel("月度访问趋势")设置可访问名称,并确保数值轴语义明确。 - 长折线会按绘图区宽度使用 LTTB 降采样;如果需要逐点查看,拆成更小的范围或改用表格。
- RTL 作用域默认保持数据可视化的从左到右绘图区;只有确实需要镜像绘图区时才调用
Mirror()。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
chart.Line
func Line(c *ui.Context, data []Row) *LineWLine starts a line chart over rows.
chart.Line(c, rows).X("month").Y("value").Series("city").Smooth().Show()
返回 *LineW,可链式调用的方法见 LineW 方法。
chart.Area
func Area(c *ui.Context, data []Row) *LineWArea starts an area chart (a line chart with the area filled).
返回 *LineW,可链式调用的方法见 LineW 方法。
LineW 方法
chart.LineW:LineW builds a line or area chart.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name of the chart (default: its title or the localized chart kind, e.g. "折线图"). (来自嵌入的 common[LineW]) | A11yLabel(s string) *LineW |
Animate | Animate enables (default) or disables entry and update transitions. (来自嵌入的 common[LineW]) | Animate(on bool) *LineW |
Area | Area fills the area below each line. | Area() *LineW |
Colors | Colors overrides the palette. (来自嵌入的 common[LineW]) | Colors(cs ...theme.Color) *LineW |
Format | Format sets the value formatter used by the value axis, tooltip and labels (default: thousands separators). (来自嵌入的 common[LineW]) | Format(f Formatter) *LineW |
Height | Height sets the chart height in logical pixels (the width follows the parent). (来自嵌入的 common[LineW]) | Height(h float32) *LineW |
Key | Key gives the chart a stable identity (legend and animation state) when charts are added or removed around it. (来自嵌入的 common[LineW]) | Key(k any) *LineW |
Label | Label shows data labels on the marks. (来自嵌入的 common[LineW]) | Label() *LineW |
Layout | Layout applies layout options to the chart box (Grow, Margin ...). (来自嵌入的 common[LineW]) | Layout(o ...layout.Option) *LineW |
Legend | Legend places (or with LegendNone hides) the legend. (来自嵌入的 common[LineW]) | Legend(p LegendPos) *LineW |
Loading | Loading shows a spinner over the chart (or in place of it without data). (来自嵌入的 common[LineW]) | Loading(on bool) *LineW |
Log | Log uses a logarithmic value axis. | Log() *LineW |
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[LineW]) | Mirror() *LineW |
NoTooltip | NoTooltip disables the hover tooltip and crosshair. (来自嵌入的 common[LineW]) | NoTooltip() *LineW |
NoZero | NoZero lets the value axis start near the data instead of at zero. (来自嵌入的 xyOpts[LineW]) | NoZero() *LineW |
Points | Points draws a symbol at every data point. | Points() *LineW |
Series | Series sets the field that splits rows into series (legend, colors). (来自嵌入的 xyOpts[LineW]) | Series(field string) *LineW |
Show | Show builds the chart. | Show() View |
Smooth | Smooth draws monotone curves instead of straight segments. | Smooth() *LineW |
Stack | Stack stacks the series (stacked area when combined with Area). | Stack() *LineW |
Step | Step draws a step line (default StepEnd: horizontal, then vertical). | Step(mode ...StepMode) *LineW |
Style | Style tweaks the resolved chart style for this chart only. (来自嵌入的 common[LineW]) | Style(fn func(*Style)) *LineW |
X | X sets the x (category / time / number) field. (来自嵌入的 xyOpts[LineW]) | X(field string) *LineW |
Y | Y sets the value field. (来自嵌入的 xyOpts[LineW]) | Y(field string) *LineW |
YMax | YMax fixes the upper end of the value axis. (来自嵌入的 xyOpts[LineW]) | YMax(v float64) *LineW |
YMin | YMin fixes the lower end of the value axis. (来自嵌入的 xyOpts[LineW]) | YMin(v float64) *LineW |