Search code examples
c#wpfdatetimedynamic-data-display

Dynamic Data Display Scattered DateTime Axis


I am trying to chart candlestick markers for a chart of stock prices. I have a marker for every minute of a trading day - 9:30 AM to 4:00 PM. I don't want to display empty space for 4:01 PM to 9:29 AM for each trading day. Is there a way I can remove that time span from my X axis on my graph? I want all the data to look continuous without break in flow. I am using a NumericAxis for my X axis with a TickToDateTimeLabelProvider that I created. Is there a way to change the LabelProvider to fit my needs? Or does it stretch deeper than that?

Here is my code for the LabelProvider:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace Microsoft.Research.DynamicDataDisplay.Charts {
public class TickToDateTimeLabelProvider : NumericLabelProviderBase {
    public override UIElement[] CreateLabels(ITicksInfo<double> ticksInfo) {
        var res = new TextBlock[ticksInfo.Ticks.Length];
        for (int i = 0; i < ticksInfo.Ticks.Length; ++i) {
            long l = (long)ticksInfo.Ticks[i];
            if (l < 0)
                l = 0;
            DateTime dt = new DateTime(l);
            res[i] = new TextBlock();
            res[i].Text = dt.ToString();
        }
        return res;
    }
}
}

Solution

  • Solved this one, I ended up making my X mapping the position that the data was at in the collection, and then making a new label provider to change the positions into the DateTimes I wanted. My new label provider looks like this :

    using System;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.Research.DynamicDataDisplay.Charts.Axes;
    using System.Globalization;
    
    
    namespace Microsoft.Research.DynamicDataDisplay.Charts {
    
    public class TradingLabelProvider : NumericLabelProviderBase {
    
        private List<DateTime> m_Labels;
        public List<DateTime> Labels {
            get { return m_Labels; }
            set { m_Labels = value; }
        }
    
        public TradingLabelProvider(DateTime start, List<DateTime> labels) {                                    
            Labels = labels;                                    
        }
    
        public override UIElement[] CreateLabels(ITicksInfo<double> ticksInfo) {            
    
            var ticks = ticksInfo.Ticks;
            Init(ticks);            
    
            UIElement[] res = new UIElement[ticks.Length];
            LabelTickInfo<double> tickInfo = new LabelTickInfo<double> { Info = ticksInfo.Info };
            for (int i = 0; i < res.Length; i++) {
                tickInfo.Tick = ticks[i];
                tickInfo.Index = i;
                string labelText = "";
    
                if(Convert.ToInt32(tickInfo.Tick) <= Labels.Count && tickInfo.Tick >= 0) {
                    labelText = Labels[Convert.ToInt32(tickInfo.Tick)].ToString();
                }
    
                TextBlock label = (TextBlock)GetResourceFromPool();
                if (label == null) {
                    label = new TextBlock();
                }
    
                label.Text = labelText;
                label.ToolTip = ticks[i].ToString();
    
                res[i] = label;
    
                ApplyCustomView(tickInfo, label);
            }
            return res;
        }
    }
    }
    

    This allowed me to string the days together without a space in between them to meet my requirements. I struggled trying to do this with a DateTimeAxis, with no avail.