-
Notifications
You must be signed in to change notification settings - Fork 533
/
zoomable.js
39 lines (32 loc) · 1.41 KB
/
zoomable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'use strict';
/**
* Creates a decorated zoomable view of the passed scale. As the finance scale deals with an array and integer positions within the
* array, it does not support the d3 zoom behaviour. d3 zoom behaviour rescales the input domain.
* Finance scale is composed of an array of dates which is fixed in length and position and a linear scale mapping index
* to range. The linear scale can be zoomed. This object decorates the scale with only the methods required by zoom
* (invert, domain, copy). On zoom, calls the based zoomed callback.
*
* NOTE: This is not a complete scale, it will throw errors if it is used for anything else but zooming
*/
module.exports = function() {
function zoomable(linear, zoomed) {
var scale = {},
domainLimit = linear.domain();
scale.invert = linear.invert;
scale.domain = function(_) {
if(!arguments.length) throw "zoomable is a write only domain. Use this scale for zooming only";
linear.domain([Math.max(domainLimit[0], _[0]), Math.min(domainLimit[1], _[1])]);
if(zoomed) zoomed(); // Callback to that we have been zoomed
return scale;
};
scale.range = function(_) {
if(!arguments.length) return linear.range();
throw "zoomable is a read only range. Use this scale for zooming only";
};
scale.copy = function() {
return zoomable(linear.copy(), zoomed);
};
return scale;
}
return zoomable;
};