Skip to content

Commit 9e85f0b

Browse files
committed
Fix data null crash
1 parent 1a8643b commit 9e85f0b

9 files changed

Lines changed: 147 additions & 131 deletions

File tree

chartLib/src/main/kotlin/info/appdev/charting/highlight/PieHighlighter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import info.appdev.charting.data.Entry
55

66
open class PieHighlighter(chart: PieChart) : PieRadarHighlighter<PieChart>(chart) {
77
override fun getClosestHighlight(index: Int, x: Float, y: Float): Highlight? {
8-
val pieDataSet = chartPieRadar.data!!.dataSets[0]
8+
val pieDataSet = chartPieRadar.data?.dataSets[0]
99

10-
val entry: Entry? = pieDataSet.getEntryForIndex(index)
10+
val entry: Entry? = pieDataSet?.getEntryForIndex(index)
1111

1212
entry?.let {
1313
return Highlight(index.toFloat(), entry.y, x, y, 0, pieDataSet.axisDependency)

chartLib/src/main/kotlin/info/appdev/charting/highlight/RadarHighlighter.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,22 @@ open class RadarHighlighter(chart: RadarChart) : PieRadarHighlighter<RadarChart>
4343
val factor = chartPieRadar.factor
4444

4545
val pOut = PointF.getInstance(0f, 0f)
46-
for (i in 0..<chartPieRadar.data!!.dataSetCount) {
47-
val dataSet = chartPieRadar.data!!.getDataSetByIndex(i)
46+
chartPieRadar.data?.let { data ->
47+
for (i in 0..<data.dataSetCount) {
48+
val dataSet = data.getDataSetByIndex(i)
4849

49-
val entry: Entry? = dataSet?.getEntryForIndex(index)
50+
val entry: Entry? = dataSet?.getEntryForIndex(index)
5051

51-
val y = (entry!!.y - chartPieRadar.yChartMin)
52+
val y = (entry!!.y - chartPieRadar.yChartMin)
5253

53-
chartPieRadar.centerOffsets.changePosition(
54-
y * factor * phaseY,
55-
sliceAngle * index * phaseX + chartPieRadar.rotationAngle, pOut
56-
)
54+
chartPieRadar.centerOffsets.changePosition(
55+
y * factor * phaseY,
56+
sliceAngle * index * phaseX + chartPieRadar.rotationAngle, pOut
57+
)
5758

58-
mHighlightBuffer.add(Highlight(index.toFloat(), entry.y, pOut.x, pOut.y, i, dataSet.axisDependency))
59+
mHighlightBuffer.add(Highlight(index.toFloat(), entry.y, pOut.x, pOut.y, i, dataSet.axisDependency))
60+
}
5961
}
60-
6162
return mHighlightBuffer
6263
}
6364
}

chartLib/src/main/kotlin/info/appdev/charting/listener/BarLineChartTouchListener.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -494,22 +494,23 @@ class BarLineChartTouchListener(
494494
onChartGestureListener?.onChartDoubleTapped(e)
495495

496496
// check if double-tap zooming is enabled
497-
if (chart.isDoubleTapToZoomEnabled && chart.data!!.entryCount > 0) {
498-
val trans = getTrans(e.x, e.y)
497+
chart.data?.let { data ->
498+
if (chart.isDoubleTapToZoomEnabled && data.entryCount > 0) {
499+
val trans = getTrans(e.x, e.y)
499500

500-
val scaleX = if (chart.isScaleXEnabled) 1.4f else 1f
501-
val scaleY = if (chart.isScaleYEnabled) 1.4f else 1f
501+
val scaleX = if (chart.isScaleXEnabled) 1.4f else 1f
502+
val scaleY = if (chart.isScaleYEnabled) 1.4f else 1f
502503

503-
chart.zoom(scaleX, scaleY, trans.x, trans.y)
504+
chart.zoom(scaleX, scaleY, trans.x, trans.y)
504505

505-
if (chart.isLogging)
506-
Timber.i("Double-Tap, Zooming In, x: ${trans.x} y: ${trans.y}")
506+
if (chart.isLogging)
507+
Timber.i("Double-Tap, Zooming In, x: ${trans.x} y: ${trans.y}")
507508

508-
onChartGestureListener?.onChartScale(e, scaleX, scaleY)
509+
onChartGestureListener?.onChartScale(e, scaleX, scaleY)
509510

510-
PointF.recycleInstance(trans)
511+
PointF.recycleInstance(trans)
512+
}
511513
}
512-
513514
return super.onDoubleTap(e)
514515
}
515516

chartLib/src/main/kotlin/info/appdev/charting/renderer/DataRenderer.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ abstract class DataRenderer(
6161
}
6262

6363
protected open fun isDrawingValuesAllowed(baseProvider: IBaseProvider<*>): Boolean {
64-
return baseProvider.data!!.entryCount < baseProvider.maxVisibleCount * viewPortHandler.scaleX
64+
return baseProvider.data?.let { data ->
65+
data.entryCount < baseProvider.maxVisibleCount * viewPortHandler.scaleX
66+
} ?: run { false }
6567
}
6668

6769
/**

chartLib/src/main/kotlin/info/appdev/charting/renderer/HorizontalBarChartRenderer.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ open class HorizontalBarChartRenderer(
457457
}
458458

459459
override fun isDrawingValuesAllowed(baseProvider: IBaseProvider<*>): Boolean {
460-
return baseProvider.data!!.entryCount < baseProvider.maxVisibleCount * viewPortHandler.scaleY
460+
return baseProvider.data?.let { data ->
461+
data.entryCount < baseProvider.maxVisibleCount * viewPortHandler.scaleY
462+
} ?: false
461463
}
462464
}

chartLib/src/main/kotlin/info/appdev/charting/renderer/PieChartRenderer.kt

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,15 @@ open class PieChartRenderer(
161161
if (!dataSet.isAutomaticallyDisableSliceSpacingEnabled)
162162
return dataSet.sliceSpace
163163

164-
val spaceSizeRatio = dataSet.sliceSpace / viewPortHandler.smallestContentExtension
165-
val minValueRatio = dataSet.yMin / chart.data!!.yValueSum * 2
164+
chart.data?.let { data ->
165+
val spaceSizeRatio = dataSet.sliceSpace / viewPortHandler.smallestContentExtension
166+
val minValueRatio = dataSet.yMin / data.yValueSum * 2
166167

167-
val sliceSpace = if (spaceSizeRatio > minValueRatio) 0f else dataSet.sliceSpace
168+
val sliceSpace = if (spaceSizeRatio > minValueRatio) 0f else dataSet.sliceSpace
168169

169-
return sliceSpace
170+
return sliceSpace
171+
}
172+
return dataSet.sliceSpace
170173
}
171174

172175
protected fun drawDataSet(dataSet: IPieDataSet) {
@@ -904,46 +907,48 @@ open class PieChartRenderer(
904907
* This gives all pie-slices a rounded edge.
905908
*/
906909
protected fun drawRoundedSlices() {
907-
if (!chart.isDrawRoundedSlices)
910+
if (!chart.isDrawRoundedSlices || chart.data == null)
908911
return
909912

910-
val dataSet = chart.data!!.dataSet
913+
chart.data?.let { data ->
914+
val dataSet = data.dataSet
911915

912-
if (!dataSet.isVisible)
913-
return
916+
if (!dataSet.isVisible)
917+
return
914918

915-
val phaseX = animator.phaseX
916-
val phaseY = animator.phaseY
919+
val phaseX = animator.phaseX
920+
val phaseY = animator.phaseY
917921

918-
val center = chart.centerCircleBox
919-
val r = chart.radius
922+
val center = chart.centerCircleBox
923+
val r = chart.radius
920924

921-
// calculate the radius of the "slice-circle"
922-
val circleRadius = (r - (r * chart.holeRadius / 100f)) / 2f
925+
// calculate the radius of the "slice-circle"
926+
val circleRadius = (r - (r * chart.holeRadius / 100f)) / 2f
923927

924-
val drawAngles = chart.drawAngles
925-
var angle = chart.rotationAngle
928+
val drawAngles = chart.drawAngles
929+
var angle = chart.rotationAngle
926930

927-
for (j in 0..<dataSet.entryCount) {
928-
val sliceAngle = drawAngles[j]
931+
for (j in 0..<dataSet.entryCount) {
932+
val sliceAngle = drawAngles[j]
929933

930-
dataSet.getEntryForIndex(j)?.let { entry ->
934+
dataSet.getEntryForIndex(j)?.let { entry ->
931935

932-
// draw only if the value is greater than zero
933-
if ((abs(entry.y.toDouble()) > Utils.FLOAT_EPSILON)) {
934-
val v = Math.toRadians(
935-
((angle + sliceAngle) * phaseY).toDouble()
936-
)
937-
val x = ((r - circleRadius) * cos(v) + center.x).toFloat()
938-
val y = ((r - circleRadius) * sin(v) + center.y).toFloat()
936+
// draw only if the value is greater than zero
937+
if ((abs(entry.y.toDouble()) > Utils.FLOAT_EPSILON)) {
938+
val v = Math.toRadians(
939+
((angle + sliceAngle) * phaseY).toDouble()
940+
)
941+
val x = ((r - circleRadius) * cos(v) + center.x).toFloat()
942+
val y = ((r - circleRadius) * sin(v) + center.y).toFloat()
939943

940-
paintRender.color = dataSet.getColorByIndex(j)
941-
bitmapCanvas!!.drawCircle(x, y, circleRadius, paintRender)
944+
paintRender.color = dataSet.getColorByIndex(j)
945+
bitmapCanvas!!.drawCircle(x, y, circleRadius, paintRender)
946+
}
942947
}
948+
angle += sliceAngle * phaseX
943949
}
944-
angle += sliceAngle * phaseX
950+
PointF.recycleInstance(center)
945951
}
946-
PointF.recycleInstance(center)
947952
}
948953

949954
/**

chartLib/src/main/kotlin/info/appdev/charting/renderer/RadarChartRenderer.kt

Lines changed: 80 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -130,67 +130,69 @@ open class RadarChartRenderer(
130130

131131
val yOffset = 5f.convertDpToPixel()
132132

133-
for (i in 0..<chart.data!!.dataSetCount) {
134-
chart.data!!.getDataSetByIndex(i)?.let { dataSet ->
133+
chart.data?.let { data ->
134+
for (i in 0..<data.dataSetCount) {
135+
data.getDataSetByIndex(i)?.let { dataSet ->
135136

136-
chart.data!!.getDataSetByIndex(i)
137-
if (dataSet.entryCount == 0) {
138-
continue
139-
}
140-
if (!shouldDrawValues(dataSet)) {
141-
continue
142-
}
137+
data.getDataSetByIndex(i)
138+
if (dataSet.entryCount == 0) {
139+
continue
140+
}
141+
if (!shouldDrawValues(dataSet)) {
142+
continue
143+
}
143144

144-
// apply the text-styling defined by the DataSet
145-
applyValueTextStyle(dataSet)
146-
147-
val iconsOffset = PointF.getInstance(dataSet.iconsOffset)
148-
iconsOffset.x = iconsOffset.x.convertDpToPixel()
149-
iconsOffset.y = iconsOffset.y.convertDpToPixel()
150-
151-
for (j in 0..<dataSet.entryCount) {
152-
dataSet.getEntryForIndex(j)?.let { entry ->
153-
154-
pOut = center.getPosition(
155-
(entry.y - chart.yChartMin) * factor * phaseY,
156-
sliceAngle * j * phaseX + chart.rotationAngle
157-
)
158-
159-
if (dataSet.isDrawValues) {
160-
drawValue(
161-
canvas,
162-
dataSet.valueFormatter,
163-
entry.y,
164-
entry,
165-
i,
166-
pOut.x,
167-
pOut.y - yOffset,
168-
dataSet.getValueTextColor(j)
169-
)
170-
}
145+
// apply the text-styling defined by the DataSet
146+
applyValueTextStyle(dataSet)
147+
148+
val iconsOffset = PointF.getInstance(dataSet.iconsOffset)
149+
iconsOffset.x = iconsOffset.x.convertDpToPixel()
150+
iconsOffset.y = iconsOffset.y.convertDpToPixel()
171151

172-
if (entry.icon != null && dataSet.isDrawIcons) {
173-
val icon = entry.icon
152+
for (j in 0..<dataSet.entryCount) {
153+
dataSet.getEntryForIndex(j)?.let { entry ->
174154

175-
pIcon = center.getPosition(
176-
(entry.y) * factor * phaseY + iconsOffset.y,
155+
pOut = center.getPosition(
156+
(entry.y - chart.yChartMin) * factor * phaseY,
177157
sliceAngle * j * phaseX + chart.rotationAngle
178158
)
179159

180-
pIcon.y += iconsOffset.x
160+
if (dataSet.isDrawValues) {
161+
drawValue(
162+
canvas,
163+
dataSet.valueFormatter,
164+
entry.y,
165+
entry,
166+
i,
167+
pOut.x,
168+
pOut.y - yOffset,
169+
dataSet.getValueTextColor(j)
170+
)
171+
}
172+
173+
if (entry.icon != null && dataSet.isDrawIcons) {
174+
val icon = entry.icon
181175

182-
icon?.let {
183-
canvas.drawImage(
184-
it,
185-
pIcon.x.toInt(),
186-
pIcon.y.toInt()
176+
pIcon = center.getPosition(
177+
(entry.y) * factor * phaseY + iconsOffset.y,
178+
sliceAngle * j * phaseX + chart.rotationAngle
187179
)
180+
181+
pIcon.y += iconsOffset.x
182+
183+
icon?.let {
184+
canvas.drawImage(
185+
it,
186+
pIcon.x.toInt(),
187+
pIcon.y.toInt()
188+
)
189+
}
188190
}
189191
}
190192
}
191-
}
192193

193-
PointF.recycleInstance(iconsOffset)
194+
PointF.recycleInstance(iconsOffset)
195+
}
194196
}
195197
}
196198

@@ -218,22 +220,23 @@ open class RadarChartRenderer(
218220
webPaint.color = chart.webColor
219221
webPaint.alpha = chart.webAlpha
220222

221-
val xIncrements = 1 + chart.skipWebLineCount
222-
val maxEntryCount = chart.data!!.maxEntryCountSet?.entryCount ?: 0
223+
chart.data?.let { data ->
224+
val xIncrements = 1 + chart.skipWebLineCount
225+
val maxEntryCount = data.maxEntryCountSet?.entryCount ?: 0
223226

224-
var p = PointF.getInstance(0f, 0f)
225-
var i = 0
226-
while (i < maxEntryCount) {
227-
p = center.getPosition(
228-
chart.yRange * factor,
229-
sliceAngle * i + rotationAngle
230-
)
227+
var p = PointF.getInstance(0f, 0f)
228+
var i = 0
229+
while (i < maxEntryCount) {
230+
p = center.getPosition(
231+
chart.yRange * factor,
232+
sliceAngle * i + rotationAngle
233+
)
231234

232-
canvas.drawLine(center.x, center.y, p.x, p.y, webPaint)
233-
i += xIncrements
235+
canvas.drawLine(center.x, center.y, p.x, p.y, webPaint)
236+
i += xIncrements
237+
}
238+
PointF.recycleInstance(p)
234239
}
235-
PointF.recycleInstance(p)
236-
237240
// draw the inner-web
238241
webPaint.strokeWidth = chart.webLineWidthInner
239242
webPaint.color = chart.webColorInner
@@ -248,21 +251,23 @@ open class RadarChartRenderer(
248251
innerAreaPath.rewind()
249252
paint.color = chart.layerColorList[j]
250253
}
251-
for (i in 0..<chart.data!!.entryCount) {
252-
val r = (chart.yAxis.entries[j] - chart.yChartMin) * factor
253-
254-
p1out = center.getPosition(r, sliceAngle * i + rotationAngle)
255-
p2out = center.getPosition(r, sliceAngle * (i + 1) + rotationAngle)
256-
257-
canvas.drawLine(p1out.x, p1out.y, p2out.x, p2out.y, webPaint)
258-
if (chart.isCustomLayerColorEnable) {
259-
if (p1out.x != p2out.x) {
260-
if (i == 0) {
261-
innerAreaPath.moveTo(p1out.x, p1out.y)
262-
} else {
263-
innerAreaPath.lineTo(p1out.x, p1out.y)
254+
chart.data?.let { data ->
255+
for (i in 0..<data.entryCount) {
256+
val r = (chart.yAxis.entries[j] - chart.yChartMin) * factor
257+
258+
p1out = center.getPosition(r, sliceAngle * i + rotationAngle)
259+
p2out = center.getPosition(r, sliceAngle * (i + 1) + rotationAngle)
260+
261+
canvas.drawLine(p1out.x, p1out.y, p2out.x, p2out.y, webPaint)
262+
if (chart.isCustomLayerColorEnable) {
263+
if (p1out.x != p2out.x) {
264+
if (i == 0) {
265+
innerAreaPath.moveTo(p1out.x, p1out.y)
266+
} else {
267+
innerAreaPath.lineTo(p1out.x, p1out.y)
268+
}
269+
innerAreaPath.lineTo(p2out.x, p2out.y)
264270
}
265-
innerAreaPath.lineTo(p2out.x, p2out.y)
266271
}
267272
}
268273
}

chartLib/src/main/kotlin/info/appdev/charting/renderer/XAxisRendererRadarChart.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class XAxisRendererRadarChart(
3131

3232
val center = chart.centerOffsets
3333
var pOut = PointF.getInstance(0f, 0f)
34-
chart.data!!.maxEntryCountSet?.let { maxEntryCountSet ->
34+
chart.data?.maxEntryCountSet?.let { maxEntryCountSet ->
3535
for (i in 0..<maxEntryCountSet.entryCount) {
3636
val label = xAxis.valueFormatter?.getFormattedValue(i.toFloat(), xAxis)
3737

0 commit comments

Comments
 (0)