Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove line interaction overrides + hover.onHover #8770

Merged
merged 2 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/getting-started/v3-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ A number of changes were made to the configuration options passed to the `Chart`
* Horizontal Bar default tooltip mode was changed from `'index'` to `'nearest'` to match vertical bar charts
* `legend`, `title` and `tooltip` namespaces were moved from `Chart.defaults` to `Chart.defaults.plugins`.
* `elements.line.fill` default changed from `true` to `false`.
* Line charts no longer override the default `interaction` mode. Default is changed from `'index'` to `'nearest'`.

#### Scales

Expand Down Expand Up @@ -217,13 +218,14 @@ Animation system was completely rewritten in Chart.js v3. Each property can now
#### Interactions

* To allow DRY configuration, a root options scope for common interaction options was added. `options.hover` and `options.plugins.tooltip` now both extend from `options.interaction`. Defaults are defined at `defaults.interaction` level, so by default hover and tooltip interactions share the same mode etc.
* `interactions` are now limited to the chart area
* `interactions` are now limited to the chart area + allowed overflow
* `{mode: 'label'}` was replaced with `{mode: 'index'}`
* `{mode: 'single'}` was replaced with `{mode: 'nearest', intersect: true}`
* `modes['X-axis']` was replaced with `{mode: 'index', intersect: false}`
* `options.onClick` is now limited to the chart area
* `options.onClick` and `options.onHover` now receive the `chart` instance as a 3rd argument
* `options.onHover` now receives a wrapped `event` as the first parameter. The previous first parameter value is accessible via `event.native`.
* `options.hover.onHover` was removed, use `options.onHover`.

#### Ticks

Expand Down
2 changes: 0 additions & 2 deletions src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,6 @@ BarController.overrides = {
mode: 'index'
},

hover: {},

scales: {
_index_: {
type: 'category',
Expand Down
6 changes: 0 additions & 6 deletions src/controllers/controller.line.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ LineController.defaults = {
* @type {any}
*/
LineController.overrides = {
interaction: {
mode: 'index'
},

hover: {},

scales: {
_index_: {
type: 'category',
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ class Chart {
me._lastEvent = null;

// Invoke onHover hook
callCallback(options.onHover || hoverOptions.onHover, [e, active, me], me);
callCallback(options.onHover, [e, active, me], me);

if (e.type === 'mouseup' || e.type === 'click' || e.type === 'contextmenu') {
if (_isPointInArea(e, me.chartArea, me._minPadding)) {
Expand Down
4 changes: 1 addition & 3 deletions src/core/core.defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ export class Defaults {
lineHeight: 1.2,
weight: null
};
this.hover = {
onHover: null
};
this.hover = {};
this.hoverBackgroundColor = (ctx, options) => getHoverColor(options.backgroundColor);
this.hoverBorderColor = (ctx, options) => getHoverColor(options.borderColor);
this.hoverColor = (ctx, options) => getHoverColor(options.color);
Expand Down
35 changes: 20 additions & 15 deletions test/specs/core.controller.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,51 +94,56 @@ describe('Chart', function() {
it('should initialize config with default interaction options', function() {
var callback = function() {};
var defaults = Chart.defaults;
var defaultMode = overrides.line.interaction.mode;

defaults.hover.onHover = callback;
overrides.line.interaction.mode = 'test';
defaults.onHover = callback;
overrides.line.interaction = {
mode: 'test'
};

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

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

defaults.hover.onHover = null;
overrides.line.interaction.mode = defaultMode;
defaults.onHover = null;
delete overrides.line.interaction;
});

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

defaults.hover.onHover = callback;
overrides.line.hover.mode = 'test';
defaults.onHover = callback;
overrides.line.hover = {
mode: 'test'
};

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

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

defaults.hover.onHover = null;
delete overrides.line.hover.mode;
defaults.onHover = null;
delete overrides.line.hover;
});

it('should override default options', function() {
var callback = function() {};
var defaults = Chart.defaults;
var defaultSpanGaps = defaults.datasets.line.spanGaps;

defaults.hover.onHover = callback;
overrides.line.hover.mode = 'x-axis';
defaults.onHover = callback;
overrides.line.hover = {
mode: 'x-axis'
};
defaults.datasets.line.spanGaps = true;

var chart = acquireChart({
Expand All @@ -161,8 +166,8 @@ describe('Chart', function() {
expect(options.hover.mode).toBe('dataset');
expect(options.plugins.title.position).toBe('bottom');

defaults.hover.onHover = null;
delete overrides.line.hover.mode;
defaults.onHover = null;
delete overrides.line.hover;
defaults.datasets.line.spanGaps = defaultSpanGaps;
});

Expand Down
9 changes: 1 addition & 8 deletions types/index.esm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1343,13 +1343,6 @@ export interface CoreInteractionOptions {
axis: 'x' | 'y' | 'xy';
}

export interface HoverInteractionOptions extends CoreInteractionOptions {
/**
* Called when any of the events fire. Passed the event, an array of active elements (bars, points, etc), and the chart.
*/
onHover(event: ChartEvent, elements: ActiveElement[], chart: Chart): void;
}

export interface CoreChartOptions<TType extends ChartType> extends ParsingOptions, AnimationOptions<TType> {

datasets: {
Expand Down Expand Up @@ -1418,7 +1411,7 @@ export interface CoreChartOptions<TType extends ChartType> extends ParsingOption

interaction: CoreInteractionOptions;

hover: HoverInteractionOptions;
hover: CoreInteractionOptions;

/**
* The events option defines the browser events that the chart should listen to for tooltips and hovering.
Expand Down