将核心数据中的数据整形为可绘图格式 [英] shape data from core data into plottable format

查看:16
本文介绍了将核心数据中的数据整形为可绘图格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于基于核心数据在 SwiftUI 中绘制图表的概念性问题.假设我正在构建一个待办事项列表应用程序,并且我在核心数据中有一个待办事项实体.该实体具有属性名称和完成日期(待办事项被标记为完成的日期).

I have a conceptual question regarding plotting charts in SwiftUI based on data from core data. Lets say I am building a todo list app and I have a todo entity in core data. This entity has the attributes name and finishDate (the date on which the todo was marked as finished).

要绘制这两个变量,我需要一个包含每一天的变量和一个包含每个特定日期完成任务数的变量.

To plot these two variables, I need a variable containing each individual day and a variable containing the number of finished tasks on each specific day.

有谁知道我将如何有效地创建这些数据?我知道我可以获取 todo 实体数据并选择正确的属性.但是我如何获得每一天完成的任务数量?理想情况下不使用 for 循环创建这些变量.

Does anyone know how I would go about creating this data efficiently? I know I can fetch the todo entity data and select the correct attributes. But how do I get the number of finished tasks on each specific day? Ideally without creating those variables using for loops.

如果有人能帮助我,我将不胜感激.

It would be really appreciated if anyone can help me.

推荐答案

这很容易使用 CoreData + SwiftUI.

This is easy using CoreData + SwiftUI.

以下代码适用于 iOS15+,但您可以使用 NSFetchedResultsControllerNSFetchRequest/@FetchRequest 执行相同的操作,然后将其分组.但是要保持实时性需要一些努力.

The code below is for iOS15+ but you can do the same thing with an NSFetchedResultsController or NSFetchRequest / @FetchRequest and then group it. But it will require a bit of effort to stay real time.

此外,以下代码旨在与 CoreData 项目的标准 Apple 代码一起使用.我在 PersistenceController 中唯一改变的是为 timestamp

Also, the code below is meant to work with the standard Apple code for a CoreData project. The only thing I changed in PersistenceController is setting a random day for the timestamp

newItem.timestamp = Date().addingTimeInterval(60*60*24*Double(Int.random(in: -10...10)))

这是一个简单的图表

import SwiftUI

@available(iOS 15.0, *)
struct DayChart: View {
    @SectionedFetchRequest(entity: Item.entity(), sectionIdentifier: .completionDate, sortDescriptors: [NSSortDescriptor(keyPath: Item.timestamp, ascending: true)], predicate: nil, animation: Animation.linear)
    var sections: SectionedFetchResults<String, Item>
    @State var maxCount: Int = 1
    let spacing: CGFloat = 3
    var body: some View {
        VStack{
            GeometryReader{ geo in
                HStack(alignment: .bottom, spacing: spacing){
                    ForEach(sections){section in
                        VStack{
                            Text(section.count.description)
                            Rectangle()
                                .foregroundColor(.blue)
                                .onAppear(){
                                    maxCount = max(maxCount,section.count)
                                    
                                }
                            Text(section.id.description).minimumScaleFactor(0.5)
                            .lineLimit(2)
                        }.frame(width: (geo.size.width/CGFloat(sections.count) - spacing),height: geo.size.height * CGFloat(CGFloat(section.count)/CGFloat(maxCount)))
                    }
                }
            }
        }.padding(.leading, spacing)
    }
}

@available(iOS 15.0, *)
struct DayChart_Previews: PreviewProvider {
    static var previews: some View {
        DayChart().environment(.managedObjectContext, PersistenceController.preview.container.viewContext)
    }
}
extension Item{
    //This is the variable that determines your section/column/completion date
    @objc
    var completionDate: String{
                    
        if self.timestamp != nil{
            let dateFormatter: DateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd
MMM"
            return dateFormatter.string(from: self.timestamp!)
             
        }else{
            return ""
        }
    }
}

这篇关于将核心数据中的数据整形为可绘图格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