@@ -10,6 +10,7 @@ import { profile } from '@nativescript/core';
10
10
11
11
export class XAxisRenderer extends AxisRenderer {
12
12
protected mXAxis : XAxis ;
13
+ protected mForceLongestLabelComputation = false ;
13
14
14
15
constructor ( viewPortHandler : ViewPortHandler , xAxis : XAxis , trans : Transformer ) {
15
16
super ( viewPortHandler , trans , xAxis ) ;
@@ -56,92 +57,97 @@ export class XAxisRenderer extends AxisRenderer {
56
57
57
58
this . computeSize ( ) ;
58
59
}
59
-
60
60
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
+ }
81
85
}
82
86
83
87
@profile
84
88
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 ;
86
91
87
- const yoffset = this . mXAxis . getYOffset ( ) ;
92
+ const yoffset = axis . getYOffset ( ) ;
88
93
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 ( ) ) ;
93
98
// const align = this.mAxisLabelPaint.getTextAlign();
94
99
// this.mAxisLabelPaint.setTextAlign(Align.CENTER);
95
100
const rect = this . mAxis . isIgnoringOffsets ( ) ? this . mViewPortHandler . getChartRect ( ) : this . mViewPortHandler . getContentRect ( ) ;
96
101
const pointF = { x : 0 , y : 0 } ;
97
- if ( this . mXAxis . getPosition ( ) === XAxisPosition . TOP ) {
102
+ if ( axis . getPosition ( ) === XAxisPosition . TOP ) {
98
103
pointF . x = 0.5 ;
99
104
pointF . y = 1.0 ;
100
105
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 ) {
103
108
pointF . x = 0.5 ;
104
109
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 ) {
108
113
pointF . x = 0.5 ;
109
114
pointF . y = 0.0 ;
110
115
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 ) {
113
118
pointF . x = 0.5 ;
114
119
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 ) ;
117
122
} else {
118
123
// BOTH SIDED
119
124
pointF . x = 0.5 ;
120
125
pointF . y = 1.0 ;
121
126
this . drawLabels ( c , rect . top - yoffset , pointF ) ;
122
- this . drawMarkTicket ( c , rect . top , - yoffset / 2 ) ;
127
+ this . drawMarkTicket ( c , rect . top , - yoffset / 2 ) ;
123
128
pointF . x = 0.5 ;
124
129
pointF . y = 0.0 ;
125
130
this . drawLabels ( c , rect . bottom + yoffset , pointF ) ;
126
- this . drawMarkTicket ( c , rect . bottom , + yoffset / 2 ) ;
131
+ this . drawMarkTicket ( c , rect . bottom , + yoffset / 2 ) ;
127
132
}
128
133
// this.mAxisLabelPaint.setTextAlign(align);
129
134
// MPPointF.recycleInstance(pointF);
130
135
}
131
136
132
137
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 ;
134
140
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 ( ) ) ;
138
144
const rect = this . mAxis . isIgnoringOffsets ( ) ? this . mViewPortHandler . getChartRect ( ) : this . mViewPortHandler . getContentRect ( ) ;
139
145
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 ) {
141
147
c . drawLine ( rect . left , rect . top , rect . right , rect . top , this . mAxisLinePaint ) ;
142
148
}
143
149
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 ) {
145
151
c . drawLine ( rect . left , rect . bottom , rect . top , rect . bottom , this . mAxisLinePaint ) ;
146
152
}
147
153
}
@@ -153,17 +159,18 @@ export class XAxisRenderer extends AxisRenderer {
153
159
*/
154
160
@profile
155
161
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 ;
159
166
const positions = Utils . createNativeArray ( length ) ;
160
167
161
168
for ( let i = 0 ; i < length ; i += 2 ) {
162
169
// only fill x values
163
170
if ( centeringEnabled ) {
164
- positions [ i ] = this . mXAxis . mCenteredEntries [ i / 2 ] ;
171
+ positions [ i ] = axis . mCenteredEntries [ i / 2 ] ;
165
172
} else {
166
- positions [ i ] = this . mXAxis . mEntries [ i / 2 ] ;
173
+ positions [ i ] = axis . mEntries [ i / 2 ] ;
167
174
}
168
175
if ( i + 1 < length ) {
169
176
positions [ i + 1 ] = 0 ;
@@ -180,10 +187,10 @@ export class XAxisRenderer extends AxisRenderer {
180
187
let x = positions [ i ] ;
181
188
182
189
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 ( ) ) {
185
192
// 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 ) {
187
194
const width = Utils . calcTextWidth ( this . mAxisLabelPaint , label ) ;
188
195
189
196
if ( width > offsetRight * 2 && x + width > chartWidth ) {
@@ -196,7 +203,6 @@ export class XAxisRenderer extends AxisRenderer {
196
203
x += width / 2 ;
197
204
}
198
205
}
199
-
200
206
this . drawLabel ( c , label , x , pos , anchor , labelRotationAngleDegrees ) ;
201
207
}
202
208
}
@@ -212,7 +218,7 @@ export class XAxisRenderer extends AxisRenderer {
212
218
* @param pos
213
219
* @param length
214
220
*/
215
- protected drawMarkTicket ( c : Canvas , pos , ticklength ) {
221
+ protected drawMarkTicket ( c : Canvas , pos , ticklength ) {
216
222
if ( ! this . mXAxis . isDrawMarkTicksEnabled ( ) ) return ;
217
223
218
224
const length = this . mAxis . mEntryCount * 2 ;
@@ -222,23 +228,24 @@ export class XAxisRenderer extends AxisRenderer {
222
228
const positions = this . mRenderGridLinesBuffer ;
223
229
for ( let i = 0 ; i < length ; i += 2 ) {
224
230
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 ;
227
233
}
228
234
}
229
235
this . mTrans . pointValuesToPixel ( positions ) ;
230
236
231
237
for ( let i = 0 ; i < length ; i += 2 ) {
232
238
const x = positions [ i ] ;
233
- c . drawLine ( x , pos , x , pos + ticklength , this . mAxisLinePaint ) ;
239
+ c . drawLine ( x , pos , x , pos + ticklength , this . mAxisLinePaint ) ;
234
240
}
235
241
}
236
242
237
243
protected mRenderGridLinesPath = new Path ( ) ;
238
244
protected mRenderGridLinesBuffer = [ ] ;
239
245
240
246
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 ;
242
249
243
250
const clipRestoreCount = c . save ( ) ;
244
251
c . clipRect ( this . getGridClippingRect ( ) ) ;
@@ -250,8 +257,8 @@ export class XAxisRenderer extends AxisRenderer {
250
257
const positions = this . mRenderGridLinesBuffer ;
251
258
252
259
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 ] ;
255
262
}
256
263
257
264
this . mTrans . pointValuesToPixel ( positions ) ;
0 commit comments