Search code examples
c#observablecollectionskiasharp

Populating MicroCharts From API Get response \ Observable Collection


I have a working API that returns a collection of 'Shifts' (consider these in the context of a timesheet) The data is returned successfully from the API as highlighted in the extract below.:

[{"id":1006,"wI_Date":"2020-05-01T00:00:00","wI_Start_Time":"2020-05-01T08:00:00","wI_End_Time":"2020-05-01T17:00:00","name":"Surgery 1","rate":60.0000,"paid":"N         ","revenue":"540       "},{"id":1007,"wI_Date":"2020-05-04T00:00:00","wI_Start_Time":"2020-05-04T08:00:00","wI_End_Time":"2020-05-04T17:00:00","name":"Medical Surgery 2","rate":45.0000,"paid":"N         ","revenue":"405       "},{"id":1008,"wI_Date":"2020-05-07T00:00:00","wI_Start_Time":"2020-05-07T08:00:00","wI_End_Time":"2020-05-07T17:00:00","name":"TEST Medical Surgery","rate":45.0000,"paid":"N         ","revenue":"405"}]

I have 100s of records in the return that span over the full calendar year (By "wI_Date") and indeed as time goes by and the data set grows will also span over multiple years

Im trying to work out how to group the records by Month\Year so I can add them to a list of Entries for a Micorcharts.BarGraph - without grouping at the backend. It want to then Sum up the "Revenue" values for that month and its that value i want to pass to my chart as the Entry

A shift is defined as:

public class Shift
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public DateTime WiDate { get; set; }
        public DateTime WiStartTime { get; set; }
        public DateTime WiEndTime { get; set; }
        public int Practice { get; set; }
        public string Rate { get; set; }
        public string Paid { get; set; }
        public string Revenue { get; set; }
    }

My Call to the API looks like this:

var content = await _client.GetStringAsync(URL);
var shifts= = JsonConvert.DeserializeObject<List<Shift>>(content);

OShifts = new  ObservableCollection<Shift>(shifts);

So I have a success ObservableCollection.. Im struggling with grouping that by year\date to add to the Entries for a Micorcharts.BarGraph..

to help below is a harded example of how to populate the chart..

private List<Entry> _entries = new List<Entry>
        {
            new Entry(200)
            {
                Color = SKColor.Parse("#3333FF"),
                Label = "TEST LABEL 1",
                ValueLabel = "200",


            },

            new Entry(400)
            {
                Color = SKColor.Parse("#FF141F"),
                Label = "TEST LABEL 2",
                ValueLabel = "400",


            },

            new Entry(300)
            {
                Color = SKColor.Parse("#49FF33"),
                Label = "TEST LABEL 3",
                ValueLabel = "300",


            },

MyBarChart.Chart = new BarChart { Entries = _entries };

Anyone have an ideas or example code..??

Thanks For reading..


Solution

  • you can do so

    var groupedShifts = 
        OShifts.GroupBy(sh => new { sh.WiDate.Year, sh.WiDate.Month },(key,group)=>new {
                Period=`${key.Year}-${key.Month}`
                SumRevenue = group.Sum(sh=>Decimal.Parse(sh.Revenue))
       }).ToList();
    
    

    I assume that your Shift.Revenue is a safe decimal value and can not be something else. Here I return a list of Objects that have each one Period and SumRevenue but you can customize the return as you want.