-1

I am using Charts in SwiftUI via: https://medium.com/@zzzzbh/plotting-in-ios-using-charts-framework-with-swiftui-222034a2bea6

I just want to do a ShareSheet of specific parts of the screen.

The chart is displayed like this.

Bar(entries: [
      
        BarChartDataEntry(x: 1, y: 1),
        BarChartDataEntry(x: 2, y: 1),
        BarChartDataEntry(x: 3, y: 1),
        BarChartDataEntry(x: 4, y: 1),
        BarChartDataEntry(x: 5, y: 1)

    ])

So how can I change to an image so I can share them? I do not want to share the whole page - just each chart.

Thank you.

diogenes
  • 1,865
  • 3
  • 24
  • 51

1 Answers1

0

I got this to work. The ShareSheet is the standard stuff. the Chart comes from the site mentioned above.

this was very helpful: https://www.youtube.com/watch?v=lbyRsGCvm-Q

Warning; if in a scrollview there are issues.

        import SwiftUI
        import Charts
        
        struct ShareSheetView: View {
            
            @State private var showShareSheet = false
            
            @State var rect:CGRect = .zero
            @State var uiImage:UIImage? = nil
            
            @State var items : [Any] = []
            
            var body: some View {
                
                VStack(spacing: 20) {
                     
                    Button(action: {
                        
                        self.showShareSheet = true
                        
                        items.removeAll()
                        
                        self.uiImage = UIApplication.shared.windows[0].rootViewController?.view!.setImage(rect: self.rect)
                        
                        items.append(self.uiImage as Any)
                        
                        
                    }) {
                        Text("Share Chart").padding()
                    }
                    
                    ChartView()
                        .background(RectSettings(rect: $rect))
                        .frame(width: 300, height: 300 )
                    
                    
                } .sheet(isPresented: $showShareSheet) {
                    ShareSheet(activityItems: items )
                }
                
            }
            
        }
        
        
        struct ShareSheetView_Previews: PreviewProvider {
            static var previews: some View {
                ShareSheetView()
            }
        }
        
        struct ChartView: View {
            var body: some View {
                VStack {
                    
                    ReturnBarChart(entries: [
                        
                        BarChartDataEntry(x: 1, y: 1),
                        BarChartDataEntry(x: 2, y: 2),
                        BarChartDataEntry(x: 3, y: 3),
                        BarChartDataEntry(x: 4, y: 5),
                        BarChartDataEntry(x: 5, y: -2)
                        
                    ])
                    
                }
            }
        }
        
        struct RectSettings: View {
            
            @Binding var rect: CGRect
            
            var body: some View {
                
                GeometryReader { geo in
                    self.setView(proxy: geo)
                    
                }
                
            }
            
            func setView(proxy: GeometryProxy) -> some View {
                
                DispatchQueue.main.async {
                    
                    self.rect = proxy.frame(in: .global)
                }
                return Rectangle().fill(Color.clear)
                
            }
            
        }
        
        extension UIView {
            
            func setImage(rect: CGRect) -> UIImage {
                
                let renderer = UIGraphicsImageRenderer(bounds: rect)
                return renderer.image {  rendererContext in
                    layer.render(in: rendererContext.cgContext)
                }
                
            }
            
        }
        
diogenes
  • 1,865
  • 3
  • 24
  • 51