// (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. // All other rights reserved. using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Windows; using System.Windows.Data; namespace System.Windows.Controls.DataVisualization.Charting { /// /// An axis label for displaying DateTime values. /// public class DateTimeAxisLabel : AxisLabel { #region public DateTimeIntervalType IntervalType /// /// Gets or sets the interval type of the DateTimeAxis2. /// public DateTimeIntervalType IntervalType { get { return (DateTimeIntervalType)GetValue(IntervalTypeProperty); } set { SetValue(IntervalTypeProperty, value); } } /// /// Identifies the IntervalType dependency property. /// public static readonly System.Windows.DependencyProperty IntervalTypeProperty = System.Windows.DependencyProperty.Register( "IntervalType", typeof(DateTimeIntervalType), typeof(DateTimeAxisLabel), new System.Windows.PropertyMetadata(DateTimeIntervalType.Auto, OnIntervalTypePropertyChanged)); /// /// IntervalTypeProperty property changed handler. /// /// DateTimeAxisLabel that changed its IntervalType. /// Event arguments. private static void OnIntervalTypePropertyChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e) { DateTimeAxisLabel source = (DateTimeAxisLabel)d; DateTimeIntervalType oldValue = (DateTimeIntervalType)e.OldValue; DateTimeIntervalType newValue = (DateTimeIntervalType)e.NewValue; source.OnIntervalTypePropertyChanged(oldValue, newValue); } /// /// IntervalTypeProperty property changed handler. /// /// Old value. /// New value. protected virtual void OnIntervalTypePropertyChanged(DateTimeIntervalType oldValue, DateTimeIntervalType newValue) { UpdateFormattedContent(); } #endregion public DateTimeIntervalType IntervalType #region public string YearsIntervalStringFormat /// /// Gets or sets the format string to use when the interval is hours. /// public string YearsIntervalStringFormat { get { return GetValue(YearsIntervalStringFormatProperty) as string; } set { SetValue(YearsIntervalStringFormatProperty, value); } } /// /// Identifies the YearsIntervalStringFormat dependency property. /// public static readonly System.Windows.DependencyProperty YearsIntervalStringFormatProperty = System.Windows.DependencyProperty.Register( "YearsIntervalStringFormat", typeof(string), typeof(DateTimeAxisLabel), new System.Windows.PropertyMetadata(null, OnYearsIntervalStringFormatPropertyChanged)); /// /// YearsIntervalStringFormatProperty property changed handler. /// /// DateTimeAxisLabel that changed its YearsIntervalStringFormat. /// Event arguments. private static void OnYearsIntervalStringFormatPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DateTimeAxisLabel source = (DateTimeAxisLabel)d; source.OnYearsIntervalStringFormatPropertyChanged(); } /// /// YearsIntervalStringFormatProperty property changed handler. /// protected virtual void OnYearsIntervalStringFormatPropertyChanged() { UpdateFormattedContent(); } #endregion public string YearsIntervalStringFormat #region public string MonthsIntervalStringFormat /// /// Gets or sets the format string to use when the interval is hours. /// public string MonthsIntervalStringFormat { get { return GetValue(MonthsIntervalStringFormatProperty) as string; } set { SetValue(MonthsIntervalStringFormatProperty, value); } } /// /// Identifies the MonthsIntervalStringFormat dependency property. /// public static readonly System.Windows.DependencyProperty MonthsIntervalStringFormatProperty = System.Windows.DependencyProperty.Register( "MonthsIntervalStringFormat", typeof(string), typeof(DateTimeAxisLabel), new System.Windows.PropertyMetadata(null, OnMonthsIntervalStringFormatPropertyChanged)); /// /// MonthsIntervalStringFormatProperty property changed handler. /// /// DateTimeAxisLabel that changed its MonthsIntervalStringFormat. /// Event arguments. private static void OnMonthsIntervalStringFormatPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DateTimeAxisLabel source = (DateTimeAxisLabel)d; source.OnMonthsIntervalStringFormatPropertyChanged(); } /// /// MonthsIntervalStringFormatProperty property changed handler. /// protected virtual void OnMonthsIntervalStringFormatPropertyChanged() { UpdateFormattedContent(); } #endregion public string MonthsIntervalStringFormat #region public string WeeksIntervalStringFormat /// /// Gets or sets the format string to use when the interval is hours. /// public string WeeksIntervalStringFormat { get { return GetValue(WeeksIntervalStringFormatProperty) as string; } set { SetValue(WeeksIntervalStringFormatProperty, value); } } /// /// Identifies the WeeksIntervalStringFormat dependency property. /// public static readonly System.Windows.DependencyProperty WeeksIntervalStringFormatProperty = System.Windows.DependencyProperty.Register( "WeeksIntervalStringFormat", typeof(string), typeof(DateTimeAxisLabel), new System.Windows.PropertyMetadata(null, OnWeeksIntervalStringFormatPropertyChanged)); /// /// WeeksIntervalStringFormatProperty property changed handler. /// /// DateTimeAxisLabel that changed its WeeksIntervalStringFormat. /// Event arguments. private static void OnWeeksIntervalStringFormatPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DateTimeAxisLabel source = (DateTimeAxisLabel)d; source.OnWeeksIntervalStringFormatPropertyChanged(); } /// /// WeeksIntervalStringFormatProperty property changed handler. /// protected virtual void OnWeeksIntervalStringFormatPropertyChanged() { UpdateFormattedContent(); } #endregion public string WeeksIntervalStringFormat #region public string DaysIntervalStringFormat /// /// Gets or sets the format string to use when the interval is hours. /// public string DaysIntervalStringFormat { get { return GetValue(DaysIntervalStringFormatProperty) as string; } set { SetValue(DaysIntervalStringFormatProperty, value); } } /// /// Identifies the DaysIntervalStringFormat dependency property. /// public static readonly System.Windows.DependencyProperty DaysIntervalStringFormatProperty = System.Windows.DependencyProperty.Register( "DaysIntervalStringFormat", typeof(string), typeof(DateTimeAxisLabel), new System.Windows.PropertyMetadata(null, OnDaysIntervalStringFormatPropertyChanged)); /// /// DaysIntervalStringFormatProperty property changed handler. /// /// DateTimeAxisLabel that changed its DaysIntervalStringFormat. /// Event arguments. private static void OnDaysIntervalStringFormatPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DateTimeAxisLabel source = (DateTimeAxisLabel)d; source.OnDaysIntervalStringFormatPropertyChanged(); } /// /// DaysIntervalStringFormatProperty property changed handler. /// protected virtual void OnDaysIntervalStringFormatPropertyChanged() { UpdateFormattedContent(); } #endregion public string DaysIntervalStringFormat #region public string HoursIntervalStringFormat /// /// Gets or sets the format string to use when the interval is hours. /// public string HoursIntervalStringFormat { get { return GetValue(HoursIntervalStringFormatProperty) as string; } set { SetValue(HoursIntervalStringFormatProperty, value); } } /// /// Identifies the HoursIntervalStringFormat dependency property. /// public static readonly System.Windows.DependencyProperty HoursIntervalStringFormatProperty = System.Windows.DependencyProperty.Register( "HoursIntervalStringFormat", typeof(string), typeof(DateTimeAxisLabel), new System.Windows.PropertyMetadata(null, OnHoursIntervalStringFormatPropertyChanged)); /// /// HoursIntervalStringFormatProperty property changed handler. /// /// DateTimeAxisLabel that changed its HoursIntervalStringFormat. /// Event arguments. private static void OnHoursIntervalStringFormatPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DateTimeAxisLabel source = (DateTimeAxisLabel)d; source.OnHoursIntervalStringFormatPropertyChanged(); } /// /// HoursIntervalStringFormatProperty property changed handler. /// protected virtual void OnHoursIntervalStringFormatPropertyChanged() { UpdateFormattedContent(); } #endregion public string HoursIntervalStringFormat #region public string MinutesIntervalStringFormat /// /// Gets or sets the format string to use when the interval is hours. /// public string MinutesIntervalStringFormat { get { return GetValue(MinutesIntervalStringFormatProperty) as string; } set { SetValue(MinutesIntervalStringFormatProperty, value); } } /// /// Identifies the MinutesIntervalStringFormat dependency property. /// public static readonly System.Windows.DependencyProperty MinutesIntervalStringFormatProperty = System.Windows.DependencyProperty.Register( "MinutesIntervalStringFormat", typeof(string), typeof(DateTimeAxisLabel), new System.Windows.PropertyMetadata(null, OnMinutesIntervalStringFormatPropertyChanged)); /// /// MinutesIntervalStringFormatProperty property changed handler. /// /// DateTimeAxisLabel that changed its MinutesIntervalStringFormat. /// Event arguments. private static void OnMinutesIntervalStringFormatPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DateTimeAxisLabel source = (DateTimeAxisLabel)d; source.OnMinutesIntervalStringFormatPropertyChanged(); } /// /// MinutesIntervalStringFormatProperty property changed handler. /// protected virtual void OnMinutesIntervalStringFormatPropertyChanged() { UpdateFormattedContent(); } #endregion public string MinutesIntervalStringFormat #region public string SecondsIntervalStringFormat /// /// Gets or sets the format string to use when the interval is hours. /// public string SecondsIntervalStringFormat { get { return GetValue(SecondsIntervalStringFormatProperty) as string; } set { SetValue(SecondsIntervalStringFormatProperty, value); } } /// /// Identifies the SecondsIntervalStringFormat dependency property. /// public static readonly System.Windows.DependencyProperty SecondsIntervalStringFormatProperty = System.Windows.DependencyProperty.Register( "SecondsIntervalStringFormat", typeof(string), typeof(DateTimeAxisLabel), new System.Windows.PropertyMetadata(null, OnSecondsIntervalStringFormatPropertyChanged)); /// /// SecondsIntervalStringFormatProperty property changed handler. /// /// DateTimeAxisLabel that changed its SecondsIntervalStringFormat. /// Event arguments. private static void OnSecondsIntervalStringFormatPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DateTimeAxisLabel source = (DateTimeAxisLabel)d; source.OnSecondsIntervalStringFormatPropertyChanged(); } /// /// SecondsIntervalStringFormatProperty property changed handler. /// protected virtual void OnSecondsIntervalStringFormatPropertyChanged() { UpdateFormattedContent(); } #endregion public string SecondsIntervalStringFormat #region public string MillisecondsIntervalStringFormat /// /// Gets or sets the format string to use when the interval is hours. /// public string MillisecondsIntervalStringFormat { get { return GetValue(MillisecondsIntervalStringFormatProperty) as string; } set { SetValue(MillisecondsIntervalStringFormatProperty, value); } } /// /// Identifies the MillisecondsIntervalStringFormat dependency property. /// public static readonly System.Windows.DependencyProperty MillisecondsIntervalStringFormatProperty = System.Windows.DependencyProperty.Register( "MillisecondsIntervalStringFormat", typeof(string), typeof(DateTimeAxisLabel), new System.Windows.PropertyMetadata(null, OnMillisecondsIntervalStringFormatPropertyChanged)); /// /// MillisecondsIntervalStringFormatProperty property changed handler. /// /// DateTimeAxisLabel that changed its MillisecondsIntervalStringFormat. /// Event arguments. private static void OnMillisecondsIntervalStringFormatPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DateTimeAxisLabel source = (DateTimeAxisLabel)d; source.OnMillisecondsIntervalStringFormatPropertyChanged(); } /// /// MillisecondsIntervalStringFormatProperty property changed handler. /// protected virtual void OnMillisecondsIntervalStringFormatPropertyChanged() { UpdateFormattedContent(); } #endregion public string MillisecondsIntervalStringFormat #if !SILVERLIGHT /// /// Initializes the static members of the DateTimeAxisLabel class. /// [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline", Justification = "Dependency properties are initialized in-line.")] static DateTimeAxisLabel() { DefaultStyleKeyProperty.OverrideMetadata(typeof(DateTimeAxisLabel), new FrameworkPropertyMetadata(typeof(DateTimeAxisLabel))); } #endif /// /// Instantiates a new instance of the DateTimeAxisLabel class. /// public DateTimeAxisLabel() { #if SILVERLIGHT this.DefaultStyleKey = typeof(DateTimeAxisLabel); #endif } /// /// Updates the formatted text. /// [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Code is not overly complex.")] protected override void UpdateFormattedContent() { if (StringFormat == null) { switch (IntervalType) { case DateTimeIntervalType.Years: this.SetBinding(FormattedContentProperty, new Binding { Converter = new StringFormatConverter(), ConverterParameter = YearsIntervalStringFormat ?? StringFormat ?? "{0}" }); break; case DateTimeIntervalType.Months: this.SetBinding(FormattedContentProperty, new Binding { Converter = new StringFormatConverter(), ConverterParameter = MonthsIntervalStringFormat ?? StringFormat ?? "{0}" }); break; case DateTimeIntervalType.Weeks: this.SetBinding(FormattedContentProperty, new Binding { Converter = new StringFormatConverter(), ConverterParameter = WeeksIntervalStringFormat ?? StringFormat ?? "{0}" }); break; case DateTimeIntervalType.Days: this.SetBinding(FormattedContentProperty, new Binding { Converter = new StringFormatConverter(), ConverterParameter = DaysIntervalStringFormat ?? StringFormat ?? "{0}" }); break; case DateTimeIntervalType.Hours: this.SetBinding(FormattedContentProperty, new Binding { Converter = new StringFormatConverter(), ConverterParameter = HoursIntervalStringFormat ?? StringFormat ?? "{0}" }); break; case DateTimeIntervalType.Minutes: this.SetBinding(FormattedContentProperty, new Binding { Converter = new StringFormatConverter(), ConverterParameter = MinutesIntervalStringFormat ?? StringFormat ?? "{0}" }); break; case DateTimeIntervalType.Seconds: this.SetBinding(FormattedContentProperty, new Binding { Converter = new StringFormatConverter(), ConverterParameter = SecondsIntervalStringFormat ?? StringFormat ?? "{0}" }); break; case DateTimeIntervalType.Milliseconds: this.SetBinding(FormattedContentProperty, new Binding { Converter = new StringFormatConverter(), ConverterParameter = MillisecondsIntervalStringFormat ?? StringFormat ?? "{0}" }); break; default: base.UpdateFormattedContent(); break; } } else { base.UpdateFormattedContent(); } } } }