using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Globalization;
using System.Diagnostics;
using Microsoft.Research.DynamicDataDisplay.Charts.Axes;
namespace Microsoft.Research.DynamicDataDisplay.Charts
{
///
/// Represents an axis label provider for double ticks, generating labels with numbers in exponential form when it is appropriate.
///
public sealed class ExponentialLabelProvider : NumericLabelProviderBase
{
///
/// Initializes a new instance of the class.
///
public ExponentialLabelProvider() { }
///
/// Creates labels by given ticks info.
/// Is not intended to be called from your code.
///
/// The ticks info.
///
/// Array of s, which are axis labels for specified axis ticks.
///
public override UIElement[] CreateLabels(ITicksInfo ticksInfo)
{
var ticks = ticksInfo.Ticks;
Init(ticks);
UIElement[] res = new UIElement[ticks.Length];
LabelTickInfo tickInfo = new LabelTickInfo { Info = ticksInfo.Info };
for (int i = 0; i < res.Length; i++)
{
var tick = ticks[i];
tickInfo.Tick = tick;
tickInfo.Index = i;
string labelText = GetString(tickInfo);
TextBlock label;
if (labelText.Contains('E'))
{
string[] substrs = labelText.Split('E');
string mantissa = substrs[0];
string exponenta = substrs[1];
exponenta = exponenta.TrimStart('+');
Span span = new Span();
span.Inlines.Add(String.Format(CultureInfo.CurrentCulture, "{0}·10", mantissa));
Span exponentaSpan = new Span(new Run(exponenta));
exponentaSpan.BaselineAlignment = BaselineAlignment.Superscript;
exponentaSpan.FontSize = 8;
span.Inlines.Add(exponentaSpan);
label = new TextBlock(span);
LabelProviderProperties.SetExponentialIsCommonLabel(label, false);
}
else
{
label = (TextBlock)GetResourceFromPool();
if (label == null)
{
label = new TextBlock();
}
label.Text = labelText;
}
res[i] = label;
label.ToolTip = tick.ToString(CultureInfo.CurrentCulture);
ApplyCustomView(tickInfo, label);
}
return res;
}
protected override bool ReleaseCore(UIElement label)
{
bool isNotExponential = LabelProviderProperties.GetExponentialIsCommonLabel(label);
return isNotExponential && CustomView == null;
}
}
}