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

Add the ability to enable both wheel and drag modes at the same time #507

Merged
merged 9 commits into from
May 14, 2021
Merged
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
36 changes: 25 additions & 11 deletions docs/guide/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,35 @@ const chart = new Chart('id', {

| Name | Type | Default | Description
| ---- | ---- | ------- | ----------
| `enabled` | `boolean` | `false` | Enable zooming
| `drag` | `boolean`\|[`DragEffectOptions`](#drag-effect-options) | `undefined` | Enable drag-to-zoom behavior (disables zooming by wheel)
| `wheel` | [`WheelOptions`](#wheel-options) | `undefined` | Options of the mouse wheel behavior
etimberg marked this conversation as resolved.
Show resolved Hide resolved
| `drag` | [`DragOptions`](#drag-options) | `undefined` | Options of the drag-to-zoom behavior
| `pinch` | [`PinchOptions`](#pinch-options) | `undefined` | Options of the pinch behavior
| `mode` | `'x'`\|`'y'`\|`'xy'` | `'xy'` | Allowed zoom directions
| `overScaleMode` | `'x'`\|`'y'`\|`'xy'` | `undefined` | Which of the enabled zooming directions should only be available when the mouse cursor is over a scale for that axis
| `speed` | `number` | `0.1` | Factor of zoom speed via mouse wheel.
| `threshold` | `number` | `0` | Mimimal zoom distance required before actually applying zoom
| `wheelModifierKey` | `'ctrl'`\|`'alt'`\|`'shift'`\|`'meta'` | `null` | Modifier key required for zooming with mouse

#### Drag effect options
#### Wheel options

| Name | Type | Description
| ---- | -----| -----------
| `backgroundColor` | `Color` | Fill color
| `borderColor` | `Color` | Stroke color
| `borderWidth` | `number` | Stroke width
| Name | Type | Default | Description
| ---- | -----| ------- | -----------
| `enabled` | `boolean` | `false` | Enable zooming via mouse wheel
| `speed` | `number` | `0.1` | Factor of zoom speed via mouse wheel
| `modifierKey` | `'ctrl'`\|`'alt'`\|`'shift'`\|`'meta'` | `null` | Modifier key required for zooming with mouse

#### Drag options

| Name | Type | Default | Description
| ---- | -----| ------- | -----------
| `enabled` | `boolean` | `false` | Enable drag-to-zoom
| `backgroundColor` | `Color` | `'rgba(225,225,225,0.3)'` | Fill color
| `borderColor` | `Color` | `'rgba(225,225,225)'` | Stroke color
| `borderWidth` | `number` | `0` | Stroke width
| `threshold` | `number` | `0` | Minimal zoom distance required before actually applying zoom

#### Pinch options

| Name | Type | Default | Description
| ---- | -----| ------- | -----------
| `enabled` | `boolean` | `false` | Enable zooming via pinch

### Zoom Events

Expand Down
7 changes: 6 additions & 1 deletion docs/guide/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ const config = {
plugins: {
zoom: {
zoom: {
enabled: true,
wheel: {
enabled: true,
},
pinch: {
enabled: true
},
mode: 'xy',
}
}
Expand Down
12 changes: 9 additions & 3 deletions docs/samples/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,19 @@ const zoomOptions = {
mode: 'xy',
},
zoom: {
enabled: true,
wheel: {
enabled: true,
},
pinch: {
enabled: true
},
mode: 'xy',
}
};
// </block:zoom>

const panStatus = () => zoomOptions.pan.enabled ? 'enabled' : 'disabled';
const zoomStatus = () => zoomOptions.zoom.enabled ? 'enabled' : 'disabled';
const zoomStatus = () => zoomOptions.zoom.wheel.enabled ? 'enabled' : 'disabled';

// <block:config:1>
const config = {
Expand All @@ -95,7 +100,8 @@ const actions = [
{
name: 'Toggle zoom',
handler(chart) {
zoomOptions.zoom.enabled = !zoomOptions.zoom.enabled;
zoomOptions.zoom.wheel.enabled = !zoomOptions.zoom.wheel.enabled;
zoomOptions.zoom.pinch.enabled = !zoomOptions.zoom.pinch.enabled;
chart.update();
}
}, {
Expand Down
5 changes: 3 additions & 2 deletions docs/samples/drag/category.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ const config = {
modifierKey: 'ctrl',
},
zoom: {
enabled: true,
drag: true,
drag: {
enabled: true
},
mode: 'x',
},
}
Expand Down
6 changes: 3 additions & 3 deletions docs/samples/drag/linear.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ const zoomOptions = {
modifierKey: 'ctrl',
},
zoom: {
enabled: true,
mode: 'xy',
drag: {
enabled: true,
borderColor: 'rgb(54, 162, 235)',
borderWidth: 1,
backgroundColor: 'rgba(54, 162, 235, 0.3)'
Expand All @@ -72,7 +72,7 @@ const zoomOptions = {
};
// </block:zoom>

const zoomStatus = () => zoomOptions.zoom.enabled ? 'enabled' : 'disabled';
const zoomStatus = () => zoomOptions.zoom.drag.enabled ? 'enabled' : 'disabled';

// <block:config:1>
const config = {
Expand All @@ -96,7 +96,7 @@ const actions = [
{
name: 'Toggle zoom',
handler(chart) {
zoomOptions.zoom.enabled = !zoomOptions.zoom.enabled;
zoomOptions.zoom.drag.enabled = !zoomOptions.zoom.drag.enabled;
chart.update();
}
}, {
Expand Down
5 changes: 3 additions & 2 deletions docs/samples/drag/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ const config = {
modifierKey: 'ctrl',
},
zoom: {
enabled: true,
drag: true,
drag: {
enabled: true
},
mode: 'xy',
},
}
Expand Down
9 changes: 5 additions & 4 deletions docs/samples/drag/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ const zoomOptions = {
modifierKey: 'ctrl',
},
zoom: {
enabled: true,
drag: true,
drag: {
enabled: true
},
mode: 'xy',
},
};
// </block>

const panStatus = () => zoomOptions.pan.enabled ? 'enabled' : 'disabled';
const zoomStatus = () => zoomOptions.zoom.enabled ? 'enabled' : 'disabled';
const zoomStatus = () => zoomOptions.zoom.drag.enabled ? 'enabled' : 'disabled';

// <block:config:1>
const config = {
Expand All @@ -92,7 +93,7 @@ const actions = [
{
name: 'Toggle zoom',
handler(chart) {
zoomOptions.zoom.enabled = !zoomOptions.zoom.enabled;
zoomOptions.zoom.drag.enabled = !zoomOptions.zoom.drag.enabled;
chart.update();
}
}, {
Expand Down
9 changes: 5 additions & 4 deletions docs/samples/drag/timeseries.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ const zoomOptions = {
modifierKey: 'ctrl',
},
zoom: {
enabled: true,
drag: true,
drag: {
enabled: true,
},
mode: 'xy',
},
};
// </block>

const panStatus = () => zoomOptions.pan.enabled ? 'enabled' : 'disabled';
const zoomStatus = () => zoomOptions.zoom.enabled ? 'enabled' : 'disabled';
const zoomStatus = () => zoomOptions.zoom.drag.enabled ? 'enabled' : 'disabled';

// <block:config:1>
const config = {
Expand All @@ -95,7 +96,7 @@ const actions = [
{
name: 'Toggle zoom',
handler(chart) {
zoomOptions.zoom.enabled = !zoomOptions.zoom.enabled;
zoomOptions.zoom.drag.enabled = !zoomOptions.zoom.drag.enabled;
chart.update();
}
}, {
Expand Down
7 changes: 6 additions & 1 deletion docs/samples/pan-region.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ const zoomOptions = {
mode: 'xy',
},
zoom: {
enabled: false,
wheel: {
enabled: false,
},
pinch: {
enabled: true
},
}
};
// </block:zoom>
Expand Down
7 changes: 6 additions & 1 deletion docs/samples/wheel/category.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ const config = {
threshold: 5,
},
zoom: {
enabled: true,
wheel: {
enabled: true
},
pinch: {
enabled: true
},
mode: 'xy',
},
}
Expand Down
12 changes: 9 additions & 3 deletions docs/samples/wheel/click-zoom.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ const zoomOptions = {
mode: 'xy',
},
zoom: {
enabled: false,
wheel: {
enabled: false,
},
pinch: {
enabled: false
},
mode: 'xy',
}
};
Expand Down Expand Up @@ -97,7 +102,8 @@ const config = {
},
onClick(e) {
const chart = e.chart;
chart.options.plugins.zoom.zoom.enabled = !chart.options.plugins.zoom.zoom.enabled;
chart.options.plugins.zoom.zoom.wheel.enabled = !chart.options.plugins.zoom.zoom.wheel.enabled;
chart.options.plugins.zoom.zoom.pinch.enabled = !chart.options.plugins.zoom.zoom.pinch.enabled;
chart.update();
}
},
Expand All @@ -117,7 +123,7 @@ const actions = [
}, {
name: 'Toggle zoom',
handler(chart) {
zoomOptions.zoom.enabled = !zoomOptions.zoom.enabled;
zoomOptions.zoom.wheel.enabled = !zoomOptions.zoom.wheel.enabled;
chart.update();
}
}, {
Expand Down
7 changes: 6 additions & 1 deletion docs/samples/wheel/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ const config = {
mode: 'xy',
},
zoom: {
enabled: true,
wheel: {
enabled: true
},
pinch: {
enabled: true,
},
mode: 'xy',
},
}
Expand Down
12 changes: 9 additions & 3 deletions docs/samples/wheel/over-scale-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ Object.keys(scales).forEach(scale => Object.assign(scales[scale], scaleOpts));
// <block:zoom:0>
const zoomOptions = {
zoom: {
enabled: true,
wheel: {
enabled: true,
},
pinch: {
enabled: true,
},
mode: 'xy',
overScaleMode: 'xy',
},
Expand All @@ -68,7 +73,7 @@ const zoomOptions = {
// </block>

const panStatus = () => zoomOptions.pan.enabled ? 'enabled' : 'disabled';
const zoomStatus = () => zoomOptions.zoom.enabled ? 'enabled' : 'disabled';
const zoomStatus = () => zoomOptions.zoom.wheel.enabled ? 'enabled' : 'disabled';

// <block:config:1>
const config = {
Expand All @@ -92,7 +97,8 @@ const actions = [
{
name: 'Toggle zoom',
handler(chart) {
zoomOptions.zoom.enabled = !zoomOptions.zoom.enabled;
zoomOptions.zoom.wheel.enabled = !zoomOptions.zoom.wheel.enabled;
zoomOptions.zoom.pinch.enabled = !zoomOptions.zoom.pinch.enabled;
chart.update();
}
}, {
Expand Down
12 changes: 9 additions & 3 deletions docs/samples/wheel/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ const scales = {
// <block:zoom:0>
const zoomOptions = {
zoom: {
enabled: true,
wheel: {
enabled: true,
},
pinch: {
enabled: true,
},
mode: 'xy',
},
pan: {
Expand All @@ -65,7 +70,7 @@ const zoomOptions = {
// </block>

const panStatus = () => zoomOptions.pan.enabled ? 'enabled' : 'disabled';
const zoomStatus = () => zoomOptions.zoom.enabled ? 'enabled' : 'disabled';
const zoomStatus = () => zoomOptions.zoom.wheel.enabled ? 'enabled' : 'disabled';

// <block:config:1>
const config = {
Expand All @@ -92,7 +97,8 @@ const actions = [
{
name: 'Toggle zoom',
handler(chart) {
zoomOptions.zoom.enabled = !zoomOptions.zoom.enabled;
zoomOptions.zoom.wheel.enabled = !zoomOptions.zoom.wheel.enabled;
zoomOptions.zoom.pinch.enabled = !zoomOptions.zoom.pinch.enabled;
chart.update();
}
}, {
Expand Down
7 changes: 6 additions & 1 deletion samples/zoom-separately.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@
overScaleMode: 'y'
},
zoom: {
enabled: true,
wheel: {
enabled: true,
},
pinch: {
enabled: true,
},
mode: 'xy',
overScaleMode: 'y'
}
Expand Down
4 changes: 2 additions & 2 deletions src/hammer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function handlePinch(chart, state, e) {
}

function startPinch(chart, state) {
if (state.options.zoom.enabled) {
if (state.options.zoom.pinch.enabled) {
state.scale = 1;
}
}
Expand Down Expand Up @@ -125,7 +125,7 @@ export function startHammer(chart, options) {
const {pan: panOptions, zoom: zoomOptions} = options;

const mc = new Hammer.Manager(canvas);
if (zoomOptions && zoomOptions.enabled) {
if (zoomOptions && zoomOptions.pinch.enabled) {
mc.add(new Hammer.Pinch());
mc.on('pinchstart', () => startPinch(chart, state));
mc.on('pinch', (e) => handlePinch(chart, state, e));
Expand Down
Loading