PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.6k stars 9.02k forks source link

I want to divide the color of the circle on the linechart but it's not working well. Where is the error in my code? #5269

Open agungmrf opened 2 years ago

agungmrf commented 2 years ago

Hello everyone, I want to divide the color of the circle on the linechart but it's not working well. Where is the error in my code?

Code private fun showLineChart(listSugar: List) { val bloodsugar = ArrayList() var count = 0F for (item in listSugar) { bloodsugar.add(Entry(count, item.sugar_level!!.toFloat())) count += 1F }

    val bloodsugarLineDataSet = LineDataSet(bloodsugar, "")
    bloodsugarLineDataSet.mode = LineDataSet.Mode.LINEAR
    bloodsugarLineDataSet.circleRadius = 5f
    bloodsugarLineDataSet.setDrawValues(false)
    bloodsugarLineDataSet.circleHoleRadius = 1.75f
    bloodsugarLineDataSet.color = Color.rgb(204, 204, 204)
    bloodsugarLineDataSet.lineWidth = 2f
    bloodsugarLineDataSet.enableDashedLine(12f, 12f, 0f)

    for (item in listSugar) {
        when {
            item.sugar_level!! <= 89 -> {
                bloodsugarLineDataSet.setCircleColor(Color.rgb(252, 197, 29))
            }
            item.sugar_level in 90..199 -> {
                bloodsugarLineDataSet.setCircleColor(Color.rgb(101, 201, 123))
            }
            item.sugar_level >= 200 -> {
                bloodsugarLineDataSet.setCircleColor(Color.rgb(252, 85, 95))
            }
        }
    }

    /* Setup Legend */
    val legend = binding.chartSugar.legend
    legend.isEnabled = false
    legend.setDrawInside(false)

    binding.chartSugar.description.isEnabled = false
    binding.chartSugar.xAxis.position = XAxis.XAxisPosition.BOTTOM
    binding.chartSugar.xAxis.granularity = 1F
    binding.chartSugar.axisRight.setDrawZeroLine(false)
    binding.chartSugar.xAxis.setDrawGridLines(false)
    binding.chartSugar.xAxis.setDrawAxisLine(false)
    binding.chartSugar.axisLeft.setDrawAxisLine(false)
    binding.chartSugar.axisRight.setDrawGridLines(false)
    binding.chartSugar.axisRight.isEnabled = false
    binding.chartSugar.data = LineData(bloodsugarLineDataSet)
}

Screenshot_20220227_113546

it should be like this image

VinsonGuo commented 2 years ago

Using multiple datasets can solve this problem if possible