// (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;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
namespace System.Windows.Controls.DataVisualization.Charting
{
///
/// An axis class used to determine the plot area coordinate of values.
///
public abstract class Axis : Control, IAxis
{
#region public AxisLocation Location
///
/// Gets or sets the axis location.
///
public AxisLocation Location
{
get { return (AxisLocation)GetValue(LocationProperty); }
set { SetValue(LocationProperty, value); }
}
///
/// Identifies the Location dependency property.
///
public static readonly DependencyProperty LocationProperty =
DependencyProperty.Register(
"Location",
typeof(AxisLocation),
typeof(Axis),
new PropertyMetadata(AxisLocation.Auto, OnLocationPropertyChanged));
///
/// LocationProperty property changed handler.
///
/// Axis that changed its Location.
/// Event arguments.
private static void OnLocationPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Axis source = (Axis)d;
AxisLocation oldValue = (AxisLocation)e.OldValue;
AxisLocation newValue = (AxisLocation)e.NewValue;
source.OnLocationPropertyChanged(oldValue, newValue);
}
///
/// LocationProperty property changed handler.
///
/// Old value.
/// New value.
protected virtual void OnLocationPropertyChanged(AxisLocation oldValue, AxisLocation newValue)
{
RoutedPropertyChangedEventHandler handler = this.LocationChanged;
if (handler != null)
{
handler(this, new RoutedPropertyChangedEventArgs(oldValue, newValue));
}
}
///
/// This event is raised when the location property is changed.
///
public event RoutedPropertyChangedEventHandler LocationChanged;
#endregion public AxisLocation Location
///
/// Gets the list of child axes belonging to this axis.
///
public ObservableCollection DependentAxes { get; private set; }
#region public AxisOrientation Orientation
///
/// Gets or sets the orientation of the axis.
///
public AxisOrientation Orientation
{
get { return (AxisOrientation)GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}
///
/// Identifies the Orientation dependency property.
///
public static readonly DependencyProperty OrientationProperty =
DependencyProperty.Register(
"Orientation",
typeof(AxisOrientation),
typeof(Axis),
new PropertyMetadata(AxisOrientation.None, OnOrientationPropertyChanged));
///
/// OrientationProperty property changed handler.
///
/// Axis that changed its Orientation.
/// Event arguments.
private static void OnOrientationPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Axis source = (Axis)d;
AxisOrientation oldValue = (AxisOrientation)e.OldValue;
AxisOrientation newValue = (AxisOrientation)e.NewValue;
source.OnOrientationPropertyChanged(oldValue, newValue);
}
///
/// OrientationProperty property changed handler.
///
/// Old value.
/// New value.
protected virtual void OnOrientationPropertyChanged(AxisOrientation oldValue, AxisOrientation newValue)
{
RoutedPropertyChangedEventHandler handler = OrientationChanged;
if (handler != null)
{
handler(this, new RoutedPropertyChangedEventArgs(oldValue, newValue));
}
}
///
/// This event is raised when the Orientation property is changed.
///
public event RoutedPropertyChangedEventHandler OrientationChanged;
#endregion public AxisOrientation Orientation
///
/// Raises the invalidated event.
///
/// Information about the event.
protected virtual void OnInvalidated(RoutedEventArgs args)
{
foreach (IAxisListener listener in RegisteredListeners)
{
listener.AxisInvalidated(this);
}
}
///
/// Gets or the collection of series that are using the Axis.
///
public ObservableCollection RegisteredListeners { get; private set; }
///
/// Returns a value indicating whether the axis can plot a value.
///
/// The value to plot.
/// A value indicating whether the axis can plot a value.
///
public abstract bool CanPlot(object value);
///
/// The plot area coordinate of a value.
///
/// The value for which to retrieve the plot area
/// coordinate.
/// The plot area coordinate.
public abstract UnitValue GetPlotAreaCoordinate(object value);
///
/// Instantiates a new instance of the Axis class.
///
protected Axis()
{
RegisteredListeners = new UniqueObservableCollection();
this.RegisteredListeners.CollectionChanged += RegisteredListenersCollectionChanged;
this.DependentAxes = new ObservableCollection();
this.DependentAxes.CollectionChanged += OnChildAxesCollectionChanged;
}
///
/// Child axes collection changed.
///
/// The source of the event.
/// Information about the event.
private void OnChildAxesCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
this.OnDependentAxesCollectionChanged();
}
///
/// Child axes collection changed.
///
protected virtual void OnDependentAxesCollectionChanged()
{
}
///
/// This event is raised when the registered listeners collection is
/// changed.
///
/// The source of the event.
/// Information about the event.
private void RegisteredListenersCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
{
foreach (IAxisListener obj in e.OldItems)
{
OnObjectUnregistered(obj);
}
}
if (e.NewItems != null)
{
foreach (IAxisListener obj in e.NewItems)
{
OnObjectRegistered(obj);
}
}
}
///
/// This method is invoked when a series is registered.
///
/// The series that has been registered.
protected virtual void OnObjectRegistered(IAxisListener series)
{
}
///
/// This method is invoked when a series is unregistered.
///
/// The series that has been unregistered.
protected virtual void OnObjectUnregistered(IAxisListener series)
{
}
}
}