Swift Charts

RSS for tag

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

Swift Charts Documentation

Posts under Swift Charts tag

30 Posts
Sort by:
Post not yet marked as solved
2 Replies
110 Views
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)) }
Posted
by
Post not yet marked as solved
2 Replies
139 Views
I'm testing the the new Charts Framework from Apple with Xcode 14.0 Beta 2. But I face some strange issues when I try to scale the y-axis to some concrete values. var body: some View {         VStack {             GroupBox("Serien") {                 Chart {                     ForEach(seriesData) { series in                         BarMark(                             x: .value("Series", series.name),                             y: .value("Wert", series.value)                         )                         .cornerRadius(8.0)                         .annotation(position: .overlay, alignment: .top, spacing: 10.0) {                             Text("\(series.value)")                                 .foregroundColor(.white)                                 .fontWeight(.bold)                         }                     }                     RuleMark(y: .value("Durchschnitt", shotResult.wrappedSeriesAsInt.mean()))                         .foregroundStyle(.red)                 }                 .chartYScale(domain: minSeriesYAxis...maxSeriesYAxis)                 .frame(height: 220, alignment: .topLeading)             }             .backgroundStyle(Color.white)             .shadow(radius: 5.0, x: 2.0, y: 2.0)         }     } Resulting View The LineMark or RectangleMark looks ok. But e.g. the AreaMark has the same issue.
Posted
by
Post marked as solved
2 Replies
113 Views
I have a macOS app which was built using Xcode 13 and storyboard. I want to add swiftCharts to the app. Can I add swiftCharts code to this app and not have to migrate the storyboard UI to swiftUI? How can this be done? Thanks
Posted
by
Post not yet marked as solved
1 Replies
75 Views
In the WWDC22 video "Design an effective chart", at timestamp 1:22, it shows 4 screenshots with different charts. In the 3rd screenshot from left, it shows the most sold pancake as well as sales of all the different types of pancake. The number of pancakes sold is pinned to the end of each bar, like 278, 247, and so on. How do you show the number like that?
Posted
by
Post not yet marked as solved
1 Replies
105 Views
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!
Posted
by
Post not yet marked as solved
0 Replies
83 Views
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
Posted
by
Post not yet marked as solved
0 Replies
25 Views
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
Posted
by
Post not yet marked as solved
0 Replies
28 Views
Hello, we've been working with the Swift Chart framework, and the BarMark chart has a memory problem/bug when using it on the iPhone with iOS 16.0 Developer Beta 3. Problem When using the BarMark chart on the simulator with fake values, the chart load, however if we try to load the data on the iPhone, the code breaks with this error: Thread 1: EXC_BAD_ACCESS (code=2, address=0x16d153ff8). This however does not happen when we change the chart type from BarMark to LineMark. Then all of the sudden, the chart loads on the iPhone. Now, when the error hits, Xcode show that the memory usage is extremely high. Working Code High Memory usage and error
Posted
by
Post not yet marked as solved
0 Replies
17 Views
I'm experiencing a problem where my NavigationLink hangs when on the same view as a somewhat complicated chart. Additional Context: My Swift Chart has a fair number of data points (2000+). I'm also doing custom modification using the .chartYAxis modifier. .chartYAxis { AxisMarks(values: .stride(by: 1)) { value in            AxisGridLine()            AxisTick()            // In my app I also put a value label here, but that doesn't seem to be the thing in particular that slows this down.      } } When I do this modifier along with having a big set of data points, if I have a NavigationLink elsewhere on the same view, it hangs when I press the NavigationLink. Notably, if I remove this modifier, it doesn't hang. (By hang I mean it takes about 1s to navigate to the next view). Here's a slightly contrived example that mirrors my actual app, minus the generation of data points: import SwiftUI import Charts struct ContentView: View {     var body: some View {         NavigationStack {             NavigationLink(value: "NavigationKey") {                 Text("Link")             }             ChartView()             Text("Main View")                 .navigationDestination(for: String.self) { path in                     SomeView()                 }         }     } } struct ChartView: View {     var chartXY: [(x: Double, y: Double)] {         var entries: [(x: Double, y: Double)] = []         for i in 0..<2500             let y = Double.random(in: 0..<8000)             entries.append((x: Double(i), y: y))         }         return entries     }          var body: some View {         List {             Chart {                 ForEach(chartXY, id: \.x) {                     LineMark(x: .value("x", $0.x), y: .value("y", $0.y))                 }             }             .frame(minHeight: 100.0)             // Commenting out this block == no hang             .chartYAxis {                 AxisMarks(values: .stride(by: 1)) { value in                     AxisGridLine()                     AxisTick()                     // In my app I also put a value label here, but that doesn't seem to be the thing in particular that slows this down.                 }             }         }     } } struct SomeView: View {     var body: some View {         Text("hello")     } } Presumably, given commenting out the block solves the problem, SwiftUI is preparing some large amounts of AxisMarks/GridLines/AxisTicks for the view transition. Is there someway for me to indicate that it shouldn't do that to prevent this problem? Or is there some other reason this would be hanging?
Posted
by