Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrimmer committed Feb 13, 2019
1 parent 173d107 commit 87dba20
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
61 changes: 27 additions & 34 deletions samples/legend/callbacks.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<!doctype html>
<html>

<head>
<title>Legend Callbacks</title>
<script src="../../dist/Chart.min.js"></script>
Expand Down Expand Up @@ -40,45 +39,47 @@
<script>
var logEntry = 1;
var logElement = document.getElementById('log');
var utils = Samples.utils;
var inputs = {
min: 20,
max: 80,
count: 8,
decimals: 2,
continuity: 1
};
var config;

function log(text) {
logElement.innerText += logEntry + '. ' + text + '\n';
logElement.scrollTop = logElement.scrollHeight;
logEntry++;
}

var config = {
function generateData() {
return utils.numbers(inputs);
}
function generateLabels() {
return utils.months({count: inputs.count});
}

utils.srand(42);

config = {
type: 'line',
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
labels: generateLabels(),
datasets: [{
label: 'My First dataset',
backgroundColor: window.chartColors.red,
borderColor: window.chartColors.red,
data: [
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor()
],
backgroundColor: utils.color(0),
borderColor: utils.color(0),
data: generateData(),
fill: false,
}, {
label: 'My Second dataset',
fill: false,
backgroundColor: window.chartColors.blue,
borderColor: window.chartColors.blue,
data: [
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor(),
randomScalingFactor()
],
backgroundColor: utils.color(1),
borderColor: utils.color(1),
data: generateData(),
}]
},
options: {
Expand All @@ -93,15 +94,10 @@
log('onClick:' + legendItem.text);
}
},
responsive: true,
title: {
display: true,
text: 'Chart.js Line Chart'
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
display: true,
Expand All @@ -121,10 +117,7 @@
}
};

window.onload = function() {
var ctx = document.getElementById('canvas').getContext('2d');
window.myLine = new Chart(ctx, config);
};
new Chart(document.getElementById('canvas'), config);
</script>
</body>

Expand Down
20 changes: 9 additions & 11 deletions src/plugins/plugin.legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,20 +466,17 @@ var Legend = Element.extend({
},

/**
* Gets the legend item, if any, at a given x,y position.
* @private
* @param {number} x - The x coordinate to check
* @param {number} y - The y coordindate to check
* @return {?Object} - The legend item found
*/
getLegendItemAt: function(x, y) {
_getLegendItemAt: function(x, y) {
var me = this;
var i, hitBox, lh;

if (x >= me.left && x <= me.right && y >= me.top && y <= me.bottom) {
// See if we are touching one of the dataset boxes
var lh = me.legendHitBoxes;
for (var i = 0; i < lh.length; ++i) {
var hitBox = lh[i];
lh = me.legendHitBoxes;
for (i = 0; i < lh.length; ++i) {
hitBox = lh[i];

if (x >= hitBox.left && x <= hitBox.left + hitBox.width && y >= hitBox.top && y <= hitBox.top + hitBox.height) {
// Touching an element
Expand All @@ -500,6 +497,7 @@ var Legend = Element.extend({
var me = this;
var opts = me.options;
var type = e.type === 'mouseup' ? 'click' : e.type;
var hoveredItem;

if (type === 'mousemove') {
if (!opts.onHover && !opts.onLeave) {
Expand All @@ -509,12 +507,12 @@ var Legend = Element.extend({
if (!opts.onClick) {
return;
}
} else {
return;
} else {
return;
}

// Chart event already has relative position in it
var hoveredItem = me.getLegendItemAt(e.x, e.y);
hoveredItem = me._getLegendItemAt(e.x, e.y);

if (type === 'click') {
if (hoveredItem && opts.onClick) {
Expand Down

0 comments on commit 87dba20

Please sign in to comment.