diff --git a/samples/LineGraphSample/MainWindow.xaml.cs b/samples/LineGraphSample/MainWindow.xaml.cs index 9e05c7b..d135e48 100644 --- a/samples/LineGraphSample/MainWindow.xaml.cs +++ b/samples/LineGraphSample/MainWindow.xaml.cs @@ -19,9 +19,13 @@ public MainWindow() { InitializeComponent(); + var dates = new double[200]; double[] x = new double[200]; for (int i = 0; i < x.Length; i++) + { x[i] = 3.1415 * i / (x.Length - 1); + dates[i] = DateTime.Now.AddDays(i).Ticks; + } for (int i = 0; i < 25; i++) { @@ -30,8 +34,9 @@ public MainWindow() lg.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, (byte)(i * 10), 0)); lg.Description = String.Format("Data series {0}", i + 1); lg.StrokeThickness = 2; - lg.Plot(x, x.Select(v => Math.Sin(v + i / 10.0)).ToArray()); + lg.Plot(dates, x.Select(v => Math.Sin(v + i / 10.0)).ToArray()); } + plotter.XLabelProvider = new DateLabelProvider(); } } diff --git a/src/Axes/Axis.cs b/src/Axes/Axis.cs index a262a32..7eed9c2 100644 --- a/src/Axes/Axis.cs +++ b/src/Axes/Axis.cs @@ -169,6 +169,28 @@ public DataTransform DataTransform } })); + + [Description("Label format of Axis")] + [Category("InteractiveDataDisplay")] + public ILabelProvider LabelProvider + { + get { return (ILabelProvider)GetValue(LabelProviderProperty); } + set { SetValue(LabelProviderProperty, value); } + } + /// + /// Gets or Sets label format of axis + /// + public static readonly DependencyProperty LabelProviderProperty = + DependencyProperty.Register("LabelProvider", typeof(ILabelProvider), typeof(Axis), new PropertyMetadata(new LabelProvider(), + (o, e) => + { + Axis axis = (Axis)o; + if (axis != null) { + axis.labelProvider = e.NewValue as ILabelProvider; + axis.InvalidateMeasure(); + } + })); + /// /// Gets or sets the brush for labels and ticks of axis /// diff --git a/src/Axes/DateLabelProvider.cs b/src/Axes/DateLabelProvider.cs new file mode 100644 index 0000000..1d1cd86 --- /dev/null +++ b/src/Axes/DateLabelProvider.cs @@ -0,0 +1,42 @@ +// Copyright © Microsoft Corporation. All Rights Reserved. +// Licensed under the MIT License. + +using System; +using System.Windows; +using System.Windows.Controls; +using System.Collections.Generic; +using System.Globalization; + +namespace InteractiveDataDisplay.WPF +{ + /// + /// Provides mechanisms to generate labels displayed on an axis by DateTime ticks. + /// + public class DateLabelProvider : ILabelProvider + { + public string Format { get; set; } = "yyyy-MM-dd HH:mm:ss"; + + /// + /// Generates an array of labels from an array of double. + /// + /// An array of double ticks. + /// An array of . + public FrameworkElement[] GetLabels(double[] ticks) + { + if (ticks == null) + throw new ArgumentNullException(nameof(ticks)); + + List Labels = new List(); + foreach (double tick in ticks) + { + TextBlock text = new TextBlock + { + Text = new DateTime((long)tick).ToString(Format) + }; + Labels.Add(text); + } + return Labels.ToArray(); + } + } +} + diff --git a/src/Axes/PlotAxis.cs b/src/Axes/PlotAxis.cs index abd4291..5566be2 100644 --- a/src/Axes/PlotAxis.cs +++ b/src/Axes/PlotAxis.cs @@ -128,6 +128,21 @@ public AxisOrientation AxisOrientation public static readonly DependencyProperty AxisOrientationProperty = DependencyProperty.Register("AxisOrientation", typeof(AxisOrientation), typeof(PlotAxis), new PropertyMetadata(AxisOrientation.Bottom)); + /// + /// Gets or Sets label format of axis + /// + [Category("InteractiveDataDisplay")] + [Description("Defines label format of axis")] + public ILabelProvider LabelProvider + { + get { return (ILabelProvider)GetValue(LabelProviderProperty); } + set { SetValue(LabelProviderProperty, value); } + } + + /// Identify property + public static readonly DependencyProperty LabelProviderProperty = + DependencyProperty.Register("LabelProvider", typeof(ILabelProvider), typeof(PlotAxis), new PropertyMetadata(new LabelProvider())); + /// Gets or sets transform from user data to horizontal plot coordinate. /// By default transform is /// diff --git a/src/Chart.cs b/src/Chart.cs index 17ca448..a83a1b6 100644 --- a/src/Chart.cs +++ b/src/Chart.cs @@ -296,5 +296,28 @@ public bool IsHorizontalNavigationEnabled public static readonly DependencyProperty IsHorizontalNavigationEnabledProperty = DependencyProperty.Register("IsHorizontalNavigationEnabled", typeof(bool), typeof(Chart), new PropertyMetadata(true)); - } + /// Gets or sets X Label provider + [Category("InteractiveDataDisplay")] + public ILabelProvider XLabelProvider + { + get { return (ILabelProvider)GetValue(XLabelProviderProperty); } + set { SetValue(XLabelProviderProperty, value); } + } + + /// Identifies property + public static readonly DependencyProperty XLabelProviderProperty = + DependencyProperty.Register("XLabelProvider", typeof(ILabelProvider), typeof(Chart), new PropertyMetadata(new LabelProvider())); + + /// Gets or sets Y Label provider + [Category("InteractiveDataDisplay")] + public ILabelProvider YLabelProvider + { + get { return (ILabelProvider)GetValue(YLabelProviderProperty); } + set { SetValue(YLabelProviderProperty, value); } + } + + /// Identifies property + public static readonly DependencyProperty YLabelProviderProperty = + DependencyProperty.Register("YLabelProvider", typeof(ILabelProvider), typeof(Chart), new PropertyMetadata(new LabelProvider())); + } } diff --git a/src/InteractiveDataDisplay.WPF.csproj b/src/InteractiveDataDisplay.WPF.csproj index f93e561..7a7451b 100644 --- a/src/InteractiveDataDisplay.WPF.csproj +++ b/src/InteractiveDataDisplay.WPF.csproj @@ -76,6 +76,7 @@ + diff --git a/src/Themes/Generic.xaml b/src/Themes/Generic.xaml index 23bb035..cf1a67c 100644 --- a/src/Themes/Generic.xaml +++ b/src/Themes/Generic.xaml @@ -15,7 +15,8 @@ + Foreground="{Binding Foreground, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" + LabelProvider="{Binding LabelProvider, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" /> @@ -51,7 +52,8 @@ + Foreground="{TemplateBinding Foreground}" + LabelProvider="{TemplateBinding YLabelProvider}"> + Foreground="{TemplateBinding Foreground}" + LabelProvider="{TemplateBinding XLabelProvider}">