Example4_6.Legend.AddLegend C# (CSharp) Method

AddLegend() public method

public AddLegend ( Graphics g, DataSeries ds, ChartStyle cs ) : void
g System.Drawing.Graphics
ds DataSeries
cs ChartStyle
return void
        public void AddLegend(Graphics g, DataSeries ds, ChartStyle cs)
        {
            if (ds.DataList.Count < 1)
            {
                return;
            }
            if (!IsLegendVisible)
            {
                return;
            }
            int numberOfDataValues = ds.DataList.Count;
            string[] legendLabels = new string[ds.LabelList.Count];
            for (int i = 0; i < ds.LabelList.Count;i++)
            {
                legendLabels[i] = (string)ds.LabelList[i];
            }
            // float offSet = 20;
            float xc = 0f;
            float yc = 0f;
            SizeF size = g.MeasureString(legendLabels[0], LegendFont);
            float legendWidth = size.Width;
            for (int i = 0; i < legendLabels.Length; i++)
            {
                size = g.MeasureString(legendLabels[i], LegendFont);
                float tempWidth = size.Width;
                if (legendWidth < tempWidth)
                    legendWidth = tempWidth;
            }
            legendWidth = legendWidth + 35.0f;
            float hWidth = legendWidth / 2;
            float legendHeight = 18.0f * numberOfDataValues;
            float hHeight = legendHeight / 2;

            Rectangle rect = cs.SetPieArea();
            xc = rect.X + rect.Width + cs.Offset + 20 + hWidth / 2;
            yc = rect.Y + rect.Height / 2;
            DrawLegend(g, xc, yc, hWidth, hHeight, ds, cs);
        }

Usage Example

Example #1
0
        private void PlotPanelPaint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            g.SmoothingMode = SmoothingMode.AntiAlias;
            AddData();
            ds.AddPie(g, cs);
            lg.AddLegend(g, ds, cs);
        }