0

我找到的答案是一级结构。在我的代码中,我想找出结构的结构内部有多少相同的日期。我以前从未使用过 equatable,所以我不确定如何在 struct 中使用 struct 来实现它。

我正在使用 Charts pod,并且该数组chartMetricItemSets包含多天的不同读数。例如:["name": sugar, [["date": "Oct 10", "value": 120], ["date": "Oct 10", "value": 121], ["date": "Oct 11", "value": 120], ["date": "Oct 13", "value": 120], ["date": "Oct 13", "value": 121]]。将xAxisDates["Oct 10", "Oct 11", "Oct 13"]

x 是日期,y 是相同日期的总和

目标:chartDataEntries 应该附加[x:0(10/10), y:241], [x:1(10/11), y:120], [x:2(10/13), y: 241]

谢谢!

struct ChartData {
   let name: String
   let value: [ChartValue]
}

struct ChartValue {
    let date: Date
    let value: Double
 }

var chartMetricItemSets = [[ChartData]]()
var xAxisDates = [String]() //date array

func getChartDataSets() {
    for (index, chartHealthItemSet) in chartMetricItemSets.enumerated() {
        let dataSourceSelected = dataSourcesSelected[index]
        var chartDataEntries = [ChartDataEntry]() //array display on graph
        var chartYaxis: Double = 0
        var datePoint: Int = 0
        var countChartYaxis = 0
        var duplicateDateCount = 0

        for (index, item) in chartHealthItemSet[0].value.enumerated() {
            for (dateIndex, date) in xAxisDates.enumerated() {
                if item.date.convertDateToString(dateFormat: .monthDay) == date {
                    chartYaxis += item.value
                    datePoint = dateIndex
                    chartYaxis += 1
                    break
                }

                //missing counting duplicateDateCount 

                if duplicateDateCount == countChartYaxis {
                    chartDataEntries.append(ChartDataEntry(x: Double(datePoint), y: chartYaxis.roundUpNDecimal(n: 2)))
                }
            }
            print("chartYaxis: \(chartYaxis)")
            print("chartDataEntries: \(chartDataEntries)")
        }
        chartDataEntries.sort(by: { $0.x < $1.x })

        let set = LineChartDataSet(entries: chartDataEntries,
                                   label: HEALTH_TABLE.allCases[dataSourceSelected].info.name)

        set.mode = .linear // cubic line effect (smoother)
        set.colors = [HEALTH_TABLE.allCases[dataSourceSelected].info.chartColor]
        set.setCircleColor(.black)
        set.lineWidth = 1.5
        set.circleRadius = 3
        set.drawCircleHoleEnabled = false
        set.drawCirclesEnabled = true
        set.drawHorizontalHighlightIndicatorEnabled = true

        lineChartData?.addDataSet(set)
    }

    chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xAxisDates)
    chartView.xAxis.granularity = 1
    chartView.xAxis.axisMinLabels = 4
    chartView.xAxis.axisMaxLabels = 6

    DispatchQueue.main.async {
        self.chartView.data = self.lineChartData
    }
}
4

1 回答 1

1

在 Swift 4 中,引入了一个有用的 API,可以通过任意谓词将数组分组到字典中。

你可以分组chartHealthItemSet[0]

let grouped = Dictionary(grouping: chartHealthItemSet[0], by: {$0.date.convertDateToString(dateFormat: .monthDay)}

结果是["10/10":[ChartData], "10/11":[ChartData], ...]

对键进行排序并将它们分配给xAxisDates

xAxisDates = grouped.keys.sorted()

要获取值的总和,请从日期键的分组字典中获取数组,将其映射到值并将它们相加

for (index, date) in xAxisDates.enumerated() {
    let sum = grouped[date]!.map{$0.value}.reduce{0.0, +}
    chartDataEntries.append(ChartDataEntry(x: Double(index), y: sum.roundUpNDecimal(n: 2)))
}
于 2019-12-15T07:43:37.877 回答