Home > Mobile >  How to create a BarChart from fetched data from CoreData?
How to create a BarChart from fetched data from CoreData?

Time:05-31

I'm trying to create a barchart from fetched data from a CoreData model. I'm trying to use https://github.com/AppPear/ChartView in particular the release v1.5.5. The request returns me from the database an array of Entities, let's say its [Entity]. (Entity has two attributes a date and an Int64) I want to use this data to create the barchart with the labels created by like the example on the github readme :

BarChartView(data: ChartData(values: [("2018 Q4",63150), ("2019 Q1",50900), ("2019 Q2",77550), ("2019 Q3",79600), ("2019 Q4",92550)]), title: "Sales", legend: "Quarterly") // legend is optional

In Particular the init of ChartData I wanna use is public init<N: BinaryInteger>(values:[(String,N)])

Now I tried to map (using .map() ) the array that the fetchRequest gives to me but unsuccessfully. How do I transform the data from the array, into the required signature of the init of ChartData ? Or do you have any other suggestion in order to accomplish this task, like some order packages I could use instead of the one mentioned before?

CodePudding user response:

Have you tried ChartData(values: fetchRequestResult.map { ("\($0.date)", $0.int64value) }? Regarding other libs, you should have a look into Charts. It's in more wide known and used by a lot of apps

  • Related