Browse Source

Fix zoom - #598

pull/654/head
Masayuki Tanaka 10 years ago
parent
commit
1f4e222883
  1. 29
      c3.js
  2. 4
      c3.min.js
  3. 8
      src/core.js
  4. 21
      src/zoom.js

29
c3.js

@ -418,7 +418,7 @@
c3_chart_internal_fn.redraw = function (options, transitions) { c3_chart_internal_fn.redraw = function (options, transitions) {
var $$ = this, main = $$.main, d3 = $$.d3, config = $$.config; var $$ = this, main = $$.main, d3 = $$.d3, config = $$.config;
var areaIndices = $$.getShapeIndices($$.isAreaType), barIndices = $$.getShapeIndices($$.isBarType), lineIndices = $$.getShapeIndices($$.isLineType); var areaIndices = $$.getShapeIndices($$.isAreaType), barIndices = $$.getShapeIndices($$.isBarType), lineIndices = $$.getShapeIndices($$.isLineType);
var withY, withSubchart, withTransition, withTransitionForExit, withTransitionForAxis, withTransform, withUpdateXDomain, withUpdateOrgXDomain, withTrimXDomain, withLegend; var withY, withSubchart, withTransition, withTransitionForExit, withTransitionForAxis, withTransform, withUpdateXDomain, withUpdateOrgXDomain, withTrimXDomain, withLegend, withEventRect;
var hideAxis = $$.hasArcType(); var hideAxis = $$.hasArcType();
var drawArea, drawBar, drawLine, xForText, yForText; var drawArea, drawBar, drawLine, xForText, yForText;
var duration, durationForExit, durationForAxis; var duration, durationForExit, durationForAxis;
@ -437,6 +437,7 @@
withUpdateOrgXDomain = getOption(options, "withUpdateOrgXDomain", false); withUpdateOrgXDomain = getOption(options, "withUpdateOrgXDomain", false);
withTrimXDomain = getOption(options, "withTrimXDomain", true); withTrimXDomain = getOption(options, "withTrimXDomain", true);
withLegend = getOption(options, "withLegend", false); withLegend = getOption(options, "withLegend", false);
withEventRect = getOption(options, "withEventRect", true);
withTransitionForExit = getOption(options, "withTransitionForExit", withTransition); withTransitionForExit = getOption(options, "withTransitionForExit", withTransition);
withTransitionForAxis = getOption(options, "withTransitionForAxis", withTransition); withTransitionForAxis = getOption(options, "withTransitionForAxis", withTransition);
@ -575,8 +576,9 @@
.remove(); .remove();
// event rects will redrawn when flow called // event rects will redrawn when flow called
if (config.interaction_enabled && !options.flow) { if (config.interaction_enabled && !options.flow && withEventRect) {
$$.redrawEventRect(); $$.redrawEventRect();
if ($$.updateZoom) { $$.updateZoom(); }
} }
// transition should be derived from one transition // transition should be derived from one transition
@ -618,8 +620,6 @@
$$.mapToIds($$.data.targets).forEach(function (id) { $$.mapToIds($$.data.targets).forEach(function (id) {
$$.withoutFadeIn[id] = true; $$.withoutFadeIn[id] = true;
}); });
if ($$.updateZoom) { $$.updateZoom(); }
}; };
c3_chart_internal_fn.updateAndRedraw = function (options) { c3_chart_internal_fn.updateAndRedraw = function (options) {
@ -5087,23 +5087,19 @@
}; };
c3_chart_internal_fn.initZoom = function () { c3_chart_internal_fn.initZoom = function () {
var $$ = this, d3 = $$.d3, config = $$.config, var $$ = this, d3 = $$.d3, config = $$.config;
prevZoomTranslate, wheeled = false;
$$.zoom = d3.behavior.zoom() $$.zoom = d3.behavior.zoom()
.on("zoomstart", function () { .on("zoomstart", function () {
$$.zoom.altDomain = d3.event.sourceEvent.altKey ? $$.x.orgDomain() : null; $$.zoom.altDomain = d3.event.sourceEvent.altKey ? $$.x.orgDomain() : null;
config.zoom_onzoomstart.call($$.api, d3.event.sourceEvent); config.zoom_onzoomstart.call($$.api, d3.event.sourceEvent);
}) })
.on("zoom", function () { .on("zoom", function () {
// prevZoomTranslate is needed for the fix of unexpected zoom.translate after remaining zoom
if (prevZoomTranslate && wheeled) {
$$.zoom.translate(prevZoomTranslate);
}
$$.redrawForZoom.call($$); $$.redrawForZoom.call($$);
prevZoomTranslate = $$.zoom.translate();
wheeled = d3.event.sourceEvent.type === 'wheel';
}) })
.on('zoomend', function () { .on('zoomend', function () {
$$.redrawEventRect();
$$.updateZoom();
config.zoom_onzoomend.call($$.api, $$.x.orgDomain()); config.zoom_onzoomend.call($$.api, $$.x.orgDomain());
}); });
$$.zoom.scale = function (scale) { $$.zoom.scale = function (scale) {
@ -5126,7 +5122,7 @@
$$.main.selectAll('.' + CLASS.eventRect).call(z); $$.main.selectAll('.' + CLASS.eventRect).call(z);
}; };
c3_chart_internal_fn.redrawForZoom = function () { c3_chart_internal_fn.redrawForZoom = function () {
var $$ = this, d3 = $$.d3, config = $$.config, zoom = $$.zoom, x = $$.x, orgXDomain = $$.orgXDomain; var $$ = this, d3 = $$.d3, config = $$.config, zoom = $$.zoom, x = $$.x;
if (!config.zoom_enabled) { if (!config.zoom_enabled) {
return; return;
} }
@ -5138,13 +5134,14 @@
zoom.scale(x).updateScaleExtent(); zoom.scale(x).updateScaleExtent();
return; return;
} }
if ($$.isCategorized() && x.orgDomain()[0] === orgXDomain[0]) { if ($$.isCategorized() && x.orgDomain()[0] === $$.orgXDomain[0]) {
x.domain([orgXDomain[0] - 1e-10, x.orgDomain()[1]]); x.domain([$$.orgXDomain[0] - 1e-10, x.orgDomain()[1]]);
} }
$$.redraw({ $$.redraw({
withTransition: false, withTransition: false,
withY: config.zoom_rescale, withY: config.zoom_rescale,
withSubchart: false withSubchart: false,
withEventRect: false
}); });
if (d3.event.sourceEvent.type === 'mousemove') { if (d3.event.sourceEvent.type === 'mousemove') {
$$.cancelClick = true; $$.cancelClick = true;

4
c3.min.js vendored

File diff suppressed because one or more lines are too long

8
src/core.js

@ -413,7 +413,7 @@ c3_chart_internal_fn.updateTargets = function (targets) {
c3_chart_internal_fn.redraw = function (options, transitions) { c3_chart_internal_fn.redraw = function (options, transitions) {
var $$ = this, main = $$.main, d3 = $$.d3, config = $$.config; var $$ = this, main = $$.main, d3 = $$.d3, config = $$.config;
var areaIndices = $$.getShapeIndices($$.isAreaType), barIndices = $$.getShapeIndices($$.isBarType), lineIndices = $$.getShapeIndices($$.isLineType); var areaIndices = $$.getShapeIndices($$.isAreaType), barIndices = $$.getShapeIndices($$.isBarType), lineIndices = $$.getShapeIndices($$.isLineType);
var withY, withSubchart, withTransition, withTransitionForExit, withTransitionForAxis, withTransform, withUpdateXDomain, withUpdateOrgXDomain, withTrimXDomain, withLegend; var withY, withSubchart, withTransition, withTransitionForExit, withTransitionForAxis, withTransform, withUpdateXDomain, withUpdateOrgXDomain, withTrimXDomain, withLegend, withEventRect;
var hideAxis = $$.hasArcType(); var hideAxis = $$.hasArcType();
var drawArea, drawBar, drawLine, xForText, yForText; var drawArea, drawBar, drawLine, xForText, yForText;
var duration, durationForExit, durationForAxis; var duration, durationForExit, durationForAxis;
@ -432,6 +432,7 @@ c3_chart_internal_fn.redraw = function (options, transitions) {
withUpdateOrgXDomain = getOption(options, "withUpdateOrgXDomain", false); withUpdateOrgXDomain = getOption(options, "withUpdateOrgXDomain", false);
withTrimXDomain = getOption(options, "withTrimXDomain", true); withTrimXDomain = getOption(options, "withTrimXDomain", true);
withLegend = getOption(options, "withLegend", false); withLegend = getOption(options, "withLegend", false);
withEventRect = getOption(options, "withEventRect", true);
withTransitionForExit = getOption(options, "withTransitionForExit", withTransition); withTransitionForExit = getOption(options, "withTransitionForExit", withTransition);
withTransitionForAxis = getOption(options, "withTransitionForAxis", withTransition); withTransitionForAxis = getOption(options, "withTransitionForAxis", withTransition);
@ -570,8 +571,9 @@ c3_chart_internal_fn.redraw = function (options, transitions) {
.remove(); .remove();
// event rects will redrawn when flow called // event rects will redrawn when flow called
if (config.interaction_enabled && !options.flow) { if (config.interaction_enabled && !options.flow && withEventRect) {
$$.redrawEventRect(); $$.redrawEventRect();
if ($$.updateZoom) { $$.updateZoom(); }
} }
// transition should be derived from one transition // transition should be derived from one transition
@ -613,8 +615,6 @@ c3_chart_internal_fn.redraw = function (options, transitions) {
$$.mapToIds($$.data.targets).forEach(function (id) { $$.mapToIds($$.data.targets).forEach(function (id) {
$$.withoutFadeIn[id] = true; $$.withoutFadeIn[id] = true;
}); });
if ($$.updateZoom) { $$.updateZoom(); }
}; };
c3_chart_internal_fn.updateAndRedraw = function (options) { c3_chart_internal_fn.updateAndRedraw = function (options) {

21
src/zoom.js

@ -1,21 +1,17 @@
c3_chart_internal_fn.initZoom = function () { c3_chart_internal_fn.initZoom = function () {
var $$ = this, d3 = $$.d3, config = $$.config, var $$ = this, d3 = $$.d3, config = $$.config;
prevZoomTranslate, wheeled = false;
$$.zoom = d3.behavior.zoom() $$.zoom = d3.behavior.zoom()
.on("zoomstart", function () { .on("zoomstart", function () {
$$.zoom.altDomain = d3.event.sourceEvent.altKey ? $$.x.orgDomain() : null; $$.zoom.altDomain = d3.event.sourceEvent.altKey ? $$.x.orgDomain() : null;
config.zoom_onzoomstart.call($$.api, d3.event.sourceEvent); config.zoom_onzoomstart.call($$.api, d3.event.sourceEvent);
}) })
.on("zoom", function () { .on("zoom", function () {
// prevZoomTranslate is needed for the fix of unexpected zoom.translate after remaining zoom
if (prevZoomTranslate && wheeled) {
$$.zoom.translate(prevZoomTranslate);
}
$$.redrawForZoom.call($$); $$.redrawForZoom.call($$);
prevZoomTranslate = $$.zoom.translate();
wheeled = d3.event.sourceEvent.type === 'wheel';
}) })
.on('zoomend', function () { .on('zoomend', function () {
$$.redrawEventRect();
$$.updateZoom();
config.zoom_onzoomend.call($$.api, $$.x.orgDomain()); config.zoom_onzoomend.call($$.api, $$.x.orgDomain());
}); });
$$.zoom.scale = function (scale) { $$.zoom.scale = function (scale) {
@ -38,7 +34,7 @@ c3_chart_internal_fn.updateZoom = function () {
$$.main.selectAll('.' + CLASS.eventRect).call(z); $$.main.selectAll('.' + CLASS.eventRect).call(z);
}; };
c3_chart_internal_fn.redrawForZoom = function () { c3_chart_internal_fn.redrawForZoom = function () {
var $$ = this, d3 = $$.d3, config = $$.config, zoom = $$.zoom, x = $$.x, orgXDomain = $$.orgXDomain; var $$ = this, d3 = $$.d3, config = $$.config, zoom = $$.zoom, x = $$.x;
if (!config.zoom_enabled) { if (!config.zoom_enabled) {
return; return;
} }
@ -50,13 +46,14 @@ c3_chart_internal_fn.redrawForZoom = function () {
zoom.scale(x).updateScaleExtent(); zoom.scale(x).updateScaleExtent();
return; return;
} }
if ($$.isCategorized() && x.orgDomain()[0] === orgXDomain[0]) { if ($$.isCategorized() && x.orgDomain()[0] === $$.orgXDomain[0]) {
x.domain([orgXDomain[0] - 1e-10, x.orgDomain()[1]]); x.domain([$$.orgXDomain[0] - 1e-10, x.orgDomain()[1]]);
} }
$$.redraw({ $$.redraw({
withTransition: false, withTransition: false,
withY: config.zoom_rescale, withY: config.zoom_rescale,
withSubchart: false withSubchart: false,
withEventRect: false
}); });
if (d3.event.sourceEvent.type === 'mousemove') { if (d3.event.sourceEvent.type === 'mousemove') {
$$.cancelClick = true; $$.cancelClick = true;

Loading…
Cancel
Save