Skip to content

Commit cb6b298

Browse files
committed
fix: full refactoring using latest canvas plugin. Should be faster and use less memory
1 parent 3250ccb commit cb6b298

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+705
-1196
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,23 @@
3737
"readmeFilename": "README.md",
3838
"devDependencies": {
3939
"@angular/core": "^12.2.8",
40-
"@commitlint/cli": "^13.2.0",
40+
"@commitlint/cli": "^13.2.1",
4141
"@commitlint/config-conventional": "^13.2.0",
4242
"@nativescript-community/gesturehandler": "^0.1.47",
4343
"@nativescript-community/tween": "0.0.14",
44-
"@nativescript-community/ui-canvas": "4.1.3",
45-
"@nativescript/core": "8.1.3",
44+
"@nativescript-community/ui-canvas": "4.2.11",
45+
"@nativescript/core": "8.1.4",
4646
"@nativescript/types-android": "8.1.0",
4747
"@nativescript/types-ios": "8.1.1",
4848
"@nativescript/webpack": "5.0.0",
4949
"@tweenjs/tween.js": "18.6.4",
50-
"@types/node": "^16.10.2",
50+
"@types/node": "^16.10.3",
5151
"@types/tween.js": "^18.6.1",
5252
"@typescript-eslint/eslint-plugin": "4.33.0",
5353
"@typescript-eslint/parser": "4.33.0",
5454
"cpy-cli": "^3.1.1",
5555
"downsample": "^1.4.0",
56-
"eslint": "7.32.0",
56+
"eslint": "^7.32.0",
5757
"eslint-config-prettier": "^8.3.0",
5858
"eslint-plugin-prettier": "^4.0.0",
5959
"globby": "^12.0.2",

plugin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"dependencies": {
3535
"@nativescript-community/gesturehandler": "^0.1.47",
3636
"@nativescript-community/tween": "^0.0.14",
37-
"@nativescript-community/ui-canvas": "^4.1.3",
37+
"@nativescript-community/ui-canvas": "^4.2.11",
3838
"downsample": "^1.4.0",
3939
"number-format.js": "^2.0.9"
4040
}

src/charting/buffer/AbstractBuffer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export abstract class AbstractBuffer<T> {
1616
*/
1717
constructor(size: number) {
1818
this.index = 0;
19-
this.buffer = Utils.createNativeArray(size);
19+
this.buffer = Utils.createArrayBuffer(size);
2020
}
2121

2222
/** limits the drawing on the x-axis */

src/charting/charts/BarChart.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export class BarChart extends BarLineChartBase<Entry, BarDataSet, BarData> imple
2525
/**
2626
* flag that indicates whether the highlight should be full-bar oriented, or single-value?
2727
*/
28-
protected mHighlightFullBarEnabled = false;
28+
protected mHighlightFullBarEnabled ;
2929

30-
protected mFitBars = false;
30+
protected mFitBars;
3131

3232
/**
3333
* if set to true, all values are drawn above their bars, instead of below their top
@@ -37,7 +37,7 @@ export class BarChart extends BarLineChartBase<Entry, BarDataSet, BarData> imple
3737
/**
3838
* if set to true, a grey area is drawn behind each bar that indicates the maximum value
3939
*/
40-
private mDrawBarShadow = false;
40+
private mDrawBarShadow;
4141

4242
private mCustomRenderer: CustomRenderer;
4343

src/charting/charts/BarLineChartBase.ts

+22-33
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,35 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
3838
/**
3939
* flag that indicates if auto scaling on the y axis is enabled
4040
*/
41-
protected mAutoScaleMinMaxEnabled = false;
41+
protected mAutoScaleMinMaxEnabled: boolean;
4242

4343
/**
4444
* flag that indicates if pinch-zoom is enabled. if true, both x and y axis
4545
* can be scaled with 2 fingers, if false, x and y axis can be scaled
4646
* separately
4747
*/
48-
protected mPinchZoomEnabled = false;
48+
protected mPinchZoomEnabled: boolean;
4949

5050
/**
5151
* flag that indicates if double tap zoom is enabled or not
5252
*/
53-
protected mDoubleTapToZoomEnabled = false;
53+
protected mDoubleTapToZoomEnabled: boolean;
5454

5555
/**
5656
* flag that indicates if highlighting per dragging over a fully zoomed out
5757
* chart is enabled
5858
*/
59-
protected mHighlightPerDragEnabled = false;
59+
protected mHighlightPerDragEnabled: boolean;
6060

6161
/**
6262
* if true, dragging is enabled for the chart
6363
*/
64-
private mDragXEnabled = false;
65-
private mDragYEnabled = false;
64+
private mDragXEnabled: boolean;
65+
private mDragYEnabled: boolean;
66+
e;
6667

67-
private mScaleXEnabled = false;
68-
private mScaleYEnabled = false;
68+
private mScaleXEnabled: boolean;
69+
private mScaleYEnabled: boolean;
6970

7071
/**
7172
* palet object for the (by default) lightgrey background of the grid
@@ -77,9 +78,9 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
7778
/**
7879
* flag indicating if the grid background should be drawn or not
7980
*/
80-
protected mDrawGridBackground = false;
81+
protected mDrawGridBackground: boolean;
8182

82-
protected mDrawBorders = false;
83+
protected mDrawBorders: boolean;
8384

8485
protected mClipValuesToContent = true;
8586

@@ -95,7 +96,7 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
9596
/**
9697
* flag indicating if the chart should stay at the same position after a rotation. Default is false.
9798
*/
98-
protected mKeepPositionOnRotation = false;
99+
protected mKeepPositionOnRotation: boolean;
99100

100101
/**
101102
* the listener for user drawing on the chart
@@ -120,8 +121,6 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
120121

121122
protected mXAxisRenderer: XAxisRenderer;
122123

123-
protected mOffsetsBuffer: RectF = new RectF(0, 0, 0, 0);
124-
125124
// /** the approximator object used for data filtering */
126125
// private Approximator this.mApproximator;
127126

@@ -141,18 +140,14 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
141140

142141
get gridBackgroundPaint() {
143142
if (!this.mGridBackgroundPaint) {
144-
this.mGridBackgroundPaint = new Paint();
145-
this.mGridBackgroundPaint.setStyle(Style.FILL);
143+
this.mGridBackgroundPaint = Utils.getTemplatePaint('black-fill');
146144
this.mGridBackgroundPaint.setColor('#F0F0F0'); // light
147145
}
148146
return this.mGridBackgroundPaint;
149147
}
150148
get borderPaint() {
151149
if (!this.mBorderPaint) {
152-
this.mBorderPaint = new Paint();
153-
this.mBorderPaint.setStyle(Style.STROKE);
154-
this.mBorderPaint.setColor('black');
155-
this.mBorderPaint.setStrokeWidth(1);
150+
this.mBorderPaint = Utils.getTemplatePaint('black-stroke');
156151
}
157152
return this.mBorderPaint;
158153
}
@@ -442,17 +437,13 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
442437
}
443438
this.mOffsetsCalculated = true;
444439
if (!this.mCustomViewPortEnabled) {
445-
let offsetLeft = 0,
446-
offsetRight = 0,
447-
offsetTop = 0,
448-
offsetBottom = 0;
449-
450-
this.calculateLegendOffsets(this.mOffsetsBuffer);
440+
const offsetBuffer = Utils.getTempRectF();
441+
this.calculateLegendOffsets(offsetBuffer);
451442

452-
offsetLeft += this.mOffsetsBuffer.left;
453-
offsetTop += this.mOffsetsBuffer.top;
454-
offsetRight += this.mOffsetsBuffer.right;
455-
offsetBottom += this.mOffsetsBuffer.bottom;
443+
let offsetLeft = offsetBuffer.left;
444+
let offsetTop = offsetBuffer.top;
445+
let offsetRight = offsetBuffer.right;
446+
let offsetBottom = offsetBuffer.bottom;
456447

457448
// offsets for y-labels
458449
if (this.mAxisLeft && this.mAxisLeft.needsOffset()) {
@@ -465,7 +456,6 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
465456

466457
if (this.mXAxis.isEnabled() && this.mXAxis.isDrawLabelsEnabled()) {
467458
const xLabelHeight = this.mXAxis.mLabelRotatedHeight + this.mXAxis.getYOffset();
468-
469459
// offsets for x-labels
470460
if (this.mXAxis.getPosition() === XAxisPosition.BOTTOM) {
471461
offsetBottom += xLabelHeight;
@@ -483,7 +473,6 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
483473
offsetLeft += this.getExtraLeftOffset();
484474

485475
const minOffset = this.mMinOffset;
486-
487476
this.mViewPortHandler.restrainViewPort(Math.max(minOffset, offsetLeft), Math.max(minOffset, offsetTop), Math.max(minOffset, offsetRight), Math.max(minOffset, offsetBottom));
488477

489478
if (Trace.isEnabled()) {
@@ -926,7 +915,7 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
926915
/**
927916
* flag that indicates if a custom viewport offset has been set
928917
*/
929-
private mCustomViewPortEnabled = false;
918+
private mCustomViewPortEnabled: boolean;
930919

931920
/**
932921
* Sets custom offsets for the current ViewPort (the offsets on the sides of
@@ -1666,7 +1655,7 @@ export abstract class BarLineChartBase<U extends Entry, D extends IBarLineScatte
16661655
return this.mAutoScaleMinMaxEnabled;
16671656
}
16681657

1669-
protected mOnSizeChangedBuffer = Utils.createNativeArray(2);
1658+
protected mOnSizeChangedBuffer = Utils.createArrayBuffer(2);
16701659

16711660
public onSizeChanged(w: number, h: number, oldw: number, oldh: number) {
16721661
// Saving current position of chart.

src/charting/charts/CandleStickChart.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Canvas, Paint } from '@nativescript-community/ui-canvas';
1+
import { Canvas, Paint, TypedArray } from '@nativescript-community/ui-canvas';
22
import { CandleData } from '../data/CandleData';
33
import { CandleEntry } from '../data/CandleEntry';
44
import { Highlight } from '../highlight/Highlight';
@@ -9,11 +9,11 @@ import { BaseCustomRenderer } from '../renderer/DataRenderer';
99
import { BarLineChartBase } from './BarLineChartBase';
1010

1111
export interface CustomRenderer extends BaseCustomRenderer {
12-
drawShadows?: (c: Canvas, e: CandleEntry, lines: number[], paint: Paint) => void;
12+
drawShadows?: (c: Canvas, e: CandleEntry, lines: number[] | TypedArray, paint: Paint) => void;
1313
drawOpened?: (c: Canvas, e: CandleEntry, left: number, top: number, right: number, bottom: number, paint: Paint) => void;
1414
drawClosed?: (c: Canvas, e: CandleEntry, left: number, top: number, right: number, bottom: number, paint: Paint) => void;
1515
drawEqual?: (c: Canvas, e: CandleEntry, left: number, top: number, right: number, bottom: number, paint: Paint) => void;
16-
drawLines?: (c: Canvas, e: CandleEntry, range: number[], open: number[], close: number[], paint: Paint) => void;
16+
drawLines?: (c: Canvas, e: CandleEntry, range: number[] | TypedArray, open: number[] | TypedArray, close: number[] | TypedArray, paint: Paint) => void;
1717
drawHighlight?: (c: Canvas, e: Highlight, set: ICandleDataSet, paint: Paint) => void;
1818
}
1919
/**

src/charting/charts/Chart.ts

+11-19
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
5050
/**
5151
* flag that indicates if logging is enabled or not
5252
*/
53-
protected mLogEnabled = false;
53+
protected mLogEnabled;
5454

5555
/**
5656
* object that holds all data that was originally set for the chart, before
@@ -61,7 +61,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
6161
/**
6262
* Flag that indicates if highlighting per tap (touch) is enabled
6363
*/
64-
protected mHighLightPerTapEnabled = false;
64+
protected mHighLightPerTapEnabled;
6565

6666
/**
6767
* If set to true, chart continues to scroll after touch up
@@ -155,13 +155,13 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
155155
/**
156156
* flag that indicates if offsets calculation has already been done or not
157157
*/
158-
protected mOffsetsCalculated = false;
158+
protected mOffsetsCalculated;
159159

160160
/**
161161
* let the drawer know it does not need to compute axis and legends
162162
* (it can used the cached ones)
163163
*/
164-
protected noComputeOnNextDraw = false;
164+
protected noComputeOnNextDraw;
165165
/**
166166
* array of Highlight objects that reference the highlighted slices in the
167167
* chart
@@ -173,6 +173,11 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
173173
*/
174174
protected mMaxHighlightDistance = 500;
175175

176+
/**
177+
* tasks to be done after the view is setup
178+
*/
179+
protected mJobs = [];
180+
176181
/**
177182
* default constructor for initialization in code
178183
*/
@@ -209,21 +214,12 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
209214

210215
get infoPaint() {
211216
if (!this.mInfoPaint) {
212-
this.mInfoPaint = new Paint();
213-
this.mInfoPaint.setAntiAlias(true);
214-
this.mInfoPaint.setColor('#F7BD33'); // orange
217+
this.mInfoPaint = Utils.getTemplatePaint('black-fill');
215218
this.mInfoPaint.setTextAlign(Align.CENTER);
216219
this.mInfoPaint.setTextSize(12);
217220
}
218221
return this.mInfoPaint;
219222
}
220-
get descPaint() {
221-
if (!this.mDescPaint) {
222-
this.mDescPaint = new Paint();
223-
this.mDescPaint.setAntiAlias(true);
224-
}
225-
return this.mDescPaint;
226-
}
227223

228224
public panGestureOptions: PanGestureHandlerOptions & { gestureTag?: number };
229225
public tapGestureOptions: TapGestureHandlerOptions & { gestureTag?: number };
@@ -360,7 +356,7 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
360356
// check if description should be drawn
361357
if (this.mDescription != null && this.mDescription.isEnabled()) {
362358
const position = this.mDescription.getPosition();
363-
const paint = this.descPaint;
359+
const paint = Utils.getTempPaint();
364360
paint.setFont(this.mDescription.getFont());
365361
paint.setColor(this.mDescription.getTextColor());
366362
paint.setTextAlign(this.mDescription.getTextAlign());
@@ -1308,10 +1304,6 @@ export abstract class Chart<U extends Entry, D extends IDataSet<U>, T extends Ch
13081304
// return saveToGallery(fileName, "", "MPAndroidChart-Library Save", Bitmap.CompressFormat.PNG, 40);
13091305
// }
13101306

1311-
/**
1312-
* tasks to be done after the view is setup
1313-
*/
1314-
protected mJobs = [];
13151307

13161308
public removeViewportJob(job) {
13171309
const index = this.mJobs.indexOf(job);

src/charting/charts/CombinedChart.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ export class CombinedChart extends BarLineChartBase<Entry, BarLineScatterCandleB
4343
/**
4444
* flag that indicates whether the highlight should be full-bar oriented, or single-value?
4545
*/
46-
protected mHighlightFullBarEnabled = false;
46+
protected mHighlightFullBarEnabled;
4747

4848
/**
4949
* if set to true, a grey area is drawn behind each bar that indicates the
5050
* maximum value
5151
*/
52-
protected mDrawBarShadow = false;
52+
protected mDrawBarShadow;
5353

5454
protected mDrawOrder: DrawOrder[];
5555

src/charting/charts/HorizontalBarChart.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const LOG_TAG = 'HorizontalBarChart';
2121

2222
export class HorizontalBarChart extends BarChart {
2323
mRenderer: HorizontalBarChartRenderer;
24-
protected mGetPositionBuffer = Utils.createNativeArray(2);
2524

2625
protected init() {
2726
this.mViewPortHandler = new HorizontalViewPortHandler();
@@ -46,17 +45,13 @@ export class HorizontalBarChart extends BarChart {
4645
}
4746

4847
public calculateOffsets() {
49-
let offsetLeft = 0,
50-
offsetRight = 0,
51-
offsetTop = 0,
52-
offsetBottom = 0;
48+
const offsetBuffer = Utils.getTempRectF();
49+
this.calculateLegendOffsets(offsetBuffer);
5350

54-
this.calculateLegendOffsets(this.mOffsetsBuffer);
55-
56-
offsetLeft += this.mOffsetsBuffer.left;
57-
offsetTop += this.mOffsetsBuffer.top;
58-
offsetRight += this.mOffsetsBuffer.right;
59-
offsetBottom += this.mOffsetsBuffer.bottom;
51+
let offsetLeft = offsetBuffer.left;
52+
let offsetTop = offsetBuffer.top;
53+
let offsetRight = offsetBuffer.right;
54+
let offsetBottom = offsetBuffer.bottom;
6055

6156
// offsets for y-labels
6257
if (this.mAxisLeft && this.mAxisLeft.needsOffset()) {

src/charting/charts/PieChart.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ export class PieChart extends PieRadarChartBase<Entry, PieDataSet, PieData> {
5555
/**
5656
* if true, the hole will see-through to the inner tips of the slices
5757
*/
58-
private mDrawSlicesUnderHole: boolean = false;
58+
private mDrawSlicesUnderHole: boolean;
5959

6060
/**
6161
* if true, the values inside the piechart are drawn as percent values
6262
*/
63-
private mUsePercentValues: boolean = false;
63+
private mUsePercentValues: boolean;
6464

6565
/**
6666
* if true, the slices of the piechart are rounded
6767
*/
68-
private mDrawRoundedSlices: boolean = false;
68+
private mDrawRoundedSlices: boolean;
6969

7070
/**
7171
* variable for the text that is drawn in the center of the pie-chart

0 commit comments

Comments
 (0)