MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

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?

Open agungmrf opened this issue 3 years ago • 1 comments

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<Sugar>) { val bloodsugar = ArrayList<Entry>() 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

agungmrf avatar Feb 27 '22 04:02 agungmrf

Using multiple datasets can solve this problem if possible

VinsonGuo avatar Mar 02 '22 03:03 VinsonGuo