Skip to content

Commit

Permalink
feat: disable gesture by default, improved gestures handling
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Jan 13, 2020
1 parent 7895cd6 commit f32b625
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 112 deletions.
111 changes: 95 additions & 16 deletions src/charting/charts/BarLineChartBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ import { AnimatedMoveViewJob } from '../jobs/AnimatedMoveViewJob';
import { AnimatedZoomJob } from '../jobs/AnimatedZoomJob';
import { ZoomJob } from '../jobs/ZoomJob';
import { BarLineChartTouchListener } from '../listener/BarLineChartTouchListener';
import { getEventOrGestureName, EventData, Observable } from '@nativescript/core/ui/page/page';
import { GestureTypes, fromString as gestureFromString } from '@nativescript/core/ui/gestures/gestures';

const LOG_TAG = 'BarLineChartBase';

export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatterCandleBubbleDataSet<U>, T extends BarLineScatterCandleBubbleData<U, D>> extends Chart<U, D, T>
implements BarLineScatterCandleBubbleDataProvider {
protected mChartTouchListener: BarLineChartTouchListener;

/**
* the maximum number of entries to which values will be drawn
* (entry numbers greater than this value will cause value-labels to disappear)
Expand All @@ -49,22 +53,22 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
/**
* flag that indicates if double tap zoom is enabled or not
*/
protected mDoubleTapToZoomEnabled = true;
protected mDoubleTapToZoomEnabled = false;

/**
* flag that indicates if highlighting per dragging over a fully zoomed out
* chart is enabled
*/
protected mHighlightPerDragEnabled = true;
protected mHighlightPerDragEnabled = false;

/**
* if true, dragging is enabled for the chart
*/
private mDragXEnabled = true;
private mDragYEnabled = true;
private mDragXEnabled = false;
private mDragYEnabled = false;

private mScaleXEnabled = true;
private mScaleYEnabled = true;
private mScaleXEnabled = false;
private mScaleYEnabled = false;

/**
* palet object for the (by default) lightgrey background of the grid
Expand Down Expand Up @@ -134,7 +138,7 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte

this.setHighlighter(new ChartHighlighter(this));

this.mChartTouchListener = new BarLineChartTouchListener(this, this.mViewPortHandler.getMatrixTouch(), 3);
// this.mChartTouchListener = new BarLineChartTouchListener(this, this.mViewPortHandler.getMatrixTouch(), 3);

this.mGridBackgroundPaint = new Paint();
this.mGridBackgroundPaint.setStyle(Style.FILL);
Expand All @@ -148,6 +152,16 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
this.mBorderPaint.setStrokeWidth(Utils.convertDpToPixel(1));
}

getOrCreateBarTouchListener() {
if (!this.mChartTouchListener) {
this.mChartTouchListener = new BarLineChartTouchListener(this, this.mViewPortHandler.getMatrixTouch(), 3);
if (!!this.nativeViewProtected) {
this.mChartTouchListener.init();
}
}
return this.mChartTouchListener;
}

// for performance tracking
private totalTime = 0;
private drawCycles = 0;
Expand Down Expand Up @@ -256,7 +270,9 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
}

protected prepareValuePxMatrix() {
if (this.mLogEnabled) console.log(LOG_TAG, 'Preparing Value-Px Matrix, xmin: ' + this.mXAxis.mAxisMinimum + ', xmax: ' + this.mXAxis.mAxisMaximum + ', xdelta: ' + this.mXAxis.mAxisRange);
if (this.mLogEnabled) {
console.log(LOG_TAG, 'Preparing Value-Px Matrix, xmin: ' + this.mXAxis.mAxisMinimum + ', xmax: ' + this.mXAxis.mAxisMaximum + ', xdelta: ' + this.mXAxis.mAxisRange);
}

this.mRightAxisTransformer.prepareMatrixValuePx(this.mXAxis.mAxisMinimum, this.mXAxis.mAxisRange, this.mAxisRight.mAxisRange, this.mAxisRight.mAxisMinimum);
this.mLeftAxisTransformer.prepareMatrixValuePx(this.mXAxis.mAxisMinimum, this.mXAxis.mAxisRange, this.mAxisLeft.mAxisRange, this.mAxisLeft.mAxisMinimum);
Expand All @@ -269,13 +285,19 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte

public notifyDataSetChanged() {
if (this.mData == null) {
if (this.mLogEnabled) console.log(LOG_TAG, 'Preparing... DATA NOT SET.');
if (this.mLogEnabled) {
console.log(LOG_TAG, 'Preparing... DATA NOT SET.');
}
return;
} else if (!this.mViewPortHandler.hasChartDimens()) {
if (this.mLogEnabled) console.log(LOG_TAG, 'Preparing... NOT SIZED YET.');
if (this.mLogEnabled) {
console.log(LOG_TAG, 'Preparing... NOT SIZED YET.');
}
return;
} else {
if (this.mLogEnabled) console.log(LOG_TAG, 'Preparing...');
if (this.mLogEnabled) {
console.log(LOG_TAG, 'Preparing...');
}
}

if (this.mRenderer != null) this.mRenderer.initBuffers();
Expand Down Expand Up @@ -393,7 +415,6 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
offsetRight += this.mOffsetsBuffer.right;
offsetBottom += this.mOffsetsBuffer.bottom;


// offsets for y-labels
if (this.mAxisLeft.needsOffset()) {
offsetLeft += this.mAxisLeft.getRequiredWidthSpace(this.mAxisRendererLeft.getPaintAxisLabels());
Expand Down Expand Up @@ -476,8 +497,8 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
// }

// public computeScroll() {
// if (this.mChartTouchListener instanceof BarLineChartTouchListener)
// (this.mChartTouchListener).computeScroll();
// if (this.mChartTouchListener instanceof BarLineChartTouchListener)
// (this.mChartTouchListener).computeScroll();
// }

/**
Expand Down Expand Up @@ -1054,6 +1075,11 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
*/
public setDoubleTapToZoomEnabled(enabled) {
this.mDoubleTapToZoomEnabled = enabled;
if (enabled) {
this.getOrCreateBarTouchListener().setDoubleTap(true);
} else if (this.mChartTouchListener) {
this.mChartTouchListener.setDoubleTap(false);
}
}

/**
Expand Down Expand Up @@ -1217,7 +1243,6 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
* @return
*/
public getDataSetByTouchPoint(x, y) {
console.log('getDataSetByTouchPoint', x, y);
const h = this.getHighlightByTouchPoint(x, y);
if (h != null) {
return this.mData.getDataSetByIndex(h.dataSetIndex);
Expand Down Expand Up @@ -1482,7 +1507,7 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte

protected mOnSizeChangedBuffer = Utils.createNativeArray(2);

public onSizeChanged(w: number, h: number, oldw:number, oldh:number) {
public onSizeChanged(w: number, h: number, oldw: number, oldh: number) {
// Saving current position of chart.
this.mOnSizeChangedBuffer[0] = this.mOnSizeChangedBuffer[1] = 0;

Expand All @@ -1504,4 +1529,58 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
this.mViewPortHandler.refresh(this.mViewPortHandler.getMatrixTouch(), this, false);
}
}

public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) {
if (typeof arg === 'number') {
arg = GestureTypes[arg];
}
if (typeof arg === 'string') {
arg = getEventOrGestureName(arg);
let events = arg.split(',');
if (events.length > 0) {
for (let i = 0; i < events.length; i++) {
let evt = events[i].trim();
if (arg === 'tap') {
this.getOrCreateBarTouchListener().setTap(true);
} else if (arg === 'doubleTap') {
this.getOrCreateBarTouchListener().setDoubleTap(true);
} else if (arg === 'pan') {
this.getOrCreateBarTouchListener().setPan(true);
} else if (arg === 'pinch') {
this.getOrCreateBarTouchListener().setPinch(true);
}
Observable.prototype.addEventListener.call(this, evt, callback, thisArg);
}
} else {
Observable.prototype.addEventListener.call(this, arg, callback, thisArg);
}
}
}

public removeEventListener(arg: string | GestureTypes, callback?: any, thisArg?: any) {
if (typeof arg === 'number') {
arg = GestureTypes[arg];
}
if (typeof arg === 'string') {
arg = getEventOrGestureName(arg);
let events = arg.split(',');
if (events.length > 0) {
for (let i = 0; i < events.length; i++) {
let evt = events[i].trim();
if (arg === 'tap' && !this.isHighlightPerTapEnabled()) {
this.getOrCreateBarTouchListener().setTap(false);
} else if (arg === 'doubleTap' && !this.isDoubleTapToZoomEnabled()) {
this.getOrCreateBarTouchListener().setDoubleTap(false);
} else if (arg === 'pan' && !this.isHighlightPerDragEnabled() && !this.isDragEnabled()) {
this.getOrCreateBarTouchListener().setPan(false);
} else if (arg === 'pinch' && !this.isPinchZoomEnabled()) {
this.getOrCreateBarTouchListener().setPinch(false);
}
Observable.prototype.removeEventListener.call(this, evt, callback, thisArg);
}
} else {
Observable.prototype.removeEventListener.call(this, arg, callback, thisArg);
}
}
}
}
33 changes: 15 additions & 18 deletions src/charting/charts/Chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
/**
* Flag that indicates if highlighting per tap (touch) is enabled
*/
protected mHighLightPerTapEnabled = true;
protected mHighLightPerTapEnabled = false;

/**
* If set to true, chart continues to scroll after touch up
Expand Down Expand Up @@ -105,11 +105,6 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
*/
protected mLegend: Legend;

/**
* listener that is called when a value on the chart is selected
*/
// protected mSelectionListener: OnChartValueSelectedListener;

protected mChartTouchListener: ChartTouchListener<any>;

/**
Expand Down Expand Up @@ -139,7 +134,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
/**
* object responsible for animations
*/
protected mAnimator: ChartAnimator;
protected mAnimator: ChartAnimator;

/**
* Extra offsets to be appended to the viewport
Expand All @@ -154,20 +149,20 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
*/
constructor() {
super();
console.log('constructor')
console.log('constructor');
this.init();
}

initNativeView() {
console.log('initNativeView')
console.log('initNativeView');
if (isIOS) {
this.nativeViewProtected.opaque = false;
}
super.initNativeView();
this.mChartTouchListener && this.mChartTouchListener.init();
}
disposeNativeView() {
console.log('disposeNativeView')
console.log('disposeNativeView');
super.disposeNativeView();
this.mChartTouchListener && this.mChartTouchListener.dispose();
}
Expand All @@ -176,8 +171,8 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
* initialize all paints and stuff
*/
protected init() {
console.log('init')
this.mAnimator = new ChartAnimator(()=>{
console.log('init');
this.mAnimator = new ChartAnimator(() => {
this.invalidate();
});

Expand All @@ -204,7 +199,6 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
if (this.mLogEnabled) console.log('', 'Chart.init()');
}


// public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) {
// if (typeof arg === "string") {
// arg = getEventOrGestureName(arg);
Expand Down Expand Up @@ -327,7 +321,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
this.mData = null;
this.mOffsetsCalculated = false;
this.mIndicesToHighlight = null;
this.mChartTouchListener.setLastHighlighted(null);
this.mChartTouchListener && this.mChartTouchListener.setLastHighlighted(null);
this.invalidate();
}

Expand Down Expand Up @@ -530,6 +524,9 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
* @param highs
*/
protected setLastHighlighted(highs) {
if (!this.mChartTouchListener) {
return;
}
if (highs == null || highs.length <= 0 || highs[0] == null) {
this.mChartTouchListener.setLastHighlighted(null);
} else {
Expand Down Expand Up @@ -930,7 +927,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
* @return
*/
public getCenter() {
return { x:this.mViewPortHandler.getChartWidth() / 2, y: this.mViewPortHandler.getChartHeight() / 2 };
return { x: this.mViewPortHandler.getChartWidth() / 2, y: this.mViewPortHandler.getChartHeight() / 2 };
}

/**
Expand Down Expand Up @@ -1551,8 +1548,8 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
// this.onSizeChanged(this.getMeasuredWidth(), this.getMeasuredHeight());
// }

public onSizeChanged(w: number, h: number, oldw:number, oldh:number): void {
super.onSizeChanged(w, h,oldw,oldh);
public onSizeChanged(w: number, h: number, oldw: number, oldh: number): void {
super.onSizeChanged(w, h, oldw, oldh);
// super.setMeasuredDimension(measuredWidth, measuredHeight);
const needsDataSetChanged = !this.mViewPortHandler.hasChartDimens();
if (this.mLogEnabled) console.log(LOG_TAG, 'OnSizeChanged', w, h, needsDataSetChanged);
Expand All @@ -1562,7 +1559,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
this.mViewPortHandler.setChartDimens(w, h);
} else {
if (this.mLogEnabled) console.warn(LOG_TAG, '*Avoiding* setting chart dimens! width: ' + w + ', height: ' + h);
}
}

// This may cause the chart view to mutate properties affecting the view port --
// lets do this before we try to run any pending jobs on the view port itself
Expand Down
Loading

0 comments on commit f32b625

Please sign in to comment.