指南

配置

图表是传递给KatuCharts.chart(container, options)的一个纯选项对象。每个属性都是可选的——默认值是合理的。这是每个顶级键的完整参考。

const chart = KatuCharts.chart(container, {
  chart:    { /* chart-level options */ },
  title:    { /* title options */ },
  subtitle: { /* subtitle options */ },
  xAxis:    { /* x-axis options */ },
  yAxis:    { /* y-axis options */ },
  series:   [ /* array of series */ ],
  tooltip:  { /* tooltip options */ },
  legend:   { /* legend options */ },
  plotOptions: { /* default series options */ },
  credits:  { /* credits options */ },
  exporting: { /* export options */ },
  colorAxis: { /* color mapping */ },
  responsive: { /* responsive rules */ },
  accessibility: { /* a11y options */ },
  drilldown: { /* drilldown options */ }
});

chart

整体容器、背景、尺寸和交互模式。

属性类型默认值描述
typestring'line'不设置自己的series的默认series类型。
widthnumber | nullnull显式宽度(像素)。null填充容器。
heightnumber | string400高度,单位为像素,或相对于宽度的百分比(如 '56%')。设为 null 可根据内容自动适配高度(在较矮的移动视口中有上限)。
backgroundColorstring'#ffffff'整个图表区域的背景色。
borderColor / borderWidth / borderRadiusstring / number / number— / 0 / 0容器边框样式。
spacingnumber[][10,10,15,10]图表周围的间距[上,右,下,左]。
marginnumber[] | nullnull显式绘制区域边距;覆盖自动布局。
animationboolean | objecttrue切换所有动画,或传递{ duration, easing }。
zoomTypestring—拖动缩放轴:'x'、'y'或'xy'。
panningboolean | objectfalse缩放时平移。对象:{ type }。
panKeystring—按住的键来平移:'shift'、'ctrl'、'alt'。
invertedbooleanfalse交换X和Y轴(水平图表)。
polarbooleanfalse以极坐标(雷达)渲染所有series。
reflowbooleantrue容器调整大小时自动调整大小。
palettestring[]—覆盖主题颜色的自定义调色板。
styleobject—应用于容器元素的CSS。
eventsobject—监听器:load、redraw、render、selection、click、drilldown、drillup。
plotBackgroundColor / plotBorderColor / plotBorderWidthstring / string / number—绘制区域(轴内)样式。
scrollablePlotAreaobject—让绘图区在固定视口内滚动:{ minWidth, minHeight, scrollPositionX, scrollPositionY }。让较长的类别列表和较宽的时间序列在小屏幕上保持可用。

chart.zooming

属性类型默认值描述
typestring—缩放方向:'x'、'y'、'xy'。
keystring—修饰键:'shift'、'ctrl'、'alt'。
mouseWheelboolean | object—鼠标滚轮缩放——默认关闭;用 true 或 { enabled, sensitivity, type } 开启。在可滚动图表上,滚轮会滚动绘图区而非缩放。
pinchTypestring—触摸捏合方向:'x'、'y'、'xy'。
resetButtonobject—缩放后显示的重置按钮;恢复坐标轴的原始范围。可用 { theme, position } 自定义。

title / subtitle

相同形状;副标题在标题正下方渲染。

属性类型默认值描述
textstring'Chart title'内容;null/'隐藏。
alignstring'center''left' | 'center' | 'right'。
marginnumber15标题下方的空间。
styleobject—CSS文本样式。
x / ynumber0 / —从对齐位置的像素偏移。
floatingbooleanfalse浮在绘制区域上方。
useHTMLbooleanfalse允许HTML而非SVG文本。

xAxis / yAxis

单个对象或用于多轴的数组。xAxis和yAxis共享这些属性(默认值在注明的地方有所不同)。

