Skip to content

Commit 43b60b2

Browse files
committed
Simplify Legend
1 parent 1bb6d9e commit 43b60b2

29 files changed

Lines changed: 171 additions & 270 deletions

MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,6 @@ public void getBarBounds(BarEntry barEntry, RectF outputRect) {
224224
RectF bounds = outputRect;
225225
IBarDataSet set = mData.getDataSetForEntry(barEntry);
226226

227-
if (set == null) {
228-
outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
229-
return;
230-
}
231-
232227
float y = barEntry.getY();
233228
float x = barEntry.getX();
234229

MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.kt

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import kotlin.collections.MutableList
1818
import kotlin.collections.toTypedArray
1919
import kotlin.math.max
2020
import kotlin.math.min
21-
import kotlin.requireNotNull
2221

2322
/**
2423
* Class representing the legend of the chart. The legend will contain one entry
@@ -87,34 +86,21 @@ class Legend() : ComponentBase() {
8786
var extraEntries: Array<LegendEntry> = arrayOf<LegendEntry>()
8887
private set
8988

90-
/**
91-
* @return true if a custom legend entries has been set default
92-
* false (automatic legend)
93-
*/
9489
/**
9590
* Are the legend labels/colors a custom value or auto calculated? If false,
9691
* then it's auto, if true, then custom. default false (automatic legend)
9792
*/
9893
var isLegendCustom: Boolean = false
9994
private set
10095

101-
/**
102-
* returns the horizontal alignment of the legend
103-
*/
10496
/**
10597
* sets the horizontal alignment of the legend
10698
*/
107-
var horizontalAlignment: LegendHorizontalAlignment? = LegendHorizontalAlignment.LEFT
108-
/**
109-
* returns the vertical alignment of the legend
110-
*/
99+
var horizontalAlignment: LegendHorizontalAlignment = LegendHorizontalAlignment.LEFT
111100
/**
112101
* sets the vertical alignment of the legend
113102
*/
114-
var verticalAlignment: LegendVerticalAlignment? = LegendVerticalAlignment.BOTTOM
115-
/**
116-
* returns the orientation of the legend
117-
*/
103+
var verticalAlignment: LegendVerticalAlignment = LegendVerticalAlignment.BOTTOM
118104
/**
119105
* sets the orientation of the legend
120106
*/
@@ -126,106 +112,48 @@ class Legend() : ComponentBase() {
126112
var isDrawInsideEnabled: Boolean = false
127113
private set
128114

129-
/**
130-
* returns the text direction of the legend
131-
*/
132-
/**
133-
* sets the text direction of the legend
134-
*/
135115
/**
136116
* the text direction for the legend
137117
*/
138-
var direction: LegendDirection? = LegendDirection.LEFT_TO_RIGHT
118+
var direction: LegendDirection = LegendDirection.LEFT_TO_RIGHT
139119

140-
/**
141-
* returns the current form/shape that is set for the legend
142-
*/
143-
/**
144-
* sets the form/shape of the legend forms
145-
*/
146120
/**
147121
* the shape/form the legend colors are drawn in
148122
*/
149-
var form: LegendForm? = LegendForm.SQUARE
123+
var form: LegendForm = LegendForm.SQUARE
150124

151-
/**
152-
* returns the size in dp of the legend forms
153-
*/
154-
/**
155-
* sets the size in dp of the legend forms, default 8f
156-
*/
157125
/**
158126
* the size of the legend forms/shapes
159127
*/
160128
var formSize: kotlin.Float = 8f
161129

162-
/**
163-
* returns the line width in dp for drawing forms that consist of lines
164-
*/
165-
/**
166-
* sets the line width in dp for forms that consist of lines, default 3f
167-
*/
168130
/**
169131
* the size of the legend forms/shapes
170132
*/
171133
var formLineWidth: kotlin.Float = 3f
172134

173-
/**
174-
* @return The line dash path effect used for shapes that consist of lines.
175-
*/
176-
/**
177-
* Sets the line dash path effect used for shapes that consist of lines.
178-
*/
179135
/**
180136
* Line dash path effect used for shapes that consist of lines.
181137
*/
182138
var formLineDashEffect: DashPathEffect? = null
183139

184-
/**
185-
* returns the space between the legend entries on a horizontal axis in
186-
* pixels
187-
*/
188-
/**
189-
* sets the space between the legend entries on a horizontal axis in pixels,
190-
* converts to dp internally
191-
*/
192140
/**
193141
* the space between the legend entries on a horizontal axis, default 6f
194142
*/
195143
var xEntrySpace: kotlin.Float = 6f
196144

197-
/**
198-
* returns the space between the legend entries on a vertical axis in pixels
199-
*/
200-
/**
201-
* sets the space between the legend entries on a vertical axis in pixels,
202-
* converts to dp internally
203-
*/
204145
/**
205146
* the space between the legend entries on a vertical axis, default 5f
206147
*/
207148
var yEntrySpace: kotlin.Float = 0f
208149

209-
/**
210-
* returns the space between the form and the actual label/text
211-
*/
212-
/**
213-
* sets the space between the form and the actual label/text, converts to dp
214-
* internally
215-
*/
216150
/**
217151
* the space between the legend entries on a vertical axis, default 2f
218152
* private float mYEntrySpace = 2f; / ** the space between the form and the
219153
* actual label/text
220154
*/
221155
var formToTextSpace: kotlin.Float = 5f
222156

223-
/**
224-
* returns the space that is left out between stacked forms (with no label)
225-
*/
226-
/**
227-
* sets the space that is left out between stacked forms (with no label)
228-
*/
229157
/**
230158
* the space that should be left between stacked forms
231159
*/
@@ -239,23 +167,12 @@ class Legend() : ComponentBase() {
239167
* piechart, then this defines the size of the rectangular bounds out of the
240168
* size of the "hole". / default: 0.95f (95%)
241169
*/
242-
/**
243-
* The maximum relative size out of the whole chart view. / If
244-
* the legend is to the right/left of the chart, then this affects the width
245-
* of the legend. / If the legend is to the top/bottom of the chart, then
246-
* this affects the height of the legend. / default: 0.95f (95%)
247-
*/
248-
/**
249-
* the maximum relative size out of the whole chart view in percent
250-
*/
251170
var maxSizePercent: kotlin.Float = 0.95f
252171

253172
/**
254173
* Constructor. Provide entries for the legend.
255174
*/
256175
constructor(entries: Array<LegendEntry>) : this() {
257-
requireNotNull(entries) { "entries array is NULL" }
258-
259176
this.entries = entries
260177
}
261178

MPChartLib/src/main/java/com/github/mikephil/charting/components/LegendEntry.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LegendEntry {
1919
*/
2020
constructor(
2121
label: String?,
22-
form: LegendForm?,
22+
form: LegendForm,
2323
formSize: Float,
2424
formLineWidth: Float,
2525
formLineDashEffect: DashPathEffect?,
@@ -46,7 +46,7 @@ class LegendEntry {
4646
* `EMPTY` will avoid drawing a form, but keep its space.
4747
* `DEFAULT` will use the Legend's default.
4848
*/
49-
var form: LegendForm? = LegendForm.DEFAULT
49+
var form: LegendForm = LegendForm.DEFAULT
5050

5151
/**
5252
* Form size will be considered except for when .None is used

MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe
6565
chart!!.disableScroll()
6666
} else if (touchMode == ROTATE) {
6767
updateGestureRotation(x, y)
68-
chart!!.invalidate()
68+
chart?.invalidate()
6969
}
7070

7171
endAction(event)

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.graphics.Canvas
44
import android.graphics.Paint
55
import android.graphics.Paint.Align
66
import android.graphics.Path
7-
import android.util.Log
87
import androidx.core.graphics.withSave
98
import com.github.mikephil.charting.components.Legend
109
import com.github.mikephil.charting.components.Legend.LegendDirection
@@ -228,7 +227,7 @@ open class LegendRenderer(
228227

229228
val yOffset = legend.yOffset
230229
val xOffset = legend.xOffset
231-
var originPosX = 0f
230+
var originPosX: Float
232231

233232
when (horizontalAlignment) {
234233
LegendHorizontalAlignment.LEFT -> {
@@ -266,8 +265,6 @@ open class LegendRenderer(
266265
legend.mNeededWidth / 2.0 - xOffset).toFloat()
267266
}
268267
}
269-
270-
null -> Log.e("Chart", "Legend horizontalAlignment is null")
271268
}
272269

273270
when (orientation) {
@@ -282,10 +279,6 @@ open class LegendRenderer(
282279
LegendVerticalAlignment.TOP -> yOffset
283280
LegendVerticalAlignment.BOTTOM -> viewPortHandler.chartHeight - yOffset - legend.mNeededHeight
284281
LegendVerticalAlignment.CENTER -> (viewPortHandler.chartHeight - legend.mNeededHeight) / 2f + yOffset
285-
else -> {
286-
Log.w("Chart", "Legend verticalAlignment not set")
287-
0f
288-
}
289282
}
290283

291284
var lineIndex = 0
@@ -344,7 +337,7 @@ open class LegendRenderer(
344337
// contains the stacked legend size in pixels
345338
var stack = 0f
346339
var wasStacked = false
347-
var posY = 0f
340+
var posY: Float
348341

349342
when (verticalAlignment) {
350343
LegendVerticalAlignment.TOP -> {
@@ -366,8 +359,6 @@ open class LegendRenderer(
366359
LegendVerticalAlignment.CENTER -> posY = (viewPortHandler.chartHeight / 2f
367360
- legend.mNeededHeight / 2f
368361
+ legend.yOffset)
369-
370-
null -> Log.w("Chart", "Legend verticalAlignment is null")
371362
}
372363

373364
var i = 0
@@ -496,9 +487,6 @@ open class LegendRenderer(
496487
mLineFormPath.lineTo(x + formSize, y)
497488
canvas.drawPath(mLineFormPath, formPaint)
498489
}
499-
500-
null -> Log.w( "Chart", "Legend form is null")
501-
502490
}
503491

504492
}

app/src/main/kotlin/info/appdev/chartexample/AnotherBarActivity.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener {
9292
set1 = chart!!.data!!.getDataSetByIndex(0) as BarDataSet
9393
set1.entries = values
9494
chart!!.data!!.notifyDataChanged()
95-
chart!!.notifyDataSetChanged()
95+
chart?.notifyDataSetChanged()
9696
} else {
9797
set1 = BarDataSet(values, "Data Set")
9898
set1.setColors(*ColorTemplate.VORDIPLOM_COLORS)
@@ -106,7 +106,7 @@ class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener {
106106
chart!!.setFitBars(true)
107107
}
108108

109-
chart!!.invalidate()
109+
chart?.invalidate()
110110
}
111111

112112
override fun onCreateOptionsMenu(menu: Menu): Boolean {
@@ -128,32 +128,32 @@ class AnotherBarActivity : DemoBase(), OnSeekBarChangeListener {
128128
it?.isDrawValues = !it.isDrawValues
129129
}
130130

131-
chart!!.invalidate()
131+
chart?.invalidate()
132132
}
133133

134134
R.id.actionToggleHighlight -> {
135135
if (chart!!.data != null) {
136136
chart!!.data!!.isHighlightEnabled = !chart!!.data!!.isHighlightEnabled()
137-
chart!!.invalidate()
137+
chart?.invalidate()
138138
}
139139
}
140140

141141
R.id.actionTogglePinch -> {
142142
if (chart!!.isPinchZoomEnabled) chart!!.setPinchZoom(false)
143143
else chart!!.setPinchZoom(true)
144144

145-
chart!!.invalidate()
145+
chart?.invalidate()
146146
}
147147

148148
R.id.actionToggleAutoScaleMinMax -> {
149149
chart!!.isAutoScaleMinMaxEnabled = !chart!!.isAutoScaleMinMaxEnabled
150-
chart!!.notifyDataSetChanged()
150+
chart?.notifyDataSetChanged()
151151
}
152152

153153
R.id.actionToggleBarBorders -> {
154154
for (set in chart!!.data!!.dataSets) (set as BarDataSet).barBorderWidth = if (set.barBorderWidth == 1f) 0f else 1f
155155

156-
chart!!.invalidate()
156+
chart?.invalidate()
157157
}
158158

159159
R.id.animateX -> {

0 commit comments

Comments
 (0)