Version 4.0 is a Kotlin rewrite with the same class, package and interface names. These are the changes that need edits in your code.
- Requires minSdk 23, compileSdk 35 (37 with
MPChartCompose), Java 17 and Kotlin 2.0 or newer. - Every getter and setter pair is a property:
chart.setDragEnabled(true)ischart.isDragEnabled = true,axis.getAxisMaximum()isaxis.axisMaximum. Boolean flags are namedisXxxEnabled, sosetDrawValues(false)isisDrawValuesEnabled = falseandsetDrawVerticalHighlightIndicator(false)isisVerticalHighlightIndicatorEnabled = false. Functions with parameters keep their names. - Renamed:
dataSet.valuesisentries,highLightColorandhighLightAlphaarehighlightColorandhighlightAlpha,getChartBitmap()istoBitmap(),setOnTouchListener(ChartTouchListener)ischartTouchListener, andisUsingSliceColorAsValueLineColorisisUseValueColorForLineEnabled. - The
ValueFormatterbase class of 3.1.0 is gone. UseIAxisValueFormatter { value, axis -> }for axes andIValueFormatter { value, entry, dataSetIndex, viewPortHandler -> }for data sets. GradientColoris gone. Bars takebarDataSet.fills = listOf(Fill(startColor, endColor))orbarDataSet.setGradientColor(startColor, endColor).- Entries carry a typed payload:
Entry(x, y)has none,Entry(x, y, data = order)is anEntry<Order>. The payload is read-only. Stack values areBarEntry(x, listOf(1f, 2f))and the property isstackValues. - Colors, color templates, stack labels, data set labels, draw orders, formatter values and gradient colors are lists.
set.colors = ColorTemplate.MATERIAL_COLORSreplacesset.setColors(*ColorTemplate.MATERIAL_COLORS). setLabelCount(count, force)islabelCountplusisForceLabelsEnabled. Assigninglegend.entriesreplacessetCustom.- The empty state says "No data yet" in slate grey under an outline of the chart type, and it also shows for data without entries. Set
noDataText,noDataTextColororisNoDataIconEnabled = falseto change it. chart.notifyDataSetChanged()refreshes the data and redraws, sodata.notifyDataChanged()andinvalidate()after it are no longer needed.chart.getScaleX()ischart.zoomX.View.scaleXis untouched.setPaint(paint, PAINT_INFO)and friends are the propertiesinfoPaint,descriptionPaint,gridBackgroundPaint,legendLabelPaint,holePaintandcenterTextPaint.- Formatters and listeners are
fun interfaces and receive non-null arguments.chart.onValueSelected { entry, highlight -> }is the lambda form of the selection listener. - The
highlightValueoverloads are one function with default arguments. The third positional argument is stilldataIndex; passstackIndexandcallListenerby name:highlightValue(x, dataSetIndex, callListener = false). - Markers learn their chart when assigned,
marker.chartView = chartis no longer needed. saveToGallerywrites through MediaStore and needs no storage permission from Android 10 on.saveToPathis gone.- Deprecated members of 3.x were removed. Values in dp are stored as dp and converted when drawing.
- Value labels are now decided per data set from the entries in view, not from the entry count of the whole chart. Charts with several data sets that used to hide every label will draw them; set
chart.maxVisibleCountlower if that is too dense. - The x axis keeps its labels at least one label width apart, so a chart with long labels draws fewer of them than
labelCountasks for. Shorten the labels, rotate them withxAxis.labelRotationAngle, or overrideminimumIntervalon your ownXAxisRendererto get the old density back. - A pie or radar chart rotates only when the drag starts on the drawn chart, not in the hole or in a corner.
StackedValueFormatternow decides the top of a stack by position. A formatter of your own that needs the position can overridegetStackedFormattedValue;getFormattedValuestill works and still sees only the value.scatterShapeSizeandscatterShapeHoleRadiusare in dp, with a default shape size of 7.5 dp. Halve a 3.x pixel value to keep its look on an xhdpi screen.setCenterTextSizePixelsis gone;centerTextSizetakes dp.PieData.dataSetis nullable:pieData.dataSet?.sliceSpace.LargeValueFormatter.setSuffix(array)issuffix = listOf(...).- Jetpack Compose: add
MPChartComposeand useLineChart(data = ...)and the other chart composables withrememberChartState().