Skip to content

Commit

Permalink
Zoom for categoryAxisRenderer: manage the zoom - fix jqPlot#187
Browse files Browse the repository at this point in the history
Copy of orignal ticks and data is done in order to be able to restore them on zoom out.
Change of the ticks and the data to match the selected area.
Implementation of restoration on zoom out.

This code only works for x axis.
This code may be unefficient for large data (to be tested).
  • Loading branch information
Étienne Loks committed May 21, 2019
1 parent 71f80ec commit 855e674
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/plugins/jqplot.cursor.js
Expand Up @@ -316,7 +316,9 @@

$.jqplot.Cursor.prototype.resetZoom = function(plot, cursor) {
var axes = plot.axes;
var series = plot.series;
var cax = cursor._zoom.axes;
var cser = cursor._data_series;
if (!plot.plugins.cursor.zoomProxy && cursor._zoom.isZoomed) {
for (var ax in axes) {
// axes[ax]._ticks = [];
Expand All @@ -326,12 +328,18 @@
// axes[ax].tickInterval = cax[ax].tickInterval;
// // for date axes
// axes[ax].daTickInterval = cax[ax].daTickInterval;
axes[ax].ticks = cax[ax].ticks;
axes[ax].reset();
axes[ax]._ticks = [];
// fake out tick creation algorithm to make sure original auto
// computed format string is used if _overrideFormatString is true
axes[ax]._autoFormatString = cax[ax].tickFormatString;
}
if (cser){
for (var idxSerie=0 ; idxSerie < series.length; idxSerie++) {
series[idxSerie].data = cser[idxSerie];
}
}
plot.redraw();
cursor._zoom.isZoomed = false;
}
Expand All @@ -350,6 +358,7 @@
$.jqplot.Cursor.prototype.doZoom = function (gridpos, datapos, plot, cursor) {
var c = cursor;
var axes = plot.axes;
var series = plot.series;
var zaxes = c._zoom.axes;
var start = zaxes.start;
var end = zaxes.end;
Expand All @@ -363,6 +372,7 @@
// make a copy of the original axes to revert back.
if (c._zoom.axes[ax] == undefined) {
c._zoom.axes[ax] = {};
c._zoom.axes[ax].ticks = axes[ax].ticks;
c._zoom.axes[ax].numberTicks = axes[ax].numberTicks;
c._zoom.axes[ax].tickInterval = axes[ax].tickInterval;
// for date axes...
Expand All @@ -371,6 +381,13 @@
c._zoom.axes[ax].max = axes[ax].max;
c._zoom.axes[ax].tickFormatString = (axes[ax].tickOptions != null) ? axes[ax].tickOptions.formatString : '';
}
// make a copy of the original data to revert back.
if (c._data_series == undefined){
c._data_series = new Array();
for (var idxSerie=0 ; idxSerie < series.length; idxSerie++) {
c._data_series.push(series[idxSerie].data);
}
}


if ((c.constrainZoomTo == 'none') || (c.constrainZoomTo == 'x' && ax.charAt(0) == 'x') || (c.constrainZoomTo == 'y' && ax.charAt(0) == 'y')) {
Expand Down Expand Up @@ -400,8 +417,34 @@
_numberTicks = plot.axes.yaxis.numberTicks;
}
}

if (this.looseZoom && (axes[ax].renderer.constructor === $.jqplot.LinearAxisRenderer || axes[ax].renderer.constructor === $.jqplot.LogAxisRenderer )) { //} || axes[ax].renderer.constructor === $.jqplot.DateAxisRenderer)) {

if (axes[ax].renderer.constructor === $.jqplot.CategoryAxisRenderer && curax.name === "xaxis") { // only managed for x
var tickDelta = axes[ax].max / axes[ax].ticks.length;
var currentValue = 0;
var currentData = new Array();
var newTicks = new Array();
// only get the relevant ticks and filter data
for (var idxTick=0 ; idxTick < axes[ax].ticks.length; idxTick++) {
if (currentValue >= newmin && currentValue <= newmax){
newTicks.push(axes[ax].ticks[idxTick]);
for (var idxSerie=0 ; idxSerie < series.length; idxSerie++) {
if (currentData.length <= idxSerie) currentData.push(new Array());
currentData[idxSerie].push(
[newTicks.length, series[idxSerie].data[idxTick][1]]);
}
}
currentValue += tickDelta;
}
if (newTicks.length){
axes[ax].min = newmin;
axes[ax].max = newmax;
axes[ax].ticks = newTicks;
for (var idxSerie=0 ; idxSerie < series.length; idxSerie++) {
series[idxSerie].data = currentData[idxSerie];
}
}
}
else if (this.looseZoom && (axes[ax].renderer.constructor === $.jqplot.LinearAxisRenderer || axes[ax].renderer.constructor === $.jqplot.LogAxisRenderer )) { //} || axes[ax].renderer.constructor === $.jqplot.DateAxisRenderer)) {

ret = $.jqplot.LinearTickGenerator(newmin, newmax, curax._scalefact, _numberTicks);

Expand All @@ -423,7 +466,6 @@
ret[0] += ret[4];
ret[2] -= 1;
}

axes[ax].min = ret[0];
axes[ax].max = ret[1];
axes[ax]._autoFormatString = ret[3];
Expand Down

0 comments on commit 855e674

Please sign in to comment.