Android MP图表在图例和轴之间设置空间

时间:2019-05-23 13:35:28

标签: android

我在项目中使用MP图表。我正在使用多个数据集条形图。我可能面临以下问题:

  1. 我想在图例和x轴之间留一些空间。现在,它与轴固定在一起。我检查了文档,但未找到任何设置轴和图例之间边距的方法。 (附带的屏幕截图-突出显示为问题1)
  2. 图表开始时显示一个空白组,我要删除该组。(附加的屏幕截图-突出显示为问题2)

enter image description here

下面是我的代码:

tco_chart?.description?.isEnabled = false
    tco_chart?.setPinchZoom(false)
    tco_chart?.setDrawBarShadow(false)
    val leftYAxis = tco_chart?.axisLeft
    leftYAxis?.setDrawGridLines(false)
    leftYAxis?.axisMinimum = 0f
    tco_chart?.setDrawGridBackground(false)

    val mv = MyMarkerView(this, R.layout.custom_marker_view)
    mv.setChartView(tco_chart) // For bounds control
    tco_chart.setMarker(mv)
    val legend = tco_chart?.legend
    legend?.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM)
    legend?.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT)
    legend?.setOrientation(Legend.LegendOrientation.HORIZONTAL)
    legend?.setYOffset(0f)
    legend?.setXOffset(10f)
    legend?.setYEntrySpace(0f)
    legend?.setTextSize(5f)

    val axisLabels = ArrayList<String>()
    axisLabels.add("Year 1")
    axisLabels.add("Year 2")
    axisLabels.add("Year 3")
    axisLabels.add("Year 4")
    axisLabels.add("Year 5")

    val xAxis = tco_chart?.xAxis
    xAxis?.granularity = 1f
    xAxis?.position = XAxis.XAxisPosition.BOTTOM
    xAxis?.setCenterAxisLabels(true)
    xAxis?.setDrawLabels(true)
    xAxis?.valueFormatter = IndexAxisValueFormatter(axisLabels)

    val insuranceCostEntry = ArrayList<BarEntry>()
    val insuranceCostDataModel = tcoResultModel.insurance_cost
    if (insuranceCostDataModel != null) {
        insuranceCostEntry.add(BarEntry(0f, insuranceCostDataModel.first_year.toFloat()))
        insuranceCostEntry.add(BarEntry(1f, insuranceCostDataModel.second_year.toFloat()))
        insuranceCostEntry.add(BarEntry(2f, insuranceCostDataModel.third_year.toFloat()))
        insuranceCostEntry.add(BarEntry(3f, insuranceCostDataModel.fourth_year.toFloat()))
        insuranceCostEntry.add(BarEntry(4f, insuranceCostDataModel.fifth_year.toFloat()))
    }

    val fuelCostEntry = ArrayList<BarEntry>()
    val fuelCostDataModel = tcoResultModel.fuel_cost
    if (fuelCostDataModel != null) {
        fuelCostEntry.add(BarEntry(0f, fuelCostDataModel.first_year.toFloat()))
        fuelCostEntry.add(BarEntry(1f, fuelCostDataModel.second_year.toFloat()))
        fuelCostEntry.add(BarEntry(2f, fuelCostDataModel.third_year.toFloat()))
        fuelCostEntry.add(BarEntry(3f, fuelCostDataModel.fourth_year.toFloat()))
        fuelCostEntry.add(BarEntry(4f, fuelCostDataModel.fifth_year.toFloat()))
    }

    val serviceCostEntry = ArrayList<BarEntry>()
    val serviceCostDataModel = tcoResultModel.service_cost
    if (serviceCostDataModel != null) {
        serviceCostEntry.add(BarEntry(0f, serviceCostDataModel.first_year.toFloat()))
        serviceCostEntry.add(BarEntry(1f, serviceCostDataModel.second_year.toFloat()))
        serviceCostEntry.add(BarEntry(2f, serviceCostDataModel.third_year.toFloat()))
        serviceCostEntry.add(BarEntry(3f, serviceCostDataModel.fourth_year.toFloat()))
        serviceCostEntry.add(BarEntry(4f, serviceCostDataModel.fifth_year.toFloat()))
    }

    val tyreChangeCostEntry = ArrayList<BarEntry>()
    val tyreChangeCostDataModel = tcoResultModel.tyre_change_cost
    if (tyreChangeCostDataModel != null) {
        tyreChangeCostEntry.add(BarEntry(0f, tyreChangeCostDataModel.first_year.toFloat()))
        tyreChangeCostEntry.add(BarEntry(1f, tyreChangeCostDataModel.second_year.toFloat()))
        tyreChangeCostEntry.add(BarEntry(2f, tyreChangeCostDataModel.third_year.toFloat()))
        tyreChangeCostEntry.add(BarEntry(3f, tyreChangeCostDataModel.fourth_year.toFloat()))
        tyreChangeCostEntry.add(BarEntry(4f, tyreChangeCostDataModel.fifth_year.toFloat()))
    }

    val depreciationCostEntry = ArrayList<BarEntry>()
    val depriciationCostDataModel = tcoResultModel.depreciation_cost
    if (depriciationCostDataModel != null) {
        depreciationCostEntry.add(BarEntry(0f, depriciationCostDataModel.first_year.toFloat()))
        depreciationCostEntry.add(BarEntry(1f, depriciationCostDataModel.second_year.toFloat()))
        depreciationCostEntry.add(BarEntry(2f, depriciationCostDataModel.third_year.toFloat()))
        depreciationCostEntry.add(BarEntry(3f, depriciationCostDataModel.fourth_year.toFloat()))
        depreciationCostEntry.add(BarEntry(4f, depriciationCostDataModel.fifth_year.toFloat()))
    }

    val year1Set: BarDataSet
    val year2Set: BarDataSet
    val year3Set: BarDataSet
    val year4Set: BarDataSet
    val year5Set: BarDataSet

    var data = tco_chart?.data
    if (data != null) {
        if (data.dataSetCount > 0) {
            year1Set = data.getDataSetByIndex(0) as BarDataSet
            year1Set.values = insuranceCostEntry

            year2Set = data.getDataSetByIndex(1) as BarDataSet
            year2Set.values = fuelCostEntry

            year3Set = data.getDataSetByIndex(2) as BarDataSet
            year3Set.values = tyreChangeCostEntry

            year4Set = data.getDataSetByIndex(3) as BarDataSet
            year4Set.values = serviceCostEntry

            year5Set = data.getDataSetByIndex(4) as BarDataSet
            year5Set.values = depreciationCostEntry

            data.notifyDataChanged()
            tco_chart?.notifyDataSetChanged()
        }
    } else {
        year1Set = BarDataSet(insuranceCostEntry, "Insurance Cost")
        year1Set.color = Color.rgb(232, 128, 44)

        year2Set = BarDataSet(fuelCostEntry, "Fuel Cost")
        year2Set.color = Color.rgb(232, 199, 44)

        year3Set = BarDataSet(tyreChangeCostEntry, "Tyre Change Cost")
        year3Set.color = Color.rgb(10, 149, 221)

        year4Set = BarDataSet(serviceCostEntry, "Service Cost")
        year4Set.color = Color.rgb(47, 194, 144)

        year5Set = BarDataSet(depreciationCostEntry, "Depriciation Cost")
        year5Set.color = Color.rgb(105, 123, 192)

        data = BarData(year1Set, year2Set, year3Set, year4Set, year5Set)
        data.setValueFormatter(IndexAxisValueFormatter())
        tco_chart?.data = data
    }

    val rightYAxis = tco_chart?.axisRight
    rightYAxis?.isEnabled = false

    tco_chart?.getBarData()?.setBarWidth(0.1f)
    tco_chart?.xAxis?.axisMaximum = (0 + data.getGroupWidth(0.4f, 0.02f) * 5)
    tco_chart?.groupBars(0f, 0.4f, 0.02f)
    tco_chart?.invalidate()

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,可以使用图表的其他填充来解决它。 使用这个:

chart.setExtraOffsets(5f,5f,5f,15f)

希望有帮助。

答案 1 :(得分:0)

您像..那样给图例赋予偏移量。

 val legend = chart.legend
 legend.xOffset = 10f