-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathYAxisRendererRadarChart.kt
More file actions
218 lines (169 loc) · 6.9 KB
/
YAxisRendererRadarChart.kt
File metadata and controls
218 lines (169 loc) · 6.9 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
package info.appdev.charting.renderer
import android.graphics.Canvas
import android.graphics.Path
import info.appdev.charting.charts.RadarChart
import info.appdev.charting.components.YAxis
import info.appdev.charting.utils.PointF
import info.appdev.charting.utils.ViewPortHandler
import info.appdev.charting.utils.getPosition
import info.appdev.charting.utils.roundToNextSignificant
import kotlin.math.abs
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.log10
import kotlin.math.nextUp
import kotlin.math.pow
class YAxisRendererRadarChart(
viewPortHandler: ViewPortHandler,
yAxis: YAxis,
private val chart: RadarChart
) : YAxisRenderer(viewPortHandler, yAxis, null) {
private val renderLimitLinesPathBuffer = Path()
override fun computeAxisValues(min: Float, max: Float) {
val labelCount = axis.labelCount
val range = abs((max - min).toDouble())
if (labelCount == 0 || range <= 0 || java.lang.Double.isInfinite(range)) {
axis.entries = floatArrayOf()
axis.centeredEntries = floatArrayOf()
axis.entryCount = 0
return
}
// Find out how much spacing (in y value space) between axis values
val rawInterval = range / labelCount
var interval = rawInterval.roundToNextSignificant()
// If granularity is enabled, then do not allow the interval to go below specified granularity.
// This is used to avoid repeated values when rounding values for display.
if (axis.isGranularityEnabled) interval = if (interval < axis.granularity) axis.granularity.toDouble() else interval
// Normalize interval
val intervalMagnitude = 10.0.pow(log10(interval).toInt().toDouble()).roundToNextSignificant()
val intervalSigDigit = (interval / intervalMagnitude).toInt()
if (intervalSigDigit > 5) {
// Use one order of magnitude higher, to avoid intervals like 0.9 or 90
// if it's 0.0 after floor(), we use the old value
interval = if (floor(10.0 * intervalMagnitude) == 0.0)
interval
else floor(10.0 * intervalMagnitude)
}
val centeringEnabled = axis.isCenterAxisLabelsEnabled
var n = if (centeringEnabled) 1 else 0
// force label count
if (axis.isForceLabelsEnabled) {
val step = range.toFloat() / (labelCount - 1).toFloat()
axis.entryCount = labelCount
if (axis.entries.size < labelCount) {
// Ensure stops contains at least numStops elements.
axis.entries = FloatArray(labelCount)
}
var v = min
for (i in 0..<labelCount) {
axis.entries[i] = v
v += step
}
n = labelCount
// no forced count
} else {
var first = if (interval == 0.0) 0.0 else ceil(min / interval) * interval
if (centeringEnabled) {
first -= interval
}
val last = if (interval == 0.0) 0.0 else (floor(max / interval) * interval).nextUp()
var f: Double
if (interval != 0.0) {
f = first
while (f <= last) {
++n
f += interval
}
}
n++
axis.entryCount = n
if (axis.entries.size < n) {
// Ensure stops contains at least numStops elements.
axis.entries = FloatArray(n)
}
f = first
var i = 0
while (i < n) {
if (f == 0.0) // Fix for negative zero case (Where value == -0.0, and 0.0 == -0.0)
f = 0.0
axis.entries[i] = f.toFloat()
f += interval
++i
}
}
// set decimals
if (interval < 1) {
axis.mDecimals = ceil(-log10(interval)).toInt()
} else {
axis.mDecimals = 0
}
if (centeringEnabled) {
if (axis.centeredEntries.size < n) {
axis.centeredEntries = FloatArray(n)
}
val offset = (axis.entries[1] - axis.entries[0]) / 2f
for (i in 0..<n) {
axis.centeredEntries[i] = axis.entries[i] + offset
}
}
axis.mAxisMinimum = axis.entries[0]
axis.mAxisMaximum = axis.entries[n - 1]
axis.mAxisRange = abs((axis.mAxisMaximum - axis.mAxisMinimum).toDouble()).toFloat()
}
override fun renderAxisLabels(canvas: Canvas) {
if (!yAxis.isEnabled || !yAxis.isDrawLabelsEnabled)
return
paintAxisLabels.typeface = yAxis.typeface
paintAxisLabels.textSize = yAxis.textSize
paintAxisLabels.color = yAxis.textColor
val center = chart.centerOffsets
var pOut = PointF.getInstance(0f, 0f)
val factor = chart.factor
val from = if (yAxis.isDrawBottomYLabelEntryEnabled) 0 else 1
val to = if (yAxis.isDrawTopYLabelEntryEnabled)
yAxis.entryCount
else
(yAxis.entryCount - 1)
val xOffset = yAxis.labelXOffset
for (j in from..<to) {
val r = (yAxis.entries[j] - yAxis.mAxisMinimum) * factor
pOut = center.getPosition(r, chart.rotationAngle)
val label = yAxis.getFormattedLabel(j)
label?.let { canvas.drawText(it, pOut.x + xOffset, pOut.y, paintAxisLabels) }
}
PointF.recycleInstance(center)
PointF.recycleInstance(pOut)
}
override fun renderLimitLines(canvas: Canvas) {
val limitLines = yAxis.limitLines
val sliceAngle = chart.sliceAngle
// calculate the factor that is needed for transforming the value to
// pixels
val factor = chart.factor
val center = chart.centerOffsets
var pOut = PointF.getInstance(0f, 0f)
for (i in limitLines.indices) {
val limitLine = limitLines[i]
if (!limitLine.isEnabled) continue
limitLinePaint.color = limitLine.lineColor
limitLinePaint.pathEffect = limitLine.dashPathEffect
limitLinePaint.strokeWidth = limitLine.lineWidth
val r = (limitLine.limit - chart.yChartMin) * factor
val limitPath = renderLimitLinesPathBuffer
limitPath.reset()
chart.data!!.maxEntryCountSet?.let { maxEntryCountSet ->
for (j in 0..<maxEntryCountSet.entryCount) {
pOut = center.getPosition(r, sliceAngle * j + chart.rotationAngle)
if (j == 0)
limitPath.moveTo(pOut.x, pOut.y)
else
limitPath.lineTo(pOut.x, pOut.y)
}
}
limitPath.close()
canvas.drawPath(limitPath, limitLinePaint)
}
PointF.recycleInstance(center)
PointF.recycleInstance(pOut)
}
}