Commit 1f10d96a authored by Masayuki Tanaka's avatar Masayuki Tanaka

Enable zoom extent option

parent edc15c94
...@@ -33,7 +33,8 @@ ...@@ -33,7 +33,8 @@
var __size_width = getConfig(['size','width'], null), var __size_width = getConfig(['size','width'], null),
__size_height = getConfig(['size','height'], null); __size_height = getConfig(['size','height'], null);
var __zoom_enabled = getConfig(['zoom','enabled'], false); var __zoom_enabled = getConfig(['zoom','enabled'], false),
__zoom_extent = getConfig(['zoom','extent'], [1, 10]);
// data - data configuration // data - data configuration
checkConfig('data', 'data is required in config'); checkConfig('data', 'data is required in config');
...@@ -362,6 +363,9 @@ ...@@ -362,6 +363,9 @@
} }
return (domain[1] - domain[0]) / (extent[1] - extent[0]); return (domain[1] - domain[0]) / (extent[1] - extent[0]);
} }
function diffDomain (d) {
return d[1]-d[0];
}
//-- Cache --// //-- Cache --//
...@@ -793,13 +797,19 @@ ...@@ -793,13 +797,19 @@
//-- Define brush/zoom -// //-- Define brush/zoom -//
var brush = d3.svg.brush().on("brush", redrawForBrush); var brush = d3.svg.brush().on("brush", redrawForBrush);
var zoom = d3.behavior.zoom().on("zoomstart", function(){ zoom.startDomain = d3.event.sourceEvent.altKey ? x.orgDomain() : null; }).on("zoom", __zoom_enabled ? redrawForZoom : null); var zoom = d3.behavior.zoom().on("zoomstart", function(){ zoom.altDomain = d3.event.sourceEvent.altKey ? x.orgDomain() : null; }).on("zoom", __zoom_enabled ? redrawForZoom : null).scaleExtent(__zoom_extent);
// define functions for c3 // define functions for c3
brush.update = function () { brush.update = function () {
if (context) context.select('.x.brush').call(this); if (context) context.select('.x.brush').call(this);
return this; return this;
} }
zoom.orgScaleExtent = zoom.scaleExtent();
zoom.updateScaleExtent = function () {
var ratio = diffDomain(x.orgDomain())/diffDomain(orgXDomain);
this.scaleExtent([this.orgScaleExtent[0]*ratio, this.orgScaleExtent[1]*ratio]);
return this;
}
/*-- Draw Chart --*/ /*-- Draw Chart --*/
...@@ -1104,7 +1114,7 @@ ...@@ -1104,7 +1114,7 @@
.call( .call(
d3.behavior.drag().origin(Object).on('drag', function(d){ d3.behavior.drag().origin(Object).on('drag', function(d){
if ( ! __data_selection_enabled) return; // do nothing if not selectable if ( ! __data_selection_enabled) return; // do nothing if not selectable
if (__zoom_enabled && ! zoom.startDomain) return; // skip if zoomable because of conflict drag dehavior if (__zoom_enabled && ! zoom.altDomain) return; // skip if zoomable because of conflict drag dehavior
var sx = dragStart[0], sy = dragStart[1], var sx = dragStart[0], sy = dragStart[1],
mouse = d3.mouse(this), mouse = d3.mouse(this),
...@@ -1263,7 +1273,7 @@ ...@@ -1263,7 +1273,7 @@
// ATTENTION: call here to update tickOffset // ATTENTION: call here to update tickOffset
if (withUpdateXDomain) { if (withUpdateXDomain) {
x.domain(brush.empty() ? orgXDomain : brush.extent()); x.domain(brush.empty() ? orgXDomain : brush.extent());
if (__zoom_enabled) zoom.x(x); if (__zoom_enabled) zoom.x(x).updateScaleExtent();
} }
y.domain(getYDomain(c3.data.targets, 'y')); y.domain(getYDomain(c3.data.targets, 'y'));
y2.domain(getYDomain(c3.data.targets, 'y2')); y2.domain(getYDomain(c3.data.targets, 'y2'));
...@@ -1461,11 +1471,14 @@ ...@@ -1461,11 +1471,14 @@
}); });
} }
function redrawForZoom() { function redrawForZoom() {
if (d3.event.sourceEvent.type === 'mousemove' && zoom.startDomain) { if (d3.event.sourceEvent.type === 'mousemove' && zoom.altDomain) {
x.domain(zoom.startDomain); x.domain(zoom.altDomain);
zoom.x(x); zoom.x(x).updateScaleExtent();
return; return;
} }
if (isCategorized && x.orgDomain()[0] === orgXDomain[0]) {
x.domain([orgXDomain[0]-1e-10, x.orgDomain()[1]]);
}
redraw({ redraw({
withTransition: false, withTransition: false,
withY: false, withY: false,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment