Swift Charts

RSS for tag

Visualize data with highly customizable charts across all Apple platforms using the compositional syntax of SwifUI.

Posts under Swift Charts tag

198 Posts

Post

Replies

Boosts

Views

Activity

Sub-second X AxisMarks in SwiftUI Charts time series line chart
I already have a working app, using SwiftUI Canvas, for charting sensor milliVolt data at a frequency of 130Hz, so thought I'd see how SwiftUI Charts compared for ease of implementation. Charts is much easier to implement, saving the tedium of scaling and label context placement. However, creating the AxisMarks.Values was a problem because the stride-by method with Calendar.Component has .second as the smallest unit, and I need deciSecond. I tried some other inbuillt options, e.g. desiredCount, but no joy. After a while I realised that I can create my own values array using a function, so here it is (hoping that this helps someone, somewhere, sometime): struct ChartView: View {     var chartData : [DataPoint] // An array of CoreData objects with timeStamp and milliVolt attributes let xSecStyle = StrokeStyle(lineWidth: 1.0)     let xDeciSecStyle = StrokeStyle(lineWidth: 0.5) ... // The .chartAxis code within Chart in body .chartXAxis {                 AxisMarks(values: deciSecValues) { value in                     if Double(value.index).remainder(dividingBy: 10.0)  == 0.0 { // show the second-gridline as wider and display the time label                         AxisGridLine(stroke:xSecStyle)                                 .foregroundStyle(.orange)                         AxisValueLabel(format: .dateTime.minute().second(),centered: false)                     } else {                         AxisGridLine(stroke:xDeciSecStyle)                                 .foregroundStyle(.orange)                     }                 }             } ..... // The func for creating the X Axis Marks private var deciSecValues : [Date] {         var xDates = [Date]()         if chartData.isEmpty { return xDates }         let deciSecs = (Int(chartData.last!.timeStamp!.timeIntervalSince(chartData.first!.timeStamp!)) + 1) * 10         for i in stride(from: 0, to: deciSecs, by: 1) {             let newTime = Double(i) / 10.0             xDates.append(chartData.first!.timeStamp!.addingTimeInterval(newTime))         }         return xDates     } Regards, Michaela
0
1
1.7k
Jul ’22
SwiftUI Chart scale issue
Hey guys I'm playing around with swiftUI charts using data from Yahoo Finance public api. I'm getting a data set of charts data and trying to display it using SwiftUI charts. My chart seems broken compared to what I'm seeing on the Yahoo webpage: My code looks like this (tried to keep it super simple for the example):     VStack {       Chart {         ForEach(viewmodel.chartData1) { chartData in           LineMark(x: .value("date", chartData.date),                y: .value("amount", "\(chartData.high)"))           .interpolationMethod(.catmullRom)                                 }       }     }     .task { await viewmodel.getSymbols() }   } So I'm not sure what I'm doing wrong and why does my chart looks downward trending like that. Any help, any lead would help!
1
0
1.9k
Jul ’22
Value based colors
How to create a chart where each data mark has a color based on its value? Like this chart: https://observablehq.com/@d3/gradient-encoding With the following code, the whole curve has one color only. My function Color.forTemperature() returns different colors for different temperatures. Chart(temperatures) { temperature in LineMark(     x: .value("Day", temperature.date, unit: .day),         y: .value("Temperature", temperature.value)     ) .foregroundStyle(Color.forTemperature(temperature.value)) }
2
0
1.7k
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
1
0
1.9k
Jul ’22
How to use SwiftUI Charts in a scrollable view
I want to replace my custom chart implementation with the new SwiftUI Charts. Because I use a big dataset I want to have like 6 Bars visible in my view and the ability to scroll through the rest of the data. My View is implemented like this: Chart { ForEach(data) { BarMark(x: .value("Total Count", $0.value), y: .value("Shape Type", $0.text)) .foregroundStyle($0.color) } } This is the amount of Bars I want to have in my view: And this is my current view: Did I overlook something? Cheers Nils
2
0
2.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
6.2k
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
960
Jun ’22
Changing the Timeframe of Charts
I'm playing around with the new Swift Charts, so far everything is rather straight forward, however I'm looking for a way to change the time frame/scope of the chart. For example, recreating the Heart Rate chart in the Health App or in other words, only showing a subsection of the data, and being able to move the x-axis for left/right for more data. I know it's early, but anyone have any ideas how that might be done?
1
0
1.8k
Jun ’22
Axis with fixed domain range without rounding
I am using .chartYScale(domain: 0...12) to define a Y axis from 0 to 12, but the chart will always display 0 to 15, i.e. it will round the given values up to the next "nice one". However, I don't want this in my use case. How do I tell the chart to use exactly the provided range and don't adjust it automatically?
1
1
905
Jun ’22
LineMark Does Not Respect .foregroundStyle Color choice
Working through the new Swift Chart framework and I am noticing LineMark does not seem to want to respect .foregroundStyle(.pink) (or any respective color) for more than one line. Apple's own Chart page lists an example that, when copied into Xcode 14 (beta 1) it does not render in preview as it does in their screenshot Data used: struct ProfitOverTime { var week: Int var profit: Double } let departmentAProfile: [ProfitOverTime] = [ .init(week: 1, profit: 21.5), .init(week: 2, profit: 19.2), .init(week: 3, profit: 18.4), .init(week: 4, profit: 21.0), .init(week: 5, profit: 19.7), .init(week: 6, profit: 14.7), .init(week: 7, profit: 22.1), .init(week: 8, profit: 18.0) ] let departmentBProfile: [ProfitOverTime] = [ .init(week: 1, profit: 5.7), .init(week: 2, profit: 12.0), .init(week: 3, profit: 11.9), .init(week: 4, profit: 18.0), .init(week: 5, profit: 15.9), .init(week: 6, profit: 16.7), .init(week: 7, profit: 12.1), .init(week: 8, profit: 19.0) ] Content View: struct ContentView: View {     var body: some View { Chart { ForEach(departmentAProfile, id: \.week) { LineMark( x: .value("Week", $0.week), y: .value("Profit A", $0.profit) ).foregroundStyle(.green) } ForEach(departmentBProfile, id: \.week) { LineMark( x: .value("Week", $0.week), y: .value("Profit B", $0.profit) ) .foregroundStyle(.green) } RuleMark( y: .value("Threshold", 20.0) ) .foregroundStyle(.teal) }     } } Produces
2
0
2.3k
Jun ’22
Auto zooming on data variation chart type & Axis breaks symbols
A lot of time when you plot a chart (say a bar chart here) for business you wants to emphasize the small variation between data, say daily you have roughly 5 millions messages sent but you need to visualize the 3k variations common each days. To utilize the landscape it's best for one to visualize not from zero but zooming in between 4.9m ~ 5.2m range instead of showing a slightly wiggling line. While we could likely (I haven't get to it) to customize our chart to do that it'd be a good and handy option for the standard charts and Swift Chart would auto-adjust the range. On top of it, being able to add standard axis break symbols would be a great help to keep it visualized and keep the chart honest.
0
0
906
Jun ’22
Sub-second X AxisMarks in SwiftUI Charts time series line chart
I already have a working app, using SwiftUI Canvas, for charting sensor milliVolt data at a frequency of 130Hz, so thought I'd see how SwiftUI Charts compared for ease of implementation. Charts is much easier to implement, saving the tedium of scaling and label context placement. However, creating the AxisMarks.Values was a problem because the stride-by method with Calendar.Component has .second as the smallest unit, and I need deciSecond. I tried some other inbuillt options, e.g. desiredCount, but no joy. After a while I realised that I can create my own values array using a function, so here it is (hoping that this helps someone, somewhere, sometime): struct ChartView: View {     var chartData : [DataPoint] // An array of CoreData objects with timeStamp and milliVolt attributes let xSecStyle = StrokeStyle(lineWidth: 1.0)     let xDeciSecStyle = StrokeStyle(lineWidth: 0.5) ... // The .chartAxis code within Chart in body .chartXAxis {                 AxisMarks(values: deciSecValues) { value in                     if Double(value.index).remainder(dividingBy: 10.0)  == 0.0 { // show the second-gridline as wider and display the time label                         AxisGridLine(stroke:xSecStyle)                                 .foregroundStyle(.orange)                         AxisValueLabel(format: .dateTime.minute().second(),centered: false)                     } else {                         AxisGridLine(stroke:xDeciSecStyle)                                 .foregroundStyle(.orange)                     }                 }             } ..... // The func for creating the X Axis Marks private var deciSecValues : [Date] {         var xDates = [Date]()         if chartData.isEmpty { return xDates }         let deciSecs = (Int(chartData.last!.timeStamp!.timeIntervalSince(chartData.first!.timeStamp!)) + 1) * 10         for i in stride(from: 0, to: deciSecs, by: 1) {             let newTime = Double(i) / 10.0             xDates.append(chartData.first!.timeStamp!.addingTimeInterval(newTime))         }         return xDates     } Regards, Michaela
Replies
0
Boosts
1
Views
1.7k
Activity
Jul ’22
SwiftUI Chart scale issue
Hey guys I'm playing around with swiftUI charts using data from Yahoo Finance public api. I'm getting a data set of charts data and trying to display it using SwiftUI charts. My chart seems broken compared to what I'm seeing on the Yahoo webpage: My code looks like this (tried to keep it super simple for the example):     VStack {       Chart {         ForEach(viewmodel.chartData1) { chartData in           LineMark(x: .value("date", chartData.date),                y: .value("amount", "\(chartData.high)"))           .interpolationMethod(.catmullRom)                                 }       }     }     .task { await viewmodel.getSymbols() }   } So I'm not sure what I'm doing wrong and why does my chart looks downward trending like that. Any help, any lead would help!
Replies
1
Boosts
0
Views
1.9k
Activity
Jul ’22
Value based colors
How to create a chart where each data mark has a color based on its value? Like this chart: https://observablehq.com/@d3/gradient-encoding With the following code, the whole curve has one color only. My function Color.forTemperature() returns different colors for different temperatures. Chart(temperatures) { temperature in LineMark(     x: .value("Day", temperature.date, unit: .day),         y: .value("Temperature", temperature.value)     ) .foregroundStyle(Color.forTemperature(temperature.value)) }
Replies
2
Boosts
0
Views
1.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.9k
Activity
Jul ’22
How to use SwiftUI Charts in a scrollable view
I want to replace my custom chart implementation with the new SwiftUI Charts. Because I use a big dataset I want to have like 6 Bars visible in my view and the ability to scroll through the rest of the data. My View is implemented like this: Chart { ForEach(data) { BarMark(x: .value("Total Count", $0.value), y: .value("Shape Type", $0.text)) .foregroundStyle($0.color) } } This is the amount of Bars I want to have in my view: And this is my current view: Did I overlook something? Cheers Nils
Replies
2
Boosts
0
Views
2.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
6.2k
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
960
Activity
Jun ’22
Changing the Timeframe of Charts
I'm playing around with the new Swift Charts, so far everything is rather straight forward, however I'm looking for a way to change the time frame/scope of the chart. For example, recreating the Heart Rate chart in the Health App or in other words, only showing a subsection of the data, and being able to move the x-axis for left/right for more data. I know it's early, but anyone have any ideas how that might be done?
Replies
1
Boosts
0
Views
1.8k
Activity
Jun ’22
Axis with fixed domain range without rounding
I am using .chartYScale(domain: 0...12) to define a Y axis from 0 to 12, but the chart will always display 0 to 15, i.e. it will round the given values up to the next "nice one". However, I don't want this in my use case. How do I tell the chart to use exactly the provided range and don't adjust it automatically?
Replies
1
Boosts
1
Views
905
Activity
Jun ’22
LineMark Does Not Respect .foregroundStyle Color choice
Working through the new Swift Chart framework and I am noticing LineMark does not seem to want to respect .foregroundStyle(.pink) (or any respective color) for more than one line. Apple's own Chart page lists an example that, when copied into Xcode 14 (beta 1) it does not render in preview as it does in their screenshot Data used: struct ProfitOverTime { var week: Int var profit: Double } let departmentAProfile: [ProfitOverTime] = [ .init(week: 1, profit: 21.5), .init(week: 2, profit: 19.2), .init(week: 3, profit: 18.4), .init(week: 4, profit: 21.0), .init(week: 5, profit: 19.7), .init(week: 6, profit: 14.7), .init(week: 7, profit: 22.1), .init(week: 8, profit: 18.0) ] let departmentBProfile: [ProfitOverTime] = [ .init(week: 1, profit: 5.7), .init(week: 2, profit: 12.0), .init(week: 3, profit: 11.9), .init(week: 4, profit: 18.0), .init(week: 5, profit: 15.9), .init(week: 6, profit: 16.7), .init(week: 7, profit: 12.1), .init(week: 8, profit: 19.0) ] Content View: struct ContentView: View {     var body: some View { Chart { ForEach(departmentAProfile, id: \.week) { LineMark( x: .value("Week", $0.week), y: .value("Profit A", $0.profit) ).foregroundStyle(.green) } ForEach(departmentBProfile, id: \.week) { LineMark( x: .value("Week", $0.week), y: .value("Profit B", $0.profit) ) .foregroundStyle(.green) } RuleMark( y: .value("Threshold", 20.0) ) .foregroundStyle(.teal) }     } } Produces
Replies
2
Boosts
0
Views
2.3k
Activity
Jun ’22
Auto zooming on data variation chart type & Axis breaks symbols
A lot of time when you plot a chart (say a bar chart here) for business you wants to emphasize the small variation between data, say daily you have roughly 5 millions messages sent but you need to visualize the 3k variations common each days. To utilize the landscape it's best for one to visualize not from zero but zooming in between 4.9m ~ 5.2m range instead of showing a slightly wiggling line. While we could likely (I haven't get to it) to customize our chart to do that it'd be a good and handy option for the standard charts and Swift Chart would auto-adjust the range. On top of it, being able to add standard axis break symbols would be a great help to keep it visualized and keep the chart honest.
Replies
0
Boosts
0
Views
906
Activity
Jun ’22