金融 · katucharts.js/finance
K线图
type: 'candlestick'时间序列的OHLC蜡烛图。实心体表示看跌蜡烛(收盘 < 开盘),空心表示看涨;灯芯显示高/低范围。
编辑右侧的JSON并按Run(或⌘/Ctrl + Enter)——数据、标题、类别和颜色全部实时更新。
实时
K线图
编辑 · JSON
数据格式
{ x: timestamp, open, high, low, close } · [[o,h,l,c]]
数据结构
传入图表的 JSON——可直接放进你的 series。
data: [
[172, 178, 170, 176],
[176, 182, 174, 180],
[180, 185, 175, 181]
]示例
import KatuCharts from 'katucharts.js';
KatuCharts.chart('container', {
chart: { type: 'candlestick' },
series: [{
name: 'KATU',
data: [
[172, 178, 170, 176],
[176, 182, 174, 180],
[180, 185, 175, 181]
]
}]
});类型特定选项
| 选项 | 描述 |
|---|---|
upColor | 看涨蜡烛体颜色(收盘 > 开盘)。 |
color | 看跌蜡烛体颜色。 |
lineColor / upLineColor | 灯芯和边框描边 / 看涨灯芯覆盖。 |
变体
叠加移动平均线
用 type + linkedTo 在价格序列上叠加 SMA 指标。
import KatuCharts from 'katucharts.js';
import 'katucharts.js/finance';
KatuCharts.chart('container', {
chart: { type: 'candlestick' },
series: [
{ id: 'px', name: 'KATU', data: ohlc },
{ type: 'sma', linkedTo: 'px', params: { period: 5 } }
]
});Common errors
- Why does my k线图 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 k线图 chart in JavaScript?
- Install with `npm install katucharts.js`, import the module with `import 'katucharts.js/finance';`, then call KatuCharts.chart(container, { chart: { type: 'candlestick' }, series: [...] }).
- Why does my k线图 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.