Home Articles

MPAndroidChart BarChart在重绘时不考虑条宽

Viewed 367 times

我在应用程序中使用 BarChart 但有时当我重新绘制它时,条形宽度不受尊重并且我得到不一致的结果(即使X轴中的值总是100) .


enter image description here


enter image description here

enter image description here


我认为它与X轴的值范围有关,因为当范围从0到50或类似时图表看起来很好,但是当范围较小时它看起来很糟糕 .


barChartView?.data?.barWidth = 0.3f

这是我正在使用的完整图表配置(此方法仅在创建 Fragment 时运行一次):

private fun setUpChartView() {

    val xAxis = barChartView?.xAxis
    val yAxis = barChartView?.axisLeft

    // Hide all parts of the chart that we don't want to show
    barChartView?.legend?.isEnabled = false
    barChartView?.description?.isEnabled = false
    barChartView?.axisRight?.isEnabled = false

    // Show the Y axis grid lines as dashed
    yAxis?.enableGridDashedLine(5f, 10f, 0f)

    val barChartTextColor = getColor(R.color.chart_text)
    val barChartTextSize = 12f
    val barChartLabelsTypeface = Typeface.SANS_SERIF

    // Show the X axis labels at the bottom and set the proper style
    xAxis?.position = XAxis.XAxisPosition.BOTTOM
    xAxis?.textColor = barChartTextColor
    xAxis?.textSize = barChartTextSize
    xAxis?.typeface = barChartLabelsTypeface

    // Set the proper style for the Y axis labels
    yAxis?.textSize = barChartTextSize
    yAxis?.textColor = barChartTextColor
    yAxis?.typeface = barChartLabelsTypeface


    val viewPortHandler = barChartView?.viewPortHandler
    val axisTransformer = barChartView?.getTransformer(LEFT)

    // Set a custom axises so that we can control the drawing of the labels and grid lines
    barChartView?.setXAxisRenderer(CustomEntriesXAxisRenderer(viewPortHandler = viewPortHandler,
                                                                 xAxis = xAxis,
                                                                 transformer = axisTransformer))
    barChartView?.rendererLeftYAxis = CustomEntriesYAxisRenderer(viewPortHandler = viewPortHandler,
                                                                    yAxis = yAxis,
                                                                    transformer = axisTransformer)


private fun drawBarChart() {
    // The data list has always 100 items
    val entries = data.map { BarEntry(it.x, it.y) }

    // Prepare the chart data set by adding the entries and configuring its style (colors, etc.)
    val dataSet = BarDataSet(entries, null)
    dataSet.colors = colorsList

    // Load data to chart
    if (barChartView?.data == null) {
        // If there is no data set yet, create a new data object and add it to the chart (this happens the first
        // time we draw the chart after the Fragment was created, or after an empty data list was returned)
        barChartView?.data = BarData(dataSet)
    } else {
        // If there is already data in the chart, remove the existing data set and add the new one


    // Set a custom width for the bars or the chart will draw them too wide
    barChartView?.data?.barWidth = 0.3f

    val xAxis = barChartView?.xAxis
    val yAxis = barChartView?.axisLeft

    // Set the min and max values for the Y axis or the chart lib will calculate them and add more values than
    // we need
    yAxis?.axisMinimum = 0f
    yAxis?.axisMaximum = entries.last().y

    // Set the entries that need to be drawn in the X axis, so the chart doesn't calculate them automatically
    (barChartView?.rendererXAxis as CustomEntriesXAxisRenderer).entries = xAxisEntries

    // Set the entries that need to be drawn in the Y axis, so the chart doesn't calculate them automatically
    (barChartView?.rendererLeftYAxis as CustomEntriesYAxisRenderer).entries = yAxisEntries

    // Add the label count to avoid the chart from automatically setting a range of labels instead of the ones we need,
    // which prevents the axis value formatter below to set the correct labels
    xAxis?.setLabelCount(xAxisEntries.size, true)
    yAxis?.setLabelCount(yAxisEntries.size, true)

    // Use a custom value formatter that sets only the needed labels on the axises
    xAxis?.setValueFormatter { value, _ ->
        // TODO
    yAxis?.setValueFormatter { value, _ ->
        // TODO

    // Draw chart


class CustomEntriesXAxisRenderer(viewPortHandler: ViewPortHandler?, xAxis: XAxis?, transformer: Transformer?)
    : XAxisRenderer(viewPortHandler, xAxis, transformer) {

     * Entries used to draw the grid lines and labels on the X axis.
    var entries: List<Float>? = null

    override fun computeSize() {
        entries?.forEachIndexed { i, value ->
            mAxis.mEntries[i] = value



class CustomEntriesYAxisRenderer(viewPortHandler: ViewPortHandler?, yAxis: YAxis?, transformer: Transformer?)
    : YAxisRenderer(viewPortHandler, yAxis, transformer) {

     * Entries used to draw the grid lines and labels on the Y axis.
    var entries: List<Float>? = null

    override fun computeAxisValues(min: Float, max: Float) {
        super.computeAxisValues(min, max)

        entries?.forEachIndexed { i, value ->
            mAxis.mEntries[i] = value


1 Answer

  • 0

    其中一个MPAndroidChart开发人员说这确实是图书馆中的一个错误,现在它正处于待办事项清单中 . 如果有人遇到此问题,您可以按照进度here进行操作 .


    barChartView?.data?.barWidth = when (entries.last().x) {
        in 0..30 -> 0.1f
        in 30..60 -> 0.2f
        else -> 0.3f

    您可以将值调整为适合您的数据条目的值,但基本上通过根据数据集中X值的范围设置不同的条宽,您至少可以在修复错误之前使其更好一些 .