Skip to content

Commit b41fdde

Browse files
committed
fix: move to new tween library. Smaller bundle for same result
1 parent 0275c52 commit b41fdde

File tree

2 files changed

+65
-100
lines changed

2 files changed

+65
-100
lines changed
+47-51
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,58 @@
1-
import TWEEN from '@nativescript-community/tween';
2-
const Easing = TWEEN.Easing;
3-
export type EasingFunction = (k: number) => number;
4-
export { Easing };
5-
/**
6-
* Object responsible for all animations in the Chart. Animations require API level 11.
7-
*
1+
import { time } from '@nativescript/core/profiling';
2+
import { AdditiveTweening, EasingFunction } from 'additween';
3+
export { EasingFunction };
4+
if (!global.window) {
5+
window = global.window = {
6+
requestAnimationFrame,
7+
cancelAnimationFrame,
8+
performance: {
9+
now: time
10+
}
11+
} as any;
12+
}
813

9-
*/
14+
function clamp(value) {
15+
return Math.min(1, Math.max(0, value));
16+
}
17+
18+
class Tween<T extends Record<string, number>> extends AdditiveTweening<T> {}
1019
export class ChartAnimator {
1120
/** object that is updated upon animation update */
12-
private mListener: () => void;
21+
private mListener: (state) => void;
1322

1423
/** The phase of drawn values on the y-axis. 0 - 1 */
1524
protected mPhaseY = 1;
1625

1726
/** The phase of drawn values on the x-axis. 0 - 1 */
1827
protected mPhaseX = 1;
1928

20-
constructor(listener?: () => void) {
29+
constructor(listener?: (state) => void) {
2130
this.mListener = listener;
2231
}
2332

24-
private xAnimator(duration, easing: EasingFunction = Easing.Linear.None, listener?: () => void) {
25-
return new TWEEN.Tween({ value: 0 })
26-
.to({ value: 1 }, duration)
27-
.easing(easing)
28-
.onUpdate((obj) => {
29-
this.setPhaseX(obj.value);
33+
private startAnim(duration, easing?: EasingFunction, listener?: (state) => void) {
34+
const anim = new Tween({
35+
onRender: (state) => {
3036
if (listener) {
31-
listener();
37+
listener(state);
3238
}
33-
});
39+
}
40+
});
41+
anim.tween({ value: 0 }, { value: 1 }, duration);
42+
return anim;
43+
}
44+
private startXAnim(duration, easing?: EasingFunction, listener?: (state) => void) {
45+
return this.startAnim(duration, easing, (state) => {
46+
this.setPhaseX(state.value);
47+
listener?.(state);
48+
});
3449
}
3550

36-
private yAnimator(duration, easing: EasingFunction = Easing.Linear.None, listener?: () => void) {
37-
return new TWEEN.Tween({ value: 0 })
38-
.to({ value: 1 }, duration)
39-
.easing(easing)
40-
.onUpdate((obj) => {
41-
this.setPhaseY(obj.value);
42-
if (listener) {
43-
listener();
44-
}
45-
});
51+
private startYAnim(duration, easing?: EasingFunction, listener?: (state) => void) {
52+
return this.startAnim(duration, easing, (state) => {
53+
this.setPhaseY(state.value);
54+
listener?.(state);
55+
});
4656
}
4757

4858
/**
@@ -51,9 +61,8 @@ export class ChartAnimator {
5161
* @param durationMillis animation duration
5262
* @param easing EasingFunction
5363
*/
54-
public animateX(durationMillis, easing: EasingFunction = Easing.Linear.None) {
55-
const animatorX = this.xAnimator(durationMillis, easing, this.mListener);
56-
animatorX.start(0);
64+
public animateX(durationMillis, easing?: EasingFunction) {
65+
this.startXAnim(durationMillis, easing, this.mListener);
5766
}
5867

5968
/**
@@ -64,11 +73,9 @@ export class ChartAnimator {
6473
* @param easingX EasingFunction for the X axis
6574
* @param easingY EasingFunction for the Y axis
6675
*/
67-
public animateXY(durationMillisX, durationMillisY, easingX: EasingFunction = Easing.Linear.None, easingY: EasingFunction = Easing.Linear.None) {
68-
const xAnimator = this.xAnimator(durationMillisX, easingX, durationMillisX > durationMillisY ? this.mListener : undefined);
69-
const yAnimator = this.yAnimator(durationMillisY, easingY || easingX, durationMillisX > durationMillisY ? undefined : this.mListener);
70-
xAnimator.start(0);
71-
yAnimator.start(0);
76+
public animateXY(durationMillisX, durationMillisY, easingX?: EasingFunction, easingY?: EasingFunction) {
77+
this.startXAnim(durationMillisX, easingX, durationMillisX > durationMillisY ? this.mListener : undefined);
78+
this.startYAnim(durationMillisY, easingY || easingX, durationMillisX > durationMillisY ? undefined : this.mListener);
7279
}
7380

7481
/**
@@ -77,9 +84,8 @@ export class ChartAnimator {
7784
* @param durationMillis animation duration
7885
* @param easing EasingFunction
7986
*/
80-
public animateY(durationMillis, easing: EasingFunction = Easing.Linear.None) {
81-
const animatorY = this.yAnimator(durationMillis, easing, this.mListener);
82-
animatorY.start(0);
87+
public animateY(durationMillis, easing?: EasingFunction) {
88+
this.startYAnim(durationMillis, easing, this.mListener);
8389
}
8490

8591
/**
@@ -97,12 +103,7 @@ export class ChartAnimator {
97103
* @param phase let value between 0 - 1
98104
*/
99105
public setPhaseY(phase) {
100-
if (phase > 1) {
101-
phase = 1;
102-
} else if (phase < 0) {
103-
phase = 0;
104-
}
105-
this.mPhaseY = phase;
106+
this.mPhaseY = clamp(phase);
106107
}
107108

108109
/**
@@ -120,11 +121,6 @@ export class ChartAnimator {
120121
* @param phase let value between 0 - 1
121122
*/
122123
public setPhaseX(phase) {
123-
if (phase > 1) {
124-
phase = 1;
125-
} else if (phase < 0) {
126-
phase = 0;
127-
}
128-
this.mPhaseX = phase;
124+
this.mPhaseX = clamp(phase);
129125
}
130126
}

src/charting/charts/Chart.ts

+18-49
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
import { IDataSet } from '../interfaces/datasets/IDataSet';
2-
import { DataSet } from '../data/DataSet';
3-
import { Entry } from '../data/Entry';
4-
import { ChartData } from '../data/ChartData';
5-
import { ChartInterface } from '../interfaces/dataprovider/ChartInterface';
1+
import { PanGestureHandlerOptions, PinchGestureHandlerOptions, TapGestureHandlerOptions } from '@nativescript-community/gesturehandler';
62
import { Align, Canvas, CanvasView, Paint } from '@nativescript-community/ui-canvas';
7-
import { DefaultValueFormatter } from '../formatter/DefaultValueFormatter';
8-
import { CLog, CLogTypes, Utils } from '../utils/Utils';
9-
import { Color } from '@nativescript/core/color';
10-
import { Highlight } from '../highlight/Highlight';
11-
import { Legend } from '../components/Legend';
12-
import { ViewPortHandler } from '../utils/ViewPortHandler';
13-
import { XAxis } from '../components/XAxis';
3+
import { EventData, Trace } from '@nativescript/core';
4+
import { layout } from '@nativescript/core/utils/utils';
5+
import { ChartAnimator, EasingFunction } from '../animation/ChartAnimator';
146
import { Description } from '../components/Description';
15-
import { DataRenderer } from '../renderer/DataRenderer';
167
import { IMarker } from '../components/IMarker';
17-
import { LegendRenderer } from '../renderer/LegendRenderer';
8+
import { Legend } from '../components/Legend';
9+
import { XAxis } from '../components/XAxis';
10+
import { ChartData } from '../data/ChartData';
11+
import { Entry } from '../data/Entry';
12+
import { DefaultValueFormatter } from '../formatter/DefaultValueFormatter';
13+
import { Highlight } from '../highlight/Highlight';
1814
import { IHighlighter } from '../highlight/IHighlighter';
19-
import { profile } from '@nativescript/core/profiling';
20-
import { ChartAnimator, EasingFunction } from '../animation/ChartAnimator';
15+
import { ChartInterface } from '../interfaces/dataprovider/ChartInterface';
16+
import { IDataSet } from '../interfaces/datasets/IDataSet';
2117
import { ViewPortJob } from '../jobs/ViewPortJob';
2218
import { ChartTouchListener } from '../listener/ChartTouchListener';
23-
import { layout } from '@nativescript/core/utils/utils';
24-
import { EventData, Trace } from '@nativescript/core';
25-
import { addWeakEventListener, removeWeakEventListener } from '@nativescript/core/ui/core/weak-event-listener';
26-
import { PanGestureHandlerOptions, PinchGestureHandlerOptions, TapGestureHandlerOptions } from '@nativescript-community/gesturehandler';
19+
import { DataRenderer } from '../renderer/DataRenderer';
20+
import { LegendRenderer } from '../renderer/LegendRenderer';
21+
import { CLog, CLogTypes, Utils } from '../utils/Utils';
22+
import { ViewPortHandler } from '../utils/ViewPortHandler';
2723

2824
const LOG_TAG = 'NSChart';
2925

@@ -47,11 +43,6 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
4743
abstract getYChartMax();
4844
abstract getMaxVisibleCount();
4945

50-
/**
51-
* flag that indicates if logging is enabled or not
52-
*/
53-
protected mLogEnabled;
54-
5546
/**
5647
* object that holds all data that was originally set for the chart, before
5748
* it was modified or any filtering algorithms had been applied
@@ -176,7 +167,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
176167
/**
177168
* tasks to be done after the view is setup
178169
*/
179-
protected mJobs = [];
170+
protected mJobs = [];
180171

181172
/**
182173
* default constructor for initialization in code
@@ -199,7 +190,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
199190
* initialize all paints and stuff
200191
*/
201192
protected init() {
202-
this.mAnimator = new ChartAnimator(() => {
193+
this.mAnimator = new ChartAnimator((state) => {
203194
// during animations we dont need to compute axis things
204195
this.noComputeOnNextDraw = true;
205196
this.invalidate();
@@ -918,27 +909,6 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
918909
return this.mExtraLeftOffset;
919910
}
920911

921-
/**
922-
* Set this to true to enable logcat outputs for the chart. Beware that
923-
* logcat output decreases rendering performance. Default: disabled.
924-
*
925-
* @deprecated use Nativescript Trace with ChartTraceCategory
926-
* @param enabled
927-
*/
928-
public setLogEnabled(enabled) {
929-
this.mLogEnabled = enabled;
930-
}
931-
932-
/**
933-
* Returns true if log-output is enabled for the chart, fals if not.
934-
*
935-
* @deprecated use Nativescript Trace with ChartTraceCategory
936-
* @return
937-
*/
938-
public isLogEnabled() {
939-
return this.mLogEnabled;
940-
}
941-
942912
/**
943913
* Sets the text that informs the user that there is no data available with
944914
* which to draw the chart.
@@ -1304,7 +1274,6 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
13041274
// return saveToGallery(fileName, "", "MPAndroidChart-Library Save", Bitmap.CompressFormat.PNG, 40);
13051275
// }
13061276

1307-
13081277
public removeViewportJob(job) {
13091278
const index = this.mJobs.indexOf(job);
13101279
if (index >= 0) {

0 commit comments

Comments
 (0)