Skip to content

Commit

Permalink
index-y interaction mode + convert horizontal bar defaults to new mode
Browse files Browse the repository at this point in the history
  • Loading branch information
etimberg committed Jul 4, 2017
1 parent 225bfd3 commit 07803d4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
17 changes: 16 additions & 1 deletion docs/general/interactions/modes.md
Expand Up @@ -41,7 +41,7 @@ Finds the first item that intersects the point and returns it. Behaves like 'nea
See `'index'` mode

## index
Finds item at the same index. If the `intersect` setting is true, the first intersecting item is used to determine the index in the data. If `intersect` false the nearest item is used to determine the index.
Finds item at the same index. If the `intersect` setting is true, the first intersecting item is used to determine the index in the data. If `intersect` false the nearest item, in the x direction, is used to determine the index.

```javascript
var chart = new Chart(ctx, {
Expand All @@ -55,6 +55,21 @@ var chart = new Chart(ctx, {
})
```

## index-y
Finds the item at the same index along the Y axis. If the `intersect` setting is true, the first intersecting item is used to determine the index in the data. If `intersect` false the nearest item, in the y direction, is used to determine the index.

```javascript
var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
tooltips: {
mode: 'index-y'
}
}
})
```

## x-axis (deprecated)
Behaves like `'index'` mode with `intersect = false`.

Expand Down
5 changes: 3 additions & 2 deletions src/controllers/controller.bar.js
Expand Up @@ -314,7 +314,7 @@ module.exports = function(Chart) {
// it extends bar (like pie extends doughnut)
Chart.defaults.horizontalBar = {
hover: {
mode: 'label'
mode: 'index-y'
},

scales: {
Expand Down Expand Up @@ -361,7 +361,8 @@ module.exports = function(Chart) {
var datasetLabel = data.datasets[tooltipItem.datasetIndex].label || '';
return datasetLabel + ': ' + tooltipItem.xLabel;
}
}
},
mode: 'index-y'
}
};

Expand Down
60 changes: 39 additions & 21 deletions src/core/core.interaction.js
Expand Up @@ -98,32 +98,37 @@ module.exports = function(Chart) {
return nearestItems;
}

function indexMode(chart, e, options) {
var position = getRelativePosition(e, chart);
var distanceMetric = function(pt1, pt2) {
return Math.abs(pt1.x - pt2.x);
};
var items = options.intersect ? getIntersectItems(chart, position) : getNearestItems(chart, position, false, distanceMetric);
var elements = [];

if (!items.length) {
return [];
}
function buildIndexMode(distanceMetric) {
return function(chart, e, options) {
var position = getRelativePosition(e, chart);
var items = options.intersect ? getIntersectItems(chart, position) : getNearestItems(chart, position, false, distanceMetric);
var elements = [];

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

chart.data.datasets.forEach(function(dataset, datasetIndex) {
if (chart.isDatasetVisible(datasetIndex)) {
var meta = chart.getDatasetMeta(datasetIndex),
element = meta.data[items[0]._index];
chart.data.datasets.forEach(function(dataset, datasetIndex) {
if (chart.isDatasetVisible(datasetIndex)) {
var meta = chart.getDatasetMeta(datasetIndex),
element = meta.data[items[0]._index];

// don't count items that are skipped (null data)
if (element && !element._view.skip) {
elements.push(element);
// don't count items that are skipped (null data)
if (element && !element._view.skip) {
elements.push(element);
}
}
}
});
});

return elements;
return elements;
};
}
var indexMode = buildIndexMode(function(pt1, pt2) {
return Math.abs(pt1.x - pt2.x);
});
var indexYMode = buildIndexMode(function(pt1, pt2) {
return Math.abs(pt1.y - pt2.y);
});

/**
* @interface IInteractionOptions
Expand Down Expand Up @@ -175,6 +180,19 @@ module.exports = function(Chart) {
*/
index: indexMode,

/**
* Returns items at the same label along the Y axis
* If the options.intersect parameter is true, we only return items if we intersect something
* If the options.intersect mode is false, we find the nearest item and return the items at the same index as that item
* @function Chart.Interaction.modes.index-y
* @since v2.7.0
* @param chart {chart} the chart we are returning items from
* @param e {Event} the event we are find things at
* @param options {IInteractionOptions} options to use during interaction
* @return {Chart.Element[]} Array of elements that are under the point. If none are found, an empty array is returned
*/
'index-y': indexYMode,

/**
* Returns items in the same dataset. If the options.intersect parameter is true, we only return items if we intersect something
* If the options.intersect is false, we find the nearest item and return the items in that dataset
Expand Down

0 comments on commit 07803d4

Please sign in to comment.