Sum time based chart data - swiftui

I have a chart which displays data over time (months in my case). The data for this chart is randomly generated by getting any date from now to 500 days in the past (chartDate), and a random number (sales). It generates 500 of these rows.
func dailySales2() -> [(chartDate: Date, year: String, sales: Int)] {
var temp: [(chartDate: Date, year: String, sales: Int)] = []
for _ in 1...500 {
let dateT = Calendar.current.date(byAdding: .day, value: -Int.random(in: 1...500), to: Date())!
temp.append((chartDate: dateT, year: dateT.formatted(.dateTime.year()), sales: Int.random(in: 100...500)))
}
return temp.sorted(by: { $0.chartDate < $1.chartDate })
}
Chart {
ForEach(dailySales2(), id: \.chartDate) { chartD in
BarMark(
x: .value("Day", chartD.chartDate, unit: .month),
y: .value("Sales", chartD.sales)
)
.foregroundStyle(by: .value("Year:", chartD.year))
}
}
The above takes all the rows for each month and totals them, so each bar is a sum of all the sales for each month. If I change this to a LineMark, it displays each row individually, how can I stop this and instead sum all the individual rows into one point for each month? Thanks.
I'm expecting each month column to display a single point which sums all the rows that contains data for that month.

Related

Reuse month columns in SwiftUI Chart

I have a chart which displays data over time (months in my case). The data for this chart is randomly generated by getting any date from now to 500 days in the past (chartDate), and a random number (sales). It generates 500 of these rows.
func dailySales2() -> [(chartDate: Date, year: String, sales: Int)] {
var temp: [(chartDate: Date, year: String, sales: Int)] = []
for _ in 1...500 {
let dateT = Calendar.current.date(byAdding: .day, value: -Int.random(in: 1...500), to: Date())!
temp.append((chartDate: dateT, year: dateT.formatted(.dateTime.year()), sales: Int.random(in: 100...500)))
}
return temp.sorted(by: { $0.chartDate < $1.chartDate })
}
Chart {
ForEach(dailySales2(), id: \.chartDate) { chartD in
BarMark(
x: .value("Day", chartD.chartDate, unit: .month),
y: .value("Sales", chartD.sales)
)
.foregroundStyle(by: .value("Year:", chartD.year))
}
}
.chartXAxis {
AxisMarks(values: .stride(by: .month)) {
AxisValueLabel(format: .dateTime.month(.narrow), centered: true)
}
}
This generates A S O N D J F M A M J J A S O N D columns (August 2021 - December 2021 and then January 2022 - December 2022). How do I go about only having one set of January - December columns and putting both years into these columns (separated by series)? Thanks.
Having 12 columns, one for each month, which any year uses.

Pick conditional values from ForEach in SwiftUI

I have this struct created.
struct MedItem: Identifiable {
let id = UUID() //Generates a unique string for each item listed below
let medThumbnail: String
let medTitle: String
let medDose: String
let medDoseval: String
let purpose: String
let takeAt: String
let refillDate: String
let refillRem: String
}
With this data added:
struct MedData {
static let mListData = [
MedItem(medThumbnail: "Levo", medTitle: "Levothyroxine", medDose: "15", takeAt: "8 am", medDoseval: "mcg", purpose: "Hypothyroidism", refillDate: "December 3, 2021", refillRem: "Refill Remaning: 1"),
] //Imagine more sample data here
I'm trying to sort values based on time range linked to the timeAt value.
ForEach (meds, id: \.id) { MedItem in
HStack(spacing: 10.0){
}
If time is between 2am - 12pm then morning, 12pm - 3pm then noon, 3pm - 6pm evening, and 7pm - 2am then night. Some meds will not have a time and they need to be catoegorized in a different bucket.
Thank you for your help :)

I got the from date and to date from the user. I want to return the Sold Product's price and its count in the basis of Daily sold order. how?

models.Order.objects.filter(purchasedate__range=[from_date, to_date])
today_total = 0
quant = 0
for values in daterangeorder:
today_total += values.price
quant += values.quantity
return response.Response({
"from_date": from_date,
"to_date": to_date,
"Amount Sold": str(today_total),
"Count": str(quant)
})
This is the output:
{
"from_date": "2021-11-19",
"to_date": "2021-11-23",
"Amount Sold": "27000",
"Count": "9"
}
I want like this (day by day sold products count):-
{
date: “'2021-10-20”,
count: 20,
total_price_sold: 5000.00
},
{
date: “'2021-10-21”,
count: 4,
total_price_sold: 300.00
}
Instead of solving your problem with Python code, you can let the database handle it for you using aggregation, see the docs: https://docs.djangoproject.com/en/3.2/topics/db/aggregation/#values
from django.db.models import Sum
result = models.Order.objects.values('purchasedate')
.order_by('purchasedate')
.annotate(total_price_sold=Sum('price'), count=Sum('quantity'))
return response.Response(result)

Flutter Filter List base on Month

How I can categorise list based on Month
let say I have example list like below :
Record(
id: '01',
nameFood: 'ravioli',
date : 2021/may/14,
price: 20
),
Record(
id: '02',
nameFood: 'cheese garlic',
date : 2021/june/02,
price: 30,
),
Record(
id: '03',
nameFood: 'steak',
date : 2021/march/21
price 25,
),
I want to Categorize it to like this
June(
nameFood: steak,
total price: 240,
),
and etc
the problem is I dnt know how to filter each List(month) because each month have different length of days like 30,31 and 28
If you store your dates as a DateTime, you can try using the month number. I'm going on a limb here, assuming you have a List of objects of type Record, where Record is.
class Record {
Record(
this.id,
this.price,
this.date,
this.nameFood,
);
final String id;
final double price;
final DateTime date;
final String nameFood;
}
You can loop through your list and do whatever it is you want to do with it, like so.
for (final record in _listOfRecords) {
final _monthOfRecord = record.date.month; // Gives the month number. January = 1, April = 4, etc.
// Do other stuff, based on the current monthNumber
switch (_monthOfRecord) {
case 1:
// Do something
break;
....
default:
break;
}
}

How to print the sum of types in a record in F#?

I have the below record for how many items were sold at a shop:
type ShopEarnings = { Item: string; Price: int; Quantity: int}
let pencils = { Item= "Pencils"; Price = 2; Quantity = 500 }
let pens = { Item= "Pens"; Price = 5; Quantity = 200 }
let labels = { Item= "Labels"; Price = 3; Quantity = 300 }
let rulers = { Item= "Rulers"; Price = 5; Quantity = 100 }
I want to calculate the total amount of items sold so I can print something like:
printfn "%i items were sold" totalItemsSold
and it will output
1100 items were sold
Also, I should not need to enter the items for addition manually in case there are 100 items.
I tried different approaches like List.sum but it would not work.
What would be the best way to calculate the total sum of "Quantity"?
List.sumBy will do it:
let totalItemsSold =
[pencils; pens; labels; rulers]
|> List.sumBy (fun earnings -> earnings.Quantity)
printfn "%i items were sold" totalItemsSold