Example3_4.Legend.AddLegend C# (CSharp) Method

AddLegend() public method

public AddLegend ( Graphics g, DataCollection dc, ChartStyle cs ) : void
g System.Drawing.Graphics
dc DataCollection
cs ChartStyle
return void
        public void AddLegend(Graphics g, DataCollection dc, ChartStyle cs)
        {
            if (dc.DataSeriesList.Count < 1)
            {
                return;
            }
            if (!IsLegendVisible)
            {
                return;
            }
            int numberOfDataSeries = dc.DataSeriesList.Count;
            string[] legendLabels = new string[dc.DataSeriesList.Count];
            int n = 0;
            foreach (DataSeries ds in dc.DataSeriesList)
            {
                legendLabels[n] = ds.SeriesName;
                n++;
            }
            float offSet = 10;
            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 + 50.0f;
            float hWidth = legendWidth / 2;
            float legendHeight = 18.0f * numberOfDataSeries;
            float hHeight = legendHeight / 2;

            switch (LegendPosition)
            {
                case LegendPositionEnum.East:
                    xc = cs.PlotArea.X + cs.PlotArea.Width - offSet - hWidth;
                    yc = cs.PlotArea.Y + cs.PlotArea.Height / 2;
                    break;
                case LegendPositionEnum.North:
                    xc = cs.PlotArea.X + cs.PlotArea.Width / 2;
                    yc = cs.PlotArea.Y + offSet + hHeight;
                    break;
                case LegendPositionEnum.NorthEast:
                    xc = cs.PlotArea.X + cs.PlotArea.Width - offSet - hWidth;
                    yc = cs.PlotArea.Y + offSet + hHeight;
                    break;
                case LegendPositionEnum.NorthWest:
                    xc = cs.PlotArea.X + offSet + hWidth;
                    yc = cs.PlotArea.Y + offSet + hHeight;
                    break;
                case LegendPositionEnum.South:
                    xc = cs.PlotArea.X + cs.PlotArea.Width / 2;
                    yc = cs.PlotArea.Y + cs.PlotArea.Height - offSet - hHeight;
                    break;
                case LegendPositionEnum.SouthEast:
                    xc = cs.PlotArea.X + cs.PlotArea.Width - offSet - hWidth;
                    yc = cs.PlotArea.Y + cs.PlotArea.Height - offSet - hHeight;
                    break;
                case LegendPositionEnum.SouthWest:
                    xc = cs.PlotArea.X + offSet + hWidth;
                    yc = cs.PlotArea.Y + cs.PlotArea.Height - offSet - hHeight;
                    break;
                case LegendPositionEnum.West:
                    xc = cs.PlotArea.X + offSet + hWidth;
                    yc = cs.PlotArea.Y + cs.PlotArea.Height / 2;
                    break;
            }
            DrawLegend(g, xc, yc, hWidth, hHeight, dc, cs);
        }

Usage Example

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

            g.Clear(backColor);

            cs.ChartArea = this.ClientRectangle;
            AddData();
            SetPlotArea(g);
            cs.AddChartStyle(g);
            dc.AddLines(g, cs);
            lg.AddLegend(g, dc, cs);
            g.Dispose();
        }
All Usage Examples Of Example3_4.Legend::AddLegend