Pie Chart's Legend doesn't fit in 1 page

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v7)  /  Pie Chart's Legend doesn't fit in 1 page

DynamicPDF CoreSuite for .NET (v7) Forum

Hi I have some problems here with creating a new dynamic pages based on content.
I am creating a Pie chart and it's already done. Great result with small data (1 page).
Unfortunately I Have about 200 data / Chart-Legend value (automatically created by sql server's stored procedure) and it doesn't fit in 1 page.
What I need is to make the legends moved below the chart and divided into 2 columns with it's value and the rest is automatically generated in 2nd page, 3rd page etc until it reach the end of the legend contents.

I hope you'd like to help me soon.

Regards,
Linggar
Posted by a ceTe Software moderator
Hello Linngar,

The default chart legend does not have the capability to flow to multiple pages. However, you can turn off the default chart legend and build your own custom legend using the Table2 class. Here is the sample code:

            Document document = new Document();
            Page page = new Page(PageSize.A4, PageOrientation.Landscape);
            document.Pages.Add(page);

            Chart chart = new Chart(0, 0, 400, 400);
            PlotArea plotArea = chart.PlotAreas.Add(50, 50, 200, 200);
            PieSeries pieSeries = new PieSeries();

            plotArea.Series.Add(pieSeries);
            int j = 10;
            for (int i = 1; i < 200; i++)
            {
                PieSeriesElement pieSeriesElement1 = new PieSeriesElement(i + j, "Website " + i);
                pieSeries.Elements.Add(pieSeriesElement1);
                j = j + 10;
            }
            chart.Layout();
            chart.AutoLayout = false;
            //pieSeries.Radius = 200;
            Table2 legendTable = new Table2(0, 0, 200, 10);
            legendTable.Border.Width = 0;
            legendTable.CellDefault.Border.Width = 0;
            Column2 column1 = legendTable.Columns.Add(30);
            Column2 column2 = legendTable.Columns.Add(170);

            for (int k = 0; k < pieSeries.Elements.Count; k++)
            {
                Color colorObj = pieSeries.Elements[k].Color;
                Rectangle rect = new Rectangle(0, 0, 10, 10);
                rect.FillColor = colorObj;
                string strpieLabel = pieSeries.Elements[k].Name;
                Row2 row = legendTable.Rows.Add();
                Cell2 cell1 = row.Cells.Add(rect);
                cell1.Align = TextAlign.Right;
                Cell2 cell2 = row.Cells.Add(strpieLabel);
            }
            legendTable.Height = legendTable.GetRequiredHeight();
            pieSeries.Legend.Visible = false;
            page.Elements.Add(chart);
            float xPosition = 0;
            float yPosition = chart.PlotAreas[0].Y + chart.PlotAreas[0].Height;
            float avalaibleSpace = page.Dimensions.Body.Height - yPosition;

            if (avalaibleSpace > 20)
            {
                legendTable.Height = avalaibleSpace;
                legendTable.X = xPosition;
                legendTable.Y = yPosition;
            }
            page.Elements.Add(legendTable);
            legendTable = legendTable.GetOverflowRows();
            xPosition = legendTable.X + legendTable.Width;
            legendTable.Height = avalaibleSpace;
            legendTable.X = xPosition;
            legendTable.Y = yPosition;
            page.Elements.Add(legendTable);
            legendTable = legendTable.GetOverflowRows();
            avalaibleSpace = page.Dimensions.Body.Height - (legendTable.Y + legendTable.Height);
            legendTable.Height = legendTable.GetRequiredHeight();

            if (legendTable != null)
            {
                do
                {
                    Page newpage = new Page();
                    document.Pages.Add(newpage);
                    xPosition = 0;
                    yPosition = 0;
                    if (legendTable.GetRequiredHeight() < newpage.Dimensions.Body.Height)
                    {
                        legendTable.Height = legendTable.GetRequiredHeight();
                    }
                    else
                    {
                        legendTable.Height = newpage.Dimensions.Body.Height;
                    }
                    legendTable.X = xPosition;
                    legendTable.Y = yPosition;
                    newpage.Elements.Add(legendTable);
                    legendTable = legendTable.GetOverflowRows();
                    if (legendTable != null)
                    {
                        xPosition = legendTable.X + legendTable.Width + 10;
                        yPosition = 0;
                        legendTable.X = xPosition;
                        legendTable.Y = yPosition;
                        newpage.Elements.Add(legendTable);
                        legendTable = legendTable.GetOverflowRows();
                    }

                } while (legendTable != null);
            }

            document.Draw(@"output.pdf");
          

Thanks,
ceTe Software Support Team.
Hi, thank you so much for your best support.
This is exactly what I need a.k.a PROBLEM SOLVED!

Regards,
Linggar

All times are US Eastern Standard time. The time now is 1:42 AM.