@@ -80,20 +80,22 @@ open class BarChart : BarLineChartBase<BarData>, BarDataProvider {
8080 }
8181
8282 protected override fun calcMinMax () {
83- if (mFitBars) {
84- mXAxis.calculate(mData.xMin - mData.barWidth / 2f , mData.xMax + mData.barWidth / 2f )
85- } else {
86- mXAxis.calculate(mData.xMin, mData.xMax)
87- }
88-
89- // calculate axis range (min / max) according to provided data
90- mAxisLeft.calculate(mData.getYMin(YAxis .AxisDependency .LEFT ), mData.getYMax(YAxis .AxisDependency .LEFT ))
91- mAxisRight.calculate(
92- mData.getYMin(YAxis .AxisDependency .RIGHT ), mData.getYMax(
93- YAxis .AxisDependency
94- .RIGHT
83+ mData?.let { barData ->
84+ if (mFitBars) {
85+ mXAxis.calculate(barData.xMin - barData.barWidth / 2f , barData.xMax + barData.barWidth / 2f )
86+ } else {
87+ mXAxis.calculate(barData.xMin, barData.xMax)
88+ }
89+
90+ // calculate axis range (min / max) according to provided data
91+ mAxisLeft.calculate(barData.getYMin(YAxis .AxisDependency .LEFT ), barData.getYMax(YAxis .AxisDependency .LEFT ))
92+ mAxisRight.calculate(
93+ barData.getYMin(YAxis .AxisDependency .RIGHT ), barData.getYMax(
94+ YAxis .AxisDependency
95+ .RIGHT
96+ )
9597 )
96- )
98+ }
9799 }
98100
99101 /* *
@@ -134,26 +136,28 @@ open class BarChart : BarLineChartBase<BarData>, BarDataProvider {
134136 * The rect will be assigned Float.MIN_VALUE in all locations if the Entry could not be found in the charts data.
135137 */
136138 open fun getBarBounds (barEntry : BarEntry , outputRect : RectF ) {
137- val set = mData.getDataSetForEntry(barEntry)
139+ mData?.let { barData ->
140+ val set = barData.getDataSetForEntry(barEntry)
138141
139- if (set == null ) {
140- outputRect.set(Float .MIN_VALUE , Float .MIN_VALUE , Float .MIN_VALUE , Float .MIN_VALUE )
141- return
142- }
142+ if (set == null ) {
143+ outputRect.set(Float .MIN_VALUE , Float .MIN_VALUE , Float .MIN_VALUE , Float .MIN_VALUE )
144+ return
145+ }
143146
144- val y = barEntry.y
145- val x = barEntry.x
147+ val y = barEntry.y
148+ val x = barEntry.x
146149
147- val barWidth = mData .barWidth
150+ val barWidth = barData .barWidth
148151
149- val left = x - barWidth / 2f
150- val right = x + barWidth / 2f
151- val top = if (y >= 0 ) y else 0f
152- val bottom = if (y <= 0 ) y else 0f
152+ val left = x - barWidth / 2f
153+ val right = x + barWidth / 2f
154+ val top = if (y >= 0 ) y else 0f
155+ val bottom = if (y <= 0 ) y else 0f
153156
154- outputRect.set(left, top, right, bottom)
157+ outputRect.set(left, top, right, bottom)
155158
156- getTransformer(set.axisDependency)!! .rectValueToPixel(outputRect)
159+ getTransformer(set.axisDependency)!! .rectValueToPixel(outputRect)
160+ }
157161 }
158162
159163 /* *
@@ -182,7 +186,7 @@ open class BarChart : BarLineChartBase<BarData>, BarDataProvider {
182186 highlightValue(Highlight (x, dataSetIndex, stackIndex), false )
183187 }
184188
185- override val barData: BarData
189+ override val barData: BarData ?
186190 get() = mData
187191
188192 /* *
@@ -206,7 +210,7 @@ open class BarChart : BarLineChartBase<BarData>, BarDataProvider {
206210 * @param barSpace the space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f
207211 */
208212 fun groupBars (fromX : Float , groupSpace : Float , barSpace : Float ) {
209- barData.groupBars(fromX, groupSpace, barSpace)
213+ barData? .groupBars(fromX, groupSpace, barSpace)
210214 notifyDataSetChanged()
211215 }
212216
@@ -222,28 +226,29 @@ open class BarChart : BarLineChartBase<BarData>, BarDataProvider {
222226 }
223227
224228 override fun getAccessibilityDescription (): String {
225- val barData = barData
226-
227- val entryCount = barData.entryCount
228-
229- // Find the min and max index
230- val yAxisValueFormatter = axisLeft.valueFormatter
231- val minVal = yAxisValueFormatter!! .getFormattedValue(barData.yMin, null )
232- val maxVal = yAxisValueFormatter.getFormattedValue(barData.yMax, null )
233-
234- // Data range...
235- val xAxisValueFormatter = xAxis.valueFormatter
236- val minRange = xAxisValueFormatter!! .getFormattedValue(barData.xMin, null )
237- val maxRange = xAxisValueFormatter.getFormattedValue(barData.xMax, null )
238-
239- val entries = if (entryCount == 1 ) " entry" else " entries"
240-
241- // Format the values of min and max; to recite them back
242- return String .format(
243- Locale .getDefault(), " The bar chart has %d %s. " +
244- " The minimum value is %s and maximum value is %s." +
245- " Data ranges from %s to %s." ,
246- entryCount, entries, minVal, maxVal, minRange, maxRange
247- )
229+ barData?.let { barData ->
230+ val entryCount = barData.entryCount
231+
232+ // Find the min and max index
233+ val yAxisValueFormatter = axisLeft.valueFormatter
234+ val minVal = yAxisValueFormatter!! .getFormattedValue(barData.yMin, null )
235+ val maxVal = yAxisValueFormatter.getFormattedValue(barData.yMax, null )
236+
237+ // Data range...
238+ val xAxisValueFormatter = xAxis.valueFormatter
239+ val minRange = xAxisValueFormatter!! .getFormattedValue(barData.xMin, null )
240+ val maxRange = xAxisValueFormatter.getFormattedValue(barData.xMax, null )
241+
242+ val entries = if (entryCount == 1 ) " entry" else " entries"
243+
244+ // Format the values of min and max; to recite them back
245+ return String .format(
246+ Locale .getDefault(), " The bar chart has %d %s. " +
247+ " The minimum value is %s and maximum value is %s." +
248+ " Data ranges from %s to %s." ,
249+ entryCount, entries, minVal, maxVal, minRange, maxRange
250+ )
251+ }
252+ return " "
248253 }
249254}
0 commit comments