简体   繁体   English

需要条形图以适应 Section()

[英]Need Bar Chart to fit inside Section()

New to SwiftUI. I have a simple form with two Section(). SwiftUI 的新手。我有一个包含两个 Section() 的简单表单。 In one of the section, I have put BarChartView() from SwiftUICharts() as shown below.在其中一个部分中,我已将 SwiftUICharts() 中的 BarChartView() 放入,如下所示。

The Bar chart does not fit in the section, needed help here to fit the bar chart properly inside Section().条形图不适合该部分,需要帮助才能在 Section() 中正确适合条形图。 I have attached screenshot of how UI look in the simulator.我附上了 UI 在模拟器中的外观截图。

My main goal is to get Scroll view behaviour for entire data on this screen, so I put Bar Chart inside the form.我的主要目标是在此屏幕上获得整个数据的滚动视图行为,因此我将条形图放在表单中。

I cannot put Bar chart outside the form, because then only the form scrolls, not the Bar chart.我不能将条形图放在表格之外,因为这样只有表格滚动,条形图不会滚动。 ( I am aware that Putting entire form inside ScrollView() does not work well ). 我知道将整个表单放入 ScrollView() 中效果不佳)。

在此处输入图像描述

import SwiftUICharts
import SwiftUI

struct TestView: View {
    @State var pickerSelectedItem = 0
    
    var body: some View {
        Form{
            Section(){
                VStack{
                    Picker(selection: $pickerSelectedItem, label: Text("")) {
                        Text("Pass").tag(0)
                    }
                    .pickerStyle(SegmentedPickerStyle())
                    .padding(.horizontal, 24)
                    
                    if (pickerSelectedItem == 0) {
                        BarChartView(data: ChartData(values: [("12 Feb 2021",10.22), ("13 Feb 2021",20.44), ("14 Feb 2021",25.34), ("15 Feb 2021",30.56), ("16 Feb 2021",40),("17 Feb 2021",50), ("18 Feb 2021",60), ("19 Feb 2021",65), ("20 Feb 2021",70), ("21 Feb 2021",80),("22 Feb 2021",80), ("23 Feb 2021",90), ("24 Feb 2021",91), ("25 Feb 2021",90.99), ("26 Feb 2021",92.86)]), title: "Pass %", legend: "Dates",
                                     style: Styles.barChartStyleNeonBlueLight, form: ChartForm.extraLarge,
                                     cornerImage:Image(systemName: "face.smiling.fill"),valueSpecifier: "%.2f")
                    }
                }
                .padding()
            }
            
            Section(header: Text("12 Dec 2021")) {
                DisclosureGroup(
                    content: {Text("test status")
                            .font(Font.custom("Avenir", size: 15))
                    },
                    label: {Text("Updates:")
                        .font(Font.custom("Avenir Heavy", size: 16))}
                )
            }
        }
        .accentColor(.black)
        .navigationBarTitle("Status")
    }
}


If I put the BarChartView() outside the form, then I can see that the chart fits properly, but doing so i lose the scroll for the chart and only the Form below it scrolls.如果我将 BarChartView() 放在表单之外,那么我可以看到图表适合,但这样做我失去了图表的滚动,只有它下面的表单滚动。

Code for this piece is below with the screenshot.这篇文章的代码在屏幕截图下方。

在此处输入图像描述

import SwiftUICharts
import SwiftUI

struct TestView: View {
@State var pickerSelectedItem = 0
    
    var body: some View {
                        VStack{
                            Picker(selection: $pickerSelectedItem, label: Text("")) {
                                Text("Pass").tag(0)
                            }
                            .pickerStyle(SegmentedPickerStyle())
                            .padding(.horizontal, 24)
        
                            if (pickerSelectedItem == 0) {
                                BarChartView(data: ChartData(values: [("12 Feb 2021",10.22), ("13 Feb 2021",20.44), ("14 Feb 2021",25.34), ("15 Feb 2021",30.56), ("16 Feb 2021",40),("17 Feb 2021",50), ("18 Feb 2021",60), ("19 Feb 2021",65), ("20 Feb 2021",70), ("21 Feb 2021",80),("22 Feb 2021",80), ("23 Feb 2021",90), ("24 Feb 2021",91), ("25 Feb 2021",90.99), ("26 Feb 2021",92.86)]), title: "Pass %", legend: "Dates",
                                             style: Styles.barChartStyleNeonBlueLight, form: ChartForm.extraLarge,
                                             cornerImage:Image(systemName: "face.smiling.fill"),valueSpecifier: "%.2f")
                            }
                        }
                        .padding()
        
        Form{
            Section(header: Text("12 Dec 2021")) {
                DisclosureGroup(
                    content: {Text("test status")
                            .font(Font.custom("Avenir", size: 15))
                    },
                    label: {Text("Updates:")
                        .font(Font.custom("Avenir Heavy", size: 16))}
                )
            }
        }
        .accentColor(.black)
        .navigationBarTitle("Status")
    }
}

It is obvious there there is no enough space for BarChartView , so很明显BarChartView没有足够的空间,所以

1st: Try to remove extra padding - Form has enough inset by itself 1st:尝试删除额外的填充 - Form本身有足够的插图

VStack{
  // content here
}
//.padding()        // << this one !!

2nd: Give all available space for bars, like第二:为酒吧提供所有可用空间,例如

BarChartView(data: ChartData(values: [("12 Feb 2021",10.22), ("13 Feb 2021",20.44), ("14 Feb 2021",25.34), ("15 Feb 2021",30.56), ("16 Feb 2021",40),("17 Feb 2021",50), ("18 Feb 2021",60), ("19 Feb 2021",65), ("20 Feb 2021",70), ("21 Feb 2021",80),("22 Feb 2021",80), ("23 Feb 2021",90), ("24 Feb 2021",91), ("25 Feb 2021",90.99), ("26 Feb 2021",92.86)]), title: "Pass %", legend: "Dates",
             style: Styles.barChartStyleNeonBlueLight, form: ChartForm.extraLarge,
             cornerImage:Image(systemName: "face.smiling.fill"),valueSpecifier: "%.2f")
 .frame(maxWidth: .infinity, alignment: .leading)   // here !!

3d: Maybe optional maybe can be combine with 2nd (but also might affect other content, so require additional alignments in-place) - set Form inset to zero, like 3d: Maybe optional maybe can be combined with 2nd (but also might affect other content, so require additional alignments in-place) - 将 Form inset 设置为零,比如

VStack{
  // content here
}
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)) // << here !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM