Example4_6.Legend.DrawLegend C# (CSharp) Method

DrawLegend() private method

private DrawLegend ( Graphics g, float xCenter, float yCenter, float hWidth, float hHeight, DataSeries ds, ChartStyle cs ) : void
g System.Drawing.Graphics
xCenter float
yCenter float
hWidth float
hHeight float
ds DataSeries
cs ChartStyle
return void
        private void DrawLegend(Graphics g, float xCenter, float yCenter, 
            float hWidth, float hHeight, DataSeries ds, ChartStyle cs)
        {
            float spacing = 8.0f;
            float textHeight = 8.0f;
            float htextHeight = textHeight / 2.0f;
            float lineLength = 12.0f;
            float hlineLength = lineLength / 2.0f;
            Rectangle legendRectangle;
            Pen aPen = new Pen(LegendBorderColor, 1f);
            SolidBrush aBrush = new SolidBrush(LegendBackColor);

            if (isLegendVisible)
            {
                legendRectangle = new Rectangle((int)xCenter - (int)hWidth,
                    (int)yCenter - (int)hHeight,
                    (int)(2.0f * hWidth), (int)(2.0f * hHeight));
                g.FillRectangle(aBrush, legendRectangle);
                if (IsBorderVisible)
                {
                    g.DrawRectangle(aPen, legendRectangle);
                }

                for (int i = 0; i < ds.DataList.Count; i++)
                {
                    float xSymbol = legendRectangle.X + spacing + hlineLength;
                    float xText = legendRectangle.X + 2 * spacing + lineLength;
                    float yText = legendRectangle.Y + (i + 1) * spacing +
                        (2 * i + 1) * htextHeight;
                    aPen = new Pen(ds.BorderColor, ds.BorderThickness);
                    Color fillColor = Color.FromArgb(ds.CMap[i, 0], ds.CMap[i, 1],
                                ds.CMap[i, 2], ds.CMap[i, 3]);
                    aBrush = new SolidBrush(fillColor);
                    // Draw symbols:
                    float hsize = 5f;
                    PointF[] pts = new PointF[4];
                    pts[0] = new PointF(xSymbol - hsize, yText - hsize);
                    pts[1] = new PointF(xSymbol + hsize, yText - hsize);
                    pts[2] = new PointF(xSymbol + hsize, yText + hsize);
                    pts[3] = new PointF(xSymbol - hsize, yText + hsize);
                    g.FillPolygon(aBrush, pts);
                    g.DrawPolygon(aPen, pts);
                    // Draw text:
                    StringFormat sFormat = new StringFormat();
                    sFormat.Alignment = StringAlignment.Near;
                    g.DrawString((string)ds.LabelList[i], LegendFont,
                        new SolidBrush(TextColor),
                        new PointF(xText, yText - 8), sFormat);
                }
            }
            aPen.Dispose();
            aBrush.Dispose();
        }