Exemple #1
0
        /// <summary>
        /// Acquires a range axis.
        /// </summary>
        /// <param name="assignedAxis">The axis assigned to the exposed
        /// axis property.</param>
        /// <param name="firstDataPoint">A data point used to determine
        /// where a date or numeric axis is required.</param>
        /// <param name="orientation">The desired orientation of the axis.
        /// </param>
        /// <param name="axisInitializationAction">A function that initializes
        /// a newly created axis.</param>
        /// <param name="axisPropertyAccessor">A function that returns the
        /// current value of the property used to store the axis.</param>
        /// <param name="axisPropertySetter">A function that accepts an axis
        /// value and assigns it to the property intended to store a reference
        /// to it.</param>
        /// <param name="dataPointAxisValueGetter">A function that accepts a
        /// Control and returns the value that will be plot on the axis.
        /// </param>
        protected void GetRangeAxis(
            IAxis assignedAxis,
            DataPoint firstDataPoint,
            AxisOrientation orientation,
            Func <IRangeAxis> axisInitializationAction,
            Func <IRangeAxis> axisPropertyAccessor,
            Action <IRangeAxis> axisPropertySetter,
            Func <DataPoint, object> dataPointAxisValueGetter)
        {
            if (assignedAxis != null)
            {
                if (assignedAxis.Orientation == orientation)
                {
                    IRangeAxis assignedRangeAxis = assignedAxis as IRangeAxis;
                    if (assignedRangeAxis != null)
                    {
                        object value = dataPointAxisValueGetter(firstDataPoint);
                        if (assignedRangeAxis.CanPlot(value))
                        {
                            axisPropertySetter(assignedRangeAxis);
                            if (!assignedAxis.IsObjectRegistered(this))
                            {
                                assignedRangeAxis.Invalidated += OnAxisInvalidated;
                                this.SeriesHost.RegisterWithAxis(this, (IAxis)assignedRangeAxis);
                            }
                            return;
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.DataPointSeriesWithAxes_AxisCannotPlotValue, value));
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException(Properties.Resources.DataPointSeriesWithAxes_ExpectedAxesOfTypeICategoryAxis);
                    }
                }
                else
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.DataPointSeriesWithAxes_AxisIsIncorrectOrientation, orientation));
                }
            }

            // If current axis is not suitable...
            if (axisPropertyAccessor() == null || !axisPropertyAccessor().CanPlot(dataPointAxisValueGetter(firstDataPoint)))
            {
                // Attempt to find suitable axis
                IRangeAxis axis =
                    SeriesHost.Axes
                    .Cast <IAxis>()
                    .Where(currentAxis => currentAxis.Orientation == orientation &&
                           currentAxis.CanPlot(dataPointAxisValueGetter(firstDataPoint)) &&
                           currentAxis.CanRegister(this))
                    .OfType <IRangeAxis>()
                    .FirstOrDefault();

                if (axis == null)
                {
                    axis             = axisInitializationAction();
                    axis.Orientation = orientation;
                }

                IAxis baseAxis = (IAxis)axis;
                // Unregister with current axis if it has one.
                if (axisPropertyAccessor() != null)
                {
                    axisPropertyAccessor().Invalidated -= OnAxisInvalidated;
                    SeriesHost.UnregisterWithAxis(this, baseAxis);
                }

                axisPropertySetter(axis);

                if (!axis.IsObjectRegistered(this))
                {
                    axis.Invalidated += OnAxisInvalidated;
                    SeriesHost.RegisterWithAxis(this, baseAxis);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Acquires a category axis.
        /// </summary>
        /// <param name="assignedAxis">The axis assigned to the exposed
        /// axis property.</param>
        /// <param name="firstDataPoint">A data point used to determine
        /// where a date or numeric axis is required.</param>
        /// <param name="orientation">The desired orientation of the axis.
        /// </param>
        /// <param name="axisInitializationAction">A function that initializes
        /// a newly created axis.</param>
        /// <param name="axisPropertyAccessor">A function that returns the
        /// current value of the property used to store the axis.</param>
        /// <param name="axisPropertySetter">A function that accepts an axis
        /// value and assigns it to the property intended to store a reference
        /// to it.</param>
        /// <param name="dataPointAxisValueGetter">A function that retrieves
        /// the value to plot on the axis from the data point.</param>
        protected void GetCategoryAxis(
            IAxis assignedAxis,
            DataPoint firstDataPoint,
            AxisOrientation orientation,
            Func <ICategoryAxis> axisInitializationAction,
            Func <ICategoryAxis> axisPropertyAccessor,
            Action <ICategoryAxis> axisPropertySetter,
            Func <DataPoint, object> dataPointAxisValueGetter)
        {
            object firstCategoryValue = dataPointAxisValueGetter(firstDataPoint);

            if (assignedAxis != null)
            {
                if (assignedAxis.Orientation == orientation)
                {
                    ICategoryAxis assignedCategoryAxis = assignedAxis as CategoryAxis;
                    if (assignedCategoryAxis != null)
                    {
                        if (assignedCategoryAxis.CanPlot(firstCategoryValue))
                        {
                            axisPropertySetter(assignedCategoryAxis);
                            if (!assignedAxis.IsObjectRegistered(this))
                            {
                                assignedCategoryAxis.Invalidated += OnAxisInvalidated;
                                this.SeriesHost.RegisterWithAxis(this, (IAxis)assignedCategoryAxis);
                            }
                            return;
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.DataPointSeriesWithAxes_AxisCannotPlotValue, firstCategoryValue));
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException(Properties.Resources.DataPointSeriesWithAxes_ExpectedAxesOfTypeICategoryAxis);
                    }
                }
                else
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.DataPointSeriesWithAxes_AxisIsIncorrectOrientation, orientation));
                }
            }

            IAxis actualIndependentAxis = axisPropertyAccessor() as IAxis;

            // Only acquire new independent axis if necessary
            if (actualIndependentAxis == null ||
                actualIndependentAxis.Orientation != orientation)
            {
                ICategoryAxis categoryAxis =
                    SeriesHost
                    .Axes.Where(currentAxis => currentAxis.Orientation == orientation)
                    .OfType <ICategoryAxis>()
                    .Where(
                        currentCategoryAxis =>
                        currentCategoryAxis.CanPlot(firstCategoryValue) &&
                        currentCategoryAxis.CanRegister(this))
                    .FirstOrDefault();

                if (categoryAxis == null)
                {
                    categoryAxis             = axisInitializationAction();
                    categoryAxis.Orientation = orientation;
                }

                // Unregister with current axis if it has one.
                if (axisPropertyAccessor() != null)
                {
                    axisPropertyAccessor().Invalidated -= OnAxisInvalidated;
                    ICategoryAxis oldCategoryAxis = axisPropertyAccessor() as ICategoryAxis;
                    this.SeriesHost.UnregisterWithAxis(this, (IAxis)oldCategoryAxis);
                }

                // Set axis to be its independent axis
                axisPropertySetter(categoryAxis);

                if (!categoryAxis.IsObjectRegistered(this))
                {
                    categoryAxis.Invalidated += OnAxisInvalidated;

                    // Register new axis with series host
                    this.SeriesHost.RegisterWithAxis(this, (IAxis)categoryAxis);
                }
            }
        }