Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/Axes/DateTime/MajorDateTimeLabelProvider.cs @ 13847

Last change on this file since 13847 was 12503, checked in by aballeit, 10 years ago

#2283 added GUI and charts; fixed MCTS

File size: 4.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Windows.Controls;
7using System.Windows.Media;
8using System.Windows.Shapes;
9using System.Diagnostics;
10using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
11using Microsoft.Research.DynamicDataDisplay.Charts.Axes;
12using System.Windows.Data;
13
14namespace Microsoft.Research.DynamicDataDisplay.Charts
15{
16    /// <summary>
17    /// Represents a label provider for major ticks of <see cref="System.DateTime"/> type.
18    /// </summary>
19    public class MajorDateTimeLabelProvider : DateTimeLabelProviderBase
20    {
21        /// <summary>
22        /// Initializes a new instance of the <see cref="MajorDateTimeLabelProvider"/> class.
23        /// </summary>
24        public MajorDateTimeLabelProvider() { }
25
26        public override UIElement[] CreateLabels(ITicksInfo<DateTime> ticksInfo)
27        {
28            object info = ticksInfo.Info;
29            var ticks = ticksInfo.Ticks;
30            UIElement[] res = new UIElement[ticks.Length - 1];
31            int labelsNum = 3;
32
33            if (info is DifferenceIn)
34            {
35                DifferenceIn diff = (DifferenceIn)info;
36                DateFormat = GetDateFormat(diff);
37            }
38            else if (info is MajorLabelsInfo)
39            {
40                MajorLabelsInfo mInfo = (MajorLabelsInfo)info;
41                DifferenceIn diff = (DifferenceIn)mInfo.Info;
42                DateFormat = GetDateFormat(diff);
43                labelsNum = mInfo.MajorLabelsCount + 1;
44
45                //DebugVerify.Is(labelsNum < 100);
46            }
47
48            DebugVerify.Is(ticks.Length < 10);
49
50            LabelTickInfo<DateTime> tickInfo = new LabelTickInfo<DateTime>();
51            for (int i = 0; i < ticks.Length - 1; i++)
52            {
53                tickInfo.Info = info;
54                tickInfo.Tick = ticks[i];
55
56                string tickText = GetString(tickInfo);
57
58                Grid grid = new Grid { };
59
60                // doing binding as described at http://sdolha.spaces.live.com/blog/cns!4121802308C5AB4E!3724.entry?wa=wsignin1.0&sa=835372863
61
62                grid.SetBinding(Grid.BackgroundProperty, new Binding { Path = new PropertyPath("(0)", DateTimeAxis.MajorLabelBackgroundBrushProperty), RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(AxisControlBase) } });
63                Rectangle rect = new Rectangle
64                {
65                    StrokeThickness = 2
66                };
67                rect.SetBinding(Rectangle.StrokeProperty, new Binding { Path = new PropertyPath("(0)", DateTimeAxis.MajorLabelRectangleBorderPropertyProperty), RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorType = typeof(AxisControlBase) } });
68
69                Grid.SetColumn(rect, 0);
70                Grid.SetColumnSpan(rect, labelsNum);
71
72                for (int j = 0; j < labelsNum; j++)
73                {
74                    grid.ColumnDefinitions.Add(new ColumnDefinition());
75                }
76
77                grid.Children.Add(rect);
78
79                for (int j = 0; j < labelsNum; j++)
80                {
81                    var tb = new TextBlock
82                    {
83                        Text = tickText,
84                        HorizontalAlignment = HorizontalAlignment.Center,
85                        Margin = new Thickness(0, 3, 0, 3)
86                    };
87                    Grid.SetColumn(tb, j);
88                    grid.Children.Add(tb);
89                }
90
91                ApplyCustomView(tickInfo, grid);
92
93                res[i] = grid;
94            }
95
96            return res;
97        }
98
99        protected override string GetDateFormat(DifferenceIn diff)
100        {
101            string format = null;
102
103            switch (diff)
104            {
105                case DifferenceIn.Year:
106                    format = "yyyy";
107                    break;
108                case DifferenceIn.Month:
109                    format = "MMMM yyyy";
110                    break;
111                case DifferenceIn.Day:
112                    format = "%d MMMM yyyy";
113                    break;
114                case DifferenceIn.Hour:
115                    format = "HH:mm %d MMMM yyyy";
116                    break;
117                case DifferenceIn.Minute:
118                    format = "HH:mm %d MMMM yyyy";
119                    break;
120                case DifferenceIn.Second:
121                    format = "HH:mm:ss %d MMMM yyyy";
122                    break;
123                case DifferenceIn.Millisecond:
124                    format = "fff";
125                    break;
126                default:
127                    break;
128            }
129
130            return format;
131        }
132    }
133}
Note: See TracBrowser for help on using the repository browser.