XAxisRenderer.java
17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Path;
import android.graphics.RectF;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.utils.FSize;
import com.github.mikephil.charting.utils.MPPointD;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;
import java.util.List;
public class XAxisRenderer extends AxisRenderer {
protected XAxis mXAxis;
public XAxisRenderer(ViewPortHandler viewPortHandler, XAxis xAxis, Transformer trans) {
super(viewPortHandler, trans, xAxis);
this.mXAxis = xAxis;
mAxisLabelPaint.setColor(Color.BLACK);
mAxisLabelPaint.setTextAlign(Align.CENTER);
mAxisLabelPaint.setTextSize(Utils.convertDpToPixel(10f));
}
protected void setupGridPaint() {
mGridPaint.setColor(mXAxis.getGridColor());
mGridPaint.setStrokeWidth(mXAxis.getGridLineWidth());
mGridPaint.setPathEffect(mXAxis.getGridDashPathEffect());
}
@Override
public void computeAxis(float min, float max, boolean inverted) {
// calculate the starting and entry point of the y-labels (depending on
// zoom / contentrect bounds)
if (mViewPortHandler.contentWidth() > 10 && !mViewPortHandler.isFullyZoomedOutX()) {
MPPointD p1 = mTrans.getValuesByTouchPoint(mViewPortHandler.contentLeft(), mViewPortHandler.contentTop());
MPPointD p2 = mTrans.getValuesByTouchPoint(mViewPortHandler.contentRight(), mViewPortHandler.contentTop());
if (inverted) {
min = (float) p2.x;
max = (float) p1.x;
} else {
min = (float) p1.x;
max = (float) p2.x;
}
MPPointD.recycleInstance(p1);
MPPointD.recycleInstance(p2);
}
computeAxisValues(min, max);
}
@Override
protected void computeAxisValues(float min, float max) {
super.computeAxisValues(min, max);
computeSize();
}
protected void computeSize() {
String longest = mXAxis.getLongestLabel();
mAxisLabelPaint.setTypeface(mXAxis.getTypeface());
mAxisLabelPaint.setTextSize(mXAxis.getTextSize());
final FSize labelSize = Utils.calcTextSize(mAxisLabelPaint, longest);
final float labelWidth = labelSize.width;
final float labelHeight = Utils.calcTextHeight(mAxisLabelPaint, "Q");
final FSize labelRotatedSize = Utils.getSizeOfRotatedRectangleByDegrees(
labelWidth,
labelHeight,
mXAxis.getLabelRotationAngle());
mXAxis.mLabelWidth = Math.round(labelWidth);
mXAxis.mLabelHeight = Math.round(labelHeight);
mXAxis.mLabelRotatedWidth = Math.round(labelRotatedSize.width);
mXAxis.mLabelRotatedHeight = Math.round(labelRotatedSize.height);
FSize.recycleInstance(labelRotatedSize);
FSize.recycleInstance(labelSize);
}
@Override
public void renderAxisLabels(Canvas c) {
if (!mXAxis.isEnabled() || !mXAxis.isDrawLabelsEnabled())
return;
float yoffset = mXAxis.getYOffset();
mAxisLabelPaint.setTypeface(mXAxis.getTypeface());
mAxisLabelPaint.setTextSize(mXAxis.getTextSize());
mAxisLabelPaint.setColor(mXAxis.getTextColor());
MPPointF pointF = MPPointF.getInstance(0, 0);
if (mXAxis.getPosition() == XAxisPosition.TOP) {
pointF.x = 0.5f;
pointF.y = 1.0f;
drawLabels(c, mViewPortHandler.contentTop() - yoffset, pointF);
} else if (mXAxis.getPosition() == XAxisPosition.TOP_INSIDE) {
pointF.x = 0.5f;
pointF.y = 1.0f;
drawLabels(c, mViewPortHandler.contentTop() + yoffset + mXAxis.mLabelRotatedHeight, pointF);
} else if (mXAxis.getPosition() == XAxisPosition.BOTTOM) {
pointF.x = 0.5f;
pointF.y = 0.0f;
drawLabels(c, mViewPortHandler.contentBottom() + yoffset, pointF);
} else if (mXAxis.getPosition() == XAxisPosition.BOTTOM_INSIDE) {
pointF.x = 0.5f;
pointF.y = 0.0f;
drawLabels(c, mViewPortHandler.contentBottom() - yoffset - mXAxis.mLabelRotatedHeight, pointF);
} else { // BOTH SIDED
pointF.x = 0.5f;
pointF.y = 1.0f;
drawLabels(c, mViewPortHandler.contentTop() - yoffset, pointF);
pointF.x = 0.5f;
pointF.y = 0.0f;
drawLabels(c, mViewPortHandler.contentBottom() + yoffset, pointF);
}
MPPointF.recycleInstance(pointF);
}
@Override
public void renderAxisLine(Canvas c) {
if (!mXAxis.isDrawAxisLineEnabled() || !mXAxis.isEnabled())
return;
mAxisLinePaint.setColor(mXAxis.getAxisLineColor());
mAxisLinePaint.setStrokeWidth(mXAxis.getAxisLineWidth());
mAxisLinePaint.setPathEffect(mXAxis.getAxisLineDashPathEffect());
if (mXAxis.getPosition() == XAxisPosition.TOP
|| mXAxis.getPosition() == XAxisPosition.TOP_INSIDE
|| mXAxis.getPosition() == XAxisPosition.BOTH_SIDED) {
c.drawLine(mViewPortHandler.contentLeft(),
mViewPortHandler.contentTop(), mViewPortHandler.contentRight(),
mViewPortHandler.contentTop(), mAxisLinePaint);
}
if (mXAxis.getPosition() == XAxisPosition.BOTTOM
|| mXAxis.getPosition() == XAxisPosition.BOTTOM_INSIDE
|| mXAxis.getPosition() == XAxisPosition.BOTH_SIDED) {
c.drawLine(mViewPortHandler.contentLeft(),
mViewPortHandler.contentBottom(), mViewPortHandler.contentRight(),
mViewPortHandler.contentBottom(), mAxisLinePaint);
}
}
/**
* 绘X轴制刻度线
* @param c
*/
public void renderScaleLines(Canvas c) {
if (!mXAxis.isDrawScale() || !mXAxis.isEnabled())
return;
if (mRenderGridLinesBuffer.length != mAxis.mEntryCount * 2) {
mRenderGridLinesBuffer = new float[mXAxis.mEntryCount * 2];
}
float[] positions = mRenderGridLinesBuffer;
for (int i = 0; i < positions.length; i += 2) {
positions[i] = mXAxis.mEntries[i / 2];
positions[i + 1] = mXAxis.mEntries[i / 2];
}
mTrans.pointValuesToPixel(positions); //获得X轴点对应的像素点位置 即坐标系位置
for (int i = 0; i < positions.length - 2; i += 2) {
//计算X轴两个值之间的间距/5 = 画布偏移量 即 刻度间距 还是默认5个刻度一组
float offset = (positions[2] - positions[0]) / 5;
drawScale(c, positions[i], offset);
}
}
/**
* 绘制线
*/
protected void drawScale(Canvas canvas, float startX, float offset) {
boolean isDrawShortLine = false;
float topY = mViewPortHandler.contentTop(); //顶部X轴所在的位置
float bottomY = mViewPortHandler.contentBottom(); //底部X轴所在的位置
canvas.save();
if (mXAxis.getPosition() == XAxisPosition.BOTTOM) { //X轴位置在下方时
for (int i = 0; i <= 5; i++) {
canvas.save();
canvas.translate(offset * i, 0);
if (i % 5 == 0) {
//刻度线在图表内部
// canvas.drawLine(startX, bottomY - 20, startX, bottomY, mAxisLinePaint);//画长刻度线
//刻度线在图表外面
canvas.drawLine(startX, bottomY + 20, startX, bottomY, mAxisLinePaint);//画长刻度线
} else if (isDrawShortLine){
// canvas.drawLine(startX, bottomY - 10, startX, bottomY, mAxisLinePaint);//画短刻度线
canvas.drawLine(startX, bottomY + 10, startX, bottomY, mAxisLinePaint);//画短刻度线
}
canvas.restore();
}
} else if (mXAxis.getPosition() == XAxisPosition.TOP) { //X轴位置在上方时
for (int i = 0; i <= 5; i++) {
canvas.save();
canvas.translate(offset * i, 0);
if (i % 5 == 0) {
canvas.drawLine(startX, topY + 20, startX, topY, mAxisLinePaint);//画长刻度线
} else if (isDrawShortLine){
canvas.drawLine(startX, topY + 10, startX, topY, mAxisLinePaint);//画短刻度线
}
canvas.restore();
}
} else if (mXAxis.getPosition() == XAxisPosition.BOTH_SIDED) { //上下都有X轴时
for (int i = 0; i <= 5; i++) {
canvas.save();
canvas.translate(offset * i, 0);
if (i % 5 == 0) {
//画长刻度线
canvas.drawLine(startX, topY + 20, startX, topY, mAxisLinePaint);//顶部X轴的刻度
canvas.drawLine(startX, bottomY - 20, startX, bottomY, mAxisLinePaint);//底部X轴的刻度
} else if (isDrawShortLine){
//画短刻度线
canvas.drawLine(startX, topY + 10, startX, topY, mAxisLinePaint);//顶部X轴的刻度
canvas.drawLine(startX, bottomY - 10, startX, bottomY, mAxisLinePaint);//底部X轴的刻度
}
canvas.restore();
}
}
canvas.restore();
}
/**
* draws the x-labels on the specified y-position
*
* @param pos
*/
protected void drawLabels(Canvas c, float pos, MPPointF anchor) {
final float labelRotationAngleDegrees = mXAxis.getLabelRotationAngle();
boolean centeringEnabled = mXAxis.isCenterAxisLabelsEnabled();
float[] positions = new float[mXAxis.mEntryCount * 2];
for (int i = 0; i < positions.length; i += 2) {
// only fill x values
if (centeringEnabled) {
positions[i] = mXAxis.mCenteredEntries[i / 2];
} else {
positions[i] = mXAxis.mEntries[i / 2];
}
}
mTrans.pointValuesToPixel(positions);
for (int i = 0; i < positions.length; i += 2) {
float x = positions[i];
if (mViewPortHandler.isInBoundsX(x)) {
String label = mXAxis.getValueFormatter().getFormattedValue(mXAxis.mEntries[i / 2], mXAxis);
if (mXAxis.isAvoidFirstLastClippingEnabled()) {
// avoid clipping of the last
if (i / 2 == mXAxis.mEntryCount - 1 && mXAxis.mEntryCount > 1) {
float width = Utils.calcTextWidth(mAxisLabelPaint, label);
if (width > mViewPortHandler.offsetRight() * 2
&& x + width > mViewPortHandler.getChartWidth())
x -= width / 2;
// avoid clipping of the first
} else if (i == 0) {
float width = Utils.calcTextWidth(mAxisLabelPaint, label);
x += width / 2;
}
}
drawLabel(c, label, x, pos, anchor, labelRotationAngleDegrees);
}
}
}
protected void drawLabel(Canvas c, String formattedLabel, float x, float y, MPPointF anchor, float angleDegrees) {
Utils.drawXAxisValue(c, formattedLabel, x, y, mAxisLabelPaint, anchor, angleDegrees);
}
protected Path mRenderGridLinesPath = new Path();
protected float[] mRenderGridLinesBuffer = new float[2];
@Override
public void renderGridLines(Canvas c) {
if (!mXAxis.isDrawGridLinesEnabled() || !mXAxis.isEnabled())
return;
int clipRestoreCount = c.save();
c.clipRect(getGridClippingRect());
if (mRenderGridLinesBuffer.length != mAxis.mEntryCount * 2) {
mRenderGridLinesBuffer = new float[mXAxis.mEntryCount * 2];
}
float[] positions = mRenderGridLinesBuffer;
for (int i = 0; i < positions.length; i += 2) {
positions[i] = mXAxis.mEntries[i / 2];
positions[i + 1] = mXAxis.mEntries[i / 2];
}
mTrans.pointValuesToPixel(positions);
setupGridPaint();
Path gridLinePath = mRenderGridLinesPath;
gridLinePath.reset();
for (int i = 0; i < positions.length; i += 2) {
drawGridLine(c, positions[i], positions[i + 1], gridLinePath);
}
c.restoreToCount(clipRestoreCount);
}
protected RectF mGridClippingRect = new RectF();
public RectF getGridClippingRect() {
mGridClippingRect.set(mViewPortHandler.getContentRect());
mGridClippingRect.inset(-mAxis.getGridLineWidth(), 0.f);
return mGridClippingRect;
}
/**
* Draws the grid line at the specified position using the provided path.
*
* @param c
* @param x
* @param y
* @param gridLinePath
*/
protected void drawGridLine(Canvas c, float x, float y, Path gridLinePath) {
gridLinePath.moveTo(x, mViewPortHandler.contentBottom());
gridLinePath.lineTo(x, mViewPortHandler.contentTop());
// draw a path because lines don't support dashing on lower android versions
c.drawPath(gridLinePath, mGridPaint);
gridLinePath.reset();
}
protected float[] mRenderLimitLinesBuffer = new float[2];
protected RectF mLimitLineClippingRect = new RectF();
/**
* Draws the LimitLines associated with this axis to the screen.
*
* @param c
*/
@Override
public void renderLimitLines(Canvas c) {
List<LimitLine> limitLines = mXAxis.getLimitLines();
if (limitLines == null || limitLines.size() <= 0)
return;
float[] position = mRenderLimitLinesBuffer;
position[0] = 0;
position[1] = 0;
for (int i = 0; i < limitLines.size(); i++) {
LimitLine l = limitLines.get(i);
if (!l.isEnabled())
continue;
int clipRestoreCount = c.save();
mLimitLineClippingRect.set(mViewPortHandler.getContentRect());
mLimitLineClippingRect.inset(-l.getLineWidth(), 0.f);
c.clipRect(mLimitLineClippingRect);
position[0] = l.getLimit();
position[1] = 0.f;
mTrans.pointValuesToPixel(position);
renderLimitLineLine(c, l, position);
renderLimitLineLabel(c, l, position, 2.f + l.getYOffset());
c.restoreToCount(clipRestoreCount);
}
}
float[] mLimitLineSegmentsBuffer = new float[4];
private Path mLimitLinePath = new Path();
public void renderLimitLineLine(Canvas c, LimitLine limitLine, float[] position) {
mLimitLineSegmentsBuffer[0] = position[0];
mLimitLineSegmentsBuffer[1] = mViewPortHandler.contentTop();
mLimitLineSegmentsBuffer[2] = position[0];
mLimitLineSegmentsBuffer[3] = mViewPortHandler.contentBottom();
mLimitLinePath.reset();
mLimitLinePath.moveTo(mLimitLineSegmentsBuffer[0], mLimitLineSegmentsBuffer[1]);
mLimitLinePath.lineTo(mLimitLineSegmentsBuffer[2], mLimitLineSegmentsBuffer[3]);
mLimitLinePaint.setStyle(Paint.Style.STROKE);
mLimitLinePaint.setColor(limitLine.getLineColor());
mLimitLinePaint.setStrokeWidth(limitLine.getLineWidth());
mLimitLinePaint.setPathEffect(limitLine.getDashPathEffect());
c.drawPath(mLimitLinePath, mLimitLinePaint);
}
public void renderLimitLineLabel(Canvas c, LimitLine limitLine, float[] position, float yOffset) {
String label = limitLine.getLabel();
// if drawing the limit-value label is enabled
if (label != null && !label.equals("")) {
mLimitLinePaint.setStyle(limitLine.getTextStyle());
mLimitLinePaint.setPathEffect(null);
mLimitLinePaint.setColor(limitLine.getTextColor());
mLimitLinePaint.setStrokeWidth(0.5f);
mLimitLinePaint.setTextSize(limitLine.getTextSize());
float xOffset = limitLine.getLineWidth() + limitLine.getXOffset();
final LimitLine.LimitLabelPosition labelPosition = limitLine.getLabelPosition();
if (labelPosition == LimitLine.LimitLabelPosition.RIGHT_TOP) {
final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
mLimitLinePaint.setTextAlign(Align.LEFT);
c.drawText(label, position[0] + xOffset, mViewPortHandler.contentTop() + yOffset + labelLineHeight,
mLimitLinePaint);
} else if (labelPosition == LimitLine.LimitLabelPosition.RIGHT_BOTTOM) {
mLimitLinePaint.setTextAlign(Align.LEFT);
c.drawText(label, position[0] + xOffset, mViewPortHandler.contentBottom() - yOffset, mLimitLinePaint);
} else if (labelPosition == LimitLine.LimitLabelPosition.LEFT_TOP) {
mLimitLinePaint.setTextAlign(Align.RIGHT);
final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
c.drawText(label, position[0] - xOffset, mViewPortHandler.contentTop() + yOffset + labelLineHeight,
mLimitLinePaint);
} else {
mLimitLinePaint.setTextAlign(Align.RIGHT);
c.drawText(label, position[0] - xOffset, mViewPortHandler.contentBottom() - yOffset, mLimitLinePaint);
}
}
}
}