饼图 / 层级
树状图
type: 'treegraph'用于层级数据的整齐树形布局。节点绘制为圆形并由平滑连线相连;接受 id/parent 行或嵌套子节点。
编辑右侧的JSON并按Run(或⌘/Ctrl + Enter)——数据、标题、类别和颜色全部实时更新。
实时
树状图
编辑 · JSON
数据格式
{ id, parent?, name } · { name, children: [...] }
数据结构
传入图表的 JSON——可直接放进你的 series。
data: [
{ id: 'CEO', name: 'CEO' },
{ id: 'CTO', parent: 'CEO', name: 'CTO' },
{ id: 'Eng', parent: 'CTO', name: '工程' }
]示例
import KatuCharts from 'katucharts.js';
KatuCharts.chart('container', {
chart: { type: 'treegraph' },
series: [{
layout: 'horizontal',
data: [
{ id: 'CEO', name: 'CEO' },
{ id: 'CTO', parent: 'CEO', name: 'CTO' },
{ id: 'Eng', parent: 'CTO', name: '工程' }
]
}]
});类型特定选项
| 选项 | 描述 |
|---|---|
layout | 'horizontal' | 'vertical' — 树的生长方向。 |
layoutAlgorithm | 'tree'(默认)| 'cluster' — 将叶节点对齐到同一轴。 |
link | 连线样式 — { color, lineWidth }。 |
marker | 节点圆形 — { radius }。 |
levels | 按层级覆盖颜色 — [{ level, color }]。 |
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: 'treegraph' }, 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.