Skip to content

Commit fc69374

Browse files
authored
Add drawTime option to zoom drag configuration (#680)
* Add drawTime option to zoom drag configuration * adds tests * adds type definitions * fixes CC * adds documentation
1 parent f6adccb commit fc69374

7 files changed

Lines changed: 111 additions & 18 deletions

File tree

docs/guide/options.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,22 @@ const chart = new Chart('id', {
7777
| `backgroundColor` | `Color` | `'rgba(225,225,225,0.3)'` | Fill color
7878
| `borderColor` | `Color` | `'rgba(225,225,225)'` | Stroke color
7979
| `borderWidth` | `number` | `0` | Stroke width
80+
| [`drawTime`](#draw-time) | `string` | `beforeDatasetsDraw` | When the dragging box is dran on the chart
8081
| `threshold` | `number` | `0` | Minimal zoom distance required before actually applying zoom
8182
| `modifierKey` | `'ctrl'`\|`'alt'`\|`'shift'`\|`'meta'` | `null` | Modifier key required for drag-to-zoom
8283

84+
85+
## Draw Time
86+
87+
The `drawTime` option for zooming determines where in the chart lifecycle the drag box drawing occurs. Four potential options are available:
88+
89+
| Option | Notes
90+
| ---- | ----
91+
| `'beforeDraw'` | Occurs before any drawing takes place
92+
| `'beforeDatasetsDraw'` | Occurs after drawing of axes, but before datasets
93+
| `'afterDatasetsDraw'` | Occurs after drawing of datasets but before items such as the tooltip
94+
| `'afterDraw'` | After other drawing is completed
95+
8396
#### Pinch options
8497

8598
| Name | Type | Default | Description

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"scripts": {
1616
"build": "rollup -c",
1717
"dev": "karma start --auto-watch --no-single-run --browsers chrome",
18+
"dev:ff": "karma start --auto-watch --no-single-run --browsers firefox",
1819
"docs": "npm run build && vuepress build docs --no-cache",
1920
"docs:dev": "npm run build && vuepress dev docs --no-cache",
2021
"lint-js": "eslint \"samples/**/*.html\" \"test/**/*.js\" \"src/**/*.js\"",

src/plugin.js

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ import {panFunctions, zoomFunctions, zoomRectFunctions} from './scale.types';
66
import {getState, removeState} from './state';
77
import {version} from '../package.json';
88

9+
function draw(chart, caller, options) {
10+
const dragOptions = options.zoom.drag;
11+
const {dragStart, dragEnd} = getState(chart);
12+
13+
if (dragOptions.drawTime !== caller || !dragEnd) {
14+
return;
15+
}
16+
const {left, top, width, height} = computeDragRect(chart, options.zoom.mode, dragStart, dragEnd);
17+
const ctx = chart.ctx;
18+
19+
ctx.save();
20+
ctx.beginPath();
21+
ctx.fillStyle = dragOptions.backgroundColor || 'rgba(225,225,225,0.3)';
22+
ctx.fillRect(left, top, width, height);
23+
24+
if (dragOptions.borderWidth > 0) {
25+
ctx.lineWidth = dragOptions.borderWidth;
26+
ctx.strokeStyle = dragOptions.borderColor || 'rgba(225,225,225)';
27+
ctx.strokeRect(left, top, width, height);
28+
}
29+
ctx.restore();
30+
}
31+
932
export default {
1033
id: 'zoom',
1134

@@ -26,6 +49,7 @@ export default {
2649
},
2750
drag: {
2851
enabled: false,
52+
drawTime: 'beforeDatasetsDraw',
2953
modifierKey: null
3054
},
3155
pinch: {
@@ -75,27 +99,20 @@ export default {
7599
addListeners(chart, options);
76100
},
77101

78-
beforeDatasetsDraw: function(chart, args, options) {
79-
const {dragStart, dragEnd} = getState(chart);
80-
81-
if (dragEnd) {
82-
const {left, top, width, height} = computeDragRect(chart, options.zoom.mode, dragStart, dragEnd);
102+
beforeDatasetsDraw(chart, _args, options) {
103+
draw(chart, 'beforeDatasetsDraw', options);
104+
},
83105

84-
const dragOptions = options.zoom.drag;
85-
const ctx = chart.ctx;
106+
afterDatasetsDraw(chart, _args, options) {
107+
draw(chart, 'afterDatasetsDraw', options);
108+
},
86109

87-
ctx.save();
88-
ctx.beginPath();
89-
ctx.fillStyle = dragOptions.backgroundColor || 'rgba(225,225,225,0.3)';
90-
ctx.fillRect(left, top, width, height);
110+
beforeDraw(chart, _args, options) {
111+
draw(chart, 'beforeDraw', options);
112+
},
91113

92-
if (dragOptions.borderWidth > 0) {
93-
ctx.lineWidth = dragOptions.borderWidth;
94-
ctx.strokeStyle = dragOptions.borderColor || 'rgba(225,225,225)';
95-
ctx.strokeRect(left, top, width, height);
96-
}
97-
ctx.restore();
98-
}
114+
afterDraw(chart, _args, options) {
115+
draw(chart, 'afterDraw', options);
99116
},
100117

101118
stop: function(chart) {

test/fixtures/zoom/dragDrawTime.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const data = [];
2+
for (let i = 0; i < 100; i++) {
3+
data.push({x: i, y: Math.sin(i / 25 * Math.PI) * 10});
4+
}
5+
6+
module.exports = {
7+
tolerance: 0.004,
8+
config: {
9+
type: 'line',
10+
data: {
11+
datasets: [{
12+
data,
13+
borderColor: 'red'
14+
}]
15+
},
16+
options: {
17+
scales: {
18+
x: {type: 'linear', display: false},
19+
y: {display: false}
20+
},
21+
plugins: {
22+
legend: false,
23+
zoom: {
24+
zoom: {
25+
drag: {
26+
enabled: true,
27+
backgroundColor: 'yellow',
28+
borderColor: 'black',
29+
borderWidth: 1,
30+
drawTime: 'afterDraw'
31+
},
32+
}
33+
}
34+
},
35+
layout: {
36+
padding: 2
37+
}
38+
}
39+
},
40+
options: {
41+
run(chart) {
42+
const scaleX = chart.scales.x;
43+
const scaleY = chart.scales.y;
44+
jasmine.triggerMouseEvent(chart, 'mousedown', {
45+
x: scaleX.getPixelForValue(5),
46+
y: scaleY.getPixelForValue(10)
47+
});
48+
jasmine.triggerMouseEvent(chart, 'mousemove', {
49+
x: scaleX.getPixelForValue(60),
50+
y: scaleY.getPixelForValue(0)
51+
});
52+
chart.render = function() { };
53+
}
54+
}
55+
};
22.1 KB
Loading

test/specs/defaults.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('defaults', function() {
1414
},
1515
drag: {
1616
enabled: false,
17+
drawTime: 'beforeDatasetsDraw',
1718
modifierKey: null
1819
},
1920
pinch: {

types/options.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Chart, Color, Point } from 'chart.js';
33

44
type Mode = 'x' | 'y' | 'xy';
55
type Key = 'ctrl' | 'alt' | 'shift' | 'meta';
6+
type DrawTime = 'afterDraw' | 'afterDatasetsDraw' | 'beforeDraw' | 'beforeDatasetsDraw';
67

78
export interface WheelOptions {
89
/**
@@ -52,6 +53,11 @@ export interface DragOptions {
5253
* Modifier key required for drag-to-zoom
5354
*/
5455
modifierKey?: Key;
56+
57+
/**
58+
* Draw time required for drag-to-zoom
59+
*/
60+
drawTime?: DrawTime;
5561
}
5662

5763
export interface PinchOptions {

0 commit comments

Comments
 (0)