problem with updating time(HH:mm:ss)

Feb 26, 2009 at 4:48 AM
Hello,

I try to build an application which can display real time financial information (the price could be updated several times per second).
However when I build the following code, my chart keeps blocking on the day. I think that the problem comes from this line of code:

source1.SetXMapping(ci => TimeSpan.FromTicks(ci.Date.Ticks).Days);

How can I solve my problem?

Thanks,
Kamel


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using System.Globalization;
using System.Threading;
using System.Windows.Threading;

namespace WpfApplication38
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private ObservableDataSource<DateTime> source1;
        private ObservableDataSource<Double> source2;
        private string fileName;

        public Window1()
        {
            InitializeComponent();
            source1 = new ObservableDataSource<DateTime>();
            source2 = new ObservableDataSource<double>();
            Loaded += new RoutedEventHandler(Window1_Loaded);
            fileName = "su.txt";
        }

        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                List<DateTime> lOfdt = new List<DateTime>();
                List<Double> lOPx = new List<double>();
                
                // Create source
                source1 = new ObservableDataSource<DateTime>(lOfdt);
                source2 = new ObservableDataSource<double>(lOPx);
                
                // Set identity mapping of point in collection to point on plot
                source1.SetXMapping(ci => TimeSpan.FromTicks(ci.Date.Ticks).Days);
                source2.SetYMapping(y => y);
                
                // Add graph.
                plotter.AddLineGraph(new CompositeDataSource(source1, source2),
                    new Pen(Brushes.Magenta, 3),
                    new PenDescription("Sin(x + phase)"));

                dateAxis.ConvertToDouble = dt => TimeSpan.FromTicks(dt.Ticks).TotalDays;
                dateAxis.ConvertFromDouble = d => new DateTime(TimeSpan.FromDays(d).Ticks);

                // Force everyting to fit in view
                plotter.Viewport.FitToView();

                // Start computation process in second thread
                Thread simThread = new Thread(new ThreadStart(Simulation));
                simThread.IsBackground = true;
                simThread.Start();
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.ToString());
            }

        }


        private void Refresh(Trade ci)
        {
                Double price = ci.Price;
                DateTime time = ci.Date;
                source1.AppendAsync(Dispatcher, time);
                source2.AppendAsync(Dispatcher, price);
                
           
        }

        private void Simulation()
        {
                string[] strings = File.ReadAllLines(fileName);
                for (int i = 1; i < strings.Length; i++)
                {
                    string line = strings[i];
                    string[] subLines = line.Split('\t');

                    String symbol = subLines[0].Trim();
                    DateTime date = DateTime.Parse(subLines[1]);
                    Double price = Double.Parse(subLines[2], CultureInfo.InvariantCulture);
                    Double qty = Double.Parse(subLines[3], CultureInfo.InvariantCulture);

                    Trade newTrade = new Trade(symbol, date, price, qty);

                    Refresh(newTrade);

                    Thread.Sleep(1); // Long-long time for computations...
                }
        }
    }


    public class Trade
    {
        private DateTime int_Date;
        private Double int_Price;
        private Double int_Qty;
        private string int_Symbol;

        public DateTime Date { get { return int_Date; } }
        public Double Price { get { return int_Price; } }
        public Double Qty { get { return int_Qty; } }
        public string Symbol { get { return int_Symbol; } }

        public Trade(string Symbol, DateTime Date, Double Price, Double Qty)
        {
            int_Date = Date;
            int_Price = Price;
            int_Qty = Qty;
            int_Symbol = Symbol;
        }
    }
}



Xaml Code:

<Window x:Class="WpfApplication38.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" xmlns:my="http://research.microsoft.com/DynamicDataDisplay/1.0">
    <Grid>
        <my:ChartPlotter Margin="12,12,12,0" Name="plotter">
            <my:ChartPlotter.HorizontalAxis>
                <my:HorizontalDateTimeAxis Name="dateAxis"/>
            </my:ChartPlotter.HorizontalAxis>
        </my:ChartPlotter>
    </Grid>
</Window>



An extract from the text file "SU.txt":


SU                                  5/1/2008 12:05:51    108.18    100
SU                                  5/1/2008 12:05:51    108.14    100
SU                                  5/1/2008 12:05:52    108.16    200
SU                                  5/1/2008 12:05:52    108.25    500
SU                                  5/1/2008 12:05:52    108.25    100
SU                                  5/1/2008 12:05:52    108.27    2
SU                                  5/1/2008 12:05:55    108.25    400
SU                                  5/1/2008 12:05:55    108.25    200
SU                                  5/1/2008 12:06:01    108.21    100
SU                                  5/1/2008 12:06:03    108.16    500
SU                                  5/1/2008 12:06:07    108.16    100
SU                                  5/1/2008 12:06:07    108.16    100
SU                                  5/1/2008 12:06:07    108.16    100
SU                                  5/1/2008 12:06:13    108.14    500
SU                                  5/1/2008 12:06:14    108.14    100
SU                                  5/1/2008 12:06:14    108.14    100
SU                                  5/1/2008 12:06:16    108.14    200
SU                                  5/1/2008 12:06:16    108.14    200
SU                                  5/1/2008 12:06:17    108.13    100
SU                                  5/1/2008 12:06:17    108.13    200
SU                                  5/1/2008 12:06:17    108.12    200
SU                                  5/1/2008 12:06:20    108.07    100
SU                                  5/1/2008 12:06:20    108.07    100
SU                                  5/1/2008 12:06:21    108    100
SU                                  5/1/2008 12:06:22    108    100
SU                                  5/1/2008 12:06:22    108.06    100
SU                                  5/1/2008 12:06:22    108.06    100
SU                                  5/1/2008 12:06:22    108.06    70
SU                                  5/1/2008 12:06:22    108.06    200
SU                                  5/1/2008 12:06:23    108    100
SU                                  5/1/2008 12:06:39    108.1    100
SU                                  5/1/2008 12:06:40    108.11    100
SU                                  5/1/2008 12:06:41    108.1    100
SU                                  5/1/2008 12:06:42    108.11    100
SU                                  5/1/2008 12:06:44    108.1    300
SU                                  5/1/2008 12:06:46    108.1    200
SU                                  5/1/2008 12:06:46    108.1    500
SU                                  5/1/2008 12:06:46    108.1    100
SU                                  5/1/2008 12:06:48    108.1    200
SU                                  5/1/2008 12:06:52    108.11    300
SU                                  5/1/2008 12:06:52    108.11    500
SU                                  5/1/2008 12:06:52    108.11    500
SU                                  5/1/2008 12:06:52    108.11    100
SU                                  5/1/2008 12:06:52    108.12    500
SU                                  5/1/2008 12:06:56    108.12    500
SU                                  5/1/2008 12:06:56    108.11    600
SU                                  5/1/2008 12:06:56    108.11    100
SU                                  5/1/2008 12:06:56    108.11    300
SU                                  5/1/2008 12:06:56    108.11    100
SU                                  5/1/2008 12:06:56    108.11    200
SU                                  5/1/2008 12:06:56    108.1    200
SU                                  5/1/2008 12:06:59    108.11    100
SU                                  5/1/2008 12:06:59    108.1    100
SU                                  5/1/2008 12:06:59    108.1    100
SU                                  5/1/2008 12:06:59    108.11    100
SU                                  5/1/2008 12:06:59    108.1    400
SU                                  5/1/2008 12:07:02    108.1    100
SU                                  5/1/2008 12:07:04    108.1    100
SU                                  5/1/2008 12:07:04    108.1    100
SU                                  5/1/2008 12:07:04    108.1    100
SU                                  5/1/2008 12:07:04    108.1    100
SU                                  5/1/2008 12:07:04    108.1    100
SU                                  5/1/2008 12:07:04    108.1    100
SU                                  5/1/2008 12:07:05    108.1    100
SU                                  5/1/2008 12:07:05    108.1    100
SU                                  5/1/2008 12:07:05    108.1    100
SU                                  5/1/2008 12:07:05    108.1    100
SU                                  5/1/2008 12:07:05    108.1    100
SU                                  5/1/2008 12:07:05    108.1    200
SU                                  5/1/2008 12:07:05    108.1    100
SU                                  5/1/2008 12:07:06    108.1    700
SU                                  5/1/2008 12:07:12    108.13    400
SU                                  5/1/2008 12:07:15    108.12    100
SU                                  5/1/2008 12:07:15    108.1    200
SU                                  5/1/2008 12:07:32    108.1    200
SU                                  5/1/2008 12:07:32    108.1    100
SU                                  5/1/2008 12:07:32    108.1    200
SU                                  5/1/2008 12:07:32    108.1    100
SU                                  5/1/2008 12:07:32    108.1    100
SU                                  5/1/2008 12:07:32    108.1    200
SU                                  5/1/2008 12:07:32    108.1    100



Mar 1, 2009 at 10:28 PM
Any help, please?
Editor
Mar 2, 2009 at 5:18 PM
Hi!

Property TimeSpan.Days returns integer value, and it seems to me that this is the root of problem. You probably should use TimeSpan.TotalDays property - it returns double value of days and parts of days. We've made similar error when developing DateTime and TimeSpan axes.

Mikhail.