Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET Silverlight
Hi,
I have following two classes
 
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;
        }
    }
 
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 5-Mar-13 0:38am
Edited 5-Mar-13 0:46am
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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. Smile | :)
 
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.
  Permalink  
v2
Comments
Zafar248 at 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)

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 9 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100