Browse Source

Add zoom.initialRange option for d3.v4

pull/2246/head
Masayuki Tanaka 7 years ago
parent
commit
063ff71d74
  1. 6
      htdocs/index.html
  2. 12
      htdocs/samples/zoom.html
  3. 1
      src/config.js
  4. 8
      src/core.js
  5. 12
      src/interaction.js
  6. 7
      src/zoom.js

6
htdocs/index.html

@ -179,9 +179,9 @@
</a> </a>
</div> </div>
<div class="col-md-4"> <div class="col-md-4">
<h3>Default X Extent</h3> <h3>Default X Selection</h3>
<a href="./samples/axes_x_extent.html"> <a href="./samples/axes_x_selection.html">
Set default x extent Set default x selection
</a> </a>
</div> </div>
</div> </div>

12
htdocs/samples/zoom.html

@ -18,13 +18,9 @@
generateData(100) generateData(100)
], ],
}, },
axis: {
x: {
default: [30, 60]
}
},
zoom: { zoom: {
enabled: true, enabled: true,
initialRange: [30, 60],
onzoomstart: function (event) { onzoomstart: function (event) {
console.log("onzoomstart", event); console.log("onzoomstart", event);
}, },
@ -44,10 +40,12 @@
}, },
axis: { axis: {
x: { x: {
default: [30, 60]
} }
}, },
zoom: { enabled: true }, zoom: {
enabled: true,
initialRange: [30, 60],
},
}); });
function load() { function load() {

1
src/config.js

@ -13,6 +13,7 @@ c3_chart_internal_fn.getDefaultConfig = function () {
padding_bottom: undefined, padding_bottom: undefined,
resize_auto: true, resize_auto: true,
zoom_enabled: false, zoom_enabled: false,
zoom_initialRange: undefined,
zoom_privileged: false, zoom_privileged: false,
zoom_rescale: false, zoom_rescale: false,
zoom_onzoom: function () {}, zoom_onzoom: function () {},

8
src/core.js

@ -251,7 +251,7 @@ c3_chart_internal_fn.initWithData = function (data) {
if ($$.initTitle) { $$.initTitle(); } if ($$.initTitle) { $$.initTitle(); }
if ($$.initZoom) { $$.initZoom(); } if ($$.initZoom) { $$.initZoom(); }
// Update extent based on size and scale // Update selection based on size and scale
// TODO: currently this must be called after initLegend because of update of sizes, but it should be done in initSubchart. // TODO: currently this must be called after initLegend because of update of sizes, but it should be done in initSubchart.
if ($$.initSubchartBrush) { $$.initSubchartBrush(); } if ($$.initSubchartBrush) { $$.initSubchartBrush(); }
@ -277,9 +277,6 @@ c3_chart_internal_fn.initWithData = function (data) {
// Grid lines // Grid lines
if (config.grid_lines_front) { $$.initGridLines(); } if (config.grid_lines_front) { $$.initGridLines(); }
// Cover whole with rects for events
$$.initEventRect();
// Define g for chart // Define g for chart
$$.initChartElements(); $$.initChartElements();
@ -289,6 +286,9 @@ c3_chart_internal_fn.initWithData = function (data) {
// Set targets // Set targets
$$.updateTargets($$.data.targets); $$.updateTargets($$.data.targets);
// Cover whole with rects for events
$$.initEventRect();
// Set default extent if defined // Set default extent if defined
if (config.axis_x_selection) { $$.brush.selectionAsValue($$.getDefaultSelection()); } if (config.axis_x_selection) { $$.brush.selectionAsValue($$.getDefaultSelection()); }

12
src/interaction.js

@ -12,7 +12,13 @@ c3_chart_internal_fn.initEventRect = function () {
// event rect handle zoom event as well // event rect handle zoom event as well
if (config.zoom_enabled && $$.zoom) { if (config.zoom_enabled && $$.zoom) {
$$.main.select('.' + CLASS.eventRect).call($$.zoom).on("dblclick.zoom", null); $$.eventRect.call($$.zoom).on("dblclick.zoom", null);
if (config.zoom_initialRange) {
// WORKAROUND: Add transition to apply transform immediately when no subchart
$$.eventRect.transition().duration(0).call(
$$.zoom.transform, $$.zoomTransform(config.zoom_initialRange)
);
}
} }
}; };
c3_chart_internal_fn.redrawEventRect = function () { c3_chart_internal_fn.redrawEventRect = function () {
@ -124,9 +130,9 @@ c3_chart_internal_fn.redrawEventRect = function () {
) : function () {} ) : function () {}
); );
}; };
c3_chart_internal_fn.dispatchEvent = function (type, index, mouse) { c3_chart_internal_fn.dispatchEvent = function (type, mouse) {
var $$ = this, var $$ = this,
selector = '.' + CLASS.eventRect + (!$$.isMultipleX() ? '-' + index : ''), selector = '.' + CLASS.eventRect,
eventRect = $$.main.select(selector).node(), eventRect = $$.main.select(selector).node(),
box = eventRect.getBoundingClientRect(), box = eventRect.getBoundingClientRect(),
x = box.left + (mouse ? mouse[0] : 0), x = box.left + (mouse ? mouse[0] : 0),

7
src/zoom.js

@ -45,6 +45,11 @@ c3_chart_internal_fn.initZoom = function () {
return $$.zoom.updateExtent(); return $$.zoom.updateExtent();
}; };
c3_chart_internal_fn.zoomTransform = function (range) {
var $$ = this, s = [$$.x(range[0]), $$.x(range[1])];
return $$.d3.zoomIdentity.scale($$.width / (s[1] - s[0])).translate(-s[0], 0);
};
c3_chart_internal_fn.getZoomDomain = function () { c3_chart_internal_fn.getZoomDomain = function () {
var $$ = this, config = $$.config, d3 = $$.d3, var $$ = this, config = $$.config, d3 = $$.d3,
min = d3.min([$$.orgXDomain[0], config.zoom_x_min]), min = d3.min([$$.orgXDomain[0], config.zoom_x_min]),
@ -72,7 +77,7 @@ c3_chart_internal_fn.redrawForZoom = function () {
withEventRect: false, withEventRect: false,
withDimension: false withDimension: false
}); });
if (d3.event.sourceEvent.type === 'mousemove') { if (d3.event.sourceEvent && d3.event.sourceEvent.type === 'mousemove') {
$$.cancelClick = true; $$.cancelClick = true;
} }
config.zoom_onzoom.call($$.api, x.orgDomain()); config.zoom_onzoom.call($$.api, x.orgDomain());

Loading…
Cancel
Save