Esempio n. 1
0
        /// <summary>
        /// Paints an annotation object on the specified graphics.
        /// </summary>
        /// <param name="graphics">
        /// A <see cref="ChartGraphics"/> object, used to paint an annotation object.
        /// </param>
        /// <param name="chart">
        /// Reference to the <see cref="Chart"/> owner control.
        /// </param>
        override internal void Paint(Chart chart, ChartGraphics graphics)
        {
            // Get annotation position in relative coordinates
            PointF firstPoint  = PointF.Empty;
            PointF anchorPoint = PointF.Empty;
            SizeF  size        = SizeF.Empty;

            GetRelativePosition(out firstPoint, out size, out anchorPoint);
            PointF secondPoint = new PointF(firstPoint.X + size.Width, firstPoint.Y + size.Height);

            // Create selection rectangle
            RectangleF selectionRect = new RectangleF(firstPoint, new SizeF(secondPoint.X - firstPoint.X, secondPoint.Y - firstPoint.Y));

            // Adjust coordinates
            AdjustLineCoordinates(ref firstPoint, ref secondPoint, ref selectionRect);

            // Check if text position is valid
            if (float.IsNaN(firstPoint.X) ||
                float.IsNaN(firstPoint.Y) ||
                float.IsNaN(secondPoint.X) ||
                float.IsNaN(secondPoint.Y))
            {
                return;
            }

            // Set line caps
            bool    capChanged  = false;
            LineCap oldStartCap = LineCap.Flat;
            LineCap oldEndCap   = LineCap.Flat;

            if (this._startCap != LineAnchorCapStyle.None ||
                this._endCap != LineAnchorCapStyle.None)
            {
                capChanged  = true;
                oldStartCap = graphics.Pen.StartCap;
                oldEndCap   = graphics.Pen.EndCap;

                // Apply anchor cap settings
                if (this._startCap == LineAnchorCapStyle.Arrow)
                {
                    // Adjust arrow size for small line width
                    if (this.LineWidth < 4)
                    {
                        int adjustment = 3 - this.LineWidth;
                        graphics.Pen.StartCap       = LineCap.Custom;
                        graphics.Pen.CustomStartCap = new AdjustableArrowCap(
                            this.LineWidth + adjustment,
                            this.LineWidth + adjustment,
                            true);
                    }
                    else
                    {
                        graphics.Pen.StartCap = LineCap.ArrowAnchor;
                    }
                }
                else if (this._startCap == LineAnchorCapStyle.Diamond)
                {
                    graphics.Pen.StartCap = LineCap.DiamondAnchor;
                }
                else if (this._startCap == LineAnchorCapStyle.Round)
                {
                    graphics.Pen.StartCap = LineCap.RoundAnchor;
                }
                else if (this._startCap == LineAnchorCapStyle.Square)
                {
                    graphics.Pen.StartCap = LineCap.SquareAnchor;
                }
                if (this._endCap == LineAnchorCapStyle.Arrow)
                {
                    // Adjust arrow size for small line width
                    if (this.LineWidth < 4)
                    {
                        int adjustment = 3 - this.LineWidth;
                        graphics.Pen.EndCap       = LineCap.Custom;
                        graphics.Pen.CustomEndCap = new AdjustableArrowCap(
                            this.LineWidth + adjustment,
                            this.LineWidth + adjustment,
                            true);
                    }
                    else
                    {
                        graphics.Pen.EndCap = LineCap.ArrowAnchor;
                    }
                }
                else if (this._endCap == LineAnchorCapStyle.Diamond)
                {
                    graphics.Pen.EndCap = LineCap.DiamondAnchor;
                }
                else if (this._endCap == LineAnchorCapStyle.Round)
                {
                    graphics.Pen.EndCap = LineCap.RoundAnchor;
                }
                else if (this._endCap == LineAnchorCapStyle.Square)
                {
                    graphics.Pen.EndCap = LineCap.SquareAnchor;
                }
            }

            if (this.Common.ProcessModePaint)
            {
                // Draw line
                graphics.DrawLineRel(
                    this.LineColor,
                    this.LineWidth,
                    this.LineDashStyle,
                    firstPoint,
                    secondPoint,
                    this.ShadowColor,
                    this.ShadowOffset);
            }

            if (this.Common.ProcessModeRegions)
            {
                // Create line graphics path
                using (GraphicsPath path = new GraphicsPath())
                {
                    path.AddLine(
                        graphics.GetAbsolutePoint(firstPoint),
                        graphics.GetAbsolutePoint(secondPoint));
                    using (Pen pen = (Pen)graphics.Pen.Clone())
                    {
                        // Increase pen size by 2 pixels
                        pen.DashStyle = DashStyle.Solid;
                        pen.Width    += 2;
                        try
                        {
                            path.Widen(pen);
                        }
                        catch (OutOfMemoryException)
                        {
                            // GraphicsPath.Widen incorrectly throws OutOfMemoryException
                            // catching here and reacting by not widening
                        }
                        catch (ArgumentException)
                        {
                        }
                    }

                    // Add hot region
                    this.Common.HotRegionsList.AddHotRegion(
                        graphics,
                        path,
                        false,
                        ReplaceKeywords(this.ToolTip),
                        String.Empty,
                        String.Empty,
                        String.Empty,
                        this,
                        ChartElementType.Annotation);
                }
            }


            // Restore line caps
            if (capChanged)
            {
                graphics.Pen.StartCap = oldStartCap;
                graphics.Pen.EndCap   = oldEndCap;
            }

            // Paint selection handles
            PaintSelectionHandles(graphics, selectionRect, null);
        }