Example3_7.ChartStyle.AddLabels C# (CSharp) Method

AddLabels() private method

private AddLabels ( Graphics g ) : void
g System.Drawing.Graphics
return void
        private void AddLabels(Graphics g)
        {
            float xOffset = ChartArea.Width / 30.0f;
            float yOffset = ChartArea.Height / 30.0f;
            SizeF labelFontSize = g.MeasureString("A", LabelFont);
            SizeF titleFontSize = g.MeasureString("A", TitleFont);

            // Add horizontal axis label:
            SolidBrush aBrush = new SolidBrush(LabelFontColor);
            SizeF stringSize = g.MeasureString(XLabel, LabelFont);
            g.DrawString(XLabel, LabelFont, aBrush,
                new Point(PlotArea.Left + PlotArea.Width / 2 -
                (int)stringSize.Width / 2, ChartArea.Bottom -
                (int)yOffset - (int)labelFontSize.Height));

            // Add y-axis label:
            StringFormat sFormat = new StringFormat();
            sFormat.Alignment = StringAlignment.Center;
            stringSize = g.MeasureString(YLabel, LabelFont);
            // Save the state of the current Graphics object
            GraphicsState gState = g.Save();
            g.TranslateTransform(ChartArea.X + xOffset, ChartArea.Y
                + yOffset + titleFontSize.Height
                + yOffset / 3 + PlotArea.Height / 2);
            g.RotateTransform(-90);
            g.DrawString(YLabel, LabelFont, aBrush, 0, 0, sFormat);
            // Restore it:
            g.Restore(gState);

            // Add y2-axis label:
            if (IsY2Axis)
            {
                stringSize = g.MeasureString(Y2Label, LabelFont);
                // Save the state of the current Graphics object
                GraphicsState gState2 = g.Save();
                g.TranslateTransform(ChartArea.X + ChartArea.Width -
                    xOffset - labelFontSize.Width,
                    ChartArea.Y + yOffset + titleFontSize.Height
                    + yOffset / 3 + PlotArea.Height / 2);
                g.RotateTransform(-90);
                g.DrawString(Y2Label, LabelFont, aBrush, 0, 0, sFormat);
                // Restore it:
                g.Restore(gState2);
            }

            // Add title:
            aBrush = new SolidBrush(TitleFontColor);
            stringSize = g.MeasureString(Title, TitleFont);
            if (Title.ToUpper() != "NO TITLE")
            {
                g.DrawString(Title, TitleFont, aBrush,
                    new Point(PlotArea.Left + PlotArea.Width / 2 -
                    (int)stringSize.Width / 2, ChartArea.Top + (int)yOffset));
            }
            aBrush.Dispose();
        }