Chart.js
入门指南
入门
安装
集成
分步指南
概览
可访问性(Accessibility)
颜色(Colors)
数据结构(Data structures)
字体(Fonts)
选项(Options)
内边距(Padding)
性能(Performance)
图表配置
配置(Configuration)
动画(Animations)
画布背景(Canvas background)
数据抽取(Data Decimation)
设备像素比率(Device Pixel Ratio)
通用配置(Elements)
互动(Interactions)
布局(Layout)
图例(Legend)
本地化(Locale)
响应式图表(Responsive Charts)
副标题(Subtitle)
标题(Title)
提示(Tooltip)
Charts
面积图(Area Chart)
柱状/条形图(Bar Chart)
气泡图(Bubble Chart)
环形&饼图(Doughnut and Pie Charts)
折线图(Line Chart)
混合图表(Mixed Chart Types)
极地图(Polar Area Chart)
雷达图(Radar Chart)
离散图(Scatter Chart)
坐标轴
轴(Axes)
笛卡尔坐标(Cartesian)
笛卡尔坐标轴(Cartesian Axes)
分类轴(Category Axis)
线性轴(Linear Axis)
对数轴(Logarithmic Axis)
时间笛卡尔轴(Time Cartesian Axis)
时间序列轴(Time Series Axis)
径向(Radial)
径向轴(Radial Axes)
线性径向轴(Linear Radial Axis)
标签轴(Labelling Axes)
样式(Styling)
开发者
开发者(Developers)
Chart.js API
坐标轴扩展
图表扩展
贡献
插件
发布扩展
更新 Charts
迁移
4.x迁移指南
3.x迁移指南
示例
Chart.js Samples
Bar Charts
Bar Chart Border Radius
Floating Bars
Horizontal Bar Chart
Stacked Bar Chart
Stacked Bar Chart with Groups
Vertical Bar Chart
Line Charts
Interpolation Modes
Line Chart
Multi Axis Line Chart
Point Styling
Line Segment Styling
Stepped Line Charts
Line Styling
Other charts
Bubble
Combo bar/line
Doughnut
Multi Series Pie
Pie
Polar area
Polar area centered point labels
Radar
Radar skip points
Scatter
Scatter - Multi axis
Stacked bar/line
Area charts
Line Chart Boundaries
Line Chart Datasets
Line Chart drawTime
Line Chart Stacked
Radar Chart Stacked
Scales
Linear Scale - Min-Max
Linear Scale - Suggested Min-Max
Linear Scale - Step Size
Log Scale
Stacked Linear / Category
Time Scale
Time Scale - Max Span
Time Scale - Combo Chart
Scale Options
Center Positioning
Grid Configuration
Tick Configuration
Title Configuration
Legend
Events
HTML Legend
Point Style
Position
Alignment and Title Position
Title
Alignment
Subtitle
Basic
Tooltip
Custom Tooltip Content
External HTML Tooltip
Interaction Modes
Point Style
Position
Scriptable Options
Bar Chart
Bubble Chart
Line Chart
Pie Chart
Polar Area Chart
Radar Chart
Animations
Delay
Drop
Loop
Progressive Line
Progressive Line With Easing
Advanced
Data Decimation
Derived Axis Type
Derived Chart Type
Linear Gradient
Programmatic Event Triggers
Animation Progress Bar
Radial Gradient
Plugins
Chart Area Border
Doughnut Empty State
Quadrants
Utils
互动(Interactions) - Chart.js中文文档 - 笔下光年
网站首页
互动(Interactions)
Namespace: options.interaction, the global interaction configuration is at Chart.defaults.interaction. To configure which events trigger chart interactions, see events. | Name | Type | Default | Description | |------------------|---------|-----------|-------------| | `mode` | `string` | `'nearest'` | Sets which elements appear in the interaction. See Interaction Modes for details. | | `intersect` | `boolean` | `true` | if true, the interaction mode only applies when the mouse position intersects an item on the chart. | | `axis` | `string` | `'x'` | Can be set to 'x', 'y', 'xy' or 'r' to define which directions are used in calculating distances. Defaults to 'x' for 'index' mode and 'xy' in dataset and 'nearest' modes. | | `includeInvisible` | `boolean` | `false` | if true, the invisible points that are outside of the chart area will also be included when evaluating interactions. | By default, these options apply to both the hover and tooltip interactions. The same options can be set in the options.hover namespace, in which case they will only affect the hover interaction. Similarly, the options can be set in the options.plugins.tooltip namespace to independently configure the tooltip interactions. ## Events The following properties define how the chart interacts with events. Namespace: options | Name | Type | Default | Description | |---------|----------|-------------|-----------------| | events | string[] | ['mousemove', 'mouseout', 'click', 'touchstart', 'touchmove'] | The events option defines the browser events that the chart should listen to for. Each of these events trigger hover and are passed to plugins. more... | | onHover | function | null | Called when any of the events fire over chartArea. Passed the event, an array of active elements (bars, points, etc), and the chart. | | onClick | function | null | Called if the event is of type 'mouseup', 'click' or ''contextmenu' over chartArea. Passed the event, an array of active elements, and the chart. | ### Event Option For example, to have the chart only respond to click events, you could do: ```javascript const chart = new Chart(ctx, { type: 'line', data: data, options: { // This chart will not respond to mousemove, etc events: ['click'] } }); ``` Events for each plugin can be further limited by defining (allowed) events array in plugin options: ```javascript const chart = new Chart(ctx, { type: 'line', data: data, options: { // All of these (default) events trigger a hover and are passed to all plugins, // unless limited at plugin options events: ['mousemove', 'mouseout', 'click', 'touchstart', 'touchmove'], plugins: { tooltip: { // Tooltip will only receive click events events: ['click'] } } } }); ``` Events that do not fire over chartArea, like mouseout, can be captured using a simple plugin: ```javascript const chart = new Chart(ctx, { type: 'line', data: data, options: { // these are the default events: // events: ['mousemove', 'mouseout', 'click', 'touchstart', 'touchmove'], }, plugins: [{ id: 'myEventCatcher', beforeEvent(chart, args, pluginOptions) { const event = args.event; if (event.type === 'mouseout') { // process the event } } }] }); ``` For more information about plugins, see Plugins ### Converting Events to Data Values A common occurrence is taking an event, such as a click, and finding the data coordinates on the chart where the event occurred. Chart.js provides helpers that make this a straightforward process. ```javascript const chart = new Chart(ctx, { type: 'line', data: data, options: { onClick: (e) => { const canvasPosition = Chart.helpers.getRelativePosition(e, chart); // Substitute the appropriate scale IDs const dataX = chart.scales.x.getValueForPixel(canvasPosition.x); const dataY = chart.scales.y.getValueForPixel(canvasPosition.y); } } }); ``` When using a bundler, the helper functions have to be imported separately, for a full explanation of this please head over to the integration page ## Modes When configuring the interaction with the graph via interaction, hover or tooltips, a number of different modes are available. options.hover and options.plugins.tooltip extend from options.interaction. So if mode, intersect or any other common settings are configured only in options.interaction, both hover and tooltips obey that. The modes are detailed below and how they behave in conjunction with the intersect setting. See how different modes work with the tooltip in tooltip interactions sample ### point Finds all of the items that intersect the point. ```javascript const chart = new Chart(ctx, { type: 'line', data: data, options: { interaction: { mode: 'point' } } }); ``` ### nearest Gets the items that are at the nearest distance to the point. The nearest item is determined based on the distance to the center of the chart item (point, bar). You can use the axis setting to define which coordinates are considered in distance calculation. If intersect is true, this is only triggered when the mouse position intersects an item in the graph. This is very useful for combo charts where points are hidden behind bars. ```javascript const chart = new Chart(ctx, { type: 'line', data: data, options: { interaction: { mode: 'nearest' } } }); ``` ### index Finds item at the same index. If the intersect setting is true, the first intersecting item is used to determine the index in the data. If intersect false the nearest item, in the x direction, is used to determine the index. ```javascript const chart = new Chart(ctx, { type: 'line', data: data, options: { interaction: { mode: 'index' } } }); ``` To use index mode in a chart like the horizontal bar chart, where we search along the y direction, you can use the axis setting introduced in v2.7.0. By setting this value to 'y' on the y direction is used. ```javascript const chart = new Chart(ctx, { type: 'bar', data: data, options: { interaction: { mode: 'index', axis: 'y' } } }); ``` ### dataset Finds items in the same dataset. If the intersect setting is true, the first intersecting item is used to determine the index in the data. If intersect false the nearest item is used to determine the index. ```javascript const chart = new Chart(ctx, { type: 'line', data: data, options: { interaction: { mode: 'dataset' } } }); ``` ### x Returns all items that would intersect based on the X coordinate of the position only. Would be useful for a vertical cursor implementation. Note that this only applies to cartesian charts. ```javascript const chart = new Chart(ctx, { type: 'line', data: data, options: { interaction: { mode: 'x' } } }); ``` ### y Returns all items that would intersect based on the Y coordinate of the position. This would be useful for a horizontal cursor implementation. Note that this only applies to cartesian charts. ```javascript const chart = new Chart(ctx, { type: 'line', data: data, options: { interaction: { mode: 'y' } } }); ``` ## Custom Interaction Modes New modes can be defined by adding functions to the Chart.Interaction.modes map. You can use the Chart.Interaction.evaluateInteractionItems function to help implement these. Example: ```javascript import { Interaction } from 'chart.js'; import { getRelativePosition } from 'chart.js/helpers'; /** * Custom interaction mode * @function Interaction.modes.myCustomMode * @param {Chart} chart - the chart we are returning items from * @param {Event} e - the event we are find things at * @param {InteractionOptions} options - options to use * @param {boolean} [useFinalPosition] - use final element position (animation target) * @return {InteractionItem[]} - items that are found */ Interaction.modes.myCustomMode = function(chart, e, options, useFinalPosition) { const position = getRelativePosition(e, chart); const items = []; Interaction.evaluateInteractionItems(chart, 'x', position, (element, datasetIndex, index) => { if (element.inXRange(position.x, useFinalPosition) && myCustomLogic(element)) { items.push({element, datasetIndex, index}); } }); return items; }; // Then, to use it... new Chart.js(ctx, { type: 'line', data: data, options: { interaction: { mode: 'myCustomMode' } } }) ``` If you're using TypeScript, you'll also need to register the new mode: ```javascript declare module 'chart.js' { interface InteractionModeMap { myCustomMode: InteractionModeFunction; } } ```
上一篇:
通用配置(Elements)
下一篇:
布局(Layout)