笛卡尔坐标
柱状图
type: 'column'垂直条形图。支持分组条形、堆叠、按点着色、边框半径和最小条形长度。
编辑右侧的JSON并按Run(或⌘/Ctrl + Enter)——数据、标题、类别和颜色全部实时更新。
实时
柱状图
编辑 · JSON
数据格式
[10, 20, 30] · [{ y: 10, color: '#f00' }]
数据结构
传入图表的 JSON——可直接放进你的 series。
series: [
{ name: '销售额', data: [42, 55, 67, 81] }
]示例
import KatuCharts from 'katucharts.js';
KatuCharts.chart('container', {
chart: { type: 'column' },
xAxis: { categories: ['一季度', '二季度', '三季度', '四季度'] },
series: [{ name: '销售额', data: [42, 55, 67, 81] }]
});类型特定选项
| 选项 | 描述 |
|---|---|
groupPadding | 组之间的间距(默认0.2)。 |
pointPadding | 组内条形之间的间距(默认0.1)。 |
borderRadius / borderWidth / borderColor | 条形边框样式。 |
colorByPoint | 每个条形获得不同的调色板颜色。 |
maxPointWidth / pointWidth / minPointLength | 宽度上限和最小条形高度。 |
stacking / negativeColor | 'normal' | 'percent' / 负值颜色。 |
变体
堆叠柱状
将分组系列堆叠为每个类别一根柱子。
KatuCharts.chart('container', {
chart: { type: 'column' },
xAxis: { categories: ['一季度', '二季度', '三季度', '四季度'] },
plotOptions: { column: { stacking: 'normal' } },
series: [
{ name: '线上', data: [42, 55, 67, 81] },
{ name: '零售', data: [28, 32, 38, 41] },
{ name: 'Partner', data: [12, 18, 22, 30] }
]
});数据标签与 colorByPoint
通过 colorByPoint 为每根柱子着色,并在每列上显示数值标签。
KatuCharts.chart('container', {
chart: { type: 'column' },
xAxis: { categories: ['一季度', '二季度', '三季度', '四季度'] },
series: [{
colorByPoint: true, borderRadius: 3,
dataLabels: { enabled: true, format: '{point.y}' },
data: [142, 188, 224, 261]
}]
});下钻
点击柱子下钻到子系列;通过面包屑返回。
KatuCharts.chart('container', {
chart: { type: 'column' },
xAxis: { type: 'category' },
drilldown: {
series: [
{ id: 'chrome', name: 'Chrome',
data: [{ name: 'v120', y: 28 }, { name: 'v119', y: 20 }] }
]
},
series: [{
colorByPoint: true,
data: [
{ name: 'Chrome', y: 63, drilldown: 'chrome' },
{ name: 'Safari', y: 18 }
]
}]
});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: 'column' }, 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.