-
-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy pathYAxisRenderer.java
More file actions
358 lines (269 loc) · 12 KB
/
YAxisRenderer.java
File metadata and controls
358 lines (269 loc) · 12 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
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.YAxis;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.components.YAxis.YAxisLabelPosition;
import com.github.mikephil.charting.utils.MPPointD;
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 YAxisRenderer extends AxisRenderer {
protected YAxis mYAxis;
protected Paint mZeroLinePaint;
public YAxisRenderer(ViewPortHandler viewPortHandler, YAxis yAxis, Transformer trans) {
super(viewPortHandler, trans, yAxis);
this.mYAxis = yAxis;
if(mViewPortHandler != null) {
mAxisLabelPaint.setColor(Color.BLACK);
mAxisLabelPaint.setTextSize(Utils.convertDpToPixel(10f));
mZeroLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mZeroLinePaint.setColor(Color.GRAY);
mZeroLinePaint.setStrokeWidth(1f);
mZeroLinePaint.setStyle(Paint.Style.STROKE);
}
}
/**
* Return the axis label x position based on axis dependency and label position
* @param dependency
* @param labelPosition
* @return
*/
private float calculateAxisLabelsXPosition(AxisDependency dependency, YAxisLabelPosition labelPosition) {
float viewPortBase = dependency == AxisDependency.LEFT ? mViewPortHandler.offsetLeft() : mViewPortHandler.contentRight();
float xOffset = mYAxis.getXOffset() * (labelPosition == YAxisLabelPosition.OUTSIDE_CHART ? -1 : 1);
return viewPortBase + xOffset;
}
/**
* Return the text align based on axis dependency and label position
* @param dependency
* @param labelPosition
* @return
*/
private Align getAxisLabelTextAlign(AxisDependency dependency, YAxisLabelPosition labelPosition) {
if (dependency == AxisDependency.LEFT ^ labelPosition == YAxisLabelPosition.OUTSIDE_CHART) {
return Align.LEFT;
}
return Align.RIGHT;
}
/**
* draws the y-axis labels to the screen
*/
@Override
public void renderAxisLabels(Canvas c) {
if (!mYAxis.isEnabled() || !mYAxis.isDrawLabelsEnabled())
return;
float[] positions = getTransformedPositions();
mAxisLabelPaint.setTypeface(mYAxis.getTypeface());
mAxisLabelPaint.setTextSize(mYAxis.getTextSize());
mAxisLabelPaint.setColor(mYAxis.getTextColor());
float yOffset = Utils.calcTextHeight(mAxisLabelPaint, "A") / 2.5f + mYAxis.getYOffset();
AxisDependency dependency = mYAxis.getAxisDependency();
YAxisLabelPosition labelPosition = mYAxis.getLabelPosition();
float xPos = calculateAxisLabelsXPosition(dependency, labelPosition);
mAxisLabelPaint.setTextAlign(getAxisLabelTextAlign(dependency, labelPosition));
drawYLabels(c, xPos, positions, yOffset);
}
@Override
public void renderAxisLine(Canvas c) {
if (!mYAxis.isEnabled() || !mYAxis.isDrawAxisLineEnabled())
return;
mAxisLinePaint.setColor(mYAxis.getAxisLineColor());
mAxisLinePaint.setStrokeWidth(mYAxis.getAxisLineWidth());
if (mYAxis.getAxisDependency() == AxisDependency.LEFT) {
c.drawLine(mViewPortHandler.contentLeft(), mViewPortHandler.contentTop(), mViewPortHandler.contentLeft(),
mViewPortHandler.contentBottom(), mAxisLinePaint);
} else {
c.drawLine(mViewPortHandler.contentRight(), mViewPortHandler.contentTop(), mViewPortHandler.contentRight(),
mViewPortHandler.contentBottom(), mAxisLinePaint);
}
}
/**
* draws the y-labels on the specified x-position
*
* @param fixedPosition
* @param positions
*/
protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {
final int from = mYAxis.isDrawBottomYLabelEntryEnabled() ? 0 : 1;
final int to = mYAxis.isDrawTopYLabelEntryEnabled()
? mYAxis.mEntryCount
: (mYAxis.mEntryCount - 1);
float xOffset = mYAxis.getLabelXOffset();
// draw
for (int i = from; i < to; i++) {
String text = mYAxis.getFormattedLabel(i);
c.drawText(text,
fixedPosition + xOffset,
positions[i * 2 + 1] + offset,
mAxisLabelPaint);
}
}
protected Path mRenderGridLinesPath = new Path();
@Override
public void renderGridLines(Canvas c) {
if (!mYAxis.isEnabled())
return;
if (mYAxis.isDrawGridLinesEnabled()) {
int clipRestoreCount = c.save();
c.clipRect(getGridClippingRect());
float[] positions = getTransformedPositions();
mGridPaint.setColor(mYAxis.getGridColor());
mGridPaint.setStrokeWidth(mYAxis.getGridLineWidth());
mGridPaint.setPathEffect(mYAxis.getGridDashPathEffect());
Path gridLinePath = mRenderGridLinesPath;
gridLinePath.reset();
// draw the grid
for (int i = 0; i < positions.length; i += 2) {
// draw a path because lines don't support dashing on lower android versions
c.drawPath(linePath(gridLinePath, i, positions), mGridPaint);
gridLinePath.reset();
}
c.restoreToCount(clipRestoreCount);
}
if (mYAxis.isDrawZeroLineEnabled()) {
drawZeroLine(c);
}
}
protected RectF mGridClippingRect = new RectF();
public RectF getGridClippingRect() {
mGridClippingRect.set(mViewPortHandler.getContentRect());
mGridClippingRect.inset(0.f, -mAxis.getGridLineWidth());
return mGridClippingRect;
}
/**
* Calculates the path for a grid line.
*
* @param p
* @param i
* @param positions
* @return
*/
protected Path linePath(Path p, int i, float[] positions) {
p.moveTo(mViewPortHandler.offsetLeft(), positions[i + 1]);
p.lineTo(mViewPortHandler.contentRight(), positions[i + 1]);
return p;
}
protected float[] mGetTransformedPositionsBuffer = new float[2];
/**
* Transforms the values contained in the axis entries to screen pixels and returns them in form of a float array
* of x- and y-coordinates.
*
* @return
*/
protected float[] getTransformedPositions() {
if(mGetTransformedPositionsBuffer.length != mYAxis.mEntryCount * 2){
mGetTransformedPositionsBuffer = new float[mYAxis.mEntryCount * 2];
}
float[] positions = mGetTransformedPositionsBuffer;
for (int i = 0; i < positions.length; i += 2) {
// only fill y values, x values are not needed for y-labels
positions[i + 1] = mYAxis.mEntries[i / 2];
}
mTrans.pointValuesToPixel(positions);
return positions;
}
protected Path mDrawZeroLinePath = new Path();
protected RectF mZeroLineClippingRect = new RectF();
/**
* Draws the zero line.
*/
protected void drawZeroLine(Canvas c) {
int clipRestoreCount = c.save();
mZeroLineClippingRect.set(mViewPortHandler.getContentRect());
mZeroLineClippingRect.inset(0.f, -mYAxis.getZeroLineWidth());
c.clipRect(mZeroLineClippingRect);
// draw zero line
MPPointD pos = mTrans.getPixelForValues(0f, 0f);
mZeroLinePaint.setColor(mYAxis.getZeroLineColor());
mZeroLinePaint.setStrokeWidth(mYAxis.getZeroLineWidth());
Path zeroLinePath = mDrawZeroLinePath;
zeroLinePath.reset();
zeroLinePath.moveTo(mViewPortHandler.contentLeft(), (float) pos.y);
zeroLinePath.lineTo(mViewPortHandler.contentRight(), (float) pos.y);
// draw a path because lines don't support dashing on lower android versions
c.drawPath(zeroLinePath, mZeroLinePaint);
c.restoreToCount(clipRestoreCount);
}
protected Path mRenderLimitLines = new Path();
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 = mYAxis.getLimitLines();
if (limitLines == null || limitLines.size() <= 0)
return;
float[] pts = mRenderLimitLinesBuffer;
pts[0] = 0;
pts[1] = 0;
Path limitLinePath = mRenderLimitLines;
limitLinePath.reset();
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(0.f, -l.getLineWidth());
c.clipRect(mLimitLineClippingRect);
mLimitLinePaint.setStyle(Paint.Style.STROKE);
mLimitLinePaint.setColor(l.getLineColor());
mLimitLinePaint.setStrokeWidth(l.getLineWidth());
mLimitLinePaint.setPathEffect(l.getDashPathEffect());
pts[1] = l.getLimit();
mTrans.pointValuesToPixel(pts);
limitLinePath.moveTo(mViewPortHandler.contentLeft(), pts[1]);
limitLinePath.lineTo(mViewPortHandler.contentRight(), pts[1]);
c.drawPath(limitLinePath, mLimitLinePaint);
limitLinePath.reset();
// c.drawLines(pts, mLimitLinePaint);
String label = l.getLabel();
// if drawing the limit-value label is enabled
if (label != null && !label.equals("")) {
mLimitLinePaint.setStyle(l.getTextStyle());
mLimitLinePaint.setPathEffect(null);
mLimitLinePaint.setColor(l.getTextColor());
mLimitLinePaint.setTypeface(l.getTypeface());
mLimitLinePaint.setStrokeWidth(0.5f);
mLimitLinePaint.setTextSize(l.getTextSize());
final float labelLineHeight = Utils.calcTextHeight(mLimitLinePaint, label);
float xOffset = Utils.convertDpToPixel(4f) + l.getXOffset();
float yOffset = l.getLineWidth() + labelLineHeight + l.getYOffset();
final LimitLine.LimitLabelPosition position = l.getLabelPosition();
if (position == LimitLine.LimitLabelPosition.RIGHT_TOP) {
mLimitLinePaint.setTextAlign(Align.RIGHT);
c.drawText(label,
mViewPortHandler.contentRight() - xOffset,
pts[1] - yOffset + labelLineHeight, mLimitLinePaint);
} else if (position == LimitLine.LimitLabelPosition.RIGHT_BOTTOM) {
mLimitLinePaint.setTextAlign(Align.RIGHT);
c.drawText(label,
mViewPortHandler.contentRight() - xOffset,
pts[1] + yOffset, mLimitLinePaint);
} else if (position == LimitLine.LimitLabelPosition.LEFT_TOP) {
mLimitLinePaint.setTextAlign(Align.LEFT);
c.drawText(label,
mViewPortHandler.contentLeft() + xOffset,
pts[1] - yOffset + labelLineHeight, mLimitLinePaint);
} else {
mLimitLinePaint.setTextAlign(Align.LEFT);
c.drawText(label,
mViewPortHandler.offsetLeft() + xOffset,
pts[1] + yOffset, mLimitLinePaint);
}
}
c.restoreToCount(clipRestoreCount);
}
}
}