Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit 1fee14c

Browse files
committed
add option hitPadding
closes #21 add hitPadding option to increase the hit area for small boxes
1 parent 8ce3ddc commit 1fee14c

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ interface IBaseStyling {
7373
* @default see rectangle backgroundColor
7474
*/
7575
itemBorderColor: string;
76+
77+
/**
78+
* padding thst is added around the bounding box when computing a mouse hit
79+
* @default 1
80+
*/
81+
hitPadding: number;
7682
}
7783

7884
interface IBoxPlotStyling extends IBaseStyling {

samples/minmax.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<title>Box Plot Chart</title>
6+
<script src="../node_modules/chart.js/dist/Chart.bundle.js"></script>
7+
<script src="../build/Chart.BoxPlot.js" type="text/javascript"></script>
8+
<script src="https://unpkg.com/d3-random"></script>
9+
<script src="./utils.js"></script>
10+
<style>
11+
canvas {
12+
-moz-user-select: none;
13+
-webkit-user-select: none;
14+
-ms-user-select: none;
15+
}
16+
</style>
17+
</head>
18+
19+
<body>
20+
<div id="container" style="width: 75%;">
21+
<canvas id="canvas"></canvas>
22+
</div>
23+
<script>
24+
var boxplotData = {
25+
labels: ['A', 'B'],
26+
datasets: [{
27+
label: 'Min = Max',
28+
data: [
29+
[10, 20, 30, 40, 60, 80, 100],
30+
[20, 20, 20]
31+
]
32+
}]
33+
34+
};
35+
36+
window.onload = function() {
37+
var ctx = document.getElementById("canvas").getContext("2d");
38+
window.myBar = new Chart(ctx, {
39+
type: 'boxplot',
40+
data: boxplotData,
41+
options: {
42+
responsive: true,
43+
legend: {
44+
position: 'top',
45+
},
46+
title: {
47+
display: true,
48+
text: 'Chart.js Box Plot Chart'
49+
},
50+
scales: {
51+
xAxes: [{
52+
// Specific to Bar Controller
53+
categoryPercentage: 0.9,
54+
barPercentage: 0.8
55+
}]
56+
}
57+
}
58+
});
59+
};
60+
</script>
61+
</body>
62+
63+
</html>

src/controllers/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const array = {
2727
const options = this._elementOptions();
2828

2929
Chart.controllers.bar.prototype.updateElement.call(this, elem, index, reset);
30-
['outlierRadius', 'itemRadius', 'itemStyle', 'itemBackgroundColor', 'itemBorderColor', 'outlierColor'].forEach((item) => {
30+
['outlierRadius', 'itemRadius', 'itemStyle', 'itemBackgroundColor', 'itemBorderColor', 'outlierColor', 'hitPadding'].forEach((item) => {
3131
elem._model[item] = custom[item] !== undefined ? custom[item] : Chart.helpers.valueAtIndexOrDefault(dataset[item], index, options[item]);
3232
});
3333
},

src/elements/base.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export const defaults = Object.assign({}, Chart.defaults.global.elements.rectang
1010
itemRadius: 0,
1111
itemStyle: 'circle',
1212
itemBackgroundColor: Chart.defaults.global.elements.rectangle.backgroundColor,
13-
itemBorderColor: Chart.defaults.global.elements.rectangle.borderColor
13+
itemBorderColor: Chart.defaults.global.elements.rectangle.borderColor,
14+
hitPadding: 2
1415
});
1516

1617
const ArrayElementBase = Chart.Element.extend({
@@ -70,32 +71,42 @@ const ArrayElementBase = Chart.Element.extend({
7071
bottom: 0
7172
};
7273
},
74+
_getHitBounds() {
75+
const padding = this._view.hitPadding;
76+
const b = this._getBounds();
77+
return {
78+
left: b.left - padding,
79+
top: b.top - padding,
80+
right: b.right + padding,
81+
bottom: b.bottom + padding
82+
};
83+
},
7384
height() {
7485
return 0; // abstract
7586
},
7687
inRange(mouseX, mouseY) {
7788
if (!this._view) {
7889
return false;
7990
}
80-
const bounds = this._getBounds();
91+
const bounds = this._getHitBounds();
8192
return mouseX >= bounds.left && mouseX <= bounds.right && mouseY >= bounds.top && mouseY <= bounds.bottom;
8293
},
8394
inLabelRange(mouseX, mouseY) {
8495
if (!this._view) {
8596
return false;
8697
}
87-
const bounds = this._getBounds();
98+
const bounds = this._getHitBounds();
8899
if (this.isVertical()) {
89100
return mouseX >= bounds.left && mouseX <= bounds.right;
90101
}
91102
return mouseY >= bounds.top && mouseY <= bounds.bottom;
92103
},
93104
inXRange(mouseX) {
94-
const bounds = this._getBounds();
105+
const bounds = this._getHitBounds();
95106
return mouseX >= bounds.left && mouseX <= bounds.right;
96107
},
97108
inYRange(mouseY) {
98-
const bounds = this._getBounds();
109+
const bounds = this._getHitBounds();
99110
return mouseY >= bounds.top && mouseY <= bounds.bottom;
100111
},
101112
getCenterPoint() {

0 commit comments

Comments
 (0)