Solved

wpf databinding to observable collection not displaying in wpf chart

Posted on 2014-01-07
2
1,469 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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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