Skip to content

Commit

Permalink
Category pan (#33)
Browse files Browse the repository at this point in the history
Add `panSpeed` for panning charts
  • Loading branch information
QuantumBob authored and etimberg committed Sep 15, 2016
1 parent 1322ab7 commit fdc1cab
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 50 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

.DS_Store

node_modules/*
Expand Down
44 changes: 30 additions & 14 deletions Chart.Zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

},{}],2:[function(require,module,exports){
/*jslint browser:true, devel:true, white:true, vars:true */
/*global require*/

// hammer JS for touch support
var Hammer = require('hammerjs');
Hammer = typeof(Hammer) === 'function' ? Hammer : window.Hammer;
Expand All @@ -31,6 +34,7 @@ var defaultOptions = zoomNS.defaults = {
pan: {
enabled: true,
mode: 'xy',
speed: 20,
threshold: 10,
},
zoom: {
Expand Down Expand Up @@ -126,15 +130,24 @@ function doZoom(chartInstance, zoom, center) {
}
}

function panIndexScale(scale, delta) {
/*var options = scale.options;
var labels = scale.chart.data.labels;
var lastLabelIndex = labels.length - 1;
function panIndexScale(scale, delta, panOptions) {
var labels = scale.chart.data.labels;
var lastLabelIndex = labels.length - 1;
var offsetAmt = Math.max((scale.ticks.length - ((scale.options.gridLines.offsetGridLines) ? 0 : 1)), 1);
var panSpeed = panOptions.speed;
var minIndex = scale.minIndex;
var step = Math.round(scale.width / (offsetAmt * panSpeed));
var maxIndex;

zoomNS.panCumulativeDelta += delta;

minIndex = zoomNS.panCumulativeDelta > step ? Math.max(0, minIndex -1) : zoomNS.panCumulativeDelta < -step ? Math.min(lastLabelIndex - offsetAmt + 1, minIndex + 1) : minIndex;
zoomNS.panCumulativeDelta = minIndex !== scale.minIndex ? 0 : zoomNS.panCumulativeDelta;

var minIndex = Math.max(0, Math.round(scale.getValueForPixel(scale.getPixelForValue(null, scale.minIndex, null, true) - delta)));
var maxIndex = Math.min(lastLabelIndex, Math.round(scale.getValueForPixel(scale.getPixelForValue(null, scale.maxIndex, null, true) - delta)))
options.ticks.min = labels[minIndex];
options.ticks.max = labels[maxIndex];*/
maxIndex = Math.min(lastLabelIndex, minIndex + offsetAmt - 1);

scale.options.ticks.min = labels[minIndex];
scale.options.ticks.max = labels[maxIndex];
}

function panTimeScale(scale, delta) {
Expand All @@ -157,21 +170,22 @@ function panNumericalScale(scale, delta) {
}
}

function panScale(scale, delta) {
function panScale(scale, delta, panOptions) {
var fn = panFunctions[scale.options.type];
if (fn) {
fn(scale, delta);
fn(scale, delta, panOptions);
}
}

function doPan(chartInstance, deltaX, deltaY) {
var panOptions = chartInstance.options.pan;
if (panOptions && helpers.getValueOrDefault(panOptions.enabled, defaultOptions.pan.enabled)) {
var panMode = helpers.getValueOrDefault(chartInstance.options.pan.mode, defaultOptions.pan.mode);
panOptions.speed = helpers.getValueOrDefault(chartInstance.options.pan.speed, defaultOptions.pan.speed);

helpers.each(chartInstance.scales, function(scale, id) {
if (scale.isHorizontal() && directionEnabled(panMode, 'x') && deltaX !== 0) {
panScale(scale, deltaX);
panScale(scale, deltaX, panOptions);
} else if (!scale.isHorizontal() && directionEnabled(panMode, 'y') && deltaY !== 0) {
panScale(scale, deltaY);
}
Expand Down Expand Up @@ -207,6 +221,8 @@ zoomNS.panFunctions.category = panIndexScale;
zoomNS.panFunctions.time = panTimeScale;
zoomNS.panFunctions.linear = panNumericalScale;
zoomNS.panFunctions.logarithmic = panNumericalScale;
//global for catergory pan
zoomNS.panCumulativeDelta = 0;

// Chartjs Zoom Plugin
var zoomPlugin = {
Expand Down Expand Up @@ -341,7 +357,6 @@ var zoomPlugin = {
var deltaY = e.deltaY - currentDeltaY;
currentDeltaX = e.deltaX;
currentDeltaY = e.deltaY;

doPan(chartInstance, deltaX, deltaY);
}
};
Expand All @@ -353,8 +368,9 @@ var zoomPlugin = {
});
mc.on('panmove', handlePan);
mc.on('panend', function(e) {
currentDeltaX = null;
currentDeltaY = null;
currentDeltaX = null;
currentDeltaY = null;
zoomNS.panCumulativeDelta = 0;
});
chartInstance._mc = mc;
}
Expand Down
2 changes: 1 addition & 1 deletion Chart.Zoom.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
var gulp = require('gulp'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
util = require('gulp-util'),
jshint = require('gulp-jshint'),
replace = require('gulp-replace'),
insert = require('gulp-insert'),
inquirer = require('inquirer'),
semver = require('semver'),
exec = require('child_process').exec,
fs = require('fs'),
package = require('./package.json'),
browserify = require('browserify'),
streamify = require('gulp-streamify'),
source = require('vinyl-source-stream'),
merge = require('merge-stream');
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
util = require('gulp-util'),
jshint = require('gulp-jshint'),
replace = require('gulp-replace'),
insert = require('gulp-insert'),
inquirer = require('inquirer'),
semver = require('semver'),
exec = require('child_process').exec,
fs = require('fs'),
package = require('./package.json'),
browserify = require('browserify'),
streamify = require('gulp-streamify'),
source = require('vinyl-source-stream'),
merge = require('merge-stream');

var srcDir = './src/';
var outDir = './';
Expand Down
Loading

0 comments on commit fdc1cab

Please sign in to comment.