如何为 MPAndroidChart 中的值设置颜色? [英] How to set color for values in MPAndroidChart?
问题描述
我正在使用
I am using MPAndroidChart.
Searched through the documentation but couldn't find anything that implemented correctly. I have a graph at the moment but I want to change the color of the line if it goes over a certain amount. Graph Example In the example I have linked, it shows a line drawn through the values from 10. I would like this line (the one going through the chart) and the color of the line in the chart to change color over 10. Is this possible? Using MPAndroidChart. I have one dataset at the moment.
Thanks in advance.
With LineDataSet.setColors() you can add a list of colors. Each color entry is for one data entry. ' The trick is to calculate intermediate values for crossing the border.
Everytime I add a data entry, i call this method
private fun addDiffValue(newEntry : Entry){
val last = recordedValues[2].last()
val limit = 50f
if(last.y < limit && newEntry.y > limit ){
val gradient = (newEntry.y - last.y) / (newEntry.x - last.x)
val x_border = last.x + ((limit - last.y) / gradient)
recordedValues[2].add(Entry(x_border, limit))
diffColors.add(Color.LTGRAY)
diffColors.add(Color.RED)
}
// Vorher größer, jetzt kleiner
else if(last.y > limit && newEntry.y < limit) {
val gradient = (newEntry.y - last.y) / (newEntry.x - last.x)
val x_border = last.x + ((limit - last.y) / gradient)
recordedValues[2].add(Entry(x_border, limit))
diffColors.add(Color.RED)
diffColors.add(Color.LTGRAY)
}else if(last.y > limit ){
diffColors.add(Color.RED)
} else {
diffColors.add(Color.LTGRAY)
}
recordedValues[2].add(newEntry)
}
It is important to say, that I start with recordedValues[2].add(Entry(0f,0f))
, otherwise last()
would throw an error.
I create the LineDataSet and add the colors:
val dataSet3 = LineDataSet(recordedValues[2], "My Label")
dataSet3.setColors(diffColors)
As you can see in this screenshot, all values above 50 are red.
这篇关于如何为 MPAndroidChart 中的值设置颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!