Skip to content

Commit

Permalink
Add interaction options
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Oct 18, 2020
1 parent de96309 commit ab2b12f
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 28 deletions.
4 changes: 2 additions & 2 deletions docs/docs/configuration/tooltip.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ The tooltip configuration is passed into the `options.tooltips` namespace. The g
| ---- | ---- | ------- | -----------
| `enabled` | `boolean` | `true` | Are on-canvas tooltips enabled?
| `custom` | `function` | `null` | See [custom tooltip](#external-custom-tooltips) section.
| `mode` | `string` | `'nearest'` | Sets which elements appear in the tooltip. [more...](../general/interactions/modes.md#interaction-modes).
| `intersect` | `boolean` | `true` | If true, the tooltip mode applies only when the mouse position intersects with an element. If false, the mode will be applied at all times.
| `mode` | `string` | | Sets which elements appear in the tooltip. [more...](../general/interactions/modes.md#interaction-modes).
| `intersect` | `boolean` | | If true, the tooltip mode applies only when the mouse position intersects with an element. If false, the mode will be applied at all times.
| `position` | `string` | `'average'` | The mode for positioning the tooltip. [more...](#position-modes)
| `callbacks` | `object` | | See the [callbacks section](#tooltip-callbacks).
| `itemSort` | `function` | | Sort tooltip items. [more...](#sort-callback)
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/general/interactions/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Interactions
---

The hover configuration is passed into the `options.hover` namespace. The global hover configuration is at `Chart.defaults.hover`. To configure which events trigger chart interactions, see [events](./events.md#events).
The interaction configuration is passed into the `options.interaction` namespace. The global interaction configuration is at `Chart.defaults.interaction`. To configure which events trigger chart interactions, see [events](./events.md#events).

| Name | Type | Default | Description
| ---- | ---- | ------- | -----------
Expand Down
16 changes: 9 additions & 7 deletions docs/docs/general/interactions/modes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ title: Interaction Modes

When configuring interaction with the graph via hover or tooltips, a number of different modes are available.

`options.hover` and `options.tooltips` 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.

## point
Expand All @@ -15,7 +17,7 @@ var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
tooltips: {
interaction: {
mode: 'point'
}
}
Expand All @@ -31,7 +33,7 @@ var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
tooltips: {
interaction: {
mode: 'nearest'
}
}
Expand All @@ -47,7 +49,7 @@ var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
tooltips: {
interaction: {
mode: 'index'
}
}
Expand All @@ -61,7 +63,7 @@ var chart = new Chart(ctx, {
type: 'bar',
data: data,
options: {
tooltips: {
interaction: {
mode: 'index',
axis: 'y'
}
Expand All @@ -77,7 +79,7 @@ var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
tooltips: {
interaction: {
mode: 'dataset'
}
}
Expand All @@ -92,7 +94,7 @@ var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
tooltips: {
interaction: {
mode: 'x'
}
}
Expand All @@ -107,7 +109,7 @@ var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
tooltips: {
interaction: {
mode: 'y'
}
}
Expand Down
4 changes: 3 additions & 1 deletion samples/charts/line/multi-axis.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
data: lineChartData,
options: {
responsive: true,
hoverMode: 'index',
interaction: {
mode: 'index'
},
stacked: false,
title: {
display: true,
Expand Down
6 changes: 4 additions & 2 deletions samples/charts/scatter/multi-axis.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@
data: scatterChartData,
options: {
responsive: true,
hoverMode: 'nearest',
intersect: true,
interaction: {
intersect: true,
mode: 'nearest'
},
title: {
display: true,
text: 'Chart.js Scatter Chart - Multi Axis'
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,12 @@ BarController.defaults = {
'maxBarThickness',
'minBarLength',
],
hover: {
interaction: {
mode: 'index'
},

hover: {},

datasets: {
categoryPercentage: 0.8,
barPercentage: 0.9,
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/controller.line.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ LineController.defaults = {
showLines: true,
spanGaps: false,

hover: {
interaction: {
mode: 'index'
},

hover: {},

scales: {
_index_: {
type: 'category',
Expand Down
18 changes: 16 additions & 2 deletions src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,22 @@ function initConfig(config) {

options.scales = scaleConfig;

options.title = (options.title !== false) && merge({}, [defaults.plugins.title, options.title]);
options.tooltips = (options.tooltips !== false) && merge({}, [defaults.plugins.tooltip, options.tooltips]);
options.title = (options.title !== false) && merge(Object.create(null), [
defaults.plugins.title,
options.title
]);
options.hover = merge(Object.create(null), [
defaults.interaction,
defaults.hover,
options.interaction,
options.hover
]);
options.tooltips = (options.tooltips !== false) && merge(Object.create(null), [
defaults.interaction,
defaults.plugins.tooltip,
options.interaction,
options.tooltips
]);

return config;
}
Expand Down
7 changes: 5 additions & 2 deletions src/core/core.defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ export class Defaults {
lineWidth: 0,
strokeStyle: undefined
};
this.hover = {
onHover: null,
this.interaction = {
mode: 'nearest',
intersect: true
};
this.hover = {
onHover: null
};
this.maintainAspectRatio = true;
this.onHover = null;
this.onClick = null;
this.responsive = true;
this.showLines = true;
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/plugin.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,9 +1066,7 @@ export default {
defaults: {
enabled: true,
custom: null,
mode: 'nearest',
position: 'average',
intersect: true,
backgroundColor: 'rgba(0,0,0,0.8)',
titleFont: {
style: 'bold',
Expand Down
35 changes: 30 additions & 5 deletions test/specs/core.controller.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,38 @@ describe('Chart', function() {
expect(chart.data.datasets[0].data).toEqual([10, 11, 12]);
});

it('should initialize config with default options', function() {
it('should initialize config with default interaction options', function() {
var callback = function() {};
var defaults = Chart.defaults;
var defaultMode = defaults.line.interaction.mode;

defaults.hover.onHover = callback;
defaults.line.spanGaps = true;
defaults.line.hover.mode = 'x-axis';
defaults.line.interaction.mode = 'test';

var chart = acquireChart({
type: 'line'
});

var options = chart.options;
expect(options.font.size).toBe(defaults.font.size);
expect(options.showLines).toBe(defaults.line.showLines);
expect(options.spanGaps).toBe(true);
expect(options.hover.onHover).toBe(callback);
expect(options.hover.mode).toBe('test');

defaults.hover.onHover = null;
defaults.line.spanGaps = false;
defaults.line.interaction.mode = defaultMode;
});

it('should initialize config with default hover options', function() {
var callback = function() {};
var defaults = Chart.defaults;

defaults.hover.onHover = callback;
defaults.line.spanGaps = true;
defaults.line.hover.mode = 'test';

var chart = acquireChart({
type: 'line'
Expand All @@ -106,11 +131,11 @@ describe('Chart', function() {
expect(options.showLines).toBe(defaults.line.showLines);
expect(options.spanGaps).toBe(true);
expect(options.hover.onHover).toBe(callback);
expect(options.hover.mode).toBe('x-axis');
expect(options.hover.mode).toBe('test');

defaults.hover.onHover = null;
defaults.line.spanGaps = false;
defaults.line.hover.mode = 'index';
delete defaults.line.hover.mode;
});

it('should override default options', function() {
Expand Down Expand Up @@ -141,7 +166,7 @@ describe('Chart', function() {
expect(options.title.position).toBe('bottom');

defaults.hover.onHover = null;
defaults.line.hover.mode = 'index';
delete defaults.line.hover.mode;
defaults.line.spanGaps = false;
});

Expand Down
9 changes: 7 additions & 2 deletions types/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,18 @@ export interface Defaults {
readonly color: string;
readonly events: ('mousemove' | 'mouseout' | 'click' | 'touchstart' | 'touchmove')[];
readonly font: IFontSpec;
readonly hover: {
onHover?: () => void;
readonly interaction: {
mode: InteractionMode | string;
intersect: boolean;
};
readonly hover: {
onHover?: () => void;
mode?: InteractionMode | string;
intersect?: boolean;
};
readonly maintainAspectRatio: boolean;
readonly onClick?: () => void;
readonly onHover?: () => void;
readonly responsive: boolean;

readonly plugins: { [key: string]: any };
Expand Down

0 comments on commit ab2b12f

Please sign in to comment.