Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

wpf databinding to observable collection not displaying in wpf chart

Posted on 2014-01-07
2
Medium Priority
?
1,676 Views
Last Modified: 2014-01-08
Stuck on something that must be very basic.
I am databinding a wpf line chart to an observable collection.

in my custom class looks like

    public class InternalLoginClass
    {
        public MainWindow mainwindow;
        public MyControls.DataGrid DataGridBreakdown;
        public MyControls.DataGrid DataGridPerformance;
        public MyCharting.Chart ChartManagerBarPerc;
        public MyCharting.Chart ChartSectorTotalsBarchart;
        public MyCharting.Chart ChartTimeLineIndividual;
        public MyCharting.Chart ChartTimeLineTotals;
        public MyControls.TextBox textboxTotalPortfolioPL;

        public ObservableCollection<List<KeyValuePair<DateTime, double>>> timeLineDataSource { get; set; }


        public CurrentSelectedObject currentObject;

        BackgroundWorker backgroundWorkerTimeline;
        BackgroundWorker backgroundWorkerMainGrid;
        BackgroundWorker backgroundWorkerDatagridBreakdown;
        BackgroundWorker backgroundWorkerSectorBarchart;

  public InternalLoginClass()
        {

            //create background workers
                backgroundWorkerTimeline = new BackgroundWorker();
                backgroundWorkerTimeline.DoWork += backgroundWorkerTimeline_DoWork;

                timeLineDataSource = new ObservableCollection<List<KeyValuePair<DateTime, double>>>();

                 mainwindow = System.Windows.Application.Current.MainWindow as MainWindow;
                 currentObject = mainwindow.currentObject as CurrentSelectedObject;

                 ChartTimeLineIndividual = mainwindow.FindName("ChartTimeLineSectorTotals") as MyCharting.Chart;
                 ChartTimeLineIndividual.DataContext = timeLineDataSource;
}

Open in new window


then in a background thread I run this

                if (timeLineDataSource.Count != 11)
                {
                    //System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    //{
                        timeLineDataSource.Add(new List<KeyValuePair<DateTime, double>>());
                        timeLineDataSource.Add(new List<KeyValuePair<DateTime, double>>());
                        timeLineDataSource.Add(new List<KeyValuePair<DateTime, double>>());
                        timeLineDataSource.Add(new List<KeyValuePair<DateTime, double>>());
                        timeLineDataSource.Add(new List<KeyValuePair<DateTime, double>>());
                        timeLineDataSource.Add(new List<KeyValuePair<DateTime, double>>());
                        timeLineDataSource.Add(new List<KeyValuePair<DateTime, double>>());
                        timeLineDataSource.Add(new List<KeyValuePair<DateTime, double>>());
                        timeLineDataSource.Add(new List<KeyValuePair<DateTime, double>>());
                        timeLineDataSource.Add(new List<KeyValuePair<DateTime, double>>());
                        timeLineDataSource.Add(new List<KeyValuePair<DateTime, double>>());
                    //}));

                }

                                using (SqlDataReader reader = command.ExecuteReader())
                                {
                                    while (reader.Read())
                                    {
                                        DateTime sqlTimestamp = reader.GetDateTime(0);
                                        decimal sqlPL = reader.GetDecimal(1);
                                        double sqlPLdouble = Convert.ToDouble(sqlPL);
                                        string sqlSec = reader.GetString(2);

                                            var targetDatasourceItem = timeLineDataSource[datasourceIndex];
                                            KeyValuePair<System.DateTime, double> curKVP = new KeyValuePair<DateTime, double>(sqlTimestamp, sqlPLdouble);
                                            
                                            if (!targetDatasourceItem.Contains(curKVP))
                                            {
                                                //System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                                                //{
                                                    timeLineDataSource[datasourceIndex].Add(curKVP);
                                                //}));
                                            }

                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            System.Windows.MessageBox.Show("error in CreateTimelineLineChart " + e);
                        }

Open in new window


basically I run a sql query, and for each row returned, I check to see if already in my observable collection, if not, then I add to collection.

This works just fine and each item is added to appropriate index of observable collection.
The problem is my graph does not show any data.

I am databinding like so

                    <chartingToolkit:Chart x:Name="ChartTimeLineSectorTotals"
						Margin="0,0,0,0"
						xml:lang="en-US"
                        Grid.Row="3" 
                        Grid.RowSpan="3"
                        RenderOptions.BitmapScalingMode="NearestNeighbor"
                        MouseLeftButtonUp="ChartTimeLineSectorTotals_MouseLeftButtonUp"
                        IsHitTestVisible="True">
                        <!--DataContext="{Binding Source={StaticResource ResourceKey=MyData}, Path=timeLineDataSource}"-->

                        <chartingToolkit:LineSeries Name="Energy"
                            Title="Energy"
                            ItemsSource="{Binding [0]}"
                            DependentValuePath="Value"
                            IndependentValuePath="Key"
                            IsSelectionEnabled="True" 
                            SelectionChanged="SectorTimeLineChart_SelectionChanged"
                            IsHitTestVisible="True"
                            PolylineStyle="{StaticResource DashedPolyLine}">

                            <chartingToolkit:LineSeries.DataPointStyle>
                                <Style TargetType="{x:Type chartingToolkit:LineDataPoint}">
                                <Setter Property="Background" Value="Yellow"/>
                                <Setter Property="Height" Value="Auto" />
                                <Setter Property="Width" Value="Auto"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type chartingToolkit:LineDataPoint}">
                                            <Grid x:Name="Root">
                                                    <TextBlock 
                                                        Name="Energy"
                                                        Foreground="Yellow" 
                                                        Background="Transparent"
                                                        FontSize="10"
                                                        Margin="0,0,50,0"
                                                        MouseEnter="TextBlock_MouseEnter"
                                                        MouseLeave="TextBlock_MouseLeave">
                                                            <TextBlock.Text>
                                                                <MultiBinding Converter="{StaticResource ResourceKey=timelineClassFormatter}"
                                                                              ConverterParameter="LABEL">
                                                                    <Binding Path="."/>
                                                                    <Binding Source="Energy"/>
                                                                <!--<Binding Source="{StaticResource ResourceKey=MyData}" Path="timeLineDataSource"/>-->
                                                            </MultiBinding>
                                                            </TextBlock.Text>
                                                </TextBlock>
                                            </Grid>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                                </Style>
                            </chartingToolkit:LineSeries.DataPointStyle>
                        </chartingToolkit:LineSeries>    

Open in new window


any ideas why nothing is being displayed in my graph?
I also checked my multivalueconverter and it never fires when I add one keyvaluepair of N index to my observable collection.

I get the feeling if I ADD a new list to the observable collection, everything works, but if I add a KeyValuePair to N item of a list, in the OC, then the converter never fires.

any help would be greatly appreciated.!
0
Comment
Question by:solarissf
2 Comments
 
LVL 13

Accepted Solution

by:
Naman Goel earned 2000 total points
ID: 39765471
No charting tool can't take datasource as ObservableCollection<List<KeyValuePair<DateTime, double>>> type of object

even this should be ok ObservableCollection<KeyValuePair<DateTime, double>>

Adding a List inside ObservableCollection is creating problem for you

best approach is  you should have a custom collection.


namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        TimeLineCollection c = new TimeLineCollection();
        public Window1()
        {
            InitializeComponent();
            chartValues.ItemsSource = c;

            
        }
    }

   
    public class TimeLineCollection :ObservableCollection<TimeLine> // Extending from System.Collections.ObjectModel.Collection class
    {
        public TimeLineCollection() // Constructor to add TimeLine objects to the TimeLineCollection
        {
           
        }
    }

    public class TimeLine
    {
        public TimeLine(DateTime time, double value)    // Constructor
        {
            this.Time = time;
            this.Value = value;
        }
        public string Time                // Time Property
        {
            get;
            set;
        }
        public long Value                // Value Property
        {
            get;
            set;
        }
    }
}

Open in new window

0
 

Author Comment

by:solarissf
ID: 39765824
thank you!  that was the issue... the fact that I had an OC on a list.
Soon as I removed the list I could add individual points.

thanks again!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question