SwiftUI Charts: Is it possible to have a LineChart with a different color line above certain threshold

I have tried different things but I'm not really accomplishing the look I'm going for. Basically what I want is to have a Line Chart that changes color once it reaches certain threshold.

Answered by ITSD in 741796022

You can colour lines with gradients. If you know the min/max of your chart then you can work out what % of that range represents your colour change point and make a gradient the matches. For example, a range of 0 to 10 with anything above 9 being red would mean a gradient with 0 being green, 8.999999 being green, 9 being red.

This may not be exactly what you ask for but in the following blog are some interesting articles about swift charts:

https://nilcoalescing.com/tags/charts/

They may give you some ideas on how to solve your specific problem.

An idea:

  • find the points (x values) where curve crosses 9, 10.
  • split into partial curves between these x values
  • draw a new line of appropriate colr between 2 x points.
Accepted Answer

You can colour lines with gradients. If you know the min/max of your chart then you can work out what % of that range represents your colour change point and make a gradient the matches. For example, a range of 0 to 10 with anything above 9 being red would mean a gradient with 0 being green, 8.999999 being green, 9 being red.

SwiftUI Charts: Is it possible to have a LineChart with a different color line above certain threshold
 
 
Q