Example3_7.ChartStyle.AddChartStyle C# (CSharp) Method

AddChartStyle() public method

public AddChartStyle ( Graphics g ) : void
g System.Drawing.Graphics
return void
        public void AddChartStyle(Graphics g)
        {
            // Draw TotalChartArea, ChartArea, and PlotArea:
            SetPlotArea(g);
            Pen aPen = new Pen(ChartBorderColor, 1f);
            SolidBrush aBrush = new SolidBrush(ChartBackColor);
            g.FillRectangle(aBrush, ChartArea);
            g.DrawRectangle(aPen, ChartArea);
            aPen = new Pen(PlotBorderColor, 1f);
            aBrush = new SolidBrush(PlotBackColor);
            g.FillRectangle(aBrush, PlotArea);
            g.DrawRectangle(aPen, PlotArea);

            SizeF tickFontSize = g.MeasureString("A", TickFont);
            // Create vertical gridlines:
            float fX, fY;
            if (IsYGrid == true)
            {
                aPen = new Pen(GridColor, 1f);
                aPen.DashStyle = GridPattern;
                for (fX = XLimMin + XTick; fX < XLimMax; fX += XTick)
                {
                    g.DrawLine(aPen, Point2D(new PointF(fX, YLimMin)),
                        Point2D(new PointF(fX, YLimMax)));
                }
            }

            // Create horizontal gridlines:
            if (IsXGrid == true)
            {
                aPen = new Pen(GridColor, 1f);
                aPen.DashStyle = GridPattern;
                for (fY = YLimMin + YTick; fY < YLimMax; fY += YTick)
                {
                    g.DrawLine(aPen, Point2D(new PointF(XLimMin, fY)),
                        Point2D(new PointF(XLimMax, fY)));
                }
            }

            // Create the x-axis tick marks:
            aBrush = new SolidBrush(TickFontColor);
            for (fX = XLimMin; fX <= XLimMax; fX += XTick)
            {
                PointF yAxisPoint = Point2D(new PointF(fX, YLimMin));
                g.DrawLine(Pens.Black, yAxisPoint, new PointF(yAxisPoint.X,
                                   yAxisPoint.Y - 5f));
                StringFormat sFormat = new StringFormat();
                sFormat.Alignment = StringAlignment.Far;
                SizeF sizeXTick = g.MeasureString(fX.ToString(), TickFont);
                g.DrawString(fX.ToString(), TickFont, aBrush,
                    new PointF(yAxisPoint.X + sizeXTick.Width / 2,
                    yAxisPoint.Y + 4f), sFormat);
            }

            // Create the y-axis tick marks:
            for (fY = YLimMin; fY <= YLimMax; fY += YTick)
            {
                PointF xAxisPoint = Point2D(new PointF(XLimMin, fY));
                g.DrawLine(Pens.Black, xAxisPoint,
                    new PointF(xAxisPoint.X + 5f, xAxisPoint.Y));
                StringFormat sFormat = new StringFormat();
                sFormat.Alignment = StringAlignment.Far;
                g.DrawString(fY.ToString(), TickFont, aBrush,
                    new PointF(xAxisPoint.X - 3f,
                    xAxisPoint.Y - tickFontSize.Height / 2), sFormat);
            }

            // Create the y2-axis tick marks:
            if (IsY2Axis)
            {
                for (fY = Y2LimMin; fY <= Y2LimMax; fY += Y2Tick)
                {
                    PointF x2AxisPoint = Point2DY2(new PointF(XLimMax, fY));
                    g.DrawLine(Pens.Black, x2AxisPoint,
                        new PointF(x2AxisPoint.X - 5f, x2AxisPoint.Y));
                    StringFormat sFormat = new StringFormat();
                    sFormat.Alignment = StringAlignment.Near;
                    g.DrawString(fY.ToString(), TickFont, aBrush,
                        new PointF(x2AxisPoint.X + 3f,
                        x2AxisPoint.Y - tickFontSize.Height / 2), sFormat);
                }
            }
            aPen.Dispose();
            aBrush.Dispose();
            AddLabels(g);
        }

Usage Example

Exemplo n.º 1
0
        public override void DrawRect(CGRect dirtyRect)
        {
            var g = Graphics.FromCurrentContext();

            g.Clear(backColor);

            // Re-define TotalChartArea for resiz-redraw:
            sc.TotalChartArea = this.ClientRectangle;

            // Add data for all sub-charts:
            AddData(g);

            // Create sub-chart layout:
            Rectangle[,] subchart = sc.SetSubChart(g);

            // Create sub-chart 1:
            cs1.ChartArea = subchart[0, 0];
            cs1.AddChartStyle(g);
            dc1.AddLines(g, cs1);

            // Create sub-chart 2:
            cs2.ChartArea = subchart[0, 1];
            cs2.AddChartStyle(g);
            dc2.AddLines(g, cs2);

            // Create sub-chart 3:
            cs3.ChartArea = subchart[1, 0];
            cs3.AddChartStyle(g);
            dc3.AddLines(g, cs3);

            // Create sub-chart 4:
            cs4.ChartArea = subchart[1, 1];
            cs4.AddChartStyle(g);
            dc4.AddLines(g, cs4);
            lg.AddLegend(g, dc4, cs4);
            g.Dispose();
        }