Skip to content

Commit

Permalink
Use maxOverflow as minimum layout padding (#8650)
Browse files Browse the repository at this point in the history
* Use maxOverflow as minimum layout padding

* fixes
  • Loading branch information
kurkle committed Mar 16, 2021
1 parent 420aa02 commit 79cc6d3
Show file tree
Hide file tree
Showing 83 changed files with 98 additions and 64 deletions.
7 changes: 4 additions & 3 deletions src/controllers/controller.line.js
Expand Up @@ -86,13 +86,14 @@ export default class LineController extends DatasetController {
getMaxOverflow() {
const me = this;
const meta = me._cachedMeta;
const border = meta.dataset.options.borderWidth || 0;
const dataset = meta.dataset;
const border = dataset.options && dataset.options.borderWidth || 0;
const data = meta.data || [];
if (!data.length) {
return border;
}
const firstPoint = data[0].size();
const lastPoint = data[data.length - 1].size();
const firstPoint = data[0].size(me.resolveDataElementOptions(0));
const lastPoint = data[data.length - 1].size(me.resolveDataElementOptions(data.length - 1));
return Math.max(border, firstPoint, lastPoint) / 2;
}

Expand Down
12 changes: 7 additions & 5 deletions src/core/core.controller.js
Expand Up @@ -474,15 +474,17 @@ class Chart {
me.notifyPlugins('beforeElementsUpdate');

// Make sure all dataset controllers have correct meta data counts
let minPadding = 0;
for (let i = 0, ilen = me.data.datasets.length; i < ilen; i++) {
const {controller} = me.getDatasetMeta(i);
const reset = !animsDisabled && newControllers.indexOf(controller) === -1;
// New controllers will be reset after the layout pass, so we only want to modify
// elements added to new datasets
controller.buildOrUpdateElements(reset);
minPadding = Math.max(+controller.getMaxOverflow(), minPadding);
}

me._updateLayout();
me._minPadding = minPadding;
me._updateLayout(minPadding);

// Only reset the controllers if we have animations
if (!animsDisabled) {
Expand Down Expand Up @@ -513,14 +515,14 @@ class Chart {
* hook, in which case, plugins will not be called on `afterLayout`.
* @private
*/
_updateLayout() {
_updateLayout(minPadding) {
const me = this;

if (me.notifyPlugins('beforeLayout', {cancelable: true}) === false) {
return;
}

layouts.update(me, me.width, me.height);
layouts.update(me, me.width, me.height, minPadding);

const area = me.chartArea;
const noArea = area.width <= 0 || area.height <= 0;
Expand Down Expand Up @@ -1087,7 +1089,7 @@ class Chart {
callCallback(options.onHover || hoverOptions.onHover, [e, active, me], me);

if (e.type === 'mouseup' || e.type === 'click' || e.type === 'contextmenu') {
if (_isPointInArea(e, me.chartArea)) {
if (_isPointInArea(e, me.chartArea, me._minPadding)) {
callCallback(options.onClick, [e, active, me], me);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/core.interaction.js
Expand Up @@ -128,7 +128,7 @@ function getDistanceMetricForAxis(axis) {
function getIntersectItems(chart, position, axis, useFinalPosition) {
const items = [];

if (!_isPointInArea(position, chart.chartArea)) {
if (!_isPointInArea(position, chart.chartArea, chart._minPadding)) {
return items;
}

Expand Down Expand Up @@ -156,7 +156,7 @@ function getNearestItems(chart, position, axis, intersect, useFinalPosition) {
let minDistance = Number.POSITIVE_INFINITY;
let items = [];

if (!_isPointInArea(position, chart.chartArea)) {
if (!_isPointInArea(position, chart.chartArea, chart._minPadding)) {
return items;
}

Expand Down
7 changes: 5 additions & 2 deletions src/core/core.layouts.js
Expand Up @@ -306,8 +306,9 @@ export default {
* @param {Chart} chart - the chart
* @param {number} width - the width to fit into
* @param {number} height - the height to fit into
* @param {number} minPadding - minimum padding required for each side of chart area
*/
update(chart, width, height) {
update(chart, width, height, minPadding) {
if (!chart) {
return;
}
Expand Down Expand Up @@ -366,8 +367,10 @@ export default {
vBoxMaxWidth: availableWidth / 2 / visibleVerticalBoxCount,
hBoxMaxHeight: availableHeight / 2
});
const maxPadding = Object.assign({}, padding);
updateMaxPadding(maxPadding, toPadding(minPadding));
const chartArea = Object.assign({
maxPadding: Object.assign({}, padding),
maxPadding,
w: availableWidth,
h: availableHeight,
x: padding.left,
Expand Down
7 changes: 4 additions & 3 deletions src/elements/element.point.js
Expand Up @@ -41,9 +41,10 @@ export default class PointElement extends Element {
return {x, y};
}

size() {
const options = this.options || {};
const radius = Math.max(options.radius, options.hoverRadius) || 0;
size(options) {
options = options || this.options || {};
let radius = options.radius || 0;
radius = Math.max(radius, radius && options.hoverRadius || 0);
const borderWidth = radius && options.borderWidth || 0;
return (radius + borderWidth) * 2;
}
Expand Down
9 changes: 5 additions & 4 deletions src/helpers/helpers.canvas.js
Expand Up @@ -244,14 +244,15 @@ export function drawPoint(ctx, options, x, y) {
* Returns true if the point is inside the rectangle
* @param {object} point - The point to test
* @param {object} area - The rectangle
* @param {number} [margin] - allowed margin
* @returns {boolean}
* @private
*/
export function _isPointInArea(point, area) {
const epsilon = 0.5; // margin - to match rounded decimals
export function _isPointInArea(point, area, margin) {
margin = margin || 0.5; // margin - default is to match rounded decimals

return point.x > area.left - epsilon && point.x < area.right + epsilon &&
point.y > area.top - epsilon && point.y < area.bottom + epsilon;
return point.x > area.left - margin && point.x < area.right + margin &&
point.y > area.top - margin && point.y < area.bottom + margin;
}

export function clipArea(ctx, area) {
Expand Down
Binary file modified test/fixtures/controller.line/clip/default-y-max.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/clip/default-y.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/non-numeric-y.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/point-style-offscreen-canvas.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/point-style.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/pointBackgroundColor/indexable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/pointBackgroundColor/value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/pointBorderColor/indexable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/pointBorderColor/scriptable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/pointBorderColor/value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/pointBorderWidth/indexable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/pointBorderWidth/scriptable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/pointBorderWidth/value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/pointStyle/indexable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/pointStyle/scriptable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/pointStyle/value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/radius/indexable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/radius/scriptable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/radius/value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/rotation/indexable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/rotation/scriptable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/rotation/value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/showLine/false.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.line/stacking/stacked-scatter.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.scatter/showLine/true.png
Binary file modified test/fixtures/controller.scatter/showLine/undefined.png
Binary file modified test/fixtures/core.layouts/hidden-vertical-boxes.png
1 change: 1 addition & 0 deletions test/fixtures/core.layouts/no-boxes-all-padding.js
Expand Up @@ -6,6 +6,7 @@ module.exports = {
datasets: [{
data: [0],
radius: 16,
borderWidth: 0,
backgroundColor: 'red'
}],
},
Expand Down
Binary file modified test/fixtures/core.layouts/no-boxes-all-padding.png
Binary file modified test/fixtures/core.layouts/refit-vertical-boxes.png
Binary file modified test/fixtures/core.scale/autoSkip/fit-after.png
Binary file modified test/fixtures/core.scale/cartesian-axis-border-settings.png
Binary file modified test/fixtures/core.scale/label-align-end.png
Binary file modified test/fixtures/core.scale/label-align-start.png
Binary file modified test/fixtures/core.scale/x-axis-position-dynamic.png
Binary file modified test/fixtures/element.line/default.png
Binary file modified test/fixtures/element.line/skip/first-span.png
Binary file modified test/fixtures/element.line/skip/first.png
Binary file modified test/fixtures/element.line/skip/last-span.png
Binary file modified test/fixtures/element.line/skip/last.png
Binary file modified test/fixtures/element.line/stepped/after.png
Binary file modified test/fixtures/element.line/stepped/before.png
Binary file modified test/fixtures/element.line/stepped/default.png
Binary file modified test/fixtures/element.line/stepped/middle.png
Binary file modified test/fixtures/element.line/tension/default.png
Binary file modified test/fixtures/element.line/tension/one.png
Binary file modified test/fixtures/element.line/tension/zero.png
Binary file modified test/fixtures/plugin.filler/fill-line-dataset-interpolated.png
3 changes: 2 additions & 1 deletion test/fixtures/plugin.tooltip/positioning.js
Expand Up @@ -12,7 +12,8 @@ module.exports = {
datasets: [{
data,
backgroundColor: 'red',
radius: 8
radius: 1,
hoverRadius: 0
}],
},
options: {
Expand Down
Binary file modified test/fixtures/plugin.tooltip/positioning.png
Binary file modified test/fixtures/scale.time/autoskip-major.png
Binary file modified test/fixtures/scale.time/custom-parser.png
Binary file modified test/fixtures/scale.time/data-ty.png
Binary file modified test/fixtures/scale.time/data-xy.png
Binary file modified test/fixtures/scale.time/negative-times.png
Binary file modified test/fixtures/scale.time/source-auto-linear.png
Binary file modified test/fixtures/scale.time/source-data-linear.png
Binary file modified test/fixtures/scale.time/source-labels-linear-offset-min-max.png
Binary file modified test/fixtures/scale.time/source-labels-linear.png
Binary file modified test/fixtures/scale.time/ticks-reverse-linear-min-max.png
Binary file modified test/fixtures/scale.time/ticks-reverse-linear.png
Binary file modified test/fixtures/scale.time/ticks-reverse-offset.png
Binary file modified test/fixtures/scale.time/ticks-reverse.png
Binary file modified test/fixtures/scale.timeseries/normalize.png
Binary file modified test/fixtures/scale.timeseries/source-auto.png
Binary file modified test/fixtures/scale.timeseries/source-data-offset-min-max.png
Binary file modified test/fixtures/scale.timeseries/source-data.png
Binary file modified test/fixtures/scale.timeseries/source-labels-offset-min-max.png
Binary file modified test/fixtures/scale.timeseries/source-labels.png
Binary file modified test/fixtures/scale.timeseries/ticks-reverse-max.png
Binary file modified test/fixtures/scale.timeseries/ticks-reverse-min-max.png
Binary file modified test/fixtures/scale.timeseries/ticks-reverse-min.png
Binary file modified test/fixtures/scale.timeseries/ticks-reverse.png
6 changes: 3 additions & 3 deletions test/specs/controller.bar.tests.js
Expand Up @@ -691,9 +691,9 @@ describe('Chart.controllers.bar', function() {
var bar2 = meta.data[1];

expect(bar1.x).toBeCloseToPixel(179);
expect(bar1.y).toBeCloseToPixel(114);
expect(bar2.x).toBeCloseToPixel(435);
expect(bar2.y).toBeCloseToPixel(0);
expect(bar1.y).toBeCloseToPixel(117);
expect(bar2.x).toBeCloseToPixel(431);
expect(bar2.y).toBeCloseToPixel(4);
});

it('should get the bar points for hidden dataset', function() {
Expand Down
82 changes: 41 additions & 41 deletions test/specs/controller.line.tests.js
Expand Up @@ -142,8 +142,8 @@ describe('Chart.controllers.line', function() {
expect(meta._parsed.length).toBe(2);

[
{x: 0, y: 512},
{x: 171, y: 0}
{x: 5, y: 507},
{x: 171, y: 5}
].forEach(function(expected, i) {
expect(meta.data[i].x).toBeCloseToPixel(expected.x);
expect(meta.data[i].y).toBeCloseToPixel(expected.y);
Expand Down Expand Up @@ -192,7 +192,7 @@ describe('Chart.controllers.line', function() {
var meta = chart.getDatasetMeta(0);
// 1 point
var point = meta.data[0];
expect(point.x).toBeCloseToPixel(0);
expect(point.x).toBeCloseToPixel(5);

// 2 points
chart.data.labels = ['One', 'Two'];
Expand All @@ -201,8 +201,8 @@ describe('Chart.controllers.line', function() {

var points = meta.data;

expect(points[0].x).toBeCloseToPixel(0);
expect(points[1].x).toBeCloseToPixel(512);
expect(points[0].x).toBeCloseToPixel(5);
expect(points[1].x).toBeCloseToPixel(507);

// 3 points
chart.data.labels = ['One', 'Two', 'Three'];
Expand All @@ -211,9 +211,9 @@ describe('Chart.controllers.line', function() {

points = meta.data;

expect(points[0].x).toBeCloseToPixel(0);
expect(points[0].x).toBeCloseToPixel(5);
expect(points[1].x).toBeCloseToPixel(256);
expect(points[2].x).toBeCloseToPixel(512);
expect(points[2].x).toBeCloseToPixel(507);

// 4 points
chart.data.labels = ['One', 'Two', 'Three', 'Four'];
Expand All @@ -222,10 +222,10 @@ describe('Chart.controllers.line', function() {

points = meta.data;

expect(points[0].x).toBeCloseToPixel(0);
expect(points[0].x).toBeCloseToPixel(5);
expect(points[1].x).toBeCloseToPixel(171);
expect(points[2].x).toBeCloseToPixel(340);
expect(points[3].x).toBeCloseToPixel(512);
expect(points[3].x).toBeCloseToPixel(507);
});

it('should update elements when the y scale is stacked', function() {
Expand Down Expand Up @@ -261,10 +261,10 @@ describe('Chart.controllers.line', function() {
var meta0 = chart.getDatasetMeta(0);

[
{x: 0, y: 146},
{x: 171, y: 439},
{x: 341, y: 146},
{x: 512, y: 439}
{x: 5, y: 148},
{x: 171, y: 435},
{x: 341, y: 148},
{x: 507, y: 435}
].forEach(function(values, i) {
expect(meta0.data[i].x).toBeCloseToPixel(values.x);
expect(meta0.data[i].y).toBeCloseToPixel(values.y);
Expand All @@ -273,10 +273,10 @@ describe('Chart.controllers.line', function() {
var meta1 = chart.getDatasetMeta(1);

[
{x: 0, y: 0},
{x: 171, y: 73},
{x: 341, y: 146},
{x: 512, y: 497}
{x: 5, y: 5},
{x: 171, y: 76},
{x: 341, y: 148},
{x: 507, y: 492}
].forEach(function(values, i) {
expect(meta1.data[i].x).toBeCloseToPixel(values.x);
expect(meta1.data[i].y).toBeCloseToPixel(values.y);
Expand Down Expand Up @@ -326,10 +326,10 @@ describe('Chart.controllers.line', function() {
var meta0 = chart.getDatasetMeta(0);

[
{x: 0, y: 146},
{x: 171, y: 439},
{x: 341, y: 146},
{x: 512, y: 439}
{x: 5, y: 148},
{x: 171, y: 435},
{x: 341, y: 148},
{x: 507, y: 435}
].forEach(function(values, i) {
expect(meta0.data[i].x).toBeCloseToPixel(values.x);
expect(meta0.data[i].y).toBeCloseToPixel(values.y);
Expand All @@ -338,10 +338,10 @@ describe('Chart.controllers.line', function() {
var meta1 = chart.getDatasetMeta(1);

[
{x: 0, y: 0},
{x: 171, y: 73},
{x: 341, y: 146},
{x: 512, y: 497}
{x: 5, y: 5},
{x: 171, y: 76},
{x: 341, y: 148},
{x: 507, y: 492}
].forEach(function(values, i) {
expect(meta1.data[i].x).toBeCloseToPixel(values.x);
expect(meta1.data[i].y).toBeCloseToPixel(values.y);
Expand Down Expand Up @@ -406,10 +406,10 @@ describe('Chart.controllers.line', function() {
var meta0 = chart.getDatasetMeta(0);

[
{x: 0, y: 146},
{x: 171, y: 439},
{x: 341, y: 146},
{x: 512, y: 439}
{x: 5, y: 148},
{x: 171, y: 435},
{x: 341, y: 148},
{x: 507, y: 435}
].forEach(function(values, i) {
expect(meta0.data[i].x).toBeCloseToPixel(values.x);
expect(meta0.data[i].y).toBeCloseToPixel(values.y);
Expand All @@ -418,10 +418,10 @@ describe('Chart.controllers.line', function() {
var meta1 = chart.getDatasetMeta(1);

[
{x: 0, y: 0},
{x: 171, y: 73},
{x: 341, y: 146},
{x: 512, y: 497}
{x: 5, y: 5},
{x: 171, y: 76},
{x: 341, y: 148},
{x: 507, y: 492}
].forEach(function(values, i) {
expect(meta1.data[i].x).toBeCloseToPixel(values.x);
expect(meta1.data[i].y).toBeCloseToPixel(values.y);
Expand Down Expand Up @@ -462,10 +462,10 @@ describe('Chart.controllers.line', function() {
var meta0 = chart.getDatasetMeta(0);

[
{x: 0, y: 146},
{x: 171, y: 439},
{x: 341, y: 146},
{x: 512, y: 439}
{x: 5, y: 148},
{x: 171, y: 435},
{x: 341, y: 148},
{x: 507, y: 435}
].forEach(function(values, i) {
expect(meta0.data[i].x).toBeCloseToPixel(values.x);
expect(meta0.data[i].y).toBeCloseToPixel(values.y);
Expand All @@ -474,10 +474,10 @@ describe('Chart.controllers.line', function() {
var meta1 = chart.getDatasetMeta(1);

[
{x: 0, y: 0},
{x: 171, y: 73},
{x: 341, y: 146},
{x: 512, y: 497}
{x: 5, y: 5},
{x: 171, y: 76},
{x: 341, y: 148},
{x: 507, y: 492}
].forEach(function(values, i) {
expect(meta1.data[i].x).toBeCloseToPixel(values.x);
expect(meta1.data[i].y).toBeCloseToPixel(values.y);
Expand Down
24 changes: 24 additions & 0 deletions test/specs/core.controller.tests.js
Expand Up @@ -275,6 +275,30 @@ describe('Chart', function() {
expect(chart.getActiveElements()).toEqual([{datasetIndex: 0, index: 1, element: point}]);
});

it('should activate element on hover when minPadding pixels outside chart area', async function() {
var chart = acquireChart({
type: 'line',
data: {
labels: ['A', 'B', 'C', 'D'],
datasets: [{
data: [10, 20, 30, 100],
hoverRadius: 0
}],
},
options: {
scales: {
x: {display: false},
y: {display: false}
}
}
});

var point = chart.getDatasetMeta(0).data[0];

await jasmine.triggerMouseEvent(chart, 'mousemove', {x: 1, y: point.y});
expect(chart.getActiveElements()).toEqual([{datasetIndex: 0, index: 0, element: point}]);
});

it('should not activate elements when hover is disabled', async function() {
var chart = acquireChart({
type: 'line',
Expand Down

0 comments on commit 79cc6d3

Please sign in to comment.