Aspose.Slides.Examples.CSharp.Charts.DisplayPercentageAsLabels.Run C# (CSharp) Method

Run() public static method

public static Run ( ) : void
return void
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_Charts();

            // Create an instance of Presentation class
            Presentation presentation = new Presentation();

            ISlide slide = presentation.Slides[0];

            IChart chart = slide.Shapes.AddChart(ChartType.StackedColumn, 20, 20, 400, 400);
            IChartSeries series = chart.ChartData.Series[0];
            IChartCategory cat;
          
            double[] total_for_Cat = new double[chart.ChartData.Categories.Count];

            for (int k = 0; k < chart.ChartData.Categories.Count; k++)
            {
                cat = chart.ChartData.Categories[k];

                for (int i = 0; i < chart.ChartData.Series.Count; i++)
                {
                    total_for_Cat[k] = total_for_Cat[k] + Convert.ToDouble(chart.ChartData.Series[i].DataPoints[k].Value.Data);
                }
            }

            double dataPontPercent = 0f;

            for (int x = 0; x < chart.ChartData.Series.Count; x++)
            {
                series = chart.ChartData.Series[x];
                series.Labels.DefaultDataLabelFormat.ShowLegendKey = false;

                for (int j = 0; j < series.DataPoints.Count; j++)
                {
                    IDataLabel lbl = series.DataPoints[j].Label;
                    dataPontPercent = (Convert.ToDouble(series.DataPoints[j].Value.Data) / total_for_Cat[j]) * 100;

                    IPortion port = new Portion();
                    port.Text = String.Format("{0:F2} %", dataPontPercent);
                    port.PortionFormat.FontHeight = 8f;
                    lbl.TextFrameForOverriding.Text = "";
                    IParagraph para = lbl.TextFrameForOverriding.Paragraphs[0];
                    para.Portions.Add(port);

                    lbl.DataLabelFormat.ShowSeriesName = false;
                    lbl.DataLabelFormat.ShowPercentage = false;
                    lbl.DataLabelFormat.ShowLegendKey = false;
                    lbl.DataLabelFormat.ShowCategoryName = false;
                    lbl.DataLabelFormat.ShowBubbleSize = false;

                }

            }

            // Save presentation with chart
            presentation.Save(dataDir + "DisplayPercentageAsLabels_out.pptx", SaveFormat.Pptx);

        }
    }
DisplayPercentageAsLabels