Swift Charts: Raise the bar

RSS for tag

Discuss the WWDC22 Session Swift Charts: Raise the bar

Posts under wwdc2022-10137 tag

15 Posts

Post

Replies

Boosts

Views

Activity

Scrollable Plot Area
Is there a way to create scrolling/paging behavior in the charts just like is done in the Health app? I have a similar app that also displays health data (such as heart rate and steps) and with my current custom charts the user can page back to see data from previous weeks (by default it shows the current week). Just like in the health app the user can go back as long as there is data, which can be several years (so hundreds go pages). I'd love to replace my custom implementation with Swift Charts but for that I do need this scrolling behavior.
8
11
4.2k
Oct ’23
SwiftUI Charts AxisValueLabel disappears when using centered: true
Hi there, When using the AxisValueLabel in Swift Charts, the moment you add centered: true, the month or day on the right disappears. This has been a bug for over five months since I looked through other developer forums and they had the same issue. AxisValueLabel(format: .dateTime.weekday(), centered: true) leads to the following view: Now if we remove the centered: true, saturday will appear. It is probably a spacing issue, but since these are modifiers provided by Apple, it should do it correctly, right? If I am missing something and this is by desing, please enlighten me. Best Til
1
0
1.5k
Aug ’23
SwiftCharts: How to clip data to specific range?
I found that can customise the range of the x and y axes. But my problem is that for line marks some data might just be outside the axes range. If I add .clipped to the chart that would limit it, but then it also clips off some of the axis marks. No Clipping: With Clipping: you see the green line go over the AxisValueLabel area and the topmost label is clipped off. One way I can imagine addressing this without enabling clipping is to create an interpolated value for values that are just outside the chart range so that the line ends exactly at the min/max.
5
2
2.8k
Mar ’23
How can I include a function (or calculation) in the values that Charts receives to draw the chart, and overcome this bug?
Charts fails when a function or ternary condition is added to the values to be plotted. Charts. When a function is received as a value in the ForEach loop, the application crashes (Thread 1: signal SIGABRT), or if it is a ternary operator (condition ? true: false) it does not show the graph. The function should convert the value/attribute of the Core Data entity only if necessary, the data is stored in a common unit and depending on the display condition in an AppStorage variable it should be seen converted to another unit or displayed. This error occurs with any type of chart. It fails with both RulerMark and other charts like LineMark. The function works fine with Text and other similar views. The Chart @AppStorage("unitView") var unitView: String = "Celsius" ... // CHART Chart { if statsByUser.takens > 1 { RuleMark(y: .value("AVG", getViewDegrees(statsByUser.avg, unitView))) .lineStyle(StrokeStyle(lineWidth: 2, lineCap: .round, lineJoin: .round, dash: [4, 6])) .foregroundStyle(.white.opacity(0.8)) } ForEach(0..<temperatureByUserId.count, id:\.self) { item in LineMark(x: .value("Date", item + 1), y: .value("Temperature", getViewDegrees(temperatureByUserId[item].degress, unitView))) .lineStyle(.init(lineWidth: 2, lineCap: .round, lineJoin: .round)) .foregroundStyle(.white) //.mint [.pink, .purple, .mint] } .interpolationMethod(.cardinal) } // X-Axis .chartXScale(domain: 1 ... statsByUser.takens) .chartXAxis(.hidden) // Y-Axis .chartYScale(domain: statsByUser.min ... statsByUser.max) .chartYAxis { AxisMarks{ _ in AxisGridLine().foregroundStyle(.white) AxisValueLabel(centered: true).foregroundStyle(.white) } } } The Functions let unitDegress: [String] = ["Celsius", "Fahrenheit"] func getViewDegrees(_ value: Double, _ unit: String) -> Double { var returnValue = value if unit == unitDegress[1] { // is Fahrenheit let t = Measurement(value: value, unit: UnitTemperature.celsius) returnValue = t.converted(to: .fahrenheit).value } return returnValue } Text works properly. Text("\(getViewDegrees(temperatureByUser.last, unitView), specifier: decimalSpecifier)")
2
0
1.1k
Oct ’22
How to create bar chart with two or more bars per time period
Environment: Xcode 14.1 beta 3 on macOS Ventura Beta 9. No longer need this, I found example of this. struct LocationsChart: View {     var body: some View {         Chart {             ForEach(seriesData, id: \.city) { series in                 ForEach(series.data, id: \.weekday) {                     BarMark(                         x: .value("Weekday", $0.weekday, unit: .day),                         y: .value("Sales", $0.sales)                     )                 }                 .foregroundStyle(by: .value("City", series.city))                 .position(by: .value("City", series.city))             }         }     }     let seriesData = [         (             city: "Cupertino", data: [                 (weekday: date(year: 2022, month: 5, day: 2), sales: 54),                 (weekday: date(year: 2022, month: 5, day: 3), sales: 42),                 (weekday: date(year: 2022, month: 5, day: 4), sales: 88),                 (weekday: date(year: 2022, month: 5, day: 5), sales: 49),                 (weekday: date(year: 2022, month: 5, day: 6), sales: 42),                 (weekday: date(year: 2022, month: 5, day: 7), sales: 125),                 (weekday: date(year: 2022, month: 5, day: 8), sales: 67)             ]         ),         (             city: "San Francisco", data: [                 (weekday: date(year: 2022, month: 5, day: 2), sales: 81),                 (weekday: date(year: 2022, month: 5, day: 3), sales: 90),                 (weekday: date(year: 2022, month: 5, day: 4), sales: 52),                 (weekday: date(year: 2022, month: 5, day: 5), sales: 72),                 (weekday: date(year: 2022, month: 5, day: 6), sales: 84),                 (weekday: date(year: 2022, month: 5, day: 7), sales: 84),                 (weekday: date(year: 2022, month: 5, day: 8), sales: 137)             ]         )     ] } func date(year: Int, month: Int, day: Int = 1) -> Date {     Calendar.current.date(from: DateComponents(year: year, month: month, day: day)) ?? Date() }
0
0
1.2k
Oct ’22
Plotting a Stock Chart with ScaleType <missing> WeekDay
I'm wondering if Apple will add a weekDay type ScaleType for plotting stock market charts. The Date scaleType assumes that the weekend days (Saturday/Sunday) should be within the plotted data. But the typical stock market chart omits the weekends and holidays (non-market days) from the charts. Would Apple be interested in providing this new Chart Scaling Type. Or is there a way to extend the Charts Framework to allow this type of chart? For Example: the X Axis might look like: M T W T F M T W T F T W T F
1
1
1.8k
Aug ’22
"font" and "foregroundstyle" modifier on AxisValueLabel don't work!
Hi, I use a barmark/linemark SwiftUI chart in my app and when i want to change the fontsize and color of the AxisValueLabel for the x-axis then it does not seem to work properly. When i set the following: AxisValueLabel() .foregroundStyle(.red) or AxisValueLabel() .font(.system(size: 30)) it doesn't show up. Kind regards, Tom
1
0
1.8k
Jul ’22
Charts: customising chartYAxis values
anyone figured out how to customise the Y axis values? id like to be able to process the. axis values and. then display them in the. format id like. eg. "1000" I'd like to display as "1k" the furthest Ive been able to get is to get the axis to display a static text as the number ie "Y" in the code below. but I haven't been able to access the value. in the code below 'value' seems to be an AxisValue which stores the value but I can't seem to access it in a format I can process. .chartYAxis {                 AxisMarks() { value in                     AxisGridLine()                     AxisTick()                     AxisValueLabel {                     Text("Y")                     }                 }             } id like to be able to do something like this: .chartYAxis {                 AxisMarks() { value in                     AxisGridLine()                     AxisTick()                     AxisValueLabel {                         if value > 1000000000000.0 {                             Text("\(value / 1000000000000.0)t")                         } else if value > 1000000000.0 {                             Text("\(value / 1000000000.0)b")                         } else if value > 1000000.0 {                             Text("\(value / 1000000.0)m")                         } else if value > 1000.0 {                             Text("\(value / 1000.0)k")                         }                     }                 }             }
5
2
6k
Jun ’22
SwiftCharts: how to manage colors and groupings
I want to draw a chart where I have two line marks (one for each child each) and behind it area marks for percentiles. If I don't add any foregroundStyle then the line marks get connected. struct GrowthChart: View { @ChartContentBuilder func oneSlice(row: PercentileRow, slice: Slice) -> some ChartContent { AreaMark( x: .value("Day", row.month), yStart: .value("Max", row.dictionary[slice.startKey]!), yEnd: .value("Min", row.dictionary[slice.endKey]!) ) .foregroundStyle(by: .value("Percentile", slice.number)) } let percentileColors: [Color] = [ .blue.opacity(0.1),   .blue.opacity(0.2),   .blue.opacity(0.3),   .blue.opacity(0.4),   .blue.opacity(0.5),   .blue.opacity(0.4),   .blue.opacity(0.3),   .blue.opacity(0.2),   .blue.opacity(0.1)   ] var body: some View { VStack { Chart { ForEach(percentiles.rows) { row in ForEach(slices) { slice in oneSlice(row: row, slice: slice) } } ForEach(children) { child in ForEach(child.data) { dataPoint in LineMark( x: .value("Month", dataPoint.month), y: .value("Height", dataPoint.height) ) .foregroundStyle(by: .value("Name", child.name)) } } } .chartForegroundStyleScale( range: Gradient (colors: percentileColors) ) .aspectRatio(contentMode: .fit) .padding() Spacer() }.padding() } } I don't understand how to group the individual marks. Apparently you have to add a foregroundStyle to each with a .by specifying a category. I found this chartForegroundStyleScale in an example on the internet, but I can only get it to show this gradient. But how do I specify different colors for the LineMarks?
1
0
2.3k
Jun ’22
Orientation (vertical, angle) in Swift Charts is not working
Hi! I want to use the new SwiftUI Charts (iOS 16) in my app and it seems that the orientation does not work. I want to make the AxisLabels vertical oder verticalReversed or give it a certain angle but all three methods don't seem to work. AxisMarks() { _ in AxisValueLabel(orientation: .angle(Angle.degrees(45.0))) } } Am i doing something wrong or is it a bug in the beta framework? Kind regards, Tom
0
0
942
Jun ’22
Scrollable Plot Area
Is there a way to create scrolling/paging behavior in the charts just like is done in the Health app? I have a similar app that also displays health data (such as heart rate and steps) and with my current custom charts the user can page back to see data from previous weeks (by default it shows the current week). Just like in the health app the user can go back as long as there is data, which can be several years (so hundreds go pages). I'd love to replace my custom implementation with Swift Charts but for that I do need this scrolling behavior.
Replies
8
Boosts
11
Views
4.2k
Activity
Oct ’23
SwiftUI Charts AxisValueLabel disappears when using centered: true
Hi there, When using the AxisValueLabel in Swift Charts, the moment you add centered: true, the month or day on the right disappears. This has been a bug for over five months since I looked through other developer forums and they had the same issue. AxisValueLabel(format: .dateTime.weekday(), centered: true) leads to the following view: Now if we remove the centered: true, saturday will appear. It is probably a spacing issue, but since these are modifiers provided by Apple, it should do it correctly, right? If I am missing something and this is by desing, please enlighten me. Best Til
Replies
1
Boosts
0
Views
1.5k
Activity
Aug ’23
SwiftCharts: How to clip data to specific range?
I found that can customise the range of the x and y axes. But my problem is that for line marks some data might just be outside the axes range. If I add .clipped to the chart that would limit it, but then it also clips off some of the axis marks. No Clipping: With Clipping: you see the green line go over the AxisValueLabel area and the topmost label is clipped off. One way I can imagine addressing this without enabling clipping is to create an interpolated value for values that are just outside the chart range so that the line ends exactly at the min/max.
Replies
5
Boosts
2
Views
2.8k
Activity
Mar ’23
Swift Charts Interactive Scaling
I currently have a bar chart using Xcodes built in charts. I would like to make it so when I drag over an individual cell, that cell will increase it's width so that the user knows that is the cell they are selecting. How would I go about getting this effect. Thanks
Replies
0
Boosts
0
Views
855
Activity
Nov ’22
How can I include a function (or calculation) in the values that Charts receives to draw the chart, and overcome this bug?
Charts fails when a function or ternary condition is added to the values to be plotted. Charts. When a function is received as a value in the ForEach loop, the application crashes (Thread 1: signal SIGABRT), or if it is a ternary operator (condition ? true: false) it does not show the graph. The function should convert the value/attribute of the Core Data entity only if necessary, the data is stored in a common unit and depending on the display condition in an AppStorage variable it should be seen converted to another unit or displayed. This error occurs with any type of chart. It fails with both RulerMark and other charts like LineMark. The function works fine with Text and other similar views. The Chart @AppStorage("unitView") var unitView: String = "Celsius" ... // CHART Chart { if statsByUser.takens > 1 { RuleMark(y: .value("AVG", getViewDegrees(statsByUser.avg, unitView))) .lineStyle(StrokeStyle(lineWidth: 2, lineCap: .round, lineJoin: .round, dash: [4, 6])) .foregroundStyle(.white.opacity(0.8)) } ForEach(0..<temperatureByUserId.count, id:\.self) { item in LineMark(x: .value("Date", item + 1), y: .value("Temperature", getViewDegrees(temperatureByUserId[item].degress, unitView))) .lineStyle(.init(lineWidth: 2, lineCap: .round, lineJoin: .round)) .foregroundStyle(.white) //.mint [.pink, .purple, .mint] } .interpolationMethod(.cardinal) } // X-Axis .chartXScale(domain: 1 ... statsByUser.takens) .chartXAxis(.hidden) // Y-Axis .chartYScale(domain: statsByUser.min ... statsByUser.max) .chartYAxis { AxisMarks{ _ in AxisGridLine().foregroundStyle(.white) AxisValueLabel(centered: true).foregroundStyle(.white) } } } The Functions let unitDegress: [String] = ["Celsius", "Fahrenheit"] func getViewDegrees(_ value: Double, _ unit: String) -> Double { var returnValue = value if unit == unitDegress[1] { // is Fahrenheit let t = Measurement(value: value, unit: UnitTemperature.celsius) returnValue = t.converted(to: .fahrenheit).value } return returnValue } Text works properly. Text("\(getViewDegrees(temperatureByUser.last, unitView), specifier: decimalSpecifier)")
Replies
2
Boosts
0
Views
1.1k
Activity
Oct ’22
How to create bar chart with two or more bars per time period
Environment: Xcode 14.1 beta 3 on macOS Ventura Beta 9. No longer need this, I found example of this. struct LocationsChart: View {     var body: some View {         Chart {             ForEach(seriesData, id: \.city) { series in                 ForEach(series.data, id: \.weekday) {                     BarMark(                         x: .value("Weekday", $0.weekday, unit: .day),                         y: .value("Sales", $0.sales)                     )                 }                 .foregroundStyle(by: .value("City", series.city))                 .position(by: .value("City", series.city))             }         }     }     let seriesData = [         (             city: "Cupertino", data: [                 (weekday: date(year: 2022, month: 5, day: 2), sales: 54),                 (weekday: date(year: 2022, month: 5, day: 3), sales: 42),                 (weekday: date(year: 2022, month: 5, day: 4), sales: 88),                 (weekday: date(year: 2022, month: 5, day: 5), sales: 49),                 (weekday: date(year: 2022, month: 5, day: 6), sales: 42),                 (weekday: date(year: 2022, month: 5, day: 7), sales: 125),                 (weekday: date(year: 2022, month: 5, day: 8), sales: 67)             ]         ),         (             city: "San Francisco", data: [                 (weekday: date(year: 2022, month: 5, day: 2), sales: 81),                 (weekday: date(year: 2022, month: 5, day: 3), sales: 90),                 (weekday: date(year: 2022, month: 5, day: 4), sales: 52),                 (weekday: date(year: 2022, month: 5, day: 5), sales: 72),                 (weekday: date(year: 2022, month: 5, day: 6), sales: 84),                 (weekday: date(year: 2022, month: 5, day: 7), sales: 84),                 (weekday: date(year: 2022, month: 5, day: 8), sales: 137)             ]         )     ] } func date(year: Int, month: Int, day: Int = 1) -> Date {     Calendar.current.date(from: DateComponents(year: year, month: month, day: day)) ?? Date() }
Replies
0
Boosts
0
Views
1.2k
Activity
Oct ’22
Plotting a Stock Chart with ScaleType <missing> WeekDay
I'm wondering if Apple will add a weekDay type ScaleType for plotting stock market charts. The Date scaleType assumes that the weekend days (Saturday/Sunday) should be within the plotted data. But the typical stock market chart omits the weekends and holidays (non-market days) from the charts. Would Apple be interested in providing this new Chart Scaling Type. Or is there a way to extend the Charts Framework to allow this type of chart? For Example: the X Axis might look like: M T W T F M T W T F T W T F
Replies
1
Boosts
1
Views
1.8k
Activity
Aug ’22
Little question
Is it possible to have a custom symbol just at the head of the line chart? EX: Live line chart where the head is a little circle
Replies
0
Boosts
0
Views
1k
Activity
Aug ’22
Will Swift Charts be available on iOS 15?
Would love to use Swift Charts framework. But I still need to support older iOS versions. Do I need create custom chart solution for iOS15 or any chance SwiftCharts will be backward compatible?
Replies
2
Boosts
4
Views
3.7k
Activity
Jul ’22
"font" and "foregroundstyle" modifier on AxisValueLabel don't work!
Hi, I use a barmark/linemark SwiftUI chart in my app and when i want to change the fontsize and color of the AxisValueLabel for the x-axis then it does not seem to work properly. When i set the following: AxisValueLabel() .foregroundStyle(.red) or AxisValueLabel() .font(.system(size: 30)) it doesn't show up. Kind regards, Tom
Replies
1
Boosts
0
Views
1.8k
Activity
Jul ’22
Charts: customising chartYAxis values
anyone figured out how to customise the Y axis values? id like to be able to process the. axis values and. then display them in the. format id like. eg. "1000" I'd like to display as "1k" the furthest Ive been able to get is to get the axis to display a static text as the number ie "Y" in the code below. but I haven't been able to access the value. in the code below 'value' seems to be an AxisValue which stores the value but I can't seem to access it in a format I can process. .chartYAxis {                 AxisMarks() { value in                     AxisGridLine()                     AxisTick()                     AxisValueLabel {                     Text("Y")                     }                 }             } id like to be able to do something like this: .chartYAxis {                 AxisMarks() { value in                     AxisGridLine()                     AxisTick()                     AxisValueLabel {                         if value > 1000000000000.0 {                             Text("\(value / 1000000000000.0)t")                         } else if value > 1000000000.0 {                             Text("\(value / 1000000000.0)b")                         } else if value > 1000000.0 {                             Text("\(value / 1000000.0)m")                         } else if value > 1000.0 {                             Text("\(value / 1000.0)k")                         }                     }                 }             }
Replies
5
Boosts
2
Views
6k
Activity
Jun ’22
SwiftCharts: How do I make PointMarks only a dot?
Is there a way to have a point mark only come out as a very small circle? I want PointMarks that are basically just 1 point large black dots.
Replies
1
Boosts
0
Views
1.6k
Activity
Jun ’22
SwiftCharts: how to manage colors and groupings
I want to draw a chart where I have two line marks (one for each child each) and behind it area marks for percentiles. If I don't add any foregroundStyle then the line marks get connected. struct GrowthChart: View { @ChartContentBuilder func oneSlice(row: PercentileRow, slice: Slice) -> some ChartContent { AreaMark( x: .value("Day", row.month), yStart: .value("Max", row.dictionary[slice.startKey]!), yEnd: .value("Min", row.dictionary[slice.endKey]!) ) .foregroundStyle(by: .value("Percentile", slice.number)) } let percentileColors: [Color] = [ .blue.opacity(0.1),   .blue.opacity(0.2),   .blue.opacity(0.3),   .blue.opacity(0.4),   .blue.opacity(0.5),   .blue.opacity(0.4),   .blue.opacity(0.3),   .blue.opacity(0.2),   .blue.opacity(0.1)   ] var body: some View { VStack { Chart { ForEach(percentiles.rows) { row in ForEach(slices) { slice in oneSlice(row: row, slice: slice) } } ForEach(children) { child in ForEach(child.data) { dataPoint in LineMark( x: .value("Month", dataPoint.month), y: .value("Height", dataPoint.height) ) .foregroundStyle(by: .value("Name", child.name)) } } } .chartForegroundStyleScale( range: Gradient (colors: percentileColors) ) .aspectRatio(contentMode: .fit) .padding() Spacer() }.padding() } } I don't understand how to group the individual marks. Apparently you have to add a foregroundStyle to each with a .by specifying a category. I found this chartForegroundStyleScale in an example on the internet, but I can only get it to show this gradient. But how do I specify different colors for the LineMarks?
Replies
1
Boosts
0
Views
2.3k
Activity
Jun ’22
Orientation (vertical, angle) in Swift Charts is not working
Hi! I want to use the new SwiftUI Charts (iOS 16) in my app and it seems that the orientation does not work. I want to make the AxisLabels vertical oder verticalReversed or give it a certain angle but all three methods don't seem to work. AxisMarks() { _ in AxisValueLabel(orientation: .angle(Angle.degrees(45.0))) } } Am i doing something wrong or is it a bug in the beta framework? Kind regards, Tom
Replies
0
Boosts
0
Views
942
Activity
Jun ’22
Custom Marks
How does one create a custom mark for Swift Charts? I'm trying to add a live indicator to a stock app line mark. Is a custom mark the best way to do this?
Replies
0
Boosts
0
Views
1k
Activity
Jun ’22