MPAndroid图表未显示任何标签,缺少什么?

时间:2020-10-21 04:37:31

标签: android kotlin

enter image description here我正在实现一项功能,用于显示属性的“目标”和“实际”。

因此,我必须根据选择的过滤器显示目标标签,例如,如果选择了3个月的过滤器,则需要显示3月,2月和1月,但是当前未在XAxis上显示什么标签。

这是我的代码,请纠正任何问题:

  fun showOrderChart(){
        try {
            orderChart.description.isEnabled=false
            orderChart.setMaxVisibleValueCount(60)
            // scaling can now only be done on x- and y-axis separately

            // scaling can now only be done on x- and y-axis separately
            orderChart.setPinchZoom(true)

            orderChart.setDrawBarShadow(false)
            orderChart.setDrawGridBackground(false)

          /*  val xAxis: XAxis = orderChart.xAxis
            xAxis.position = XAxisPosition.BOTTOM
            xAxis.setDrawGridLines(false)*/

            orderChart.axisLeft.setDrawGridLines(false)

            // add a nice and smooth animation

            // add a nice and smooth animation
            orderChart.animateY(1500)

            orderChart.legend.isEnabled = false




            val xVals = ArrayList<String>()
            xVals.add("Jan")
            xVals.add("Feb")
            xVals.add("Mar")
            xVals.add("Apr")
            xVals.add("May")
            xVals.add("Jun")
            val yVals1 = ArrayList<BarEntry>()
            val yVals2 = ArrayList<BarEntry>()
            yVals1.add(BarEntry(1f, 1.toFloat()))
            yVals2.add(BarEntry(1f, 2.toFloat()))

            yVals1.add(BarEntry(2f, 3.toFloat()))
            yVals2.add(BarEntry(2f, 4.toFloat()))

            yVals1.add(BarEntry(3f, 5.toFloat()))
            yVals2.add(BarEntry(3f, 6.toFloat()))

            yVals1.add(BarEntry(4f, 7.toFloat()))
            yVals2.add(BarEntry(4f, 5.toFloat()))

            yVals1.add(BarEntry(5f, 9.toFloat()))
            yVals2.add(BarEntry(5f, 12.toFloat()))

            yVals1.add(BarEntry(6f, 11.toFloat()))
            yVals2.add(BarEntry(6f, 8.toFloat()))

            val set1:BarDataSet
            val set2:BarDataSet
            set1 = BarDataSet(yVals1, "Target")
            set1.color = Color.RED
            set2 = BarDataSet(yVals2, "Actual")
            set2.color = Color.BLUE
            val data = BarData(set1, set2)
            data.setValueFormatter(LargeValueFormatter())
            orderChart.data = data
            orderChart.getBarData().setBarWidth(0.3f)
            orderChart.xAxis.setAxisMinimum(0f)
            orderChart.getXAxis().setAxisMaximum(0 + orderChart.getBarData().getGroupWidth(0.4f, 0f) * 6)
            orderChart.groupBars(0f, 0.4f, 0f)
            orderChart.getData().setHighlightEnabled(false)
            orderChart.invalidate()

            val l = orderChart.getLegend()
            l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP)
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT)
            l.setOrientation(Legend.LegendOrientation.VERTICAL)
            l.setDrawInside(true)
            l.setYOffset(20f)
            l.setXOffset(0f)
            l.setYEntrySpace(0f)
            l.setTextSize(8f)

            val xAxis = orderChart.getXAxis()
            xAxis.granularity = 1f
            xAxis.setGranularityEnabled(true)
            xAxis.setCenterAxisLabels(true)
            xAxis.setDrawGridLines(false)
            xAxis.setAxisMaximum(6f)
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM)
            xAxis.setValueFormatter(IndexAxisValueFormatter(xVals))
            orderChart.getAxisRight().setEnabled(false)
            val leftAxis = orderChart.getAxisLeft()
            leftAxis.setValueFormatter(LargeValueFormatter())
            leftAxis.setDrawGridLines(true)
            leftAxis.setSpaceTop(35f)
            leftAxis.setAxisMinimum(0f)



        }catch (e: Exception){
            e.printStackTrace()
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要标签代码。这样。

YAxis yAxisRight = lineChart.getAxisRight(); //set right of Y
yAxisRight.setDrawLabels(true);
相关问题