Home > Blockchain >  CombinedChart: is it possible to apply LinearGradient to a line within a CombinedChart?
CombinedChart: is it possible to apply LinearGradient to a line within a CombinedChart?

Time:11-25

I would like to apply a LinearGradient to the green line in the image below. CombinedChart

Applying LinearGradient to a LineChart is straight forward as seen below, but this method does not work for CombinedChart.

        val gradient = LinearGradient(
            0f, 500F, 0f, 0f,
            ContextCompat.getColor(context, R.color.red),
            ContextCompat.getColor(context, R.color.blue),
            Shader.TileMode.CLAMP
        )

        binding.lineChart.renderer.paintRender.shader = gradient

Any help is much appreciated.

CodePudding user response:

I've had a similar problem and my solution was to create custom renderer for combined chart renderer with special setup method like this:

class CustomRenderer(private val chart: CombinedChart) : CombinedChartRenderer(chart, chart.animator, chart.viewPortHandler) {
    fun setup() {
        mRenderers.forEach { renderer ->
            if (renderer is LineChartRenderer) {
                chart.lineData.dataSets.forEach { dataSet ->
                    setLineGradient(dataSet, renderer) // apply your gradient here
                }
            }
        }
    }
}

You can apply this renderer like this in the chart scope:

yourChart.apply {
   val customRenderer = CustomRenderer(this)
   renderer = customRenderer
}

Call setup method when you are done adding data to the datasets:

customRenderer.setup()
  • Related