参考

高级

使用自定义series和模块扩展KatuCharts,流传输实时数据,处理非常大的数据集,并访问内部构建块。

插件系统

自定义series类型

继承BaseSeries,在render()内绘制,并通过SeriesRegistry注册该类。

import KatuCharts, { BaseSeries, SeriesRegistry } from 'katucharts.js';

class MyCustomSeries extends BaseSeries {
  render() {
    const { plotArea, xAxis, yAxis } = this.context;
    this.context.chartGroup
      .selectAll('circle.custom-point')
      .data(this.processData())
      .join('circle')
      .attr('cx', d => xAxis.getPixelForValue(d.x) + plotArea.x)
      .attr('cy', d => yAxis.getPixelForValue(d.y) + plotArea.y)
      .attr('r', 6)
      .attr('fill', this.context.colors[this.context.colorIndex]);
  }
}

SeriesRegistry.registerType('mycustom', MyCustomSeries);
KatuCharts.chart(container, { series: [{ type: 'mycustom', data: [{ x: 1, y: 10 }] }] });

模块注册

使用init(KC)方法将多个扩展捆绑到模块中,并通过KatuCharts.use()激活它。

const myModule = {
  init(KC) {
    KC.SeriesRegistry.registerType('mycustom', MyCustomSeries);
    KC.SeriesRegistry.registerType('mycustom2', MyOtherSeries);
  }
};
KatuCharts.use(myModule);

BaseSeries扩展点

方法描述
render()主绘制方法——必须覆盖。
init(context)在创建时调用一次;接收SeriesContext。
processData()将原始选项数据规范化为渲染格式。
animateUpdate(duration)数据更改时的自定义过渡。
getDataExtents()返回{ xMin, xMax, yMin, yMax }用于轴自动缩放。
getCategories()series驱动分类轴时返回string[]。
setVisible(visible, animated?)显示/隐藏series。
destroy()在移除时清理DOM和监听器。

SeriesContext

属性类型描述
plotArea{ x, y, width, height }边距内的可绘制区域。
xAxis / yAxisAxisInstance已解析的轴;getPixelForValue(v)将数据映射到像素。
colorIndex / colorsnumber / string[]此series的调色板索引 / 完整调色板。
eventsEventBus组件间共享的事件总线。
chartGroupSelection此series的<g>的SVG选择。
totalSeriesOfType / indexInTypenumber在相同类型series中的计数 / 位置。
animateboolean是否启用动画。

实时流传输

从三个源将实时点推入series。传递{{ shift: true }}保持固定滚动窗口;返回的订阅有unsubscribe()。

import { StreamAdapter, UpdateScheduler } from 'katucharts.js';

const sub = StreamAdapter.fromWebSocket(ws, series, parseMessage, { shift: true });
// StreamAdapter.fromEventSource(es, series, 'tick', parseMessage, { shift })
// StreamAdapter.fromPolling(fetchFn, series, 1000, { shift })
sub.unsubscribe();

// Batch many updates into one redraw
UpdateScheduler.batch(() => {
  series.addPoint(p1, false);
  series.addPoint(p2, false);
});

大数据集——数据抽取

在保留视觉形状的同时减少数万个点。

import { lttbDecimate, minMaxDecimate } from 'katucharts.js';

// LTTB — keeps the most visually significant point per bucket
const reduced = lttbDecimate(bigDataset, 500);   // 50 000 → 500

// Min/Max — keeps the extremes per bucket (preserves outliers)
const bounds  = minMaxDecimate(bigDataset, 500); // 50 000 → 1 000 (2/bucket)
曼哈顿图在~20k个点以上自动切换到canvas,并配有交互式SVG覆盖;对于非常大的series禁用动画(chart.animation = false)。

导出的内部

核心类

导出描述
Chart主控制器——生命周期、渲染、更新。
EventBus跨组件共享的Pub/sub事件系统。
SVGRenderer低级SVG渲染层。
OptionsParser合并用户选项与默认值并验证。
SeriesRegistry / ModuleRegistry类型注册表 / 已安装模块跟踪器。
BaseSeries所有series继承的抽象基类。

组件和特性

导出描述
Tooltip / Legend / DataLabels / Crosshair / ExportButtonUI组件。
ExportModule编程式PNG/SVG/PDF导出(PDF需要jspdf)。
ResponsiveEngine / A11yModule调整大小时重新渲染 / ARIA + 键盘导航。
Drilldown / Zoom钻取过渡 / 鼠标滚轮 + 框缩放。
Navigator / RangeSelector / DataGrouping股票图表功能。

注册函数

函数注册
registerCoreSeriesTypes()line、area、column、scatter、pie、bar、spline、areaspline、histogram、heatmap、waterfall、funnel、pyramid、bubble、boxplot、errorbar、gauge、solidgauge、polar、windbarb
registerFinancialSeriesTypes()candlestick、ohlc
registerFlowSeriesTypes()sankey、networkgraph、dependencywheel
registerBioSeriesTypes()volcano、manhattan、circos、dotplot、lollipop

TypeScript导出

import type {
  KatuChartsOptions, ChartOptions, SeriesOptions, AxisOptions,
  TooltipOptions, LegendOptions, PlotOptions, PointOptions,
  MarkerOptions, DataLabelOptions, SeriesType, PlotArea,
  InternalConfig, ExportingOptions
} from 'katucharts.js';

实用程序

导出描述
dateFormat(format, timestamp)strftime风格日期格式化。
numberFormat(value, decimals, decPoint?, thousandsSep?)区域设置风格数字格式化。
templateFormat — '{path:spec}'令牌替换具有可选数字格式规范,例如{point.y:,.0f}。
parseColor(c)→{ r, g, b, a }。
brighten(c, amount) / setOpacity(c, a)变亮/变暗 / 设置透明度。
getPalette(name) / setGlobalOptions / getGlobalOptions调色板查找 / 全局默认值。
import { parseColor, brighten, setOpacity, getPalette } from 'katucharts.js';

parseColor('#1f77b4');           // { r: 31, g: 119, b: 180, a: 1 }
brighten('#1f77b4', 0.3);        // '#5da5d5'
setOpacity('#1f77b4', 0.4);      // 'rgba(31,119,180,0.4)'

// Template tokens (tooltip / dataLabels / axis formatters)
'{point.name}: {point.y:,.0f}'   // 'Revenue: 8,900'
'{series.name} — {point.percentage:.1f}%'