swift 图表窗格:为什么要向图表中添加额外条目?

xggvc2p6  于 2022-12-22  发布在  Swift
关注(0)|答案(2)|浏览(138)

我有一个饼图,显示添加到它的数据它看起来像这样:

class HomeViewController: UIViewController {

    var pieChart = PieChartView()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        pieChart.delegate = self               
        self.setChart()
    }
    
    func setChart() {
        var entries: [ChartDataEntry] = []
                
        for entry in 0..<(self.cellArr.count) {
            if self.cellArr.count > 0 {
                let dataEntry = PieChartDataEntry(value: Double(self.cellArr[entry].amount), label: String(self.cellArr[entry].type))
                entries.append(dataEntry)
            }
        }
        updateSorting(values: entries)
    }
    
    func updateSorting(values: [ChartDataEntry]){
        let dataSet = PieChartDataSet(entries: values, label: "")
        let data = PieChartData(dataSets: [dataSet])
        
        dataSet.colors = ChartColorTemplates.colorful()
        pieChart.data = data
        pieChart.notifyDataSetChanged()        
    }
}

问题出在entries变量上,它位于setChart函数中。我需要在函数之外使用它,但如果我将它放在一个类中-

class HomeViewController: UIViewController {

    var pieChart = PieChartView()
    var entries: [ChartDataEntry] = [] // <------------ !!!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        pieChart.delegate = self               
        self.setChart()
    }
}

然后图表将开始显示额外值
一个说明性示例:
enter image description hereenter image description here
在第一个屏幕截图中,entries位于函数中,而在第二个屏幕截图中,entries已经位于函数之后
我尝试设置断点并查看for循环,但我仍然不明白为什么每次迭代时数组都会将旧数据添加到新数据中

d8tt03nd

d8tt03nd1#

因为你是从viewWillAppear调用setChart的,所以你会追加数组中的很多项(如果视图在一个会话中出现/消失),你需要适当地清除变量,或者完全覆盖它们。
不相关,但是可以大大简化for循环代码:
开始日期:

func setChart1() {
    var entries: [ChartDataEntry] = []
            
    for entry in 0..<(self.cellArr.count) {
        if self.cellArr.count > 0 {
            let dataEntry = PieChartDataEntry(value: Double(self.cellArr[entry].amount), label: String(self.cellArr[entry].type))
            entries.append(dataEntry)
        }
    }
    updateSorting(values: entries)
}

删除无用的if语句:

func setChart2() {
    var entries: [ChartDataEntry] = []
            
    for entry in 0..<(self.cellArr.count) {
        let dataEntry = PieChartDataEntry(value: Double(self.cellArr[entry].amount), label: String(self.cellArr[entry].type))
        entries.append(dataEntry)
    }
    updateSorting(values: entries)
}

将手动构造的索引范围替换为简单的Array.indices

func setChart3() {
    var entries: [ChartDataEntry] = []
            
    for entry in self.cellArr.indices { // "entry" is a total misnomer. It's not an entry, it's an index that can be used to get an entry
        let dataEntry = PieChartDataEntry(value: Double(self.cellArr[entry].amount), label: String(self.cellArr[entry].type))
        entries.append(dataEntry)
    }
    updateSorting(values: entries)
}

要知道,你甚至不需要指数,你只关心价值观本身:

func setChart4() {
    var entries: [ChartDataEntry] = []
            
    for entry in self.cellArr {
        let dataEntry = PieChartDataEntry(value: Double(entry.amount), label: String(entry.type))
        entries.append(dataEntry)
    }
    updateSorting(values: entries)
}

内联局部变量(其名称不添加任何有用信息):

func setChart5() {
    var entries: [ChartDataEntry] = []
            
    for entry in self.cellArr {
        entries.append(PieChartDataEntry(value: Double(entry.amount), label: String(entry.type)))
    }
    updateSorting(values: entries)
}

请注意,这只是一个map操作:

func setChart6() {
    var entries: [ChartDataEntry] = self.cellArr.map { entry in
        PieChartDataEntry(value: Double(entry.amount), label: String(entry.type)))
    }
    updateSorting(values: entries)
}

entries现在可以是不可变的,并且可以使用类型推断:

func setChart7() {
    let entries = self.cellArr.map { PieChartDataEntry(value: Double($0.amount), label: String($0.type))) }
    updateSorting(values: entries)
}
2lpgd968

2lpgd9682#

我将entries数组放入一个类中,并在setChart7函数中清除它

class HomeViewController: UIViewController {

    var pieChart = PieChartView()

    var entries: [ChartDataEntry] = []

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

    self.setChart()
    }

    func setChart7() {
        entries.removeAll()
        let entries = self.cellArr.map { PieChartDataEntry(value: Double($0.amount), label: String($0.type))) }
        updateSorting(values: entries)
    }
}

相关问题