Skip to content

Commit 79cc6d3

Browse files
authored
Use maxOverflow as minimum layout padding (#8650)
* Use maxOverflow as minimum layout padding * fixes
1 parent 420aa02 commit 79cc6d3

File tree

83 files changed

+98
-64
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+98
-64
lines changed

src/controllers/controller.line.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ export default class LineController extends DatasetController {
8686
getMaxOverflow() {
8787
const me = this;
8888
const meta = me._cachedMeta;
89-
const border = meta.dataset.options.borderWidth || 0;
89+
const dataset = meta.dataset;
90+
const border = dataset.options && dataset.options.borderWidth || 0;
9091
const data = meta.data || [];
9192
if (!data.length) {
9293
return border;
9394
}
94-
const firstPoint = data[0].size();
95-
const lastPoint = data[data.length - 1].size();
95+
const firstPoint = data[0].size(me.resolveDataElementOptions(0));
96+
const lastPoint = data[data.length - 1].size(me.resolveDataElementOptions(data.length - 1));
9697
return Math.max(border, firstPoint, lastPoint) / 2;
9798
}
9899

src/core/core.controller.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,15 +474,17 @@ class Chart {
474474
me.notifyPlugins('beforeElementsUpdate');
475475

476476
// Make sure all dataset controllers have correct meta data counts
477+
let minPadding = 0;
477478
for (let i = 0, ilen = me.data.datasets.length; i < ilen; i++) {
478479
const {controller} = me.getDatasetMeta(i);
479480
const reset = !animsDisabled && newControllers.indexOf(controller) === -1;
480481
// New controllers will be reset after the layout pass, so we only want to modify
481482
// elements added to new datasets
482483
controller.buildOrUpdateElements(reset);
484+
minPadding = Math.max(+controller.getMaxOverflow(), minPadding);
483485
}
484-
485-
me._updateLayout();
486+
me._minPadding = minPadding;
487+
me._updateLayout(minPadding);
486488

487489
// Only reset the controllers if we have animations
488490
if (!animsDisabled) {
@@ -513,14 +515,14 @@ class Chart {
513515
* hook, in which case, plugins will not be called on `afterLayout`.
514516
* @private
515517
*/
516-
_updateLayout() {
518+
_updateLayout(minPadding) {
517519
const me = this;
518520

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

523-
layouts.update(me, me.width, me.height);
525+
layouts.update(me, me.width, me.height, minPadding);
524526

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

10891091
if (e.type === 'mouseup' || e.type === 'click' || e.type === 'contextmenu') {
1090-
if (_isPointInArea(e, me.chartArea)) {
1092+
if (_isPointInArea(e, me.chartArea, me._minPadding)) {
10911093
callCallback(options.onClick, [e, active, me], me);
10921094
}
10931095
}

src/core/core.interaction.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function getDistanceMetricForAxis(axis) {
128128
function getIntersectItems(chart, position, axis, useFinalPosition) {
129129
const items = [];
130130

131-
if (!_isPointInArea(position, chart.chartArea)) {
131+
if (!_isPointInArea(position, chart.chartArea, chart._minPadding)) {
132132
return items;
133133
}
134134

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

159-
if (!_isPointInArea(position, chart.chartArea)) {
159+
if (!_isPointInArea(position, chart.chartArea, chart._minPadding)) {
160160
return items;
161161
}
162162

src/core/core.layouts.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,9 @@ export default {
306306
* @param {Chart} chart - the chart
307307
* @param {number} width - the width to fit into
308308
* @param {number} height - the height to fit into
309+
* @param {number} minPadding - minimum padding required for each side of chart area
309310
*/
310-
update(chart, width, height) {
311+
update(chart, width, height, minPadding) {
311312
if (!chart) {
312313
return;
313314
}
@@ -366,8 +367,10 @@ export default {
366367
vBoxMaxWidth: availableWidth / 2 / visibleVerticalBoxCount,
367368
hBoxMaxHeight: availableHeight / 2
368369
});
370+
const maxPadding = Object.assign({}, padding);
371+
updateMaxPadding(maxPadding, toPadding(minPadding));
369372
const chartArea = Object.assign({
370-
maxPadding: Object.assign({}, padding),
373+
maxPadding,
371374
w: availableWidth,
372375
h: availableHeight,
373376
x: padding.left,

src/elements/element.point.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ export default class PointElement extends Element {
4141
return {x, y};
4242
}
4343

44-
size() {
45-
const options = this.options || {};
46-
const radius = Math.max(options.radius, options.hoverRadius) || 0;
44+
size(options) {
45+
options = options || this.options || {};
46+
let radius = options.radius || 0;
47+
radius = Math.max(radius, radius && options.hoverRadius || 0);
4748
const borderWidth = radius && options.borderWidth || 0;
4849
return (radius + borderWidth) * 2;
4950
}

src/helpers/helpers.canvas.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,15 @@ export function drawPoint(ctx, options, x, y) {
244244
* Returns true if the point is inside the rectangle
245245
* @param {object} point - The point to test
246246
* @param {object} area - The rectangle
247+
* @param {number} [margin] - allowed margin
247248
* @returns {boolean}
248249
* @private
249250
*/
250-
export function _isPointInArea(point, area) {
251-
const epsilon = 0.5; // margin - to match rounded decimals
251+
export function _isPointInArea(point, area, margin) {
252+
margin = margin || 0.5; // margin - default is to match rounded decimals
252253

253-
return point.x > area.left - epsilon && point.x < area.right + epsilon &&
254-
point.y > area.top - epsilon && point.y < area.bottom + epsilon;
254+
return point.x > area.left - margin && point.x < area.right + margin &&
255+
point.y > area.top - margin && point.y < area.bottom + margin;
255256
}
256257

257258
export function clipArea(ctx, area) {
-270 Bytes
-236 Bytes
253 Bytes
4.37 KB

0 commit comments

Comments
 (0)