属性类型默认值描述
typestring'linear''linear' | 'logarithmic' | 'datetime' | 'category'。
title.textstringnull轴标题;null隐藏。
min / maxnumber—硬轴边界。
softMin / softMaxnumber—软边界——如果数据未达到则扩展到此。
floor / ceilingnumber—绝对边界,padding无法跨越。
categoriesstring[]—分类标签;设置类型为'category'。
tickInterval / tickAmountnumber—刻度间距(datetime时为ms)/精确刻度计数。
tickLength / tickWidth / tickColornumber / number / string10 / 1 / '#ccd6eb'刻度标记样式。
gridLineWidthnumber0 (x) / 1 (y)每个刻度的网格线宽度。
gridLineColor / gridLineDashStylestring / string'#e6e6e6' / 'Solid'网格线颜色和虚线。
lineWidth / lineColornumber / string1 / '#ccd6eb'轴线样式。
offsetnumber0从绘制区域边缘的偏移。
oppositebooleanfalse放在对侧。
reversed / visiblebooleanfalse / true反向 / 隐藏轴。
crosshairboolean | objectfalse指针十字准线;对象:{ color, width, dashStyle }。
plotBands / plotLinesobject[]—彩色条带 / 参考线(下方)。
startOnTick / endOnTickbooleantrue将轴极值对齐到刻度。
minPadding / maxPaddingnumber0.01作为范围分数的padding。
allowDecimalsbooleantrue允许小数刻度值。
id / linkedTostring / number|string—轴id / 将极值链接到另一轴。
scrollbarboolean | objectfalse当绘图区滚动时用于平移的按轴滚动条 — { enabled, barBackgroundColor, trackBorderColor, … }。

轴标签

属性类型默认值描述
enabledbooleantrue显示/隐藏刻度标签。
formatstring'{value}'模板,例如'${value}'。
formatterfunction—回调;接收this.value。
styleobject—CSS样式。
rotationnumber0固定旋转(度)。
autoRotation / autoRotationLimitnumber[] / number[-45] / 80当标签重叠时自动旋转角度。
stepnumber—显示每第n个标签。
paddingnumber5从轴线的padding。

plotBands & plotLines

yAxis: {
  plotBands: [
    { from: 0, to: 50, color: 'rgba(68,170,213,0.1)', label: { text: 'Low' } },
    { from: 100, to: 200, color: 'rgba(255,68,68,0.1)', label: { text: 'High' } }
  ],
  plotLines: [
    { value: 75, color: '#d62728', width: 2, dashStyle: 'Dash',
      label: { text: 'Target', align: 'right' }, zIndex: 5 }
  ]
}

series

配置的核心数组——每个数据集一个条目。首先是通用选项;然后是特定于类型的选项。

属性类型默认值描述
typestring'line'Series类型;覆盖chart.type。
namestring—图例 / tooltip标签。
dataarray[]点:数字、[x,y]或{ x, y, name }。
idstring—用于chart.get()和drilldown的id。
color / colorIndexstring / number—覆盖调色板颜色 / 调色板索引。
visible / showInLegendbooleantrue初始可见性 / 图例存在。
lineWidth / lineColornumber / string2 / —线样式。
dashStylestring'Solid'实线、破折号、点、短划线、长划线、破折号点……
fillColor / fillOpacitystring / number— / 0.75区域填充颜色和透明度。
opacitynumber1整体series透明度。
marker / dataLabelsobject—点标记 / 标签(下方)。
stacking / stackstring / string|number—'normal' | 'percent' / 堆栈组id。
stepstring | booleanfalse阶梯线:'left' | 'center' | 'right'。
connectNullsbooleanfalse跨越空值点绘制。
threshold / negativeColornumber / string0 / —区域基线 / 阈值下方的颜色。
enableMouseTracking / allowPointSelectbooleantrue / false悬停和点击 / 点击选择点。
clipbooleantrue将绘制裁剪到绘制区域。
zones / zoneAxisobject[] / string— / 'y'基于值的样式区域(下方)。
animation / cursorboolean|object / stringtrue / —进入动画 / 悬停光标。
xAxis / yAxis / zIndexnumber|string0 / 0 / —绑定轴 / 堆栈顺序。
pointStart / pointIntervalnumber0 / 1平面数字数组的X起点和步长。
keysstring[]—将数组列映射到属性,例如['x','y','z']。
drilldownstring—点击时的钻取series的id。

柱状图 / 条形图

