I have implement that chart, however, when I switch to another tabview and return back, the config setting get ignores or sometimes doesn't show the data at all.
Here is my code:
`var body:some View {
let drag = DragGesture()
.onChanged({ self.offset = $0.translation })
.onEnded({
if $0.translation.width < -50 {
self.offset = .init(width:-1000, height: 0)
dragDirection = .left
} else if $0.translation.width > 50 {
self.offset = .init(width: 1000, height: 0)
dragDirection = .right
} else {
self.offset = .zero
}
})
ZStack {
Text("No Data Available...").opacity(self.entries.isEmpty ? 1.0:0.0)
VStack(alignment: .leading, spacing: 0) {
self.selectionIndicatorView()
SelectableBarChartView<SelectionLine>(config: self.config)
.onBarSelection { entry, location in
self.selectedBarTopCentreLocation = location
self.selectedEntry = entry
}
.selectionView {
SelectionLine(location: self.selectedBarTopCentreLocation,
height: 150)
}
.onAppear() {
HealthKitData.instance.configDataEntities(index, dragDirection, endDate: endDate, dataType: .chartData, requestedData: requestedData, completionChartData: { (data,total,begin,end,endDate) in
self.entries = data
self.config.data.entries = data
configChartSettings()
self.totalSteps = total
self.beginTime = begin
self.endTime = end
self.endDate = endDate
}, completionComponentData: {_ in })
}
.animation(.easeInOut)
.onDisappear() {
tempIndex = .Day
index = .Day
endDate = Date()
dragDirection = .nothing
self.hasAppeared = true
}
.onReceive([index].publisher.last(), perform: { value in
if value != tempIndex {
if randomData {
self.entries = randomEntries()
} else {
switch index {
case .Day:
self.config.xAxis.ticksInterval = 4
case .Week:
self.config.xAxis.ticksInterval = 1
case .Month:
self.config.xAxis.ticksInterval = 7
case .Year:
self.config.xAxis.ticksInterval = 1
}
tempIndex = index
endDate = Date()
HealthKitData.instance.configDataEntities(index, dragDirection, endDate: endDate, dataType: .chartData, requestedData: requestedData, completionChartData: { (data,total,begin,end,endDate) in
self.entries = data
self.config.data.entries = data
configChartSettings()
self.totalSteps = total
self.beginTime = begin
self.endTime = end
//self.dragDirection = .nothing
self.endDate = endDate
tempIndex = index
}, completionComponentData: {_ in })
}
self.selectedEntry = nil
self.selectedBarTopCentreLocation = nil
}
})
.onReceive([dragDirection].publisher.first(), perform: { value in
if value != .nothing && dragDirection != .nothing {
HealthKitData.instance.configDataEntities(index, dragDirection, endDate: endDate, dataType: .chartData, requestedData: requestedData, completionChartData: { (data,total,begin,end,endDate) in
self.entries = data
self.config.data.entries = data
configChartSettings()
self.totalSteps = total
self.beginTime = begin
self.endTime = end
self.dragDirection = .nothing
self.endDate = endDate
}, completionComponentData: {_ in })
}
})
}.padding(15)
}
.gesture(drag)
}
func configChartSettings() {
let labelsFont = CTFontCreateWithName(("SFProText-Regular" as CFString), 10, nil)
tempIndex = index
self.config.labelsCTFont = labelsFont
self.config.data.gradientColor = GradientColor(start: Color("GraphGradientStart"), end: Color("GraphGradientEnd"))
self.config.xAxis.ticksDash = [2, 4]
self.config.xAxis.labelsColor = .gray
self.config.xAxis.ticksColor = .clear //.gray
self.config.yAxis.labelsColor = .gray
self.config.yAxis.ticksColor = .gray
self.config.yAxis.ticksDash = [2, 2]
self.config.yAxis.minTicksSpacing = 20.0
self.config.yAxis.formatter = { (value, decimals) in
let format = value == 0 ? "" : ""
return String(format: " %.\(decimals)f\(format)", value)
}
}`
and here is an screen of the app
https://user-images.githubusercontent.com/16289125/115393740-41ed5900-a1f7-11eb-93f3-65e4edf51e60.mov