Skip to content

Commit 7a91c17

Browse files
authored
Merge pull request #358 from AppDevNext/AxisCodeSimplify
Axis code simplify
2 parents 5d35513 + 914fcf5 commit 7a91c17

File tree

3 files changed

+117
-146
lines changed

3 files changed

+117
-146
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,16 @@ abstract class AxisRenderer(
2525
@JvmField
2626
protected var axis: AxisBase
2727
) : Renderer(viewPortHandler) {
28-
/**
29-
* Returns the Transformer object used for transforming the axis values.
30-
*
31-
* @return
32-
*/
3328

34-
/**
35-
* Returns the Paint object that is used for drawing the grid-lines of the
36-
* axis.
37-
*
38-
* @return
39-
*/
4029
/**
4130
* paint object for the grid lines
4231
*/
43-
var paintGrid: Paint? = null
32+
var paintGrid = Paint().apply {
33+
color = Color.GRAY
34+
strokeWidth = 1f
35+
style = Paint.Style.STROKE
36+
alpha = 90
37+
}
4438
protected set
4539

4640
/**
@@ -51,45 +45,25 @@ abstract class AxisRenderer(
5145
/**
5246
* paint for the x-label values
5347
*/
54-
var paintAxisLabels: Paint? = null
48+
var paintAxisLabels = Paint(Paint.ANTI_ALIAS_FLAG)
5549
protected set
5650

57-
/**
58-
* Returns the Paint object that is used for drawing the axis-line that goes
59-
* alongside the axis.
60-
*
61-
* @return
62-
*/
6351
/**
6452
* paint for the line surrounding the chart
6553
*/
66-
var paintAxisLine: Paint? = null
54+
var paintAxisLine = Paint().apply {
55+
color = Color.BLACK
56+
strokeWidth = 1f
57+
style = Paint.Style.STROKE
58+
}
6759
protected set
6860

6961
/**
7062
* paint used for the limit lines
7163
*/
7264
@JvmField
73-
protected var limitLinePaint: Paint
74-
75-
init {
76-
paintAxisLabels = Paint(Paint.ANTI_ALIAS_FLAG)
77-
78-
paintGrid = Paint().apply {
79-
color = Color.GRAY
80-
strokeWidth = 1f
81-
style = Paint.Style.STROKE
82-
alpha = 90
83-
}
84-
85-
paintAxisLine = Paint().apply {
86-
color = Color.BLACK
87-
strokeWidth = 1f
88-
style = Paint.Style.STROKE
89-
}
90-
limitLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
91-
style = Paint.Style.STROKE
92-
}
65+
protected var limitLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
66+
style = Paint.Style.STROKE
9367
}
9468

