Ejemplo n.º 1
0
        // vert axis line is drawn topy to bottomy.

        public float MapValueInverted(float value)
        {
            var topY          = (GraphicsSize.Height * RangeStart) + Margins.Top;
            var bottomY       = (GraphicsSize.Height * RangeEnd) - Margins.Bottom;
            var verticalRange = bottomY - topY;
            var valueRange    = ValueStart - ValueEnd;

            float coordinate = LineGraphHelper.MapValueToExactCoordinate(
                topY,
                bottomY,
                ValueStart,
                ValueEnd,
                value);

            return(coordinate);
        }
Ejemplo n.º 2
0
        private void graphPanel_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                if (Model == null)
                {
                    return;
                }

                using (Graphics g = e.Graphics)
                {
                    for (int fieldIdx = 0; fieldIdx < Model.FieldCount; fieldIdx++)
                    {
                        var field = Model.GetSeries(fieldIdx);

                        float minimumY             = (field.YAxis.RangeStart * e.ClipRectangle.Height) + field.YAxis.Margins.Top;
                        float maximumY             = (field.YAxis.RangeEnd * e.ClipRectangle.Height) - field.YAxis.Margins.Bottom;
                        float rangeY               = maximumY - minimumY;
                        float startYPosition       = field.YAxis.InvertRange ? -field.YAxis.EffectiveMaximum : field.YAxis.EffectiveMinimum;
                        float maxYPosition         = field.YAxis.InvertRange ? -field.YAxis.EffectiveMinimum : field.YAxis.EffectiveMaximum;
                        float scalingRangeMinimum  = field.YAxis.InvertRange ? -maximumY : minimumY;
                        float scalingRangeMaximum  = field.YAxis.InvertRange ? -minimumY : maximumY;
                        float scalingValuesMinimum = field.YAxis.InvertRange ? -field.YAxis.EffectiveMinimum : field.YAxis.EffectiveMinimum;
                        float scalingValuesMaximum = field.YAxis.InvertRange ? -field.YAxis.EffectiveMaximum : field.YAxis.EffectiveMaximum;

                        if (field.ShowMaximumWarning)
                        {
                            using (Pen maxWarningPen = new Pen(field.Color, field.LineThickness)
                            {
                                DashPattern = new float[] { 4.0F, 2.0F }
                            })
                            {
                                var maxWarningY = LineGraphHelper.Scale(scalingRangeMinimum, scalingRangeMaximum,
                                                                        scalingValuesMinimum, scalingValuesMaximum, field.MaxWarning.GetValueOrDefault(0));
                                g.DrawLine(maxWarningPen, 0, maxWarningY, graphPanel.Width, maxWarningY);
                            }
                        }
                        if (field.ShowMinimumWarning)
                        {
                            using (Pen minWarningPen = new Pen(field.Color, field.LineThickness)
                            {
                                DashPattern = new float[] { 4.0F, 2.0F }
                            })
                            {
                                var minWarningY = LineGraphHelper.Scale(scalingRangeMinimum, scalingRangeMaximum,
                                                                        scalingValuesMinimum, scalingValuesMaximum, field.MinWarning.GetValueOrDefault(1));
                                g.DrawLine(minWarningPen, 0, minWarningY, graphPanel.Width, minWarningY);
                            }
                        }

                        using (Pen pen = new Pen(field.Color, field.LineThickness))
                        {
                            float xScale = (float)Model.FrameCount / (float)graphPanel.Width;

                            float newX = 0;
                            float val  = Model.GetValue(_selectedLap, 0, fieldIdx);
                            float newY = LineGraphHelper.Scale(scalingRangeMinimum, scalingRangeMaximum,
                                                               scalingValuesMinimum, scalingValuesMaximum, val);


                            float lastPointX = newX;
                            float lastPointY = newY;

                            for (int frameidx = 1; frameidx < Model.FrameCount; frameidx++)
                            {
                                newX = xScale * frameidx;

                                val  = Model.GetValue(_selectedLap, frameidx, fieldIdx);
                                newY = LineGraphHelper.Scale(scalingRangeMinimum, scalingRangeMaximum,
                                                             scalingValuesMinimum, scalingValuesMaximum, val);

                                g.DrawLine(pen, lastPointX, lastPointY, newX, newY);

                                lastPointX = newX;
                                lastPointY = newY;
                            }
                        }
                    }

                    using (Pen selectedFramePen = new Pen(Color.Gray, 1F))
                    {
                        float selectedFrameX = graphPanel.Width * _selectedFramePercent;
                        g.DrawLine(selectedFramePen, selectedFrameX, 0, selectedFrameX, graphPanel.Height);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Ejemplo n.º 3
0
        private AxisPrintCoordinates GetPrintCoordinates(Size printArea, int offset)
        {
            Size actualyPrintArea = new Size(printArea.Width - offset, printArea.Height);

            AxisPrintCoordinates c = new AxisPrintCoordinates();

            c.offset = offset;

            // minimum X coordinate (starts at zero)
            c.minimumX = Margins.Left;

            // maximum X coordinate
            c.maximumX = actualyPrintArea.Width - Margins.Right;

            // x range
            c.rangeX = c.maximumX - c.minimumX;

            // X offset from index
            c.indexOffsetX = AxisIndexOffsetAmount;

            // minimum Y coordinate
            c.minimumY = (RangeStart * actualyPrintArea.Height) + Margins.Top;

            // maximum Y coordinate
            c.maximumY = (RangeEnd * actualyPrintArea.Height) - Margins.Bottom;

            // y range
            c.rangeY = c.maximumY - c.minimumY;

            // X coordinate of the vertical axis line (all other y positions should be based off of this value)
            c.axisVerticalLineX = (Position == YAxisPosition.Left) ?
                                  c.maximumX - c.indexOffsetX :
                                  c.indexOffsetX;

            if (c.axisVerticalLineX < MinimumVerticalLineXOffset)
            {
                c.axisVerticalLineX = MinimumVerticalLineXOffset;
            }

            // effective tick widths
            c.tickWidth = DefaultTickWidth;

            // outermost x coordinate (away from vertical axis line) for all ticks
            c.outermostTickX = (Position == YAxisPosition.Left) ?
                               c.axisVerticalLineX - c.tickWidth :
                               c.axisVerticalLineX + c.tickWidth;

            // actual tick X coordinates

            c.tickX = (Position == YAxisPosition.Left) ?
                      c.axisVerticalLineX - c.tickWidth :
                      c.axisVerticalLineX;

            // horizontal margin between tick line and label
            c.tickLineToLabelMargin = -2F;

            c.labelSize = TextRenderer.MeasureText((Maximum).ToString(Format), Font);

            c.maximumLablelWidth = c.labelSize.Width;

            // all labels are aligned vertically on the same Y coordinate.
            c.labelX = (Position == YAxisPosition.Left) ?
                       c.outermostTickX - c.tickLineToLabelMargin - c.maximumLablelWidth :
                       c.outermostTickX + c.tickLineToLabelMargin;

            var actualTitle = string.IsNullOrEmpty(Unit) ? Name : $"{Name} ({Unit})";

            if (DrawTitleVertically)
            {
                Size horizontalSize = TextRenderer.MeasureText(actualTitle, Font);
                // flip horizontal size to vertical
                c.titleSize = new Size(
                    horizontalSize.Height,
                    horizontalSize.Width);
            }
            else
            {
                c.titleSize = TextRenderer.MeasureText(actualTitle, Font);
            }

            c.titleX = (Position == YAxisPosition.Left) ?
                       c.labelX - c.titleToLabelMargin - c.titleSize.Width :
                       c.labelX + c.titleToLabelMargin;

            c.titleY = c.minimumY + ((c.maximumY - c.minimumY) / 2);

            c.TitleLabel = new TitleLabel()
            {
                Text = actualTitle,
                X    = c.titleX,
                Y    = c.titleY
            };

            float valueRange = Math.Abs(Maximum - Minimum);
            float largeStep  = TickStep == 0 ? Maximum / 4 : TickStep;

            float startYPosition       = InvertRange ? -Maximum : Minimum;
            float maxYPosition         = InvertRange ? -Minimum : Maximum;
            float scalingRangeMinimum  = InvertRange ? -c.maximumY : c.minimumY;
            float scalingRangeMaximum  = InvertRange ? -c.minimumY : c.maximumY;
            float scalingValuesMinimum = InvertRange ? -Minimum : Minimum;
            float scalingValuesMaximum = InvertRange ? -Maximum : Maximum;

            for (float i = startYPosition; i < maxYPosition; i += largeStep)
            {
                var largeStepValue = i;
                var largeScaledY   = LineGraphHelper.MapValueToExactCoordinate(scalingRangeMinimum, scalingRangeMaximum,
                                                                               scalingValuesMinimum, scalingValuesMaximum, largeStepValue);
                c.Ticks.Add(new Tick()
                {
                    X = c.tickX,
                    Y = largeScaledY
                });
                c.TickLabels.Add(new TickLabel()
                {
                    X    = c.labelX,
                    Y    = largeScaledY - (Font.SizeInPoints),
                    Text = FormatLabel(Math.Abs(Math.Round(largeStepValue, Precision)))
                });
            }

            var lastScaledY = LineGraphHelper.MapValueToExactCoordinate(scalingRangeMinimum, scalingRangeMaximum,
                                                                        scalingValuesMinimum, scalingValuesMaximum, scalingValuesMaximum);

            c.Ticks.Add(new Tick()
            {
                X = c.tickX,
                Y = Math.Abs(InvertRange ? scalingRangeMinimum : scalingRangeMaximum)
            });
            c.TickLabels.Add(new TickLabel()
            {
                X    = c.labelX,
                Y    = Math.Abs(InvertRange ? scalingRangeMinimum : scalingRangeMaximum) - (Font.SizeInPoints),
                Text = FormatLabel(Math.Abs(InvertRange ? scalingValuesMinimum : scalingValuesMaximum))
            });

            return(c);
        }
Ejemplo n.º 4
0
        private int PaintAxis(Graphics g, Size size, ILineGraphSeries series, int offset)
        {
            int axisWidth  = 0;
            int axisMargin = 2;

            try
            {
                g.SmoothingMode = smoothingMode;

                var topY              = (size.Height * series.RangeStart) + series.Margins.Top;
                var bottomY           = (size.Height * series.RangeEnd) - series.Margins.Bottom;
                var verticalRange     = bottomY - topY;
                var valueRange        = series.Maximum - series.Minimum;
                var verticalAxisLineX = offset + (series.Position == YAxisPosition.Left ? size.Width - 2 : 0);

                using (Pen axisPen = new Pen(series.Color, series.AxisBaseLineThickness))
                {
                    var axisBaseLineStartPoint = new PointF(verticalAxisLineX, topY);
                    var axisBaseLineEndPoint   = new PointF(verticalAxisLineX, bottomY);

                    g.DrawLine(
                        axisPen,
                        axisBaseLineStartPoint,
                        axisBaseLineEndPoint);

                    // Get the label formatting info
                    SizeF        labelSize   = g.MeasureString(series.Maximum.ToString(series.Format), series.Font);
                    StringFormat labelFormat = new StringFormat()
                    {
                        Alignment = StringAlignment.Far
                    };

                    axisWidth = DefaultTickWidth + (int)labelSize.Width;

                    int labelHorizontalOffset = (int)(series.Position == YAxisPosition.Left ?
                                                      -(DefaultTickWidth + labelSize.Width) :
                                                      DefaultTickWidth + labelSize.Width);

                    var ticks = LineGraphHelper.MapTicks(series, verticalAxisLineX, topY, bottomY);

                    if (series.InvertRange)
                    {
                        foreach (Tick tick in ticks.AllTicks)
                        {
                            var startX = series.Position == YAxisPosition.Left ? verticalAxisLineX - DefaultTickWidth : verticalAxisLineX;
                            var endX   = series.Position == YAxisPosition.Left ? verticalAxisLineX : verticalAxisLineX + DefaultTickWidth;

                            g.DrawLine(axisPen, new PointF(startX, tick.Y), new PointF(endX, tick.Y));

                            if (tick.Label != null)
                            {
                                using (Brush labelBrush = new SolidBrush(series.Color))
                                {
                                    RectangleF labelRectangle = new RectangleF(
                                        tick.Label.X + labelHorizontalOffset,
                                        tick.Label.Y - (labelSize.Height / 2),
                                        labelSize.Width,
                                        labelSize.Height);

                                    g.DrawString(
                                        tick.Label.Text,
                                        series.Font,
                                        labelBrush,
                                        labelRectangle,
                                        labelFormat);
                                }
                            }
                        }
                    }
                    else
                    {
                        float invertedTickStartY = topY + bottomY;
                        foreach (Tick tick in ticks.AllTicks)
                        {
                            var startX = series.Position == YAxisPosition.Left ? verticalAxisLineX - DefaultTickWidth : verticalAxisLineX;
                            var endX   = series.Position == YAxisPosition.Left ? verticalAxisLineX : verticalAxisLineX + DefaultTickWidth;

                            PointF tickStartPoint = new PointF(startX, invertedTickStartY - tick.Y);
                            PointF tickEndPoint   = new PointF(endX, invertedTickStartY - tick.Y);

                            g.DrawLine(
                                axisPen,
                                tickStartPoint,
                                tickEndPoint);

                            if (tick.Label != null)
                            {
                                using (Brush labelBrush = new SolidBrush(series.Color))
                                {
                                    RectangleF labelRectangle = new RectangleF(
                                        tick.Label.X + labelHorizontalOffset,
                                        invertedTickStartY - tick.Label.Y - (labelSize.Height / 2),
                                        labelSize.Width,
                                        labelSize.Height);

                                    g.DrawString(
                                        tick.Label.Text,
                                        series.Font,
                                        labelBrush,
                                        labelRectangle,
                                        labelFormat);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler(ex);
            }

            return(axisWidth + axisMargin);
        }