饼图 / 层级

饼图

type: 'pie'

分成比例切片的圆形图(无坐标轴)。支持切片偏移、自定义开始/结束角度和连接器数据标签。

编辑右侧的JSON并按Run(或⌘/Ctrl + Enter)——数据、标题、类别和颜色全部实时更新。

实时
饼图
编辑 · JSON

数据格式

{ name, y, color?, sliced? } · [['name', value]]

数据结构

传入图表的 JSON——可直接放进你的 series。

data: [
  { name: 'A', y: 45 },
  { name: 'B', y: 30 },
  { name: 'C', y: 25 }
]

示例

import KatuCharts from 'katucharts.js';

KatuCharts.chart('container', {
  chart: { type: 'pie' },
  series: [{
    data: [
      { name: 'A', y: 45 },
      { name: 'B', y: 30 },
      { name: 'C', y: 25 }
    ]
  }]
});

类型特定选项

选项描述
innerSize内半径(0 =完整饼图,'50%' =环形图)。
size / center饼图半径 / 中心['50%','50%']。
startAngle / endAngle弧形范围(度数)。
slicedOffset / borderRadius / depth切片偏移、圆角切片、伪3D深度。

变体

百分比数据标签

用带引线的标签显示每个扇区的名称和百分比。

KatuCharts.chart('container', {
  chart: { type: 'pie' },
  series: [{
    dataLabels: { enabled: true, format: '{point.name}: {point.percentage:.0f}%' },
    data: [
      { name: 'Product A', y: 35, sliced: true },
      { name: 'Product B', y: 25 }
    ]
  }]
});

下钻

点击扇区下钻查看其细分。

KatuCharts.chart('container', {
  chart: { type: 'pie' },
  drilldown: {
    series: [
      { id: 'emea', name: 'EMEA',
        data: [{ name: 'UK', y: 18 }, { name: 'Germany', y: 14 }] }
    ]
  },
  series: [{
    data: [
      { name: 'EMEA', y: 42, drilldown: 'emea' },
      { name: 'APAC', y: 25, drilldown: 'apac' }
    ]
  }]
});

Common errors

Why does my 饼图 chart render as a line chart?
The chart type was not registered. KatuCharts logs "KatuCharts: unknown series type "<type>", falling back to line" and draws a line chart rather than throwing. Import the module the type belongs to — for example `import 'katucharts.js/bio';` — before creating the chart.
Why do I get "Cannot read properties of null (reading 'appendChild')"?
The container element does not exist when the chart is created. Create the chart after the element is in the DOM: in React that means inside useEffect, not during render.
Why is the chart rendering with zero height?
The container has no height. A div with no content collapses to zero. Give it a height in CSS, or pass `chart: { height: 400 }` in the options.

FAQ

How do I create a 饼图 chart in JavaScript?
Install with `npm install katucharts.js`, then call KatuCharts.chart(container, { chart: { type: 'pie' }, series: [...] }).
Why does my 饼图 chart render as a line chart?
The chart type was not registered. KatuCharts logs "KatuCharts: unknown series type "<type>", falling back to line" and draws a line chart rather than throwing. Import the module the type belongs to — for example `import 'katucharts.js/bio';` — before creating the chart.
Why do I get "Cannot read properties of null (reading 'appendChild')"?
The container element does not exist when the chart is created. Create the chart after the element is in the DOM: in React that means inside useEffect, not during render.
Why is the chart rendering with zero height?
The container has no height. A div with no content collapses to zero. Give it a height in CSS, or pass `chart: { height: 400 }` in the options.
通用选项(color、visible、marker、dataLabels、animation、events、zones…)适用于每个类型——请参阅配置→series。在图库中查看其渲染。