属性类型默认值描述
pointPadding / groupPaddingnumber0.1 / 0.2组内 / 组间的padding。
borderWidth / borderColor / borderRadiusnumber / string / number1 / '#fff' / 0柱形边框样式。
colorByPointbooleanfalse从调色板为每列着色。
minPointLengthnumber0微小值的最小条形高度。
maxPointWidth / pointWidthnumber—最大 / 固定列宽(像素)。

饼图 / 环形图

属性类型默认值描述
innerSizenumber | string0环形图的内直径,例如'50%'。
size / centernumber|string / array— / ['50%','50%']外直径 / 中心[x,y]。
startAngle / endAnglenumber0 / 360饼图扫过(使用<360用于部分饼图)。
slicedOffsetnumber10切出点的像素偏移。

标记

属性类型默认值描述
enabledboolean | nullnullnull当点少时自动显示。
symbolstring'circle'circle、square、diamond、triangle、triangle-down或URL。
radiusnumber4标记半径。
fillColor / lineColor / lineWidthstring / string / number— / — / 0标记填充和边框。
states.hover / states.selectobject—{ enabled, radius, fillColor, lineColor, lineWidth }。

数据标签

属性类型默认值描述
enabledbooleanfalse在每个点上启用标签。
format / formatterstring / function'{point.y}' / —模板 / 回调(this.point, this.series)。
style / colorobject / string—文本样式。
backgroundColor / borderColor / borderRadius / paddingstring / string / number / number— / — / 0 / 5标签框样式。
align / verticalAlignstring'center' / —在点内的对齐。
x / y / rotationnumber0偏移和旋转。
inside / allowOverlapbooleanfalse放在条形内 / 允许重叠。
distance / connectorWidth / connectorColornumber / number / string— / 1 / —饼图标签距离和连接线。

Series状态

plotOptions: {
  series: {
    states: {
      hover:    { lineWidthPlus: 2, halo: { size: 10, opacity: 0.25 } },
      select:   { color: '#d62728', borderColor: '#000', borderWidth: 2 },
      inactive: { opacity: 0.2, animation: { duration: 50 } }
    }
  }
}

区域

series: [{
  data: [29, 71, 106, 82, 44, 13],
  zoneAxis: 'y',
  zones: [
    { value: 50,  color: '#2ca02c' },   // below 50
    { value: 100, color: '#ff7f0e' },   // 50–100
    { color: '#d62728' }                // above 100
  ]
}]

常用选项(已渲染)

这些选项适用于所有系列类型。每个示例都是看到该选项效果所需的最小配置。

color · colorByPoint

用 colorByPoint 让每个点取自调色板,或为单个点设置显式 color。

KatuCharts.chart('container', {
  chart: { type: 'column' },
  series: [{
    colorByPoint: true,
    data: [
      { name: 'Apples', y: 5 },
      { name: 'Pears', y: 3, color: '#8C2810' },
      { name: 'Plums', y: 7 }
    ]
  }]
});

visible · showInLegend

用 visible: false 在加载时隐藏某个系列,或用 showInLegend: false 将其排除在图例之外。

KatuCharts.chart('container', {
  chart: { type: 'line' },
  series: [
    { name: 'Revenue',  data: [4, 6, 5, 8] },
    { name: 'Forecast', data: [3, 5, 6, 7], visible: false },
    { name: 'Internal', data: [2, 2, 3, 3], showInLegend: false }
  ]
});

marker

开启数据点标记,并选择符号与半径。

KatuCharts.chart('container', {
  chart: { type: 'spline' },
  series: [{
    data: [3, 5, 4, 7, 6],
    marker: { enabled: true, symbol: 'diamond', radius: 6 }
  }]
});

dataLabels

在每个点上显示数值;format 支持 {point.y} 之类的占位符。

KatuCharts.chart('container', {
  chart: { type: 'column' },
  series: [{
    data: [29, 71, 40, 55],
    dataLabels: { enabled: true, format: '{point.y}%' }
  }]
});

dashStyle · lineWidth · lineColor

通过线宽、虚线样式和颜色区分各条线。

