Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have following two classes

C#
public class MyDateObject
    {
        private double? _value1;
        private double? _value2;
        private double? _value3;
        private DateTime _date;

        public double? Value1
        {
            get
            {
                return this._value1;
            }
            set
            {
                this._value1 = value;
            }
        }

        public double? Value2
        {
            get
            {
                return this._value2;
            }
            set
            {
                this._value2 = value;
            }
        }

        public double? Value3
        {
            get
            {
                return this._value3;
            }
            set
            {
                this._value3 = value;
            }
        }

        public DateTime Date
        {
            get
            {
                return this._date;
            }
            set
            {
                this._date = value;
            }
        }

        public MyDateObject(double? value1, double? value2, double? value3, DateTime date)
        {
            this._value1 = value1;
            this._value2 = value2;
            this._value3 = value3;
            this._date = date;
        }
    }


C#
public class MainPageViewModel
    {
        DateTime baseDate = DateTime.Today.AddDays(-15);
        public const int min = 0;
        public const int max = 79;
        Random r = new Random();

        private ObservableCollection<MyDateObject> _chartData;
        public ObservableCollection<MyDateObject> ChartData
        {
            get
            {
                return this._chartData;
            }
            set
            {
                this._chartData = value;
            }
        }

        public MainPageViewModel()
        {
            this.ChartData = new ObservableCollection<MyDateObject>();
            for (int i = 0; i < 30; i++)
            {
                ChartData.Add(new MyDateObject(r.Next(min, max), r.Next(min, max), r.Next(min, max), baseDate.AddDays(i)));
                string str = ChartData[i].Date.ToString();
                pivot.str = ChartData[i].Value1;                
            }
        }
    }


Now I want to Pivot ChartData on the basis of day to bind to a grid which looks like the following format

...............1 2 3 4
Value 1 6 9 56 78
Value 2 8 51 7 21
Value 3 79 45 57 25
Posted
Updated 4-Mar-13 23:46pm
v3

1 solution

Theoretically speaking you can just group your data by date (using Linq), use the sum function to fill in Value1, Value2 and Value3 columns (assuming that you have one item per one date it will sum only that one, and in that case it will work the same with Min, Max and Average functions instead of Sum) and just transpose that table and you are done.

So grouping you will get you something like this
        Value1 Value2 Value3
1            6      8     79
2            9     51     45
3           56      7     57
4           78     21     25


and transpose will get you to your goal.
But, where would we be without at least one but. Right. :)

In order to do this kind of thing right, you'll have to use dynamic expressions that will adapt to your data and create dynamic type accordingly (dynamic type will have properties that correspond to date values in your ObservableCollection). Generally this is not a problem in full .net framework 4.0 (fastest way is to use [dynamic linq] - download dynamic linq [here]), but since you are using Silverlight we are talking about very restricted dynamic expression framework (i.e. dynamic linq will not work in Silverlight).

Since you have grouped data I will skip to the fun part. And as it will be too much code to write right now, I will try to describe how I would do that.

You can forget about binding data and instead make your own grid (table) and fill it with right data manually. Create structure to hold all possible dates ordered (and distinct, I assume). Create first row as header row and fill in the date descriptions for columns of your table. Create first column as header column with descriptions Value1, Value2 and Value3. Then iterate over your collection of MyDateObject's objects and for each item find index of Date in structure that holds all dates as that will be the column index of your values in grid.

Happy coding.
 
Share this answer
 
v2
Comments
Zafar248 6-May-14 9:35am    
Had to do the same thing in the end. Thanks bro.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900