android - 如何根据y轴值是正值还是负值来更改MPAndroidChart折线图的线条和填充颜色

标签 android mpandroidchart

想知道是否可以根据 y 轴值是正值还是负值来更改折线图的线条和填充颜色。下面是一个例子
enter image description here
以下是我可以使用以下代码实现的目标
enter image description here

    private fun setUpLineChart() {
        val lineData = getDataSet()
        view.lineChart.apply {
            data = lineData
            description.isEnabled = false
            setScaleEnabled(false)
            setTouchEnabled(false)
            legend.isEnabled = false
            axisLeft.apply {
                setDrawLabels(false)
                setDrawGridLines(false)
                setDrawAxisLine(false)
                spaceBottom = 30f
            }
            axisRight.apply {
                setDrawLabels(false)
                setDrawGridLines(false)
                setDrawAxisLine(false)
            }
            xAxis.apply {
                setDrawLabels(false)
                setDrawGridLines(false)
                setDrawAxisLine(false)
            }
            animateXY(700, 1000, Easing.EaseInOutQuad)
        }
    }

    private fun getDataSet(): LineData {
        val entries = mutableListOf<Entry>()
        val dataList = listOf(1, 20, -20, 33, 54, 7, -18, 2)

        dataList.forEachIndexed { index, element ->
            entries.add(Entry(index.toFloat(), element.toFloat()))
        }

        val dataSet = LineDataSet(entries, "")
        dataSet.apply {
            setDrawCircles(false)
            valueTextSize = 0f
            lineWidth = 3f
            mode = LineDataSet.Mode.HORIZONTAL_BEZIER
            color = ContextCompat.getColor(view.context, R.color.colorOnSurface)
            setDrawFilled(true)
            fillColor = ContextCompat.getColor(view.context, R.color.colorSurface2)
        }
        return LineData(dataSet)
    }

最佳答案

线条和填充颜色绑定(bind)到特定的 LineDataSet。因此,要根据上面的示例实现您想要的结果,您必须将当前数据集分成 4 个 LineDataSet(2 个正数和 2 个负数),通过这样做,每个数据集都可以拥有自己的填充和线条颜色,您可以灵活地拥有为每个数据集提供尽可能多的颜色。当然,您必须执行自己的逻辑来将正 LineDataSets 与负 LineDataSets 分开。我修改了你的获取数据集() 函数给你一个例子,说明如何实现正 LineDataSets 和负 LineDataSets 的分离,每个 LineDataSets 都有自己的线条和填充颜色。

private fun getDataSet(): LineData? {
        val dataSets: MutableList<ILineDataSet> = ArrayList()
        val yArray = floatArrayOf(1f, 20f, -20f, 33f, 54f, 7f, -18f, 2f)
        var entries = ArrayList<Entry?>()
        var prevValueIsPositive = false
        var prevValueIsNegative = false
        val step = 1f
        for (i in yArray.indices) {
            val y = yArray[i]
            //positive y values
            if (y >= 0) {
                //we are changing to positive values so draw the current negative dataSets
                if (prevValueIsNegative) {
                    //calculate the common mid point between a positive and negative y
                    val midEntry = Entry(i.toFloat() - step / 2, 0f)
                    entries.add(midEntry)

                    //draw the current negative dataSet to Red color
                    dataSets.add(getLineDataSet(entries, android.R.color.holo_red_dark, android.R.color.holo_purple))

                    //and initialize a new DataSet starting from the above mid point Entry
                    entries = ArrayList()
                    entries.add(midEntry)
                    prevValueIsNegative = false
                }

                //we are already in a positive dataSet continue adding positive y values
                entries.add(Entry(i.toFloat(), y))
                prevValueIsPositive = true
                //not having any other positive-negative changes so add the remaining positive values in the final dataSet
                if (i == yArray.size - 1) {
                    dataSets.add(getLineDataSet(entries, android.R.color.holo_green_light, android.R.color.holo_orange_dark))
                }
            } else {
                //we are changing to negative values so draw the current positive dataSets
                if (prevValueIsPositive) {
                    //calculate the common mid point between a positive and negative y
                    val midEntry = Entry(i.toFloat() - step / 2, 0f)
                    entries.add(midEntry)

                    //draw the current positive dataSet to Green color
                    dataSets.add(getLineDataSet(entries, android.R.color.holo_green_light, android.R.color.holo_orange_dark))

                    //and initialize a new DataSet starting from the above mid point Entry
                    entries = ArrayList()
                    entries.add(midEntry)
                    prevValueIsPositive = false
                }

                //we are already in a negative dataSet continue adding negative y values
                entries.add(Entry(i.toFloat(), y))
                prevValueIsNegative = true
                //not having any other positive-negative changes so add the remaining negative values in the final dataSet
                if (i == yArray.size - 1) {
                    dataSets.add(getLineDataSet(entries, android.R.color.holo_red_dark, android.R.color.holo_purple))
                }
            }
        }
        return LineData(dataSets)
    }
使用以下辅助函数来准备一个新的 LineDataSet 及其指定的线条和填充颜色:
private fun getLineDataSet(entries: ArrayList<Entry?>, fillColor: Int, lineColor: Int): LineDataSet {
        val dataSet = LineDataSet(entries, "")
        dataSet.setDrawCircles(false)
        dataSet.valueTextSize = 0f
        dataSet.lineWidth = 3f
        dataSet.mode = LineDataSet.Mode.HORIZONTAL_BEZIER
        dataSet.color = ContextCompat.getColor(this, lineColor)
        dataSet.setDrawFilled(true)
        dataSet.fillColor = ContextCompat.getColor(this, fillColor)
        return dataSet
    }

关于android - 如何根据y轴值是正值还是负值来更改MPAndroidChart折线图的线条和填充颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64089807/

相关文章:

java - Retrofit API Post 调用返回错误 500,与 Postman 配合良好

android - mpandroidchart x轴值重复

android - 在 MPAndroidChart 中同步缩放和转换 2 个图表

android - MPAndroidChart:BubbleChart 第一个和最后一个气泡呈现一半显示

android - 如何检测 Adob​​e AIR 中的 Android 操作系统版本?

android - android-getpath方法返回错误的地址

java - 我怎样才能轻松地将 HTTPS(自签名证书)与 JSoup 一起使用?

java - 工作条件根据男性和女性的选择而匹配

android - 在 MPAndroidChart 库的 BarChart 条内显示轴值

android - setVisibleXRangeMaximum() 和 setVisibleYRangeMaximum() 在 MPAndroidChart 中不起作用