1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Windows;
|
---|
6 | using System.Windows.Controls;
|
---|
7 | using System.Windows.Documents;
|
---|
8 | using System.Globalization;
|
---|
9 | using System.Diagnostics;
|
---|
10 | using Microsoft.Research.DynamicDataDisplay.Charts.Axes;
|
---|
11 |
|
---|
12 | namespace Microsoft.Research.DynamicDataDisplay.Charts
|
---|
13 | {
|
---|
14 | /// <summary>
|
---|
15 | /// Represents an axis label provider for double ticks, generating labels with numbers in exponential form when it is appropriate.
|
---|
16 | /// </summary>
|
---|
17 | public sealed class ExponentialLabelProvider : NumericLabelProviderBase
|
---|
18 | {
|
---|
19 | /// <summary>
|
---|
20 | /// Initializes a new instance of the <see cref="ExponentialLabelProvider"/> class.
|
---|
21 | /// </summary>
|
---|
22 | public ExponentialLabelProvider() { }
|
---|
23 |
|
---|
24 | /// <summary>
|
---|
25 | /// Creates labels by given ticks info.
|
---|
26 | /// Is not intended to be called from your code.
|
---|
27 | /// </summary>
|
---|
28 | /// <param name="ticksInfo">The ticks info.</param>
|
---|
29 | /// <returns>
|
---|
30 | /// Array of <see cref="UIElement"/>s, which are axis labels for specified axis ticks.
|
---|
31 | /// </returns>
|
---|
32 | public override UIElement[] CreateLabels(ITicksInfo<double> ticksInfo)
|
---|
33 | {
|
---|
34 | var ticks = ticksInfo.Ticks;
|
---|
35 |
|
---|
36 | Init(ticks);
|
---|
37 |
|
---|
38 | UIElement[] res = new UIElement[ticks.Length];
|
---|
39 |
|
---|
40 | LabelTickInfo<double> tickInfo = new LabelTickInfo<double> { Info = ticksInfo.Info };
|
---|
41 |
|
---|
42 | for (int i = 0; i < res.Length; i++)
|
---|
43 | {
|
---|
44 | var tick = ticks[i];
|
---|
45 | tickInfo.Tick = tick;
|
---|
46 | tickInfo.Index = i;
|
---|
47 |
|
---|
48 | string labelText = GetString(tickInfo);
|
---|
49 |
|
---|
50 | TextBlock label;
|
---|
51 | if (labelText.Contains('E'))
|
---|
52 | {
|
---|
53 | string[] substrs = labelText.Split('E');
|
---|
54 | string mantissa = substrs[0];
|
---|
55 | string exponenta = substrs[1];
|
---|
56 | exponenta = exponenta.TrimStart('+');
|
---|
57 | Span span = new Span();
|
---|
58 | span.Inlines.Add(String.Format(CultureInfo.CurrentCulture, "{0}·10", mantissa));
|
---|
59 | Span exponentaSpan = new Span(new Run(exponenta));
|
---|
60 | exponentaSpan.BaselineAlignment = BaselineAlignment.Superscript;
|
---|
61 | exponentaSpan.FontSize = 8;
|
---|
62 | span.Inlines.Add(exponentaSpan);
|
---|
63 |
|
---|
64 | label = new TextBlock(span);
|
---|
65 | LabelProviderProperties.SetExponentialIsCommonLabel(label, false);
|
---|
66 | }
|
---|
67 | else
|
---|
68 | {
|
---|
69 | label = (TextBlock)GetResourceFromPool();
|
---|
70 | if (label == null)
|
---|
71 | {
|
---|
72 | label = new TextBlock();
|
---|
73 | }
|
---|
74 |
|
---|
75 | label.Text = labelText;
|
---|
76 | }
|
---|
77 | res[i] = label;
|
---|
78 | label.ToolTip = tick.ToString(CultureInfo.CurrentCulture);
|
---|
79 |
|
---|
80 | ApplyCustomView(tickInfo, label);
|
---|
81 | }
|
---|
82 |
|
---|
83 | return res;
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected override bool ReleaseCore(UIElement label)
|
---|
87 | {
|
---|
88 | bool isNotExponential = LabelProviderProperties.GetExponentialIsCommonLabel(label);
|
---|
89 | return isNotExponential && CustomView == null;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|