Skip to content

Commit dff4482

Browse files
authored
fix(#11615): fix calculating caretX position on stacked bar with index interaction. (#11616)
1 parent 84e7238 commit dff4482

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/plugins/plugin.tooltip.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,24 @@ const positioners = {
2424
}
2525

2626
let i, len;
27-
let x = 0;
27+
let xSet = new Set();
2828
let y = 0;
2929
let count = 0;
3030

3131
for (i = 0, len = items.length; i < len; ++i) {
3232
const el = items[i].element;
3333
if (el && el.hasValue()) {
3434
const pos = el.tooltipPosition();
35-
x += pos.x;
35+
xSet.add(pos.x);
3636
y += pos.y;
3737
++count;
3838
}
3939
}
4040

41+
const xAverage = [...xSet].reduce((a, b) => a + b) / xSet.size;
42+
4143
return {
42-
x: x / count,
44+
x: xAverage,
4345
y: y / count
4446
};
4547
},

test/specs/plugin.tooltip.tests.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,62 @@ describe('Plugin.Tooltip', function() {
10881088
expect(Object.prototype.hasOwnProperty.call(fn.calls.first().args[1], 'y')).toBe(true);
10891089
expect(fn.calls.first().object instanceof Tooltip).toBe(true);
10901090
});
1091+
1092+
it('Should ignore same x position when calculating average position with index interaction on stacked bar', async function() {
1093+
var chart = window.acquireChart({
1094+
type: 'bar',
1095+
data: {
1096+
datasets: [{
1097+
label: 'Dataset 1',
1098+
data: [10, 20, 30],
1099+
pointHoverBorderColor: 'rgb(255, 0, 0)',
1100+
pointHoverBackgroundColor: 'rgb(0, 255, 0)',
1101+
stack: 'stack1',
1102+
}, {
1103+
label: 'Dataset 2',
1104+
data: [40, 40, 40],
1105+
pointHoverBorderColor: 'rgb(0, 0, 255)',
1106+
pointHoverBackgroundColor: 'rgb(0, 255, 255)',
1107+
stack: 'stack1',
1108+
}, {
1109+
label: 'Dataset 3',
1110+
data: [90, 100, 110],
1111+
pointHoverBorderColor: 'rgb(0, 0, 255)',
1112+
pointHoverBackgroundColor: 'rgb(0, 255, 255)'
1113+
}],
1114+
labels: ['Point 1', 'Point 2', 'Point 3']
1115+
},
1116+
options: {
1117+
interaction: {
1118+
mode: 'index'
1119+
},
1120+
plugins: {
1121+
position: 'average',
1122+
},
1123+
}
1124+
});
1125+
1126+
// Trigger an event over top of the
1127+
var pointIndex = 1;
1128+
var datasetIndex = 0;
1129+
var meta = chart.getDatasetMeta(datasetIndex);
1130+
var point = meta.data[pointIndex];
1131+
await jasmine.triggerMouseEvent(chart, 'mousemove', point);
1132+
1133+
var tooltipModel = chart.tooltip;
1134+
const activeElements = tooltipModel.getActiveElements();
1135+
1136+
const xPositionArray = activeElements.map((element) => element.element.x);
1137+
const xPositionArrayAverage = xPositionArray.reduce((a, b) => a + b) / xPositionArray.length;
1138+
1139+
const xPositionSet = new Set(xPositionArray);
1140+
const xPositionSetAverage = [...xPositionSet].reduce((a, b) => a + b) / xPositionSet.size;
1141+
1142+
expect(xPositionArray.length).toBe(3);
1143+
expect(xPositionSet.size).toBe(2);
1144+
expect(tooltipModel.caretX).not.toBe(xPositionArrayAverage);
1145+
expect(tooltipModel.caretX).toBe(xPositionSetAverage);
1146+
});
10911147
});
10921148

10931149
it('Should avoid tooltip truncation in x axis if there is enough space to show tooltip without truncation', async function() {

0 commit comments

Comments
 (0)