I want to use symbols in a bar chart to represent a habit the user specifies. Text can be too long and gets cut off by the next chart.
Here is what I tried:
Chart {
ForEach(habitItems) { habit in
BarMark(x: .value("Type", Image(systemName: habit.symbolHI)),
y: .value("Tracked", countTrackedDates(for: habit))
)
.foregroundStyle(getBarColor(for: habit).gradient)
.cornerRadius(20)
}
}
habit.symbolHI would equal a string for example: "square.fill"
I am getting an error:
Initializer 'init(x:y:width:height:stacking:)' requires that 'Image' conform to 'Plottable'
Maybe there is a way to make an image plottable but I'm not sure how.
For simplicity, I'll assume you have an array of these:
You can just plot the name of the image.
Then use
chartXAxisto change the X axis marks to images:I am casting
valuetoStringhere because I know I'm plotting strings on the x axis (as inx: .value("Type", habit.symbolName)). You can also usehabitItems[value.index].symbolNameif the indices of the x axis marks match the indices of your data (this would work if the data is anArray, but would not be the case if it is anArraySlicein the middle of an array for example).