参考

API参考

全局KatuCharts命名空间创建图表并管理默认值;每个图表实例公开方法来更新、导出、检查和销毁。

全局——KatuCharts

方法签名 → 返回值描述
chartchart(containerOrId: string | HTMLElement, options) → Chart在容器中创建图表(id或DOM节点)。
setOptionssetOptions(options) → void设置由后续图表继承的全局默认值。
getOptionsgetOptions() → Partial<Options>读取当前全局默认值。
useuse(module) → void注册具有install(KC)方法的插件/模块。
dateFormatdateFormat(format: string, timestamp: number) → string格式化ms时间戳(令牌见下)。
numberFormatnumberFormat(value, decimals, decPoint?, thousandsSep?) → string格式化数字;默认使用'.'和','。
colorcolor(input: string) → KatuChartsColor可链式颜色对象:.brighten(n)、.setOpacity(a)、.toString()。
palettesRecord<string, string[]>调色板名称 → 十六进制颜色的映射。
getPalettegetPalette(name: string) → string[]指定调色板的副本(未知时抛出异常)。
setThemesetTheme(name: string) → void应用'dark'|'light'|'nordic'。
SeriesRegistry.registerTyperegisterType(name, seriesClass) → void注册自定义series类型。
SeriesRegistry.getTypegetType(name) → class | undefined查找已注册的series类。

dateFormat令牌

令牌输出示例
%Y四位数年份2026
%m / %d零填充月/日03 / 22
%H / %M / %S小时(24h)/ 分钟 / 秒14 / 05 / 09
%B / %A完整月名 / 工作日名March / Sunday

调色板和主题

20个内置调色板:default, tableau10, d3classic, brewer, google, ocean, sunset, earth, forest, berry, corporate, minimal, nordic, darkMode, neon, pastel, soft, classic, tupi, monochrome。18种主题(9个系列,各有浅色和深色)设置背景+调色板,例如:

主题背景调色板
dark#1a1f24monochrome
light#ffffffmonochrome
nordic#eceff4nordic
import KatuCharts from 'katucharts.js';
import 'katucharts.js/bio'; // modules register their chart types on import

KatuCharts.setTheme('dark');
KatuCharts.setOptions({ colors: KatuCharts.getPalette('sunset') });

const chart = KatuCharts.chart('container', {
  series: [{ type: 'volcano', data: [/* … */] }]
});

实例——数据和布局

方法签名 → 返回值描述
addSeriesaddSeries(options, redraw=true) → BaseSeries追加一条series;redraw=false可批量操作。
getget(id) → BaseSeries | Axis | undefined通过id查找series或轴。
updateupdate(options, redraw=true) → void合并部分选项(title、axes、series…)。
addAxisaddAxis(options, isX=false, redraw=true) → Axis在运行时添加轴。
redrawredraw() → void立即应用待决的更改。
animatedRedrawanimatedRedraw(duration=500) → void使用过渡重绘。
reflowreflow() → void根据当前容器尺寸重新计算布局。
setSizesetSize(width, height) → void设置像素尺寸并重绘。
setTitlesetTitle(titleOptions?, subtitleOptions?) → void更新标题/副标题;null移除。

实例——导出、数据和UI

方法签名 → 返回值描述
getSVGgetSVG() → string独立SVG标记。
getCSVgetCSV() → stringCSV格式的数据。
getTablegetTable() → stringHTML <table> 字符串格式的数据。
getDataRowsgetDataRows() → array[][]二维数组;首行是标头。
exportChartexportChart({ type, filename, width, scale })下载PNG/JPEG/SVG/PDF。
showLoadingshowLoading(text='Loading...') → void显示加载覆盖层。
hideLoadinghideLoading() → void隐藏加载覆盖层。
getSeriesInstancesgetSeriesInstances() → BaseSeries[]所有series实例。
getXAxes / getYAxes→ Axis[]X / Y轴实例(多轴返回多个)。
destroydestroy() → void从DOM移除并解绑监听器——在移除容器前调用。
chart.showLoading('Fetching data…');
fetch('/api/data').then(r => r.json()).then(data => {
  chart.addSeries({ data });
  chart.hideLoading();
});

chart.exportChart({ type: 'image/svg+xml', filename: 'report', width: 1200 });

// on teardown
chart.destroy();

绘制渲染器

chart.plotRenderer 公开低级原始元素用于在绘制区域中自定义形状和注释。

方法签名绘制
rectrect(x, y, width, height, attrs?)矩形
circle / ellipsecircle(cx, cy, r, attrs?) / ellipse(cx, cy, rx, ry, attrs?)圆形 / 椭圆
pathpath(d, attrs?)来自d字符串的SVG路径
lineline(x1, y1, x2, y2, attrs?)直线
texttext(str, x, y, attrs?)文本标签
groupgroup(attrs?)<g>嵌套组

坐标助手:plotRenderer.plotArea → { x, y, width, height };xAxis.toPixels(value)和yAxis.toPixels(value)将数据转换为像素。

const [xAxis] = chart.getXAxes();
const [yAxis] = chart.getYAxes();
const plot = chart.plotRenderer.plotArea;

const yPx = yAxis.toPixels(6);
chart.plotRenderer.line(plot.x, yPx, plot.x + plot.width, yPx, {
  stroke: '#d62728', strokeWidth: 2, strokeDasharray: '6 3'
});
chart.plotRenderer.text('Target', plot.x + plot.width + 8, yPx + 4, {
  fill: '#d62728', fontSize: 12, fontWeight: 'bold'
});