Bar Graph with Linq

Is there a way to make a segmented bar graph with linq? I saw several examples where you can count the number of events of a particular object. Is it possible to create a linq based historical schema that counts the number of events in a series of objects between two values?

I don’t know how you would group by a number of objects to create the necessary buckets for the histogram? Suppose you use the starting border and width to create a range.

You will need to iterate over an array of numbers and group each number, be it <= Upper bound and> Lower bound. Then you just summarize each group. I do not know how to complete a group in parts

+3
source share
2 answers

Something like that?

        Random randF = new Random();
        List<double> nums = new List<double>();
        for (int i = 0; i < 100000; i++)
        {
            nums.Add(randF.NextDouble()*100);
        }

        double fromXF = 30;
        double toXF = 80;
        int groupCount = 10; // number of groups between values
        var histF = from i in nums
                    let groupKeyF = ((i-fromXF)/(toXF-fromXF)*groupCount) // even distribution of "groupCount" groups between fromXF and toXF, simple math, really
                    where groupKeyF >= 0 && groupKeyF < groupCount // only groups we want
                    let groupKey = (int)groupKeyF // clamp it to only group number
                    group i by groupKey into gr  // group it according to group number
                    orderby gr.Key
                    select new { Value = gr.Key, Count = gr.Count() };

        foreach (var item in histF)
        {
            Console.WriteLine("Group number: " + item.Value + ", Count: " + item.Count);
        }
+3
source

You can do something like:

var groups = input.GroupBy(x => (int)((x.value - start)/width));

which creates an integer value for each column and groups.

+3
source

Source: https://habr.com/ru/post/1783557/


All Articles