// (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.Collections.Generic; #if !DEFINITION_SERIES_COMPATIBILITY_MODE namespace System.Windows.Controls.DataVisualization.Charting { /// /// Represents a control that contains a data series to be rendered in X/Y scatter format. /// /// Preview [StyleTypedProperty(Property = DataPointStyleName, StyleTargetType = typeof(ScatterDataPoint))] [StyleTypedProperty(Property = "LegendItemStyle", StyleTargetType = typeof(LegendItem))] [TemplatePart(Name = DataPointSeries.PlotAreaName, Type = typeof(Canvas))] public partial class ScatterSeries : DataPointSingleSeriesWithAxes { /// /// Initializes a new instance of the ScatterSeries class. /// public ScatterSeries() { } /// /// Gets the dependent axis as a range axis. /// public IRangeAxis ActualDependentRangeAxis { get { return this.InternalActualDependentAxis as IRangeAxis; } } #region public IRangeAxis DependentRangeAxis /// /// Gets or sets the dependent range axis. /// public IRangeAxis DependentRangeAxis { get { return GetValue(DependentRangeAxisProperty) as IRangeAxis; } set { SetValue(DependentRangeAxisProperty, value); } } /// /// Identifies the DependentRangeAxis dependency property. /// public static readonly DependencyProperty DependentRangeAxisProperty = DependencyProperty.Register( "DependentRangeAxis", typeof(IRangeAxis), typeof(ScatterSeries), new PropertyMetadata(null, OnDependentRangeAxisPropertyChanged)); /// /// DependentRangeAxisProperty property changed handler. /// /// ScatterSeries that changed its DependentRangeAxis. /// Event arguments. private static void OnDependentRangeAxisPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ScatterSeries source = (ScatterSeries)d; IRangeAxis newValue = (IRangeAxis)e.NewValue; source.OnDependentRangeAxisPropertyChanged(newValue); } /// /// DependentRangeAxisProperty property changed handler. /// /// New value. private void OnDependentRangeAxisPropertyChanged(IRangeAxis newValue) { this.InternalDependentAxis = (IAxis)newValue; } #endregion public IRangeAxis DependentRangeAxis /// /// Gets the independent axis as a range axis. /// public IAxis ActualIndependentAxis { get { return this.InternalActualIndependentAxis as IAxis; } } #region public IAxis IndependentAxis /// /// Gets or sets the independent range axis. /// public IAxis IndependentAxis { get { return GetValue(IndependentAxisProperty) as IAxis; } set { SetValue(IndependentAxisProperty, value); } } /// /// Identifies the IndependentAxis dependency property. /// public static readonly DependencyProperty IndependentAxisProperty = DependencyProperty.Register( "IndependentAxis", typeof(IAxis), typeof(ScatterSeries), new PropertyMetadata(null, OnIndependentAxisPropertyChanged)); /// /// IndependentAxisProperty property changed handler. /// /// ScatterSeries that changed its IndependentAxis. /// Event arguments. private static void OnIndependentAxisPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ScatterSeries source = (ScatterSeries)d; IAxis newValue = (IAxis)e.NewValue; source.OnIndependentAxisPropertyChanged(newValue); } /// /// IndependentAxisProperty property changed handler. /// /// New value. private void OnIndependentAxisPropertyChanged(IAxis newValue) { this.InternalIndependentAxis = (IAxis)newValue; } #endregion public IAxis IndependentAxis /// /// Acquire a horizontal linear axis and a vertical linear axis. /// /// The first data point. protected override void GetAxes(DataPoint firstDataPoint) { GetAxes( firstDataPoint, (axis) => axis.Orientation == AxisOrientation.X, () => { IAxis axis = CreateRangeAxisFromData(firstDataPoint.IndependentValue); if (axis == null) { axis = new CategoryAxis(); } axis.Orientation = AxisOrientation.X; return axis; }, (axis) => axis.Orientation == AxisOrientation.Y && axis is IRangeAxis, () => { DisplayAxis axis = (DisplayAxis)CreateRangeAxisFromData(firstDataPoint.DependentValue); if (axis == null) { throw new InvalidOperationException(Properties.Resources.DataPointSeriesWithAxes_NoSuitableAxisAvailableForPlottingDependentValue); } axis.ShowGridLines = true; axis.Orientation = AxisOrientation.Y; return axis; }); } /// /// Creates a new scatter data point. /// /// A scatter data point. protected override DataPoint CreateDataPoint() { return new ScatterDataPoint(); } /// /// Returns the custom ResourceDictionary to use for necessary resources. /// /// /// ResourceDictionary to use for necessary resources. /// protected override IEnumerator GetResourceDictionaryEnumeratorFromHost() { return GetResourceDictionaryWithTargetType(SeriesHost, typeof(ScatterDataPoint), true); } /// /// This method updates a single data point. /// /// The data point to update. protected override void UpdateDataPoint(DataPoint dataPoint) { double PlotAreaHeight = ActualDependentRangeAxis.GetPlotAreaCoordinate(ActualDependentRangeAxis.Range.Maximum).Value; double dataPointX = ActualIndependentAxis.GetPlotAreaCoordinate(dataPoint.ActualIndependentValue).Value; double dataPointY = ActualDependentRangeAxis.GetPlotAreaCoordinate(dataPoint.ActualDependentValue).Value; if (ValueHelper.CanGraph(dataPointX) && ValueHelper.CanGraph(dataPointY)) { dataPoint.Visibility = Visibility.Visible; // Set the Position Canvas.SetLeft( dataPoint, Math.Round(dataPointX - (dataPoint.ActualWidth / 2))); Canvas.SetTop( dataPoint, Math.Round(PlotAreaHeight - (dataPointY + (dataPoint.ActualHeight / 2)))); } else { dataPoint.Visibility = Visibility.Collapsed; } } } } #endif