9569
/**

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ open class XAxisRenderer(
2626
) :
2727
AxisRenderer(viewPortHandler, trans, xAxis) {
2828
protected fun setupGridPaint() {
29-
paintGrid!!.color = xAxis.gridColor
30-
paintGrid!!.strokeWidth = xAxis.gridLineWidth
31-
paintGrid!!.setPathEffect(xAxis.gridDashPathEffect)
29+
paintGrid.color = xAxis.gridColor
30+
paintGrid.strokeWidth = xAxis.gridLineWidth
31+
paintGrid.setPathEffect(xAxis.gridDashPathEffect)
3232
}
3333

3434
override fun computeAxis(min: Float, max: Float, inverted: Boolean) {
@@ -65,8 +65,8 @@ open class XAxisRenderer(
6565
protected open fun computeSize() {
6666
val longest = xAxis.longestLabel
6767

68-
paintAxisLabels!!.setTypeface(xAxis.typeface)
69-
paintAxisLabels!!.textSize = xAxis.textSize
68+
paintAxisLabels.setTypeface(xAxis.typeface)
69+
paintAxisLabels.textSize = xAxis.textSize
7070

7171
val labelSize = Utils.calcTextSize(paintAxisLabels, longest)
7272

@@ -92,7 +92,7 @@ open class XAxisRenderer(
9292

9393
val yoffset = xAxis.yOffset
9494

95-
paintAxisLabels!!.color = xAxis.textColor
95+
paintAxisLabels.color = xAxis.textColor
9696

9797
val pointF = MPPointF.getInstance(0f, 0f)
9898
when (xAxis.position) {
@@ -131,23 +131,23 @@ open class XAxisRenderer(
131131
override fun renderAxisLine(c: Canvas) {
132132
if (!xAxis.isDrawAxisLineEnabled || !xAxis.isEnabled) return
133133

134-
paintAxisLine!!.color = xAxis.axisLineColor
135-
paintAxisLine!!.strokeWidth = xAxis.axisLineWidth
136-
paintAxisLine!!.setPathEffect(xAxis.axisLineDashPathEffect)
134+
paintAxisLine.color = xAxis.axisLineColor
135+
paintAxisLine.strokeWidth = xAxis.axisLineWidth
136+
paintAxisLine.setPathEffect(xAxis.axisLineDashPathEffect)
137137

138138
if (xAxis.position == XAxisPosition.TOP || xAxis.position == XAxisPosition.TOP_INSIDE || xAxis.position == XAxisPosition.BOTH_SIDED) {
139139
c.drawLine(
140140
viewPortHandler.contentLeft(),
141141
viewPortHandler.contentTop(), viewPortHandler.contentRight(),
142-
viewPortHandler.contentTop(), paintAxisLine!!
142+
viewPortHandler.contentTop(), paintAxisLine
143143
)
144144
}
145145

146146
if (xAxis.position == XAxisPosition.BOTTOM || xAxis.position == XAxisPosition.BOTTOM_INSIDE || xAxis.position == XAxisPosition.BOTH_SIDED) {
147147
c.drawLine(
148148
viewPortHandler.contentLeft(),
149149
viewPortHandler.contentBottom(), viewPortHandler.contentRight(),
150-
viewPortHandler.contentBottom(), paintAxisLine!!
150+
viewPortHandler.contentBottom(), paintAxisLine
151151
)
152152
}
153153
}
@@ -223,8 +223,8 @@ open class XAxisRenderer(
223223
Utils.drawXAxisValue(c, formattedLabel, x, y, paintAxisLabels, anchor, angleDegrees)
224224
}
225225

226-
protected var mRenderGridLinesPath: Path = Path()
227-
protected var mRenderGridLinesBuffer: FloatArray = FloatArray(2)
226+
protected open var mRenderGridLinesPath: Path = Path()
227+
protected open var mRenderGridLinesBuffer: FloatArray = FloatArray(2)
228228
override fun renderGridLines(c: Canvas) {
229229
if (!xAxis.isDrawGridLinesEnabled || !xAxis.isEnabled) return
230230

@@ -280,6 +280,21 @@ open class XAxisRenderer(
280280
return mGridClippingRect
281281
}
282282

283+
@JvmField
284+
protected var mRenderLimitLinesBuffer: FloatArray = FloatArray(2)
285+
286+
@JvmField
287+
protected var mLimitLineClippingRect: RectF = RectF()
288+
289+
var limitLineSegmentsBuffer: FloatArray = FloatArray(4)
290+
private val mLimitLinePath = Path()
291+
292+
init {
293+
paintAxisLabels.color = Color.BLACK
294+
paintAxisLabels.textAlign = Align.CENTER
295+
paintAxisLabels.textSize = Utils.convertDpToPixel(10f)
296+
}
297+
283298
/**
284299
* Draws the grid line at the specified position using the provided path.
285300
*
@@ -293,16 +308,28 @@ open class XAxisRenderer(
293308
gridLinePath.lineTo(x, viewPortHandler.contentTop())
294309

295310
// draw a path because lines don't support dashing on lower android versions
296-
c.drawPath(gridLinePath, paintGrid!!)
311+
c.drawPath(gridLinePath, paintGrid)
297312

298313
gridLinePath.reset()
299314
}
300315

301-
@JvmField
302-
protected var mRenderLimitLinesBuffer: FloatArray = FloatArray(2)
316+
fun renderLimitLineLine(c: Canvas, limitLine: LimitLine, position: FloatArray) {
317+
limitLineSegmentsBuffer[0] = position[0]
318+
limitLineSegmentsBuffer[1] = viewPortHandler.contentTop()
319+
limitLineSegmentsBuffer[2] = position[0]
320+
limitLineSegmentsBuffer[3] = viewPortHandler.contentBottom()
303321

304-
@JvmField
305-
protected var mLimitLineClippingRect: RectF = RectF()
322+
mLimitLinePath.reset()
323+
mLimitLinePath.moveTo(limitLineSegmentsBuffer[0], limitLineSegmentsBuffer[1])
324+
mLimitLinePath.lineTo(limitLineSegmentsBuffer[2], limitLineSegmentsBuffer[3])
325+
326+
limitLinePaint.style = Paint.Style.STROKE
327+
limitLinePaint.color = limitLine.lineColor
328+
limitLinePaint.strokeWidth = limitLine.lineWidth
329+
limitLinePaint.setPathEffect(limitLine.dashPathEffect)
330+
331+
c.drawPath(mLimitLinePath, limitLinePaint)
332+
}
306333

307334
/**
308335
* Draws the LimitLines associated with this axis to the screen.
@@ -340,33 +367,6 @@ open class XAxisRenderer(
340367
}
341368
}
342369

343-
var mLimitLineSegmentsBuffer: FloatArray = FloatArray(4)
344-
private val mLimitLinePath = Path()
345-
346-
init {
347-
paintAxisLabels!!.color = Color.BLACK
348-
paintAxisLabels!!.textAlign = Align.CENTER
349-
paintAxisLabels!!.textSize = Utils.convertDpToPixel(10f)
350-
}
351-
352-
fun renderLimitLineLine(c: Canvas, limitLine: LimitLine, position: FloatArray) {
353-
mLimitLineSegmentsBuffer[0] = position[0]
354-
mLimitLineSegmentsBuffer[1] = viewPortHandler.contentTop()
355-
mLimitLineSegmentsBuffer[2] = position[0]
356-
mLimitLineSegmentsBuffer[3] = viewPortHandler.contentBottom()
357-
358-
mLimitLinePath.reset()
359-
mLimitLinePath.moveTo(mLimitLineSegmentsBuffer[0], mLimitLineSegmentsBuffer[1])
360-
mLimitLinePath.lineTo(mLimitLineSegmentsBuffer[2], mLimitLineSegmentsBuffer[3])
361-
362-
limitLinePaint.style = Paint.Style.STROKE
363-
limitLinePaint.color = limitLine.lineColor
364-
limitLinePaint.strokeWidth = limitLine.lineWidth
365-
limitLinePaint.setPathEffect(limitLine.dashPathEffect)
366-
367-
c.drawPath(mLimitLinePath, limitLinePaint)
368-
}
369-
370370
fun renderLimitLineLabel(c: Canvas, limitLine: LimitLine, position: FloatArray, yOffset: Float) {
371371
val label = limitLine.label
372372

0 commit comments

Comments
 (0)