this is what i get when i try to change the color sorry can't post image here yet so i suggested to use link
I used RectangleMark from apple Charts
Api
here is my chart, my chartData has low, high, open, and close prices
I want to have it to look like a normal candle stick chart
, red when it is down, green when it is up using the open and close price value
I made a @State chartColor to have it changed with func to trigger it, but I get all of the chart red instead of a proper candle stick chart
Here is my code
import SwiftUI
import Charts
struct CoinChart: View {
@ObservedObject var chartManager: ChartManager
@State var chartColor: Color
func colorChange(){
for content in chartManager.chartData.data {
let openPrice = Double(content.open)
let closePrice = Double(content.close)
if closePrice! < openPrice! {
chartColor = Color.red
} else {
chartColor = Color.green
}
}
}
var body: some View {
VStack {
Chart{
ForEach(chartManager.chartData.data, id: \.self) { chartData in
let date = chartManager.unixConverter(unixTime: Double(chartData.period))
let dates = Date(timeIntervalSince1970: Double(chartData.period))
let lowPrice = Double(chartData.low)
let highPrice = Double(chartData.high)
let openPrice = Double(chartData.open)
let closePrice = Double(chartData.close)
RectangleMark(x: .value("Time", date),
yStart: .value("Low Price", lowPrice!),
yEnd: .value("High Price", highPrice!), width: 1)
.foregroundStyle(chartColor)
RectangleMark(x: .value("Time", date),
yStart: .value("Open Price", openPrice!),
yEnd: .value("Close Price", closePrice!), width: 6)
.foregroundStyle(chartColor)
}
}
.chartYScale(domain: 18000...21000)
.chartXAxis {
AxisMarks(position: .bottom) { _ in
AxisGridLine().foregroundStyle(.clear)
AxisTick().foregroundStyle(.clear)
AxisValueLabel()
}
}
.frame(height: 300)
Text(String(chartManager.chartData.data.count))
}
.task {
await chartManager.loadChartData()
colorChange()
}
}
}
I called the func colorChange()
in the task cuz if I call it in the chart it will give me a purple warning
Modifying state during view update, this will cause undefined behavior.
Any help will be appreciated everyone?
Thanks in advance