KatuCharts.chart('container', {
  chart: { type: 'line' },
  series: [
    { name: 'Thick',  data: [4, 5, 6, 7], lineWidth: 4 },
    { name: 'Dashed', data: [3, 4, 4, 5], dashStyle: 'Dash', lineColor: '#A85429' }
  ]
});

opacity · fillOpacity

控制系列与区域填充的透明度。

KatuCharts.chart('container', {
  chart: { type: 'area' },
  series: [{
    data: [5, 7, 6, 9, 8],
    fillOpacity: 0.3        // also: opacity, fillColor
  }]
});

zones

用 zones 按数值区间沿 y 轴为系列设置样式。

KatuCharts.chart('container', {
  chart: { type: 'line' },
  series: [{
    data: [-4, -1, 2, 5, 3, 7],
    zones: [
      { value: 0, color: '#B43417' },
      { color: '#33503A' }
    ]
  }]
});

threshold · negativeColor

设定基准线,并为其下方的部分使用不同颜色。

KatuCharts.chart('container', {
  chart: { type: 'column' },
  series: [{
    data: [3, -2, 5, -4, 2],
    threshold: 0,
    negativeColor: '#B43417'
  }]
});

animation

按系列调整入场动画时长。

KatuCharts.chart('container', {
  chart: { type: 'column' },
  series: [{
    data: [5, 8, 4, 9, 6],
    animation: { duration: 1200 }
  }]
});

events · cursor

用 cursor 让数据点可点击,并通过 events 处理函数响应。

KatuCharts.chart('container', {
  chart: { type: 'column' },
  series: [{
    cursor: 'pointer',
    data: [5, 8, 4],
    events: {
      click: (e) => console.log(e.point.category)
    }
  }]
});

step · connectNulls

绘制阶梯线,并连接由 null 点造成的间断。

KatuCharts.chart('container', {
  chart: { type: 'line' },
  series: [
    { name: 'Step', data: [3, 5, 5, 7, 6, 8], step: 'center' },
    { name: 'Gaps', data: [2, null, 4, 5, null, 6], connectNulls: true }
  ]
});

stacking · stack

用 stacking: 'normal' 将系列堆叠在一起。

KatuCharts.chart('container', {
  chart: { type: 'column' },
  series: [
    { name: 'Online', data: [3, 5, 4, 6], stacking: 'normal' },
    { name: 'Store',  data: [2, 3, 3, 4], stacking: 'normal' }
  ]
});

pointStart · pointInterval

为扁平数字数组生成 x 值。

KatuCharts.chart('container', {
  chart: { type: 'line' },
  series: [{
    pointStart: 2019,
    pointInterval: 1,
    data: [4, 6, 5, 8, 7, 9]
  }]
});

tooltip

属性类型默认值描述
enabledbooleantrue切换tooltip。
shared / splitbooleanfalse一个框中的所有series / 单个系列标注。
useHTMLbooleanfalseHTML渲染而非SVG。
headerFormat / pointFormat / footerFormatstring—标头 / 每行 / 页脚的模板。
formatterfunction—完全控制;覆盖格式字符串。
backgroundColor / borderColor / borderWidth / borderRadiusstring / string / number / number'#fff' / — / 1 / 3框样式。
padding / shadow / animationnumber / boolean / boolean8 / true / true框padding、阴影、过渡。
hideDelaynumber500离开后隐藏前的ms数。
followPointer / stickOnContactbooleanfalse跟随光标 / 悬停时保持。
snap / distancenumber10 / 16触发距离 / 与点的间隙。
outsidebooleanfalse在容器外渲染以避免裁剪。
valueDecimals / valuePrefix / valueSuffixnumber / string / string—数字格式化助手。

模板令牌

在headerFormat、pointFormat、footerFormat和dataLabels.format中使用:

Token描述
{point.x} / {point.y} / {point.z}X、Y和Z(气泡)值。
{point.key} / {point.name}类别/标头 / 点名称(饼、漏斗……)。
{point.color} / {point.percentage}点颜色 / 堆栈或饼图series的百分比。
{series.name}Series名称。
{point.y:.2f}Y格式化为2位小数。
tooltip: {
  shared: true,
  formatter() {
    let html = '<b>' + this.x + '</b><br/>';
    this.points.forEach(p => {
      html += '<span style="color:' + p.color + '">\u25CF</span> '
            + p.series.name + ': <b>' + p.y.toLocaleString() + '</b><br/>';
    });
    return html;
  }
}

legend

属性类型默认值描述
enabledbooleantrue显示/隐藏图例。
layoutstring'horizontal''horizontal' | 'vertical'。
align / verticalAlignstring'center' / 'bottom'在图表内的位置。
floatingbooleanfalse浮在图表内容上。
x / y / margin / paddingnumber0 / 0 / 15 / 8偏移和间距。
itemDistance / itemMarginTop / itemMarginBottomnumber20 / 0 / 0项目之间的间距。
symbolWidth / symbolHeight / symbolRadius / symbolPaddingnumber16 / 12 / 2 / 5图例符号尺寸。
squareSymbol / reversedbooleantrue / false正方形符号 / 反向项目顺序。
itemStyle / itemHoverStyle / itemHiddenStyleobject—按状态的项目文本样式。
backgroundColor / borderColor / borderWidthstring / string / number— / — / 0图例框样式。
maxHeightnumber—上限高度;超出时分页。
labelFormatterfunction—自定义项目文本(this = series/point)。

plotOptions

默认series选项在三个级别合并: plotOptions.series (所有类型) → plotOptions[type] (一种类型) → series[i] (最高优先级)。

plotOptions: {
  series: { animation: { duration: 1500 }, cursor: 'pointer' },
  column: { borderRadius: 4, groupPadding: 0.15 },
  line:   { lineWidth: 3, marker: { enabled: true, radius: 5 } }
}

colorAxis

将值映射到颜色梯度(热力图、矩形树图、地理着色图)。单个对象或数组。

属性类型默认值描述
min / maxnumber—值范围;如果省略则从数据自动推断。
minColor / maxColorstring—标度每端的颜色。
stopsarray[]—梯度停止[[0,color],[0.5,color],[1,color]]。
type / reversedstring / boolean'linear' / false标度类型 / 反向方向。
dataClassesobject[]—离散类{ from, to, color, name }。
labelsobject—颜色轴图例标签(轴标签形状)。

exporting

属性类型默认值描述
enabledbooleantrue启用导出模块+上下文按钮。
filenamestring'chart'默认文件名(无扩展名)。
typestring'image/png'默认导出MIME类型。
scalenumber2光栅分辨率缩放。
printMaxWidthnumber780打印时的最大宽度。
buttons.contextButtonobject—{ enabled, menuItems, x, y }。
csvobject—{ columnHeaderFormatter, dateFormat, itemDelimiter, lineDelimiter, decimalPoint }。

格式: image/png, image/jpeg, image/svg+xml, application/pdf (需要可选的 jspdf 对等方)和CSV。

responsive

在视口条件匹配时合并选项覆盖的规则。

responsive: {
  rules: [
    { condition: { maxWidth: 500 },
      chartOptions: { legend: { enabled: false }, yAxis: { title: { text: null } } } },
    { condition: { maxWidth: 300 },
      chartOptions: { title: { text: null }, subtitle: { text: null } } }
  ]
}
条件类型描述
maxWidth / minWidthnumber在此宽度或以下 / 以上应用。
maxHeight / minHeightnumber在此高度或以下 / 以上应用。
callbackfunction自定义谓词;接收图表。

accessibility

属性类型默认值描述
enabledbooleantrue切换a11y模块。
descriptionstring—图表的屏幕阅读器描述。
keyboardNavigation.enabledbooleantrue通过元素进行键盘导航。
keyboardNavigation.focusBorderobject—焦点指示器{ color, margin, style }。
keyboardNavigation.orderstring[]—组件的Tab顺序。
keyboardNavigation.seriesNavigationobject—{ mode, skipNullPoints, pointNavigationEnabledThreshold }。
announceNewData.enabledbooleanfalse通过ARIA实时区域公告新数据。
highContrastModestring | boolean'auto'true | false | 'auto'(检测OS)。
point.descriptionFormatterfunction—自定义每点的无障碍描述。

drilldown

属性类型默认值描述
animationboolean | objecttrue动画钻取过渡。
allowPointDrilldownbooleantrue通过点击单个点进行钻取。
seriesarray[]钻取子series,每个都有唯一id。
drillUpButtonobject—{ relativeTo, position, theme }。
breadcrumbsobject—{ enabled, format, formatter, style, position }。
series: [{
  colorByPoint: true,
  data: [{ name: 'North', y: 310, drilldown: 'north' },
         { name: 'South', y: 260, drilldown: 'south' }]
}],
drilldown: {
  breadcrumbs: { enabled: true },
  series: [
    { id: 'north', data: [['NYC', 120], ['Boston', 95]] },
    { id: 'south', data: [['Miami', 90], ['Atlanta', 85]] }
  ]
}

股票/时间序列chrome通过 katucharts.js 入口点。导航器是迷你概览图表;范围选择器添加预设时间范围按钮。

属性类型默认值描述
navigator.enabled / height / marginboolean / number / numbertrue / 40 / 25切换、调整大小和导航器的间隙。
navigator.maskFill / outlineColor / outlineWidthstring / string / numberrgba(102,133,194,.3) / '#ccc' / 1遮罩和轮廓样式。
navigator.baseSeries / seriesnumber|string / object0 / —源series / 迷你图表覆盖。
rangeSelector.enabled / selectedboolean / numbertrue / —切换 / 初始按钮索引。
rangeSelector.inputEnabledbooleantrue日期输入字段。
rangeSelector.buttonsobject[]—{ type, count, text } — type ∈ second…year、ytd、all。
rangeSelector.dropdownstring'responsive''always' | 'never' | 'responsive'。

loading &amp; credits

属性类型默认值描述
loading.showDuration / hideDurationnumber100加载覆盖层的淡入/淡出(ms)。
loading.style / labelStyleobject—覆盖和标签CSS。
credits.enabled / text / hrefboolean / string / stringtrue / 'Powered by: KatuCharts' / —致谢标签。
credits.position / styleobject{ align:'right', verticalAlign:'bottom' }致谢放置和样式。
通过显示加载覆盖层 chart.showLoading() 并通过隐藏 chart.hideLoading()。

动态更新

图表创建后会暴露一套命令式 API,可在运行时更改数据、系列和配置,而无需重新创建。

方法描述
chart.update(options, redraw?)合并新配置并重绘图表(类型、标题、坐标轴、系列等)。
chart.series[i]访问已渲染的系列实例以单独更新它们。
series.setData(data, redraw?, animation?)替换某个系列的全部数据。
series.addPoint(point, redraw?, shift?)追加一个点;启用 shift 时移除最旧的点(滑动窗口)。
chart.addSeries(options, redraw?)在运行时添加新系列。
chart.setTitle(title, subtitle?)更新标题和副标题文本。
chart.showLoading(text?) / hideLoading()显示和隐藏加载遮罩。
chart.redraw() / reflow()强制重绘或使图表重新适配容器。
chart.destroy()销毁图表并释放事件监听器。

实时示例

下面两个图表使用运行时 API:一个持续接收新点,另一个每次点击切换类型。

实时数据

读数的滑动窗口:每秒进入一个新值、移除最旧的值,并通过 chart.series[0].setData(history) 重绘系列。

const chart = KatuCharts.chart('container', {
  chart: { type: 'line', animation: false },
  yAxis: { min: 0, max: 100 },
  series: [{ name: 'Requests/s', data: history, marker: { enabled: false } }]
});

setInterval(() => {
  history.push(nextReading());
  history.shift();
  chart.series[0].setData(history);
}, 1000);

切换类型

通过 chart.update 设置新的 chart.type,将相同数据重绘为其他类型,无需重新创建。

const chart = KatuCharts.chart('container', {
  chart: { type: 'column' },
  xAxis: { categories: ['Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6'] },
  series: [{ name: 'Revenue', data: [42, 58, 35, 71, 49, 63] }]
});

columnButton.onclick = () => chart.update({ chart: { type: 'column' } });
barButton.onclick = () => chart.update({ chart: { type: 'bar' } });