-
-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy pathAxisRenderer.java
More file actions
245 lines (194 loc) · 6.28 KB
/
AxisRenderer.java
File metadata and controls
245 lines (194 loc) · 6.28 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
package com.github.mikephil.charting.renderer;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;
/**
* Baseclass of all axis renderers.
*
* @author Philipp Jahoda
*/
public abstract class AxisRenderer extends Renderer {
/** base axis this axis renderer works with */
protected AxisBase mAxis;
/** transformer to transform values to screen pixels and return */
protected Transformer mTrans;
/**
* paint object for the grid lines
*/
protected Paint mGridPaint;
/**
* paint for the x-label values
*/
protected Paint mAxisLabelPaint;
/**
* paint for the line surrounding the chart
*/
protected Paint mAxisLinePaint;
/**
* paint used for the limit lines
*/
protected Paint mLimitLinePaint;
public AxisRenderer(ViewPortHandler viewPortHandler, Transformer trans, AxisBase axis) {
super(viewPortHandler);
this.mTrans = trans;
this.mAxis = axis;
if(mViewPortHandler != null) {
mAxisLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mGridPaint = new Paint();
mGridPaint.setColor(Color.GRAY);
mGridPaint.setStrokeWidth(1f);
mGridPaint.setStyle(Style.STROKE);
mGridPaint.setAlpha(90);
mAxisLinePaint = new Paint();
mAxisLinePaint.setColor(Color.BLACK);
mAxisLinePaint.setStrokeWidth(1f);
mAxisLinePaint.setStyle(Style.STROKE);
mLimitLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mLimitLinePaint.setStyle(Paint.Style.STROKE);
}
}
/**
* Returns the Paint object used for drawing the axis (labels).
*
* @return
*/
public Paint getPaintAxisLabels() {
return mAxisLabelPaint;
}
/**
* Returns the Paint object that is used for drawing the grid-lines of the
* axis.
*
* @return
*/
public Paint getPaintGrid() {
return mGridPaint;
}
/**
* Returns the Paint object that is used for drawing the axis-line that goes
* alongside the axis.
*
* @return
*/
public Paint getPaintAxisLine() {
return mAxisLinePaint;
}
/**
* Returns the Transformer object used for transforming the axis values.
*
* @return
*/
public Transformer getTransformer() {
return mTrans;
}
/**
* Computes the axis values.
*
* @param min - the minimum value in the data object for this axis
* @param max - the maximum value in the data object for this axis
*/
public abstract void computeAxis(float min, float max, boolean inverted);
/**
* Compute axis interval and desired number of labels between the two given extremes
* @param min
* @param max
*/
protected abstract void computeAxisInterval(float min, float max);
/**
* Setup axis entries and decimals based on the previously computed interval and labels count
* @param min
* @param max
* @param labelCount
* @param range
* @param interval
*/
protected void computeAxisValues(float min, float max, int labelCount, double range, double interval) {
int n = mAxis.isCenterAxisLabelsEnabled() ? 1 : 0;
// force label count
if (mAxis.isForceLabelsEnabled()) {
interval = (float) range / (float) (labelCount - 1);
mAxis.mEntryCount = labelCount;
if (mAxis.mEntries.length < labelCount) {
// Ensure stops contains at least numStops elements.
mAxis.mEntries = new float[labelCount];
}
float v = min;
for (int i = 0; i < labelCount; i++) {
mAxis.mEntries[i] = v;
v += interval;
}
n = labelCount;
// no forced count
} else {
double first = interval == 0.0 ? 0.0 : Math.ceil(min / interval) * interval;
if(mAxis.isCenterAxisLabelsEnabled()) {
first -= interval;
}
double last = interval == 0.0 ? 0.0 : Utils.nextUp(Math.floor(max / interval) * interval);
double f;
int i;
if (interval != 0.0 && last != first) {
for (f = first; f <= last; f += interval) {
++n;
}
}
else if (last == first && n == 0) {
n = 1;
}
mAxis.mEntryCount = n;
if (mAxis.mEntries.length < n) {
// Ensure stops contains at least numStops elements.
mAxis.mEntries = new float[n];
}
for (f = first, i = 0; i < n; f += interval, ++i) {
if (f == 0.0) // Fix for negative zero case (Where value == -0.0, and 0.0 == -0.0)
f = 0.0;
mAxis.mEntries[i] = (float) f;
}
}
// set decimals
if (interval < 1) {
mAxis.mDecimals = (int) Math.ceil(-Math.log10(interval));
} else {
mAxis.mDecimals = 0;
}
if (mAxis.isCenterAxisLabelsEnabled()) {
if (mAxis.mCenteredEntries.length < n) {
mAxis.mCenteredEntries = new float[n];
}
float offset = (float)interval / 2f;
for (int i = 0; i < n; i++) {
mAxis.mCenteredEntries[i] = mAxis.mEntries[i] + offset;
}
}
}
/**
* Draws the axis labels to the screen.
*
* @param c
*/
public abstract void renderAxisLabels(Canvas c);
/**
* Draws the grid lines belonging to the axis.
*
* @param c
*/
public abstract void renderGridLines(Canvas c);
/**
* Draws the line that goes alongside the axis.
*
* @param c
*/
public abstract void renderAxisLine(Canvas c);
/**
* Draws the LimitLines associated with this axis to the screen.
*
* @param c
*/
public abstract void renderLimitLines(Canvas c);
}