Ejemplo n.º 1
0
        /// <summary>
        /// Points of each data in screen coordinate
        /// </summary>
        /// <param name="pane"></param>
        /// <returns></returns>
        virtual public PointF[] ScreenPoints(PaneBase pane)
        {
            //PointF[] points = new PointF[4];

            //points[0] = _location.TransformTopLeft(pane);
            //points[2] = _location.TransformBottomRight(pane);
            //points[1].X = points[2].X;
            //points[1].Y = points[0].Y;
            //points[3].X = points[0].X;
            //points[3].Y = points[2].Y;

            PointF[] points = new PointF[4];

            points[0] = _location.TransformTopLeft(pane);
            points[1] = _location.TransformBottomRight(pane);

            return(points);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Render this object to the specified <see cref="Graphics"/> device.
        /// </summary>
        /// <remarks>
        /// This method is normally only called by the Draw method
        /// of the parent <see cref="GraphObjList"/> collection object.
        /// </remarks>
        /// <param name="g">
        /// A graphic device object to be drawn into.  This is normally e.Graphics from the
        /// PaintEventArgs argument to the Paint() method.
        /// </param>
        /// <param name="pane">
        /// A reference to the <see cref="PaneBase"/> object that is the parent or
        /// owner of this object.
        /// </param>
        /// <param name="scaleFactor">
        /// The scaling factor to be used for rendering objects.  This is calculated and
        /// passed down by the parent <see cref="GraphPane"/> object using the
        /// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
        /// font sizes, etc. according to the actual size of the graph.
        /// </param>
        public override void Draw(Graphics g, PaneBase pane, float scaleFactor)
        {
            // Convert the arrow coordinates from the user coordinate system
            // to the screen coordinate system
            PointF pix1 = Location.TransformTopLeft(pane);
            PointF pix2 = Location.TransformBottomRight(pane);

            if (pix1.X > -10000 && pix1.X < 100000 && pix1.Y > -100000 && pix1.Y < 100000 &&
                pix2.X > -10000 && pix2.X < 100000 && pix2.Y > -100000 && pix2.Y < 100000)
            {
                // calculate the length and the angle of the arrow "vector"
                double dy     = pix2.Y - pix1.Y;
                double dx     = pix2.X - pix1.X;
                float  angle  = (float)Math.Atan2(dy, dx) * 180.0F / (float)Math.PI;
                var    length = (float)Math.Sqrt(dx * dx + dy * dy);

                // Save the old transform matrix
                Matrix transform = g.Transform;
                // Move the coordinate system so it is located at the starting point
                // of this arrow
                g.TranslateTransform(pix1.X, pix1.Y);
                // Rotate the coordinate system according to the angle of this arrow
                // about the starting point
                g.RotateTransform(angle);

                // get a pen according to this arrow properties
                using (var pen = new Pen(_color, pane.ScaledPenWidth(_penWidth, scaleFactor)))
                {
                    pen.DashStyle = _style;

                    g.DrawLine(pen, 0, 0, length, 0);
                }

                // Restore the transform matrix back to its original state
                g.Transform = transform;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Render this object to the specified <see cref="Graphics"/> device.
        /// </summary>
        /// <remarks>
        /// This method is normally only called by the Draw method
        /// of the parent <see cref="GraphObjList"/> collection object.
        /// </remarks>
        /// <param name="g">
        /// A graphic device object to be drawn into.  This is normally e.Graphics from the
        /// PaintEventArgs argument to the Paint() method.
        /// </param>
        /// <param name="pane">
        /// A reference to the <see cref="PaneBase"/> object that is the parent or
        /// owner of this object.
        /// </param>
        /// <param name="scaleFactor">
        /// The scaling factor to be used for rendering objects.  This is calculated and
        /// passed down by the parent <see cref="GraphPane"/> object using the
        /// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
        /// font sizes, etc. according to the actual size of the graph.
        /// </param>
        public override void Draw(Graphics g, PaneBase pane, float scaleFactor)
        {
            // Convert the arrow coordinates from the user coordinate system
            // to the screen coordinate system
            PointF pix1 = Location.TransformTopLeft(pane);
            PointF pix2 = Location.TransformBottomRight(pane);

            if (pix1.X > -10000 && pix1.X < 100000 && pix1.Y > -100000 && pix1.Y < 100000 &&
                pix2.X > -10000 && pix2.X < 100000 && pix2.Y > -100000 && pix2.Y < 100000)
            {
                // get a scaled size for the arrowhead
                float scaledSize = (_size * scaleFactor);

                // calculate the length and the angle of the arrow "vector"
                double dy     = pix2.Y - pix1.Y;
                double dx     = pix2.X - pix1.X;
                float  angle  = (float)Math.Atan2(dy, dx) * 180.0F / (float)Math.PI;
                var    length = (float)Math.Sqrt(dx * dx + dy * dy);

                // Save the old transform matrix
                Matrix transform = g.Transform;
                // Move the coordinate system so it is located at the starting point
                // of this arrow
                g.TranslateTransform(pix1.X, pix1.Y);
                // Rotate the coordinate system according to the angle of this arrow
                // about the starting point
                g.RotateTransform(angle);

                // get a pen according to this arrow properties
                using (var pen = new Pen(_color, pane.ScaledPenWidth(_penWidth, scaleFactor)))
                {
                    pen.DashStyle = _style;

                    // Only show the arrowhead if required
                    if (_isArrowHead)
                    {
                        // Draw the line segment for this arrow
                        g.DrawLine(pen, 0, 0, length - scaledSize + 1, 0);

                        // Create a polygon representing the arrowhead based on the scaled
                        // size
                        var   polyPt = new PointF[4];
                        float hsize  = scaledSize / 3.0F;
                        polyPt[0].X = length;
                        polyPt[0].Y = 0;
                        polyPt[1].X = length - scaledSize;
                        polyPt[1].Y = hsize;
                        polyPt[2].X = length - scaledSize;
                        polyPt[2].Y = -hsize;
                        polyPt[3]   = polyPt[0];

                        using (var brush = new SolidBrush(_color))
                            // render the arrowhead
                            g.FillPolygon(brush, polyPt);
                    }
                    else
                    {
                        g.DrawLine(pen, 0, 0, length, 0);
                    }
                }

                // Restore the transform matrix back to its original state
                g.Transform = transform;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Calculate the <see cref="Legend"/> rectangle (<see cref="Rect"/>),
        /// taking into account the number of required legend
        /// entries, and the legend drawing preferences.
        /// </summary>
        /// <remarks>Adjust the size of the
        /// <see cref="Chart.Rect"/> for the parent <see cref="GraphPane"/> to accomodate the
        /// space required by the legend.
        /// </remarks>
        /// <param name="g">
        /// A graphic device object to be drawn into.  This is normally e.Graphics from the
        /// PaintEventArgs argument to the Paint() method.
        /// </param>
        /// <param name="pane">
        /// A reference to the <see cref="PaneBase"/> object that is the parent or
        /// owner of this object.
        /// </param>
        /// <param name="scaleFactor">
        /// The scaling factor to be used for rendering objects.  This is calculated and
        /// passed down by the parent <see cref="GraphPane"/> object using the
        /// <see cref="PaneBase.CalcScaleFactor"/> method, and is used to proportionally adjust
        /// font sizes, etc. according to the actual size of the graph.
        /// </param>
        /// <param name="tChartRect">
        /// The rectangle that contains the area bounded by the axes, in pixel units.
        /// <seealso cref="Chart.Rect" />
        /// </param>
        public void CalcRect(
            Graphics g,
            PaneBase pane,
            float scaleFactor,
            ref RectangleF tChartRect)
        {
            // Start with an empty rectangle
            Rect              = Rectangle.Empty;
            _hStack           = 1;
            _legendItemWidth  = 1;
            _legendItemHeight = 0;

            var clientRect = pane.CalcClientRect(g, scaleFactor);

            // If the legend is invisible, don't do anything
            if (!_isVisible)
            {
                return;
            }

            var nCurve = 0;

            var paneList = GetPaneList(pane);

            _tmpSize = GetMaxHeight(paneList, scaleFactor);

            float halfGap  = _tmpSize / 2.0F,
                  maxWidth = 0;
            var gapPix     = _gap * _tmpSize;

            foreach (var tmpPane in paneList)
            {
                // Loop through each curve in the curve list
                // Find the maximum width of the legend labels
                //foreach ( CurveItem curve in tmpPane.CurveList )
                //foreach ( CurveItem curve in GetIterator( tmpPane.CurveList, _isReverse ) )
                var count = tmpPane.CurveList.Count;
                for (var i = 0; i < count; i++)
                {
                    var curve = tmpPane.CurveList[_isReverse ? count - i - 1 : i];
                    if (curve._label._text == string.Empty || !curve._label._isVisible)
                    {
                        continue;
                    }
                    // Calculate the width of the label save the max width
                    var tmpFont = curve._label._fontSpec ?? FontSpec;

                    var tmpWidth = tmpFont.GetWidth(g, curve._label._text, scaleFactor);

                    if (tmpWidth > maxWidth)
                    {
                        maxWidth = tmpWidth;
                    }

                    // Save the maximum symbol height for line-type curves
                    if (curve is LineItem && ((LineItem)curve).Symbol.Size > _legendItemHeight)
                    {
                        _legendItemHeight = ((LineItem)curve).Symbol.Size;
                    }

                    nCurve++;
                }

                if (pane is MasterPane && ((MasterPane)pane).IsUniformLegendEntries)
                {
                    break;
                }
            }

            // Is this legend horizontally stacked?

            if (_isHStack)
            {
                // Determine the available space for horizontal stacking
                float widthAvail;
                switch (_position)
                {
                // Never stack if the legend is to the right or left
                case LegendPos.Right:
                case LegendPos.Left:
                    widthAvail = 0;
                    break;

                // for the top & bottom, the axis border width is available
                case LegendPos.Top:
                case LegendPos.TopCenter:
                case LegendPos.Bottom:
                case LegendPos.BottomCenter:
                    widthAvail = tChartRect.Width;
                    break;

                // for the top & bottom flush left, the panerect less margins is available
                case LegendPos.TopFlushLeft:
                case LegendPos.BottomFlushLeft:
                    widthAvail = clientRect.Width;
                    break;

                // for inside the axis area or Float, use 1/2 of the axis border width
                case LegendPos.InsideTopRight:
                case LegendPos.InsideTopLeft:
                case LegendPos.InsideBotRight:
                case LegendPos.InsideBotLeft:
                case LegendPos.Float:
                    widthAvail = tChartRect.Width / 2;
                    break;

                // shouldn't ever happen
                default:
                    widthAvail = 0;
                    break;
                }

                // width of one legend entry
                if (_isShowLegendSymbols)
                {
                    _legendItemWidth = 3.0f * _tmpSize + maxWidth;
                }
                else
                {
                    _legendItemWidth = 0.5f * _tmpSize + maxWidth;
                }

                // Calculate the number of columns in the legend
                // Normally, the legend is:
                //     available width / ( max width of any entry + space for line&symbol )
                if (maxWidth > 0)
                {
                    _hStack = (int)((widthAvail - halfGap) / _legendItemWidth);
                }

                // You can never have more columns than legend entries
                if (_hStack > nCurve)
                {
                    _hStack = nCurve;
                }

                // a saftey check
                if (_hStack == 0)
                {
                    _hStack = 1;
                }
            }
            else if (_isShowLegendSymbols)
            {
                _legendItemWidth = 3.0F * _tmpSize + maxWidth;
            }
            else
            {
                _legendItemWidth = 0.5F * _tmpSize + maxWidth;
            }

            // legend is:
            //   item:     space  line  space  text   space
            //   width:     wid  4*wid   wid  maxWid   wid
            // The symbol is centered on the line
            //
            // legend begins 3 * wid to the right of the plot rect
            //
            // The height of the legend is the actual height of the lines of text
            //   (nCurve * hite) plus wid on top and wid on the bottom

            // total legend width
            var totLegWidth = _hStack * _legendItemWidth;

            // The total legend height
            _legendItemHeight = _legendItemHeight * scaleFactor + halfGap;
            if (_tmpSize > _legendItemHeight)
            {
                _legendItemHeight = _tmpSize;
            }
            var totLegHeight = (float)Math.Ceiling(nCurve / (double)_hStack)
                               * _legendItemHeight;

            var newRect = new RectangleF();

            // Now calculate the legend rect based on the above determined parameters
            // Also, adjust the ChartRect to reflect the space for the legend
            if (nCurve > 0)
            {
                newRect = new RectangleF(0, 0, totLegWidth, totLegHeight);

                // The switch statement assigns the left and top edges, and adjusts the ChartRect
                // as required.  The right and bottom edges are calculated at the bottom of the switch.
                switch (_position)
                {
                case LegendPos.Right:
                    newRect.X = clientRect.Right - totLegWidth;
                    newRect.Y = tChartRect.Top;

                    tChartRect.Width -= totLegWidth + gapPix;
                    break;

                case LegendPos.Top:
                    newRect.X = tChartRect.Left;
                    newRect.Y = clientRect.Top;

                    tChartRect.Y      += totLegHeight + gapPix;
                    tChartRect.Height -= totLegHeight + gapPix;
                    break;

                case LegendPos.TopFlushLeft:
                    newRect.X = clientRect.Left;
                    newRect.Y = clientRect.Top;

                    tChartRect.Y      += totLegHeight + gapPix * 1.5f;
                    tChartRect.Height -= totLegHeight + gapPix * 1.5f;
                    break;

                case LegendPos.TopCenter:
                    newRect.X = tChartRect.Left + (tChartRect.Width - totLegWidth) / 2;
                    newRect.Y = tChartRect.Top;

                    tChartRect.Y      += totLegHeight + gapPix;
                    tChartRect.Height -= totLegHeight + gapPix;
                    break;

                case LegendPos.Bottom:
                    newRect.X = tChartRect.Left;
                    newRect.Y = clientRect.Bottom - totLegHeight;

                    tChartRect.Height -= totLegHeight + gapPix;
                    break;

                case LegendPos.BottomFlushLeft:
                    newRect.X = clientRect.Left;
                    newRect.Y = clientRect.Bottom - totLegHeight;

                    tChartRect.Height -= totLegHeight + gapPix;
                    break;

                case LegendPos.BottomCenter:
                    newRect.X = tChartRect.Left + (tChartRect.Width - totLegWidth) / 2;
                    newRect.Y = clientRect.Bottom - totLegHeight;

                    tChartRect.Height -= totLegHeight + gapPix;
                    break;

                case LegendPos.Left:
                    newRect.X = clientRect.Left;
                    newRect.Y = tChartRect.Top;

                    tChartRect.X     += totLegWidth + halfGap;
                    tChartRect.Width -= totLegWidth + gapPix;
                    break;

                case LegendPos.InsideTopRight:
                    newRect.X = tChartRect.Right - totLegWidth;
                    newRect.Y = tChartRect.Top;
                    break;

                case LegendPos.InsideTopLeft:
                    newRect.X = tChartRect.Left;
                    newRect.Y = tChartRect.Top;
                    break;

                case LegendPos.InsideBotRight:
                    newRect.X = tChartRect.Right - totLegWidth;
                    newRect.Y = tChartRect.Bottom - totLegHeight;
                    break;

                case LegendPos.InsideBotLeft:
                    newRect.X = tChartRect.Left;
                    newRect.Y = tChartRect.Bottom - totLegHeight;
                    break;

                case LegendPos.Float:
                    newRect.Location = Location.TransformTopLeft(pane, totLegWidth, totLegHeight);
                    break;
                }
            }

            Rect = newRect;
        }