-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add onPanStart and onZoomStart callbacks (#487)
* Add onPanStart and onZoomStart callbacks * Split out preconditions from wheel * Add mouseup to cancelled events * cancel === false
- Loading branch information
Showing
10 changed files
with
381 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,7 @@ module.exports = { | |
'drag', | ||
'api', | ||
'click-zoom', | ||
'pan-region', | ||
], | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Pan Region | ||
|
||
In this example pan is only accepted at the middle region (50%) of the chart. This region is highlighted by a red border. | ||
|
||
```js chart-editor | ||
// <block:data:1> | ||
const NUMBER_CFG = {count: 20, min: -100, max: 100}; | ||
const data = { | ||
datasets: [{ | ||
label: 'My First dataset', | ||
borderColor: Utils.randomColor(0.4), | ||
backgroundColor: Utils.randomColor(0.1), | ||
pointBorderColor: Utils.randomColor(0.7), | ||
pointBackgroundColor: Utils.randomColor(0.5), | ||
pointBorderWidth: 1, | ||
data: Utils.points(NUMBER_CFG), | ||
}, { | ||
label: 'My Second dataset', | ||
borderColor: Utils.randomColor(0.4), | ||
backgroundColor: Utils.randomColor(0.1), | ||
pointBorderColor: Utils.randomColor(0.7), | ||
pointBackgroundColor: Utils.randomColor(0.5), | ||
pointBorderWidth: 1, | ||
data: Utils.points(NUMBER_CFG), | ||
}] | ||
}; | ||
// </block:data> | ||
|
||
// <block:scales:2> | ||
const scaleOpts = { | ||
ticks: { | ||
callback: (val, index, ticks) => index === 0 || index === ticks.length - 1 ? null : val, | ||
}, | ||
grid: { | ||
borderColor: Utils.randomColor(1), | ||
color: 'rgba( 0, 0, 0, 0.1)', | ||
}, | ||
title: { | ||
display: true, | ||
text: (ctx) => ctx.scale.axis + ' axis', | ||
} | ||
}; | ||
const scales = { | ||
x: { | ||
position: 'top', | ||
}, | ||
y: { | ||
position: 'right', | ||
}, | ||
}; | ||
Object.keys(scales).forEach(scale => Object.assign(scales[scale], scaleOpts)); | ||
// </block:scales> | ||
|
||
// <block:zoom:0> | ||
const zoomOptions = { | ||
limits: { | ||
x: {min: -200, max: 200, minRange: 50}, | ||
y: {min: -200, max: 200, minRange: 50} | ||
}, | ||
pan: { | ||
enabled: true, | ||
onPanStart({chart, point}) { | ||
const area = chart.chartArea; | ||
const w25 = area.width * 0.25; | ||
const h25 = area.height * 0.25; | ||
if (point.x < area.left + w25 || point.x > area.right - w25 | ||
|| point.y < area.top + h25 || point.y > area.bottom - h25) { | ||
return false; // abort | ||
} | ||
}, | ||
mode: 'xy', | ||
}, | ||
zoom: { | ||
enabled: false, | ||
} | ||
}; | ||
// </block:zoom> | ||
|
||
// <block:border:3> | ||
const borderPlugin = { | ||
id: 'panAreaBorder', | ||
beforeDraw(chart, args, options) { | ||
const {ctx, chartArea: {left, top, width, height}} = chart; | ||
ctx.save(); | ||
ctx.strokeStyle = 'rgba(255, 0, 0, 0.3)'; | ||
ctx.lineWidth = 1; | ||
ctx.strokeRect(left + width * 0.25, top + height * 0.25, width / 2, height / 2); | ||
ctx.restore(); | ||
} | ||
}; | ||
// </block:border> | ||
|
||
// <block:config:1> | ||
const config = { | ||
type: 'scatter', | ||
data: data, | ||
options: { | ||
scales: scales, | ||
plugins: { | ||
zoom: zoomOptions, | ||
}, | ||
}, | ||
plugins: [borderPlugin] | ||
}; | ||
// </block:config> | ||
|
||
module.exports = { | ||
config, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,93 @@ | ||
describe('pan', function() { | ||
describe('auto', jasmine.fixture.specs('pan')); | ||
|
||
const data = { | ||
datasets: [{ | ||
data: [{ | ||
x: 1, | ||
y: 3 | ||
}, { | ||
x: 2, | ||
y: 2 | ||
}, { | ||
x: 3, | ||
y: 1 | ||
}] | ||
}] | ||
}; | ||
|
||
describe('events', function() { | ||
it('should call onPanStart', function(done) { | ||
const startSpy = jasmine.createSpy('started'); | ||
const chart = window.acquireChart({ | ||
type: 'scatter', | ||
data, | ||
options: { | ||
plugins: { | ||
zoom: { | ||
pan: { | ||
enabled: true, | ||
mode: 'xy', | ||
onPanStart: startSpy | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
Simulator.gestures.pan(chart.canvas, {deltaX: -350, deltaY: 0, duration: 50}, function() { | ||
expect(startSpy).toHaveBeenCalled(); | ||
expect(chart.scales.x.min).not.toBe(1); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should call onPanRejected when onStartPan returns false', function(done) { | ||
const rejectSpy = jasmine.createSpy('rejected'); | ||
const chart = window.acquireChart({ | ||
type: 'scatter', | ||
data, | ||
options: { | ||
plugins: { | ||
zoom: { | ||
pan: { | ||
enabled: true, | ||
mode: 'xy', | ||
onPanStart: () => false, | ||
onPanRejected: rejectSpy | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
Simulator.gestures.pan(chart.canvas, {deltaX: -350, deltaY: 0, duration: 50}, function() { | ||
expect(rejectSpy).toHaveBeenCalled(); | ||
expect(chart.scales.x.min).toBe(1); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should call onPanComplete', function(done) { | ||
const chart = window.acquireChart({ | ||
type: 'scatter', | ||
data, | ||
options: { | ||
plugins: { | ||
zoom: { | ||
pan: { | ||
enabled: true, | ||
mode: 'xy', | ||
onPanComplete(ctx) { | ||
expect(ctx.chart.scales.x.min).not.toBe(1); | ||
done(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
Simulator.gestures.pan(chart.canvas, {deltaX: -350, deltaY: 0, duration: 50}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.