DualAxes 双轴图
在共享分类横轴上同时绘制左轴柱状系列和右轴折线系列,适合明确不同量纲的对照。
何时使用
- 需要在相同月份或阶段上对照数量与比例等不同量纲指标时。
- 柱状系列表达规模、折线系列表达率或趋势时。
- 两个指标可以共用量纲时优先用 Line 折线图 或 Column 柱状图,避免双轴造成误读。
在线体验
Gallery 展示销售额、订单量和转化率的柱线组合,并包含别名、百分比格式和平滑曲线。
基本用法
Column() 添加左轴柱状字段,Line() 添加右轴折线字段;两者共享 X() 分类字段。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
var dualRows = []chart.Row{
{"month": "1月", "sales": 320, "rate": 0.12},
{"month": "2月", "sales": 460, "rate": 0.15},
{"month": "3月", "sales": 390, "rate": 0.13},
}
func page(c *ui.Context) {
chart.DualAxes(c, dualRows).X("month").Column("sales").Line("rate").LineFormat(chart.Percent(0)).Show()
}常见用法
多个柱状字段
一次 Column() 可以接收多个字段,它们会在左轴上分组显示;每个字段都应使用相同量纲。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func dualColumns(c *ui.Context) {
rows := []chart.Row{{"month": "1月", "sales": 320, "orders": 180, "rate": 0.12}, {"month": "2月", "sales": 460, "orders": 260, "rate": 0.15}}
chart.DualAxes(c, rows).X("month").Column("sales", "orders").Line("rate").Show()
}别名和格式化
Alias() 同时影响图例和提示名称;LineFormat() 只格式化右轴和折线值,普通 Format() 适用于左轴。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func namedDualAxes(c *ui.Context) {
rows := []chart.Row{{"month": "1月", "sales": 320, "rate": 0.12}, {"month": "2月", "sales": 460, "rate": 0.15}}
chart.DualAxes(c, rows).X("month").Column("sales").Line("rate").
Alias("sales", "销售额").Alias("rate", "转化率").
Format(chart.Thousands(0)).LineFormat(chart.Percent(0)).Show()
}平滑曲线和高度
Smooth() 只影响右轴折线;Height() 适合在统计卡片下方固定图表高度。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func smoothDualAxes(c *ui.Context) {
rows := []chart.Row{{"month": "1月", "sales": 320, "rate": 0.12}, {"month": "2月", "sales": 460, "rate": 0.15}}
chart.DualAxes(c, rows).X("month").Column("sales").Line("rate").Smooth().Height(340).Show()
}交互和无障碍
默认悬停会同时展示两侧轴上的值;静态报告可用 NoTooltip(),并在 A11yLabel() 中说清两个量纲。
package main
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/chart"
)
func accessibleDualAxes(c *ui.Context) {
rows := []chart.Row{{"month": "1月", "sales": 320, "rate": 0.12}, {"month": "2月", "sales": 460, "rate": 0.15}}
chart.DualAxes(c, rows).X("month").Column("sales").Line("rate").A11yLabel("月度销售额与转化率").NoTooltip().Show()
}主题与样式
DualAxes 使用主题的柱状、折线、轴线和图例令牌;可用 Colors() 固定字段身份,或用 Style(func(*chart.Style) { ... }) 调整单图。两侧轴的文字、别名和单位必须保持高对比度,不能只靠两种颜色区分。
注意事项
- 右轴是 TanGo
DualAxes的真实能力,但双轴容易夸大相关性;若量纲可以统一,请改用单轴图。 Column()和Line()的缺失值在绘制和悬停提示中会被跳过。折线会连接缺失位置两侧的有效点。- 两侧格式器应分别表达单位,例如左轴用
Thousands(0)、右轴用Percent(0);图例别名应包含业务语义。 - 即时模式下数据放在外部状态,条件切换图表时用
Key();RTL 需要反向绘图区时才调用Mirror()。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
chart.DualAxes
func DualAxes(c *ui.Context, data []Row) *DualWDualAxes starts a column + line chart.
chart.DualAxes(c, rows).X("month").Column("sales").Line("rate").Show()
返回 *DualW,可链式调用的方法见 DualW 方法。
DualW 方法
chart.DualW:DualW builds a dual-axes chart: columns on the left axis, lines on the right axis, over a shared category x.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name of the chart (default: its title or the localized chart kind, e.g. "折线图"). (来自嵌入的 common[DualW]) | A11yLabel(s string) *DualW |
Alias | Alias sets the legend / tooltip name of a field. | Alias(field, name string) *DualW |
Animate | Animate enables (default) or disables entry and update transitions. (来自嵌入的 common[DualW]) | Animate(on bool) *DualW |
Colors | Colors overrides the palette. (来自嵌入的 common[DualW]) | Colors(cs ...theme.Color) *DualW |
Column | Column adds fields drawn as grouped columns on the left axis. | Column(fields ...string) *DualW |
Format | Format sets the value formatter used by the value axis, tooltip and labels (default: thousands separators). (来自嵌入的 common[DualW]) | Format(f Formatter) *DualW |
Height | Height sets the chart height in logical pixels (the width follows the parent). (来自嵌入的 common[DualW]) | Height(h float32) *DualW |
Key | Key gives the chart a stable identity (legend and animation state) when charts are added or removed around it. (来自嵌入的 common[DualW]) | Key(k any) *DualW |
Label | Label shows data labels on the marks. (来自嵌入的 common[DualW]) | Label() *DualW |
Layout | Layout applies layout options to the chart box (Grow, Margin ...). (来自嵌入的 common[DualW]) | Layout(o ...layout.Option) *DualW |
Legend | Legend places (or with LegendNone hides) the legend. (来自嵌入的 common[DualW]) | Legend(p LegendPos) *DualW |
Line | Line adds fields drawn as lines on the right axis. | Line(fields ...string) *DualW |
LineFormat | LineFormat formats the right axis and line values. | LineFormat(f Formatter) *DualW |
Loading | Loading shows a spinner over the chart (or in place of it without data). (来自嵌入的 common[DualW]) | Loading(on bool) *DualW |
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[DualW]) | Mirror() *DualW |
NoTooltip | NoTooltip disables the hover tooltip and crosshair. (来自嵌入的 common[DualW]) | NoTooltip() *DualW |
Show | Show builds the chart. | Show() View |
Smooth | Smooth draws the lines as monotone curves. | Smooth() *DualW |
Style | Style tweaks the resolved chart style for this chart only. (来自嵌入的 common[DualW]) | Style(fn func(*Style)) *DualW |
X | X sets the category field. | X(field string) *DualW |