legend

legend

Forum member · Sep 3, 2015
how to add legends in piecharts common function please reply // Create a PDF Document Document document = new Document(); // Create a Page and add it to the document Page page = new Page(PageSize.A4, PageOrientation.Landscape); document.Pages.Add(page); float xPosition = 0; float yPosition = 0; float chartWidth = 300; float chartHeight = 400; float[] pieValues = new float[] {27,19,21}; Chart chart = CreatePiChart(xPosition, yPosition, chartWidth, chartHeight, pieValues); // Add the chart to the page page.Elements.Add(chart); float[] newPieValues = new float[] {50, 20, 92 }; Chart chart1 = CreatePiChart(xPosition + chart.Width + 10, yPosition, chart.Width, chart.Height, newPieValues); page.Elements.Add(chart1); // Save the pdf document.Draw(@"C:/Temp/MyDocument.pdf"); Code for CreatePieChart function. private static Chart CreatePiChart(float x, float y, float widht, float height, float[] pievalues) { // Create a chart Chart chart = new Chart(x, y, widht, height); // Add a plot area to the chart PlotArea plotArea = chart.PlotAreas.Add(10, 10, widht-10, height-10); // Create the Header titles and add it to the chart Title tTitle = new Title("Website Viewers (in millions)"); Title tTitle1 = new Title("Year 2007"); chart.HeaderTitles.Add(tTitle); chart.HeaderTitles.Add(tTitle1); // Create a scalar datalabel ScalarDataLabel da = new ScalarDataLabel(true, false, false); // Create a pie series PieSeries pieSeries = new PieSeries(); // Set scalar datalabel to the pie series pieSeries.DataLabel = da; // Add series to the plot area plotArea.Series.Add(pieSeries); //Add pie series elements to the pie series pieSeries.Elements.Add(pievalues); return chart; }

Re: legend

Support · Sep 7, 2015
Hello, The code you posted above is adding array of pie values directly to the PieSeries and this will set the default names to the legend elements. If you would like to set the custom names for the legend elements then you will need to add the values for the pie elements by looping through the list of pie values and name them accordingly. You can use the Add method overload of Elements class which takes pie value and name for the element as arguments. Below is the modified code for it. // Create a PDF Document Document document = new Document(); // Create a Page and add it to the document Page page = new Page(PageSize.A4, ceTe.DynamicPDF.PageOrientation.Landscape); document.Pages.Add(page); float xPosition = 0; float yPosition = 0; float chartWidth = 300; float chartHeight = 400; float[] pieValues = new float[] { 40, 30, 20,10 }; string[] pieNames = new string[] {"Enery","IT","Health Care","Multi Sector" }; RgbColor[] colorList1 = new RgbColor[] { RgbColor.Red, RgbColor.Blue, RgbColor.Brown,RgbColor.Purple }; Chart chart = CreatePiChart(xPosition, yPosition, chartWidth, chartHeight, pieValues, colorList1, pieNames); // Add the chart to the page page.Elements.Add(chart); float[] newPieValues = new float[] { 33.33f, 22.22F, 22.22f, 17.78f,4.45f }; RgbColor[] newColorList = new RgbColor[] { RgbColor.Chocolate, RgbColor.DarkGray, RgbColor.DeepPink, RgbColor.Gold ,RgbColor.Green}; string[] newPieNames = new string[] { "AAA", "AA", "A", "BBB","Others" }; Chart chart1 = CreatePiChart(xPosition + chart.Width + 10, yPosition, chart.Width, chart.Height, newPieValues, newColorList, newPieNames); page.Elements.Add(chart1); // Save the pdf document.Draw(@"C:/Temp/PieChart.pdf"); Code for CreatePieChart function. private static Chart CreatePiChart(float x, float y, float widht, float height, float[] pievalues, RgbColor[] colorArr, string[] pieNames) { // Create a chart Chart chart = new Chart(x, y, widht, height); // Add a plot area to the chart PlotArea plotArea = chart.PlotAreas.Add(10, 10, widht - 10, height - 10); //Create the Header titles and add it to the chart Title tTitle = new Title("Website Viewers (in millions)"); Title tTitle1 = new Title("Year 2014"); chart.HeaderTitles.Add(tTitle); chart.HeaderTitles.Add(tTitle1); // Create a scalar datalabel ScalarDataLabel da = new ScalarDataLabel(true, false, false); // Create a pie series PieSeries pieSeries = new PieSeries(); // Set scalar datalabel to the pie series pieSeries.DataLabel = da; // Add series to the plot area plotArea.Series.Add(pieSeries); //Add pie series elements to the pie series for(int i=0;i<pievalues.Length;i++) { pieSeries.Elements.Add(pievalues[i],pieNames[i]); pieSeries.Elements[i].Color = colorArr[i]; } pieSeries.DataLabelPosition = ScalarDataLabelPosition.Outside; return chart; } Thanks, ceTe Software Support Team.