SwiftUICharts icon indicating copy to clipboard operation
SwiftUICharts copied to clipboard

Pie Charts with gradients

Open Vincz opened this issue 3 years ago • 1 comments

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

Gradient pie!

Vincz avatar May 05 '22 05:05 Vincz

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?

Vincz avatar Aug 08 '22 07:08 Vincz