Skip to content

Commit b15fe42

Browse files
committed
fix: some optimisations
1 parent 44468c3 commit b15fe42

File tree

3 files changed

+70
-62
lines changed

3 files changed

+70
-62
lines changed

src/charting/renderer/XAxisRenderer.ts

+68-61
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { profile } from '@nativescript/core';
1010

1111
export class XAxisRenderer extends AxisRenderer {
1212
protected mXAxis: XAxis;
13+
protected mForceLongestLabelComputation = false;
1314

1415
constructor(viewPortHandler: ViewPortHandler, xAxis: XAxis, trans: Transformer) {
1516
super(viewPortHandler, trans, xAxis);
@@ -56,92 +57,97 @@ export class XAxisRenderer extends AxisRenderer {
5657

5758
this.computeSize();
5859
}
59-
6060
protected computeSize() {
61-
const longest = this.mXAxis.getLongestLabel();
62-
63-
this.mAxisLabelPaint.setTypeface(this.mXAxis.getTypeface());
64-
this.mAxisLabelPaint.setTextSize(this.mXAxis.getTextSize());
65-
66-
const labelSize = Utils.calcTextSize(this.mAxisLabelPaint, longest);
67-
68-
const labelWidth = labelSize.width;
69-
const labelHeight = Utils.calcTextHeight(this.mAxisLabelPaint, 'Q');
70-
// const labelHeight = Utils.getLineHeight(this.mAxisLabelPaint);
71-
72-
const labelRotatedSize = Utils.getSizeOfRotatedRectangleByDegrees(labelWidth, labelHeight, this.mXAxis.getLabelRotationAngle());
73-
74-
this.mXAxis.mLabelWidth = Math.round(labelWidth);
75-
this.mXAxis.mLabelHeight = Math.round(labelHeight);
76-
this.mXAxis.mLabelRotatedWidth = Math.round(labelRotatedSize.width);
77-
this.mXAxis.mLabelRotatedHeight = Math.round(labelRotatedSize.height);
78-
79-
// FSize.recycleInstance(labelRotatedSize);
80-
// FSize.recycleInstance(labelSize);
61+
const axis = this.mXAxis;
62+
this.mAxisLabelPaint.setTypeface(axis.getTypeface());
63+
this.mAxisLabelPaint.setTextSize(axis.getTextSize());
64+
65+
const rotation = axis.getLabelRotationAngle();
66+
if (this.mForceLongestLabelComputation || rotation % 360 !== 0) {
67+
const longest = axis.getLongestLabel();
68+
const labelSize = Utils.calcTextSize(this.mAxisLabelPaint, longest);
69+
const labelWidth = labelSize.width;
70+
const labelHeight = Utils.calcTextHeight(this.mAxisLabelPaint, 'Q');
71+
// const labelHeight = Utils.getLineHeight(this.mAxisLabelPaint);
72+
73+
const labelRotatedSize = Utils.getSizeOfRotatedRectangleByDegrees(labelWidth, labelHeight, axis.getLabelRotationAngle());
74+
75+
axis.mLabelWidth = Math.round(labelWidth);
76+
axis.mLabelHeight = Math.round(labelHeight);
77+
axis.mLabelRotatedWidth = Math.round(labelRotatedSize.width);
78+
axis.mLabelRotatedHeight = Math.round(labelRotatedSize.height);
79+
} else {
80+
axis.mLabelWidth = 1;
81+
axis.mLabelHeight = 1;
82+
axis.mLabelRotatedWidth = 1;
83+
axis.mLabelRotatedHeight = 1;
84+
}
8185
}
8286

8387
@profile
8488
public renderAxisLabels(c: Canvas) {
85-
if (!this.mXAxis.isEnabled() || !this.mXAxis.isDrawLabelsEnabled()) return;
89+
const axis = this.mXAxis;
90+
if (!axis.isEnabled() || !axis.isDrawLabelsEnabled()) return;
8691

87-
const yoffset = this.mXAxis.getYOffset();
92+
const yoffset = axis.getYOffset();
8893

89-
this.mAxisLabelPaint.setTypeface(this.mXAxis.getTypeface());
90-
this.mAxisLabelPaint.setTextSize(this.mXAxis.getTextSize());
91-
this.mAxisLabelPaint.setTextAlign(this.mXAxis.getLabelTextAlign());
92-
this.mAxisLabelPaint.setColor(this.mXAxis.getTextColor());
94+
this.mAxisLabelPaint.setTypeface(axis.getTypeface());
95+
this.mAxisLabelPaint.setTextSize(axis.getTextSize());
96+
this.mAxisLabelPaint.setTextAlign(axis.getLabelTextAlign());
97+
this.mAxisLabelPaint.setColor(axis.getTextColor());
9398
// const align = this.mAxisLabelPaint.getTextAlign();
9499
// this.mAxisLabelPaint.setTextAlign(Align.CENTER);
95100
const rect = this.mAxis.isIgnoringOffsets() ? this.mViewPortHandler.getChartRect() : this.mViewPortHandler.getContentRect();
96101
const pointF = { x: 0, y: 0 };
97-
if (this.mXAxis.getPosition() === XAxisPosition.TOP) {
102+
if (axis.getPosition() === XAxisPosition.TOP) {
98103
pointF.x = 0.5;
99104
pointF.y = 1.0;
100105
this.drawLabels(c, rect.top - yoffset, pointF);
101-
this.drawMarkTicket(c,rect.top,-yoffset/2);
102-
} else if (this.mXAxis.getPosition() === XAxisPosition.TOP_INSIDE) {
106+
this.drawMarkTicket(c, rect.top, -yoffset / 2);
107+
} else if (axis.getPosition() === XAxisPosition.TOP_INSIDE) {
103108
pointF.x = 0.5;
104109
pointF.y = 1.0;
105-
this.drawLabels(c, rect.top + yoffset + this.mXAxis.mLabelRotatedHeight, pointF);
106-
this.drawMarkTicket(c,rect.top,-yoffset/2);
107-
} else if (this.mXAxis.getPosition() === XAxisPosition.BOTTOM) {
110+
this.drawLabels(c, rect.top + yoffset + axis.mLabelRotatedHeight, pointF);
111+
this.drawMarkTicket(c, rect.top, -yoffset / 2);
112+
} else if (axis.getPosition() === XAxisPosition.BOTTOM) {
108113
pointF.x = 0.5;
109114
pointF.y = 0.0;
110115
this.drawLabels(c, rect.bottom + yoffset, pointF);
111-
this.drawMarkTicket(c,rect.bottom,+yoffset/2);
112-
} else if (this.mXAxis.getPosition() === XAxisPosition.BOTTOM_INSIDE) {
116+
this.drawMarkTicket(c, rect.bottom, +yoffset / 2);
117+
} else if (axis.getPosition() === XAxisPosition.BOTTOM_INSIDE) {
113118
pointF.x = 0.5;
114119
pointF.y = 0.0;
115-
this.drawLabels(c, rect.bottom - yoffset - this.mXAxis.mLabelRotatedHeight, pointF);
116-
this.drawMarkTicket(c,rect.bottom,+yoffset/2);
120+
this.drawLabels(c, rect.bottom - yoffset - axis.mLabelRotatedHeight, pointF);
121+
this.drawMarkTicket(c, rect.bottom, +yoffset / 2);
117122
} else {
118123
// BOTH SIDED
119124
pointF.x = 0.5;
120125
pointF.y = 1.0;
121126
this.drawLabels(c, rect.top - yoffset, pointF);
122-
this.drawMarkTicket(c,rect.top,-yoffset/2);
127+
this.drawMarkTicket(c, rect.top, -yoffset / 2);
123128
pointF.x = 0.5;
124129
pointF.y = 0.0;
125130
this.drawLabels(c, rect.bottom + yoffset, pointF);
126-
this.drawMarkTicket(c,rect.bottom,+yoffset/2);
131+
this.drawMarkTicket(c, rect.bottom, +yoffset / 2);
127132
}
128133
// this.mAxisLabelPaint.setTextAlign(align);
129134
// MPPointF.recycleInstance(pointF);
130135
}
131136

132137
public renderAxisLine(c: Canvas) {
133-
if (!this.mXAxis.isDrawAxisLineEnabled() || !this.mXAxis.isEnabled() || this.mXAxis.getAxisLineWidth() === 0 || this.mXAxis.mEntryCount === 0) return;
138+
const axis = this.mXAxis;
139+
if (!axis.isDrawAxisLineEnabled() || !axis.isEnabled() || axis.getAxisLineWidth() === 0 || axis.mEntryCount === 0) return;
134140

135-
this.mAxisLinePaint.setColor(this.mXAxis.getAxisLineColor());
136-
this.mAxisLinePaint.setStrokeWidth(this.mXAxis.getAxisLineWidth());
137-
this.mAxisLinePaint.setPathEffect(this.mXAxis.getAxisLineDashPathEffect());
141+
this.mAxisLinePaint.setColor(axis.getAxisLineColor());
142+
this.mAxisLinePaint.setStrokeWidth(axis.getAxisLineWidth());
143+
this.mAxisLinePaint.setPathEffect(axis.getAxisLineDashPathEffect());
138144
const rect = this.mAxis.isIgnoringOffsets() ? this.mViewPortHandler.getChartRect() : this.mViewPortHandler.getContentRect();
139145

140-
if (this.mXAxis.getPosition() === XAxisPosition.TOP || this.mXAxis.getPosition() === XAxisPosition.TOP_INSIDE || this.mXAxis.getPosition() === XAxisPosition.BOTH_SIDED) {
146+
if (axis.getPosition() === XAxisPosition.TOP || axis.getPosition() === XAxisPosition.TOP_INSIDE || axis.getPosition() === XAxisPosition.BOTH_SIDED) {
141147
c.drawLine(rect.left, rect.top, rect.right, rect.top, this.mAxisLinePaint);
142148
}
143149

144-
if (this.mXAxis.getPosition() === XAxisPosition.BOTTOM || this.mXAxis.getPosition() === XAxisPosition.BOTTOM_INSIDE || this.mXAxis.getPosition() === XAxisPosition.BOTH_SIDED) {
150+
if (axis.getPosition() === XAxisPosition.BOTTOM || axis.getPosition() === XAxisPosition.BOTTOM_INSIDE || axis.getPosition() === XAxisPosition.BOTH_SIDED) {
145151
c.drawLine(rect.left, rect.bottom, rect.top, rect.bottom, this.mAxisLinePaint);
146152
}
147153
}
@@ -153,17 +159,18 @@ export class XAxisRenderer extends AxisRenderer {
153159
*/
154160
@profile
155161
protected drawLabels(c: Canvas, pos, anchor: MPPointF) {
156-
const labelRotationAngleDegrees = this.mXAxis.getLabelRotationAngle();
157-
const centeringEnabled = this.mXAxis.isCenterAxisLabelsEnabled();
158-
const length = this.mXAxis.mEntryCount * 2;
162+
const axis = this.mXAxis;
163+
const labelRotationAngleDegrees = axis.getLabelRotationAngle();
164+
const centeringEnabled = axis.isCenterAxisLabelsEnabled();
165+
const length = axis.mEntryCount * 2;
159166
const positions = Utils.createNativeArray(length);
160167

161168
for (let i = 0; i < length; i += 2) {
162169
// only fill x values
163170
if (centeringEnabled) {
164-
positions[i] = this.mXAxis.mCenteredEntries[i / 2];
171+
positions[i] = axis.mCenteredEntries[i / 2];
165172
} else {
166-
positions[i] = this.mXAxis.mEntries[i / 2];
173+
positions[i] = axis.mEntries[i / 2];
167174
}
168175
if (i + 1 < length) {
169176
positions[i + 1] = 0;
@@ -180,10 +187,10 @@ export class XAxisRenderer extends AxisRenderer {
180187
let x = positions[i];
181188

182189
if (this.mViewPortHandler.isInBoundsX(x)) {
183-
const label = this.mXAxis.getValueFormatter().getAxisLabel(this.mXAxis.mEntries[i / 2], this.mXAxis);
184-
if (this.mXAxis.isAvoidFirstLastClippingEnabled()) {
190+
const label = axis.getValueFormatter().getAxisLabel(axis.mEntries[i / 2], axis);
191+
if (axis.isAvoidFirstLastClippingEnabled()) {
185192
// avoid clipping of the last
186-
if (i / 2 === this.mXAxis.mEntryCount - 1 && this.mXAxis.mEntryCount > 1) {
193+
if (i / 2 === axis.mEntryCount - 1 && axis.mEntryCount > 1) {
187194
const width = Utils.calcTextWidth(this.mAxisLabelPaint, label);
188195

189196
if (width > offsetRight * 2 && x + width > chartWidth) {
@@ -196,7 +203,6 @@ export class XAxisRenderer extends AxisRenderer {
196203
x += width / 2;
197204
}
198205
}
199-
200206
this.drawLabel(c, label, x, pos, anchor, labelRotationAngleDegrees);
201207
}
202208
}
@@ -212,7 +218,7 @@ export class XAxisRenderer extends AxisRenderer {
212218
* @param pos
213219
* @param length
214220
*/
215-
protected drawMarkTicket ( c: Canvas, pos, ticklength) {
221+
protected drawMarkTicket(c: Canvas, pos, ticklength) {
216222
if (!this.mXAxis.isDrawMarkTicksEnabled()) return;
217223

218224
const length = this.mAxis.mEntryCount * 2;
@@ -222,23 +228,24 @@ export class XAxisRenderer extends AxisRenderer {
222228
const positions = this.mRenderGridLinesBuffer;
223229
for (let i = 0; i < length; i += 2) {
224230
positions[i] = this.mXAxis.mEntries[i / 2];
225-
if (i+1 < length) {
226-
positions[i+1] = 0;
231+
if (i + 1 < length) {
232+
positions[i + 1] = 0;
227233
}
228234
}
229235
this.mTrans.pointValuesToPixel(positions);
230236

231237
for (let i = 0; i < length; i += 2) {
232238
const x = positions[i];
233-
c.drawLine(x, pos, x, pos+ticklength , this.mAxisLinePaint);
239+
c.drawLine(x, pos, x, pos + ticklength, this.mAxisLinePaint);
234240
}
235241
}
236242

237243
protected mRenderGridLinesPath = new Path();
238244
protected mRenderGridLinesBuffer = [];
239245

240246
public renderGridLines(c: Canvas) {
241-
if (!this.mXAxis.isDrawGridLinesEnabled() || !this.mXAxis.isEnabled()) return;
247+
const axis = this.mXAxis;
248+
if (!axis.isDrawGridLinesEnabled() || !axis.isEnabled()) return;
242249

243250
const clipRestoreCount = c.save();
244251
c.clipRect(this.getGridClippingRect());
@@ -250,8 +257,8 @@ export class XAxisRenderer extends AxisRenderer {
250257
const positions = this.mRenderGridLinesBuffer;
251258

252259
for (let i = 0; i < length; i += 2) {
253-
positions[i] = this.mXAxis.mEntries[i / 2];
254-
positions[i + 1] = this.mXAxis.mEntries[i / 2];
260+
positions[i] = axis.mEntries[i / 2];
261+
positions[i + 1] = axis.mEntries[i / 2];
255262
}
256263

257264
this.mTrans.pointValuesToPixel(positions);

src/charting/renderer/XAxisRendererHorizontalBarChart.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { profile } from '@nativescript/core';
1111

1212
export class XAxisRendererHorizontalBarChart extends XAxisRenderer {
1313
protected mChart: BarChart;
14-
14+
mForceLongestLabelComputation = true;
1515
protected mRenderLimitLinesPathBuffer: Path = new Path();
1616

1717
constructor(viewPortHandler: ViewPortHandler, xAxis: XAxis, trans: Transformer, chart: BarChart) {

src/charting/renderer/XAxisRendererRadarChart.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { XAxisRenderer } from './XAxisRenderer';
88

99
export class XAxisRendererRadarChart extends XAxisRenderer {
1010
private mChart: RadarChart;
11+
mForceLongestLabelComputation = true;
1112

1213
constructor(viewPortHandler: ViewPortHandler, xAxis: XAxis, chart: RadarChart) {
1314
super(viewPortHandler, xAxis, null);

0 commit comments

Comments
 (0)