0

I am trying to build a watchOS app that has charting and I can't use Swifts built in charts because I need to support down to version 7 and swift charts are only available for watchOS 9+. So instead I am using a library I found here... https://github.com/willdale/SwiftUICharts It has some sample and examples, trying to follow them I was able to get the chart to show up and customize it some, but I can't get the bar chart items to change their color. It seems simple enough, but for whatever reason I can't get it to actually change the color.

I'll provide a simple sample that should help show what's going on.

struct ChartView: View {
  var items: [TimeEntries] = [
    TimeEntry(dateString: "01/23/2023", entry: 97, timestamp: Date().millisecondsSince1970)]
  
  var body: some View {
    let chartData = makeData(items)
    BarChart(chartData: chartData)
      .touchOverlay(chartData: chartData)
      .padding()
  }

  private func makeData(_ items: [TimeEntries]) -> BarChartData {
    var data: [BarChartDataPoint] = [BarChartDataPoint]()
      for item in items {
        let stat = BarChartDataPoint(
          value: Double(item.entry),
          xAxisLabel: "Wed",
          date: Date(milliseconds: entry.timestamp),
          colour: ColourStyle(colour: Color.purple)
        )
        data.append(stat)
      }
      let dataSet = BarDataSet(dataPoints: data)
      return BarChartData(dataSets: dataSet)
  }

}

That should give me an entry on my bar chart with purple filling, I simplified this for sake of ease of posting, my real data has 7 points in it.

However, what actually happens is I have a single red bar on my chart. I am not using red anywhere in the app at all, but it won't take the color that I specify in the colour property of the BarChartDataPoint.

I know it's based on a library, but hopefully someone here will have used this library and will know what I have done wrong. I'll attach a screenshot of the chart so you can see. Thank you.

enter image description here

Neglected Sanity
  • 1,770
  • 6
  • 23
  • 46

1 Answers1

0

If anyone else runs into this issue I found the fix finally. When you are building the chart data you need to set the BarStyle and set the colourFrom property to dataPoints like this...

private func makeData(_ items: [TimeEntries]) -> BarChartData {
var data: [BarChartDataPoint] = [BarChartDataPoint]()
  for item in items {
    let stat = BarChartDataPoint(
      value: Double(item.entry),
      xAxisLabel: "Wed",
      date: Date(milliseconds: entry.timestamp),
      colour: ColourStyle(colour: Color.purple)
    )
    data.append(stat)
  }
  let dataSet = BarDataSet(dataPoints: data)
  let style = BarStyle(colourFrom: .dataPoints) //<--Thats it
  return BarChartData(dataSets: dataSet, barStyle: style)
}

That will tell the library to render the colors from your data points and not the base style of .red

Neglected Sanity
  • 1,770
  • 6
  • 23
  • 46