Skip to content

Pie 饼图与环形图 ​

用扇区角度表示各分类占整体的比例,Donut() 在中心留下统计区域。

何时使用 ​

  • 分类数量较少且重点是各部分占整体的比例。
  • 需要在环形图中心放总量、当前筛选结果等摘要时。
  • 分类较多、需要精确比较绝对值时不要使用饼图,改用 Column 柱状图;只有一个分类时改用 Progress 进度条。

在线体验 ​

Gallery 展示带引导线的饼图、中心统计环形图、内部百分比和环形图标签。

Pie 饼图与环形图 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

Angle() 指定数值字段,Color() 指定扇区名称字段;相同名称的行会自动求和。

go
package main

import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/chart"
)

var channelRows = []chart.Row{
    {"type": "搜索引擎", "value": 1048},
    {"type": "直接访问", "value": 735},
    {"type": "邮件营销", "value": 580},
}

func page(c *ui.Context) {
    chart.Pie(c, channelRows).Angle("value").Color("type").Show()
}

常见用法 ​

环形图和中心统计 ​

Donut() 等价于带默认内圈的 Pie()。中心默认显示可见扇区的合计,悬停扇区时改为该分类的名称和值。Center() 固定显示指定的标题和值。

go
package main

import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/chart"
)

func donutSummary(c *ui.Context) {
    rows := []chart.Row{{"type": "新客", "value": 420}, {"type": "老客", "value": 680}}
    chart.Donut(c, rows).Angle("value").Color("type").Center("访问量", "1,100").Show()
}

标签和内部百分比 ​

默认会在外侧绘制分类标签和引导线;NoLabel() 隐藏它们,InnerLabel() 把百分比放入足够大的扇区内部。

go
package main

import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/chart"
)

func pieLabels(c *ui.Context) {
    rows := []chart.Row{{"type": "A", "value": 50}, {"type": "B", "value": 30}, {"type": "C", "value": 20}}
    chart.Pie(c, rows).Angle("value").Color("type").InnerLabel().Height(280).Show()
}

隐藏图例与格式化 ​

Legend(chart.LegendNone) 隐藏图例,适用于标签已经完整表达分类的场景;Format() 控制悬停值和中心总量的格式。

go
package main

import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/chart"
)

func compactPie(c *ui.Context) {
    rows := []chart.Row{{"type": "搜索引擎", "value": 104800}, {"type": "直接访问", "value": 73500}}
    chart.Pie(c, rows).Angle("value").Color("type").Legend(chart.LegendNone).Format(chart.Thousands(0)).Show()
}

镜像和交互 ​

RTL 场景默认只镜像外围文字和图例;需要扇区逆时针绘制时显式调用 Mirror()。悬停提示默认开启,需要静态展示时可用 NoTooltip()。

go
package main

import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/chart"
)

func mirroredPie(c *ui.Context) {
    rows := []chart.Row{{"type": "已完成", "value": 72}, {"type": "处理中", "value": 28}}
    chart.Donut(c, rows).Angle("value").Color("type").Mirror().NoTooltip().Show()
}

主题与样式 ​

Pie 使用主题 chart style 的分类色板、扇区分隔线和提示卡片。可用 Colors(theme.Hex("#...")...) 覆盖当前图表色板,或用 Style(func(*chart.Style) { ... }) 调整局部令牌;不要通过颜色顺序表达动态排名,类别名称应同时出现在标签、图例或提示中。

注意事项 ​

  • 饼图至少需要两个有意义的分类;分类过多时合并为“其他”或改用柱状图,避免引导线和文字拥挤。
  • 数据行中的 Angle() 值必须为正数才会绘制;相同 Color() 值会累加,空数据会进入组件空状态。
  • 图表状态由即时模式按组件位置保留;条件增删图表时给它稳定的 Key(),筛选后仍应保持分类颜色稳定。
  • A11yLabel() 设置图表可访问名称,图例或标签提供非颜色的分类信息;LegendNone 只在外侧标签足够清晰时使用。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Pie 饼图与环形图

gallery 演示 ChartPie 的 Render 函数,源码 page_chart_pie.go#L18。

加载中…

API ​

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

入口函数 ​

chart.Pie ​

go
func Pie(c *ui.Context, data []Row) *PieW

Pie starts a pie chart: Angle is the value field, Color the slice name field. Rows with the same name are summed.

chart.Pie(c, rows).Angle("value").Color("type").Inner(0.6).Show()

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

chart.Donut ​

go
func Donut(c *ui.Context, data []Row) *PieW

Donut starts a donut (ring) chart with a centre statistic.

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

PieW 方法 ​

chart.PieW:PieW builds a pie or donut chart.

方法说明签名
A11yLabelA11yLabel sets the accessible name of the chart (default: its title or the localized chart kind, e.g. "折线图"). (来自嵌入的 common[PieW])A11yLabel(s string) *PieW
AngleAngle sets the value field.Angle(field string) *PieW
AnimateAnimate enables (default) or disables entry and update transitions. (来自嵌入的 common[PieW])Animate(on bool) *PieW
CenterCenter sets the donut centre statistic (default: total of the visible slices; empty strings hide it).Center(title, value string) *PieW
ColorColor sets the slice name field.Color(field string) *PieW
ColorsColors overrides the palette. (来自嵌入的 common[PieW])Colors(cs ...theme.Color) *PieW
FormatFormat sets the value formatter used by the value axis, tooltip and labels (default: thousands separators). (来自嵌入的 common[PieW])Format(f Formatter) *PieW
HeightHeight sets the chart height in logical pixels (the width follows the parent). (来自嵌入的 common[PieW])Height(h float32) *PieW
InnerInner sets the hole radius as a fraction of the outer radius (donut).Inner(r float64) *PieW
InnerLabelInnerLabel draws percentages inside the slices instead of outer labels.InnerLabel() *PieW
KeyKey gives the chart a stable identity (legend and animation state) when charts are added or removed around it. (来自嵌入的 common[PieW])Key(k any) *PieW
LabelLabel shows data labels on the marks. (来自嵌入的 common[PieW])Label() *PieW
LayoutLayout applies layout options to the chart box (Grow, Margin ...). (来自嵌入的 common[PieW])Layout(o ...layout.Option) *PieW
LegendLegend places (or with LegendNone hides) the legend. (来自嵌入的 common[PieW])Legend(p LegendPos) *PieW
LoadingLoading shows a spinner over the chart (or in place of it without data). (来自嵌入的 common[PieW])Loading(on bool) *PieW
MirrorMirror 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[PieW])Mirror() *PieW
NoLabelNoLabel hides the outer labels and their leader lines.NoLabel() *PieW
NoTooltipNoTooltip disables the hover tooltip and crosshair. (来自嵌入的 common[PieW])NoTooltip() *PieW
ShowShow builds the chart.Show() View
StyleStyle tweaks the resolved chart style for this chart only. (来自嵌入的 common[PieW])Style(fn func(*Style)) *PieW

相关链接 ​

以 MIT 许可发布