willdale / SwiftUICharts

A charts / plotting library for SwiftUI. Works on macOS, iOS, watchOS, and tvOS and has accessibility features built in.
MIT License
837 stars 103 forks source link

Pie Charts with gradients #198

Open Vincz opened 2 years ago

Vincz commented 2 years ago

Would it be possible to handle gradients colors within Pie or Doughnut Charts ? Something like that?

Gradient pie!

Vincz commented 2 years ago

Hi @willdale! So I managed to get the result I wanted (by duplicating the code) !

Screenshot 2022-08-08 at 09 02 05

Both the fill of the PieChart and the stroke of the DoughnutChart accept a Color or a Gradient. I wanted to make a PR, but I have the following problems:

  1. How to allow both Color and Gradient in the related datapoints? If I change public var colour: Color in PieChartDataPoint by public var colour: ShapeStyle, I get the error Protocol 'ShapeStyle' can only be used as a generic constraint because it has Self or associated type requirements.
  2. The gradient may actually need the startAngle and amount of the data point itself, if we want to be able to use RadialGradient as in my example:

Here is an example of a modified version of PieChart.

PieSegmentShape(id: chartData.dataSets.dataPoints[data].id,
startAngle: chartData.dataSets.dataPoints[data].startAngle,
amount: chartData.dataSets.dataPoints[data].amount)
.fill(AngularGradient(
    gradient: Gradient(colors: [
        Color(DynamicColor(chartData.dataSets.dataPoints[data].colour).lighter(amount: 0.3)),
        Color(DynamicColor(chartData.dataSets.dataPoints[data].colour).darkened(amount: 0.1))
    ]),
    center: .center,
    startAngle: .radians(chartData.dataSets.dataPoints[data].startAngle),
    endAngle: .radians(chartData.dataSets.dataPoints[data].startAngle+chartData.dataSets.dataPoints[data].amount)
))

Could you help me with this? What would be the preferred way to handle this case?