1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using Microsoft.Research.DynamicDataDisplay.ViewportConstraints;
|
---|
6 | using System.Windows.Media;
|
---|
7 | using System.Windows;
|
---|
8 | using System.Windows.Data;
|
---|
9 |
|
---|
10 | namespace Microsoft.Research.DynamicDataDisplay.Charts
|
---|
11 | {
|
---|
12 | /// <summary>
|
---|
13 | /// Represents an axis with ticks of <see cref="System.DateTime"/> type.
|
---|
14 | /// </summary>
|
---|
15 | public class DateTimeAxis : AxisBase<DateTime>
|
---|
16 | {
|
---|
17 | /// <summary>
|
---|
18 | /// Initializes a new instance of the <see cref="DateTimeAxis"/> class.
|
---|
19 | /// </summary>
|
---|
20 | public DateTimeAxis()
|
---|
21 | : base(new DateTimeAxisControl(), DoubleToDate,
|
---|
22 | dt => dt.Ticks / 10000000000.0)
|
---|
23 | {
|
---|
24 | AxisControl.SetBinding(MajorLabelBackgroundBrushProperty, new Binding("MajorLabelBackgroundBrush") { Source = this });
|
---|
25 | AxisControl.SetBinding(MajorLabelRectangleBorderPropertyProperty, new Binding("MajorLabelRectangleBorderProperty") { Source = this });
|
---|
26 | }
|
---|
27 |
|
---|
28 | #region VisualProperties
|
---|
29 |
|
---|
30 | /// <summary>
|
---|
31 | /// Gets or sets the major tick labels' background brush. This is a DependencyProperty.
|
---|
32 | /// </summary>
|
---|
33 | /// <value>The major label background brush.</value>
|
---|
34 | public Brush MajorLabelBackgroundBrush
|
---|
35 | {
|
---|
36 | get { return (Brush)GetValue(MajorLabelBackgroundBrushProperty); }
|
---|
37 | set { SetValue(MajorLabelBackgroundBrushProperty, value); }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static readonly DependencyProperty MajorLabelBackgroundBrushProperty = DependencyProperty.Register(
|
---|
41 | "MajorLabelBackgroundBrush",
|
---|
42 | typeof(Brush),
|
---|
43 | typeof(DateTimeAxis),
|
---|
44 | new FrameworkPropertyMetadata(Brushes.Beige));
|
---|
45 |
|
---|
46 |
|
---|
47 | public Brush MajorLabelRectangleBorderProperty
|
---|
48 | {
|
---|
49 | get { return (Brush)GetValue(MajorLabelRectangleBorderPropertyProperty); }
|
---|
50 | set { SetValue(MajorLabelRectangleBorderPropertyProperty, value); }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public static readonly DependencyProperty MajorLabelRectangleBorderPropertyProperty = DependencyProperty.Register(
|
---|
54 | "MajorLabelRectangleBorderProperty",
|
---|
55 | typeof(Brush),
|
---|
56 | typeof(DateTimeAxis),
|
---|
57 | new FrameworkPropertyMetadata(Brushes.Peru));
|
---|
58 |
|
---|
59 | #endregion // end of VisualProperties
|
---|
60 |
|
---|
61 | private ViewportConstraint constraint = new DateTimeHorizontalAxisConstraint();
|
---|
62 | protected ViewportConstraint Constraint
|
---|
63 | {
|
---|
64 | get { return constraint; }
|
---|
65 | set { constraint = value; }
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override void OnPlotterAttached(Plotter2D plotter)
|
---|
69 | {
|
---|
70 | base.OnPlotterAttached(plotter);
|
---|
71 |
|
---|
72 | plotter.Viewport.Constraints.Add(constraint);
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected override void OnPlotterDetaching(Plotter2D plotter)
|
---|
76 | {
|
---|
77 | plotter.Viewport.Constraints.Remove(constraint);
|
---|
78 |
|
---|
79 | base.OnPlotterDetaching(plotter);
|
---|
80 | }
|
---|
81 |
|
---|
82 | private static readonly long minTicks = DateTime.MinValue.Ticks;
|
---|
83 | private static readonly long maxTicks = DateTime.MaxValue.Ticks;
|
---|
84 | private static DateTime DoubleToDate(double d)
|
---|
85 | {
|
---|
86 | long ticks = (long)(d * 10000000000L);
|
---|
87 |
|
---|
88 | // todo should we throw an exception if number of ticks is too big or small?
|
---|
89 | if (ticks < minTicks)
|
---|
90 | ticks = minTicks;
|
---|
91 | else if (ticks > maxTicks)
|
---|
92 | ticks = maxTicks;
|
---|
93 |
|
---|
94 | return new DateTime(ticks);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /// <summary>
|
---|
98 | /// Sets conversions of axis - functions used to convert values of axis type to and from double values of viewport.
|
---|
99 | /// Sets both ConvertToDouble and ConvertFromDouble properties.
|
---|
100 | /// </summary>
|
---|
101 | /// <param name="min">The minimal viewport value.</param>
|
---|
102 | /// <param name="minValue">The value of axis type, corresponding to minimal viewport value.</param>
|
---|
103 | /// <param name="max">The maximal viewport value.</param>
|
---|
104 | /// <param name="maxValue">The value of axis type, corresponding to maximal viewport value.</param>
|
---|
105 | public override void SetConversion(double min, DateTime minValue, double max, DateTime maxValue)
|
---|
106 | {
|
---|
107 | var conversion = new DateTimeToDoubleConversion(min, minValue, max, maxValue);
|
---|
108 |
|
---|
109 | ConvertToDouble = conversion.ToDouble;
|
---|
110 | ConvertFromDouble = conversion.FromDouble;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|