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

[2.9] Incorrect labels when using object dataset #7854

Closed
Closed
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
111 changes: 65 additions & 46 deletions src/core/core.interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,81 @@ function getDistanceMetricForAxis(axis) {
};
}

function x(chart, e, options) {
var position = getRelativePosition(e, chart);
var items = [];
var intersectsItem = false;

parseVisibleItems(chart, function(element) {
if (element.inXRange(position.x)) {
items.push(element);
}

if (element.inRange(position.x, position.y)) {
intersectsItem = true;
}
});

// If we want to trigger on an intersect and we don't have any items
// that intersect the position, return nothing
if (options.intersect && !intersectsItem) {
items = [];
}
return items;
}

function y(chart, e, options) {
var position = getRelativePosition(e, chart);
var items = [];
var intersectsItem = false;

parseVisibleItems(chart, function(element) {
if (element.inYRange(position.y)) {
items.push(element);
}

if (element.inRange(position.x, position.y)) {
intersectsItem = true;
}
});

// If we want to trigger on an intersect and we don't have any items
// that intersect the position, return nothing
if (options.intersect && !intersectsItem) {
items = [];
}
return items;
}

function indexMode(chart, e, options) {
var position = getRelativePosition(e, chart);
// Default axis for index mode is 'x' to match old behaviour
options.axis = options.axis || 'x';
var distanceMetric = getDistanceMetricForAxis(options.axis);
var items = options.intersect ? getIntersectItems(chart, position) : getNearestItems(chart, position, false, distanceMetric);
var items;
var elements = [];

if (options.intersect) {
items = options.axis === 'x' ? x(chart, e, options) : y(chart, e, options);
} else {
items = getNearestItems(chart, position, false, distanceMetric);
}

if (!items.length) {
return [];
}

chart._getSortedVisibleDatasetMetas().forEach(function(meta) {
var element = meta.data[items[0]._index];
var element;

if (options.intersect) {
var item = items.find(function(i) {
return i._datasetIndex === meta.index;
});
element = item && meta.data[item._index];
} else {
element = meta.data[items[0]._index];
}

// don't count items that are skipped (null data)
if (element && !element._view.skip) {
Expand Down Expand Up @@ -246,28 +307,7 @@ module.exports = {
* @param {IInteractionOptions} options - options to use
* @return {Chart.Element[]} Array of elements that are under the point. If none are found, an empty array is returned
*/
x: function(chart, e, options) {
var position = getRelativePosition(e, chart);
var items = [];
var intersectsItem = false;

parseVisibleItems(chart, function(element) {
if (element.inXRange(position.x)) {
items.push(element);
}

if (element.inRange(position.x, position.y)) {
intersectsItem = true;
}
});

// If we want to trigger on an intersect and we don't have any items
// that intersect the position, return nothing
if (options.intersect && !intersectsItem) {
items = [];
}
return items;
},
x: x,

/**
* y mode returns the elements that hit-test at the current y coordinate
Expand All @@ -277,27 +317,6 @@ module.exports = {
* @param {IInteractionOptions} options - options to use
* @return {Chart.Element[]} Array of elements that are under the point. If none are found, an empty array is returned
*/
y: function(chart, e, options) {
var position = getRelativePosition(e, chart);
var items = [];
var intersectsItem = false;

parseVisibleItems(chart, function(element) {
if (element.inYRange(position.y)) {
items.push(element);
}

if (element.inRange(position.x, position.y)) {
intersectsItem = true;
}
});

// If we want to trigger on an intersect and we don't have any items
// that intersect the position, return nothing
if (options.intersect && !intersectsItem) {
items = [];
}
return items;
}
y: y
}
};
12 changes: 11 additions & 1 deletion src/scales/scale.category.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,19 @@ module.exports = Scale.extend({
getLabelForIndex: function(index, datasetIndex) {
var me = this;
var chart = me.chart;
var dataset = chart.data.datasets[datasetIndex];
var data = dataset.data[index];

if (chart.getDatasetMeta(datasetIndex).controller._getValueScaleId() === me.id) {
return me.getRightValue(chart.data.datasets[datasetIndex].data[index]);
return me.getRightValue(data);
}

if (me.isHorizontal()) {
if (data.x !== undefined) {
return data.x;
}
} else if (data.y !== undefined) {
return data.y;
}

return me._getLabels()[index];
Expand Down
117 changes: 117 additions & 0 deletions test/specs/core.tooltip.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,123 @@ describe('Core.Tooltip', function() {
expect(tooltip._view.y).toBeCloseToPixel(155);
});

it('Should display axis labels when missing datapoints', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
label: 'Test',
data: [{x: 'March', y: 10}]
}],
labels: ['January', 'February', 'March']
},
options: {
tooltips: {
mode: 'label'
}
}
});

// Trigger an event over top of the
var meta0 = chart.getDatasetMeta(0);
var point0 = meta0.data[0];

var node = chart.canvas;
var rect = node.getBoundingClientRect();

var evt = new MouseEvent('mousemove', {
view: window,
bubbles: true,
cancelable: true,
clientX: rect.left + point0._model.x,
clientY: rect.top + point0._model.y
});

// Manually trigger rather than having an async test
node.dispatchEvent(evt);

// Check and see if tooltip was displayed
var tooltip = chart.tooltip;

expect(tooltip._view).toEqual(jasmine.objectContaining({
title: ['March'],
beforeBody: [],
body: [{
before: [],
lines: ['Test: 10'],
after: []
}],
afterBody: [],
footer: []
}));

expect(tooltip._view.x).toBeCloseToPixel(417);
expect(tooltip._view.y).toBeCloseToPixel(236);
});

it('Should display axis labels when missing datapoints (Vertical)', function() {
var chart = window.acquireChart({
type: 'line',
data: {
datasets: [{
label: 'Test',
data: [
{x: 'B', y: 'February'},
{x: 'C', y: 'April'}
]
}],
labels: ['A', 'B', 'C', 'D']
},
options: {
tooltips: {
mode: 'label'
},
scales: {
yAxes: [{
type: 'category',
labels: ['January', 'February', 'March', 'April', 'May']
}]
}
}
});

// Trigger an event over top of the
var meta0 = chart.getDatasetMeta(0);
var point0 = meta0.data[0];

var node = chart.canvas;
var rect = node.getBoundingClientRect();

var evt = new MouseEvent('mousemove', {
view: window,
bubbles: true,
cancelable: true,
clientX: rect.left + point0._model.x,
clientY: rect.top + point0._model.y
});

// Manually trigger rather than having an async test
node.dispatchEvent(evt);

// Check and see if tooltip was displayed
var tooltip = chart.tooltip;

expect(tooltip._view).toEqual(jasmine.objectContaining({
title: ['B'],
beforeBody: [],
body: [{
before: [],
lines: ['Test: February'],
after: []
}],
afterBody: [],
footer: []
}));

expect(tooltip._view.x).toBeCloseToPixel(218);
expect(tooltip._view.y).toBeCloseToPixel(123);
});

it('should filter items from the tooltip using the callback', function() {
var chart = window.acquireChart({
type: 'line',
Expand Down