gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:48:23 26 4
gpt4 key购买 nike

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

  • 我想在图例和 x 轴之间提供一些空间。现在它被固定在轴上。我检查了文档,但没有找到任何设置轴和图例之间边距的方法。 (附截图 - 突出显示为问题 1)
  • 该图表在开头显示了一个空组,我想将其删除。(附加屏幕截图 - 突出显示为问题 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()

    最佳答案

    我遇到了同样的问题,您可以通过为图表使用一些额外的填充来解决它。
    用这个:

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

    希望能帮助到你。

    关于Android MP图表设置图例和轴之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56276481/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com