Skip to content

Commit f32b625

Browse files
committed
feat: disable gesture by default, improved gestures handling
1 parent 7895cd6 commit f32b625

File tree

4 files changed

+273
-112
lines changed

4 files changed

+273
-112
lines changed

src/charting/charts/BarLineChartBase.ts

+95-16
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ import { AnimatedMoveViewJob } from '../jobs/AnimatedMoveViewJob';
2323
import { AnimatedZoomJob } from '../jobs/AnimatedZoomJob';
2424
import { ZoomJob } from '../jobs/ZoomJob';
2525
import { BarLineChartTouchListener } from '../listener/BarLineChartTouchListener';
26+
import { getEventOrGestureName, EventData, Observable } from '@nativescript/core/ui/page/page';
27+
import { GestureTypes, fromString as gestureFromString } from '@nativescript/core/ui/gestures/gestures';
2628

2729
const LOG_TAG = 'BarLineChartBase';
2830

2931
export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatterCandleBubbleDataSet<U>, T extends BarLineScatterCandleBubbleData<U, D>> extends Chart<U, D, T>
3032
implements BarLineScatterCandleBubbleDataProvider {
33+
protected mChartTouchListener: BarLineChartTouchListener;
34+
3135
/**
3236
* the maximum number of entries to which values will be drawn
3337
* (entry numbers greater than this value will cause value-labels to disappear)
@@ -49,22 +53,22 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
4953
/**
5054
* flag that indicates if double tap zoom is enabled or not
5155
*/
52-
protected mDoubleTapToZoomEnabled = true;
56+
protected mDoubleTapToZoomEnabled = false;
5357

5458
/**
5559
* flag that indicates if highlighting per dragging over a fully zoomed out
5660
* chart is enabled
5761
*/
58-
protected mHighlightPerDragEnabled = true;
62+
protected mHighlightPerDragEnabled = false;
5963

6064
/**
6165
* if true, dragging is enabled for the chart
6266
*/
63-
private mDragXEnabled = true;
64-
private mDragYEnabled = true;
67+
private mDragXEnabled = false;
68+
private mDragYEnabled = false;
6569

66-
private mScaleXEnabled = true;
67-
private mScaleYEnabled = true;
70+
private mScaleXEnabled = false;
71+
private mScaleYEnabled = false;
6872

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

135139
this.setHighlighter(new ChartHighlighter(this));
136140

137-
this.mChartTouchListener = new BarLineChartTouchListener(this, this.mViewPortHandler.getMatrixTouch(), 3);
141+
// this.mChartTouchListener = new BarLineChartTouchListener(this, this.mViewPortHandler.getMatrixTouch(), 3);
138142

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

155+
getOrCreateBarTouchListener() {
156+
if (!this.mChartTouchListener) {
157+
this.mChartTouchListener = new BarLineChartTouchListener(this, this.mViewPortHandler.getMatrixTouch(), 3);
158+
if (!!this.nativeViewProtected) {
159+
this.mChartTouchListener.init();
160+
}
161+
}
162+
return this.mChartTouchListener;
163+
}
164+
151165
// for performance tracking
152166
private totalTime = 0;
153167
private drawCycles = 0;
@@ -256,7 +270,9 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
256270
}
257271

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

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

270286
public notifyDataSetChanged() {
271287
if (this.mData == null) {
272-
if (this.mLogEnabled) console.log(LOG_TAG, 'Preparing... DATA NOT SET.');
288+
if (this.mLogEnabled) {
289+
console.log(LOG_TAG, 'Preparing... DATA NOT SET.');
290+
}
273291
return;
274292
} else if (!this.mViewPortHandler.hasChartDimens()) {
275-
if (this.mLogEnabled) console.log(LOG_TAG, 'Preparing... NOT SIZED YET.');
293+
if (this.mLogEnabled) {
294+
console.log(LOG_TAG, 'Preparing... NOT SIZED YET.');
295+
}
276296
return;
277297
} else {
278-
if (this.mLogEnabled) console.log(LOG_TAG, 'Preparing...');
298+
if (this.mLogEnabled) {
299+
console.log(LOG_TAG, 'Preparing...');
300+
}
279301
}
280302

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

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

478499
// public computeScroll() {
479-
// if (this.mChartTouchListener instanceof BarLineChartTouchListener)
480-
// (this.mChartTouchListener).computeScroll();
500+
// if (this.mChartTouchListener instanceof BarLineChartTouchListener)
501+
// (this.mChartTouchListener).computeScroll();
481502
// }
482503

483504
/**
@@ -1054,6 +1075,11 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
10541075
*/
10551076
public setDoubleTapToZoomEnabled(enabled) {
10561077
this.mDoubleTapToZoomEnabled = enabled;
1078+
if (enabled) {
1079+
this.getOrCreateBarTouchListener().setDoubleTap(true);
1080+
} else if (this.mChartTouchListener) {
1081+
this.mChartTouchListener.setDoubleTap(false);
1082+
}
10571083
}
10581084

10591085
/**
@@ -1217,7 +1243,6 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
12171243
* @return
12181244
*/
12191245
public getDataSetByTouchPoint(x, y) {
1220-
console.log('getDataSetByTouchPoint', x, y);
12211246
const h = this.getHighlightByTouchPoint(x, y);
12221247
if (h != null) {
12231248
return this.mData.getDataSetByIndex(h.dataSetIndex);
@@ -1482,7 +1507,7 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
14821507

14831508
protected mOnSizeChangedBuffer = Utils.createNativeArray(2);
14841509

1485-
public onSizeChanged(w: number, h: number, oldw:number, oldh:number) {
1510+
public onSizeChanged(w: number, h: number, oldw: number, oldh: number) {
14861511
// Saving current position of chart.
14871512
this.mOnSizeChangedBuffer[0] = this.mOnSizeChangedBuffer[1] = 0;
14881513

@@ -1504,4 +1529,58 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
15041529
this.mViewPortHandler.refresh(this.mViewPortHandler.getMatrixTouch(), this, false);
15051530
}
15061531
}
1532+
1533+
public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) {
1534+
if (typeof arg === 'number') {
1535+
arg = GestureTypes[arg];
1536+
}
1537+
if (typeof arg === 'string') {
1538+
arg = getEventOrGestureName(arg);
1539+
let events = arg.split(',');
1540+
if (events.length > 0) {
1541+
for (let i = 0; i < events.length; i++) {
1542+
let evt = events[i].trim();
1543+
if (arg === 'tap') {
1544+
this.getOrCreateBarTouchListener().setTap(true);
1545+
} else if (arg === 'doubleTap') {
1546+
this.getOrCreateBarTouchListener().setDoubleTap(true);
1547+
} else if (arg === 'pan') {
1548+
this.getOrCreateBarTouchListener().setPan(true);
1549+
} else if (arg === 'pinch') {
1550+
this.getOrCreateBarTouchListener().setPinch(true);
1551+
}
1552+
Observable.prototype.addEventListener.call(this, evt, callback, thisArg);
1553+
}
1554+
} else {
1555+
Observable.prototype.addEventListener.call(this, arg, callback, thisArg);
1556+
}
1557+
}
1558+
}
1559+
1560+
public removeEventListener(arg: string | GestureTypes, callback?: any, thisArg?: any) {
1561+
if (typeof arg === 'number') {
1562+
arg = GestureTypes[arg];
1563+
}
1564+
if (typeof arg === 'string') {
1565+
arg = getEventOrGestureName(arg);
1566+
let events = arg.split(',');
1567+
if (events.length > 0) {
1568+
for (let i = 0; i < events.length; i++) {
1569+
let evt = events[i].trim();
1570+
if (arg === 'tap' && !this.isHighlightPerTapEnabled()) {
1571+
this.getOrCreateBarTouchListener().setTap(false);
1572+
} else if (arg === 'doubleTap' && !this.isDoubleTapToZoomEnabled()) {
1573+
this.getOrCreateBarTouchListener().setDoubleTap(false);
1574+
} else if (arg === 'pan' && !this.isHighlightPerDragEnabled() && !this.isDragEnabled()) {
1575+
this.getOrCreateBarTouchListener().setPan(false);
1576+
} else if (arg === 'pinch' && !this.isPinchZoomEnabled()) {
1577+
this.getOrCreateBarTouchListener().setPinch(false);
1578+
}
1579+
Observable.prototype.removeEventListener.call(this, evt, callback, thisArg);
1580+
}
1581+
} else {
1582+
Observable.prototype.removeEventListener.call(this, arg, callback, thisArg);
1583+
}
1584+
}
1585+
}
15071586
}

src/charting/charts/Chart.ts

+15-18
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
5353
/**
5454
* Flag that indicates if highlighting per tap (touch) is enabled
5555
*/
56-
protected mHighLightPerTapEnabled = true;
56+
protected mHighLightPerTapEnabled = false;
5757

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

108-
/**
109-
* listener that is called when a value on the chart is selected
110-
*/
111-
// protected mSelectionListener: OnChartValueSelectedListener;
112-
113108
protected mChartTouchListener: ChartTouchListener<any>;
114109

115110
/**
@@ -139,7 +134,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
139134
/**
140135
* object responsible for animations
141136
*/
142-
protected mAnimator: ChartAnimator;
137+
protected mAnimator: ChartAnimator;
143138

144139
/**
145140
* Extra offsets to be appended to the viewport
@@ -154,20 +149,20 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
154149
*/
155150
constructor() {
156151
super();
157-
console.log('constructor')
152+
console.log('constructor');
158153
this.init();
159154
}
160155

161156
initNativeView() {
162-
console.log('initNativeView')
157+
console.log('initNativeView');
163158
if (isIOS) {
164159
this.nativeViewProtected.opaque = false;
165160
}
166161
super.initNativeView();
167162
this.mChartTouchListener && this.mChartTouchListener.init();
168163
}
169164
disposeNativeView() {
170-
console.log('disposeNativeView')
165+
console.log('disposeNativeView');
171166
super.disposeNativeView();
172167
this.mChartTouchListener && this.mChartTouchListener.dispose();
173168
}
@@ -176,8 +171,8 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
176171
* initialize all paints and stuff
177172
*/
178173
protected init() {
179-
console.log('init')
180-
this.mAnimator = new ChartAnimator(()=>{
174+
console.log('init');
175+
this.mAnimator = new ChartAnimator(() => {
181176
this.invalidate();
182177
});
183178

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

207-
208202
// public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) {
209203
// if (typeof arg === "string") {
210204
// arg = getEventOrGestureName(arg);
@@ -327,7 +321,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
327321
this.mData = null;
328322
this.mOffsetsCalculated = false;
329323
this.mIndicesToHighlight = null;
330-
this.mChartTouchListener.setLastHighlighted(null);
324+
this.mChartTouchListener && this.mChartTouchListener.setLastHighlighted(null);
331325
this.invalidate();
332326
}
333327

@@ -530,6 +524,9 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
530524
* @param highs
531525
*/
532526
protected setLastHighlighted(highs) {
527+
if (!this.mChartTouchListener) {
528+
return;
529+
}
533530
if (highs == null || highs.length <= 0 || highs[0] == null) {
534531
this.mChartTouchListener.setLastHighlighted(null);
535532
} else {
@@ -930,7 +927,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
930927
* @return
931928
*/
932929
public getCenter() {
933-
return { x:this.mViewPortHandler.getChartWidth() / 2, y: this.mViewPortHandler.getChartHeight() / 2 };
930+
return { x: this.mViewPortHandler.getChartWidth() / 2, y: this.mViewPortHandler.getChartHeight() / 2 };
934931
}
935932

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

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

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

0 commit comments

Comments
 (0)