Home > OS >  Grouping By bool produces wrong result
Grouping By bool produces wrong result

Time:04-01

I am trying to visualize data from database in charts using LiveCharts library. I have managed to get some of them working fine. However I am having hard times with PieCharts. I want to simply display data in two slices. For that matter I have column in DB with name AppIsRunning.

I have made a simple example that is working fine and displaying data in slices as expected:

  List<DataModel> records = new List<DataModel>();

  records.Add(new DataModel { Id = 1, Revenue = 43, Name = "Item 1", AppIsRunning = true });
  records.Add(new DataModel { Id = 2, Revenue = 23, Name = "Item 2", AppIsRunning = true });
  records.Add(new DataModel { Id = 3, Revenue = 13, Name = "Item 3", AppIsRunning = true });
  records.Add(new DataModel { Id = 4, Revenue = 87, Name = "Item 4", AppIsRunning = true });
  records.Add(new DataModel { Id = 5, Revenue = 23, Name = "Item 5", AppIsRunning = true });

  IEnumerable<ISeries> result1 = records.Select(x =>
    new PieSeries<double>
    {
      Values = new List<double> { x.Revenue },
      Name = x.Name,
    });

enter image description here

Now I would like to group data to display only 2 slices AppIsRunning true/false and for Values have Revenue. So I have ended up with this code:

  List<DataModel> records = new List<DataModel>();

  records.Add(new DataModel { Id = 1, Revenue = 43, Name = "Item 1", AppIsRunning = true });
  records.Add(new DataModel { Id = 2, Revenue = 23, Name = "Item 2", AppIsRunning = true });
  records.Add(new DataModel { Id = 3, Revenue = 13, Name = "Item 3", AppIsRunning = false });
  records.Add(new DataModel { Id = 4, Revenue = 87, Name = "Item 4", AppIsRunning = true });
  records.Add(new DataModel { Id = 5, Revenue = 23, Name = "Item 5", AppIsRunning = true });

  IEnumerable<ISeries> result1 = records
        .GroupBy(g => g.AppIsRunning)
        .Select(item => new PieSeries<double>
        {
          Values = item.Select(x => Convert.ToDouble(x.Revenue)),
          Name = item.Key ? "Running" : "Not running",
        });

However this makes AppIsRunning true section divided into 4 sub-slices = 4 AppIsRunning TRUE values. Please see screenshot with 4 sub-sections in blue slice:

enter image description here

My question is how to get rid of these 4 sub-sections and group that data into one? I need only 2 slices, no need to divide slices into sub-sections.

Here is original example from LiveCharts:

  this.ActivityChartSeries = new ISeries[]
  {
    new PieSeries<double> { Values = new double[] { 2 }, Name = "Section 1"},
    new PieSeries<double> { Values = new double[] { 21 }, Name = "Section 2"},
    new PieSeries<double> { Values = new double[] { 28 }, Name = "Section 3"},
    new PieSeries<double> { Values = new double[] { 2 }, Name = "Section 4"},
    new PieSeries<double> { Values = new double[] { 52 }, Name = "Section 5"},
  };

https://github.com/beto-rodriguez/LiveCharts2/blob/master/samples/ViewModelsSamples/Pies/Basic/ViewModel.cs

CodePudding user response:

Judging from the example, you want to see a breakdown of total (sum of) revenue as a single value for each status. To do that, you could do:

IEnumerable<ISeries> result1 = records
    .GroupBy(g => g.AppIsRunning)
    .Select(item => new PieSeries<double>
    {
      Values = new List<double> {item.Sum(x => Convert.ToDouble(x.Revenue))},
      Name = item.Key ? "Running" : "Not running",
    });
  • Related