Пример #1
0
        public static void Run()
        {
            //ExStart:AddColorToDataPoints

            using (Presentation pres = new Presentation())
            {
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_Charts();


                IChart chart = pres.Slides[0].Shapes.AddChart(ChartType.Sunburst, 100, 100, 450, 400);

                IChartDataPointCollection dataPoints = chart.ChartData.Series[0].DataPoints;
                dataPoints[3].DataPointLevels[0].Label.DataLabelFormat.ShowValue = true;



                IDataLabel branch1Label = dataPoints[0].DataPointLevels[2].Label;
                branch1Label.DataLabelFormat.ShowCategoryName = false;
                branch1Label.DataLabelFormat.ShowSeriesName   = true;

                branch1Label.DataLabelFormat.TextFormat.PortionFormat.FillFormat.FillType             = FillType.Solid;
                branch1Label.DataLabelFormat.TextFormat.PortionFormat.FillFormat.SolidFillColor.Color = Color.Yellow;


                IFormat steam4Format = dataPoints[9].Format;
                steam4Format.Fill.FillType = FillType.Solid;

                steam4Format.Fill.SolidFillColor.Color = Color.FromArgb(0, 176, 240, 255);

                pres.Save(dataDir + "AddColorToDataPoints.pptx", SaveFormat.Pptx);
            }
            //ExEnd:AddColorToDataPoints
        }
        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);
        }
        /// <summary>
        /// Overloaded constructor used create an instance of the DataLabelModel class during serialization.
        /// The public properties of this class are set by this contructor as they are obtained from the
        /// IDataLabel interface object passed as the parameter.
        /// </summary>
        /// <param name="dataLabel">Specifies the IDataLabel interface object to be serialized.</param>
        /// <param name="chart">Specifies the IChart interface object of the parent IChart object being serialized.</param>
        public DataLabelModel(IDataLabel dataLabel, IChart chart)
        {
            Border         = dataLabel.Border;
            ConnectingLine = dataLabel.ConnectingLine;
            Content        = dataLabel.Content;
            Offset         = dataLabel.Offset;
            Position       = dataLabel.Position;

            Style       = StyleSerializer.StyleToString(dataLabel.GetStyle("_"), chart as IBrushConverter);
            BorderStyle = StyleSerializer.StyleToString(dataLabel.GetStyle("_Border"), chart as IBrushConverter);
        }
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_Charts();

            // Create directory if it is not already present.
            bool IsExists = System.IO.Directory.Exists(dataDir);

            if (!IsExists)
            {
                System.IO.Directory.CreateDirectory(dataDir);
            }

            // Instantiate Presentation class that represents PPTX file
            Presentation pres = new Presentation();

            // Access first slide
            ISlide sld = pres.Slides[0];

            // Add chart with default data
            IChart chart = sld.Shapes.AddChart(ChartType.ClusteredColumn, 0, 0, 500, 500);

            // Setting chart Title
            // Chart.ChartTitle.TextFrameForOverriding.Text = "Sample Title";
            chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
            chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
            chart.ChartTitle.Height = 20;
            chart.HasTitle          = true;

            // Set first series to Show Values
            chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;

            // Setting the index of chart data sheet
            int defaultWorksheetIndex = 0;

            // Getting the chart data worksheet
            IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;

            // Delete default generated series and categories
            chart.ChartData.Series.Clear();
            chart.ChartData.Categories.Clear();
            int s = chart.ChartData.Series.Count;

            s = chart.ChartData.Categories.Count;

            // Adding new series
            chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type);
            chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type);

            // Adding new categories
            chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
            chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
            chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));

            // Take first chart series
            IChartSeries series = chart.ChartData.Series[0];

            // Now populating series data

            series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
            series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
            series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));

            // Setting fill color for series
            series.Format.Fill.FillType             = FillType.Solid;
            series.Format.Fill.SolidFillColor.Color = Color.Red;


            // Take second chart series
            series = chart.ChartData.Series[1];

            // Now populating series data
            series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30));
            series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10));
            series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60));

            // Setting fill color for series
            series.Format.Fill.FillType             = FillType.Solid;
            series.Format.Fill.SolidFillColor.Color = Color.Green;

            // First label will be show Category name
            IDataLabel lbl = series.DataPoints[0].Label;

            lbl.DataLabelFormat.ShowCategoryName = true;

            lbl = series.DataPoints[1].Label;
            lbl.DataLabelFormat.ShowSeriesName = true;

            // Show value for third label
            lbl = series.DataPoints[2].Label;
            lbl.DataLabelFormat.ShowValue      = true;
            lbl.DataLabelFormat.ShowSeriesName = true;
            lbl.DataLabelFormat.Separator      = "/";

            // Save presentation with chart
            pres.Save(dataDir + "AsposeChart_out.pptx", SaveFormat.Pptx);
        }
Пример #5
0
        public static void Run()
        {
            //ExStart:PieChart
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_Charts();

            // Instantiate Presentation class that represents PPTX file
            Presentation presentation = new Presentation();

            // Access first slide
            ISlide slides = presentation.Slides[0];

            // Add chart with default data
            IChart chart = slides.Shapes.AddChart(ChartType.Pie, 100, 100, 400, 400);

            // Setting chart Title
            chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
            chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
            chart.ChartTitle.Height = 20;
            chart.HasTitle          = true;

            // Set first series to Show Values
            chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;

            // Setting the index of chart data sheet
            int defaultWorksheetIndex = 0;

            // Getting the chart data worksheet
            IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;

            // Delete default generated series and categories
            chart.ChartData.Series.Clear();
            chart.ChartData.Categories.Clear();

            // Adding new categories
            chart.ChartData.Categories.Add(fact.GetCell(0, 1, 0, "First Qtr"));
            chart.ChartData.Categories.Add(fact.GetCell(0, 2, 0, "2nd Qtr"));
            chart.ChartData.Categories.Add(fact.GetCell(0, 3, 0, "3rd Qtr"));

            // Adding new series
            IChartSeries series = chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, "Series 1"), chart.Type);

            // Now populating series data
            series.DataPoints.AddDataPointForPieSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20));
            series.DataPoints.AddDataPointForPieSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50));
            series.DataPoints.AddDataPointForPieSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30));

            // Not working in new version
            // Adding new points and setting sector color
            // series.IsColorVaried = true;
            chart.ChartData.SeriesGroups[0].IsColorVaried = true;

            IChartDataPoint point = series.DataPoints[0];

            point.Format.Fill.FillType             = FillType.Solid;
            point.Format.Fill.SolidFillColor.Color = Color.Cyan;
            // Setting Sector border
            point.Format.Line.FillFormat.FillType             = FillType.Solid;
            point.Format.Line.FillFormat.SolidFillColor.Color = Color.Gray;
            point.Format.Line.Width     = 3.0;
            point.Format.Line.Style     = LineStyle.ThinThick;
            point.Format.Line.DashStyle = LineDashStyle.DashDot;

            IChartDataPoint point1 = series.DataPoints[1];

            point1.Format.Fill.FillType             = FillType.Solid;
            point1.Format.Fill.SolidFillColor.Color = Color.Brown;

            // Setting Sector border
            point1.Format.Line.FillFormat.FillType             = FillType.Solid;
            point1.Format.Line.FillFormat.SolidFillColor.Color = Color.Blue;
            point1.Format.Line.Width     = 3.0;
            point1.Format.Line.Style     = LineStyle.Single;
            point1.Format.Line.DashStyle = LineDashStyle.LargeDashDot;

            IChartDataPoint point2 = series.DataPoints[2];

            point2.Format.Fill.FillType             = FillType.Solid;
            point2.Format.Fill.SolidFillColor.Color = Color.Coral;

            // Setting Sector border
            point2.Format.Line.FillFormat.FillType             = FillType.Solid;
            point2.Format.Line.FillFormat.SolidFillColor.Color = Color.Red;
            point2.Format.Line.Width     = 2.0;
            point2.Format.Line.Style     = LineStyle.ThinThin;
            point2.Format.Line.DashStyle = LineDashStyle.LargeDashDotDot;

            // Create custom labels for each of categories for new series
            IDataLabel lbl1 = series.DataPoints[0].Label;

            // lbl.ShowCategoryName = true;
            lbl1.DataLabelFormat.ShowValue = true;

            IDataLabel lbl2 = series.DataPoints[1].Label;

            lbl2.DataLabelFormat.ShowValue      = true;
            lbl2.DataLabelFormat.ShowLegendKey  = true;
            lbl2.DataLabelFormat.ShowPercentage = true;

            IDataLabel lbl3 = series.DataPoints[2].Label;

            lbl3.DataLabelFormat.ShowSeriesName = true;
            lbl3.DataLabelFormat.ShowPercentage = true;

            // Showing Leader Lines for Chart
            series.Labels.DefaultDataLabelFormat.ShowLeaderLines = true;

            // Setting Rotation Angle for Pie Chart Sectors
            chart.ChartData.SeriesGroups[0].FirstSliceAngle = 180;

            // Save presentation with chart
            presentation.Save(dataDir + "PieChart_out.pptx", SaveFormat.Pptx);
            //ExEnd:PieChart
        }
Пример #6
0
        public static void add3DPieChartToSlide(ISlide slide, ChartProperty chartProperty, Dictionary <string, decimal> data)
        {
            string title  = chartProperty.title;
            float  x      = chartProperty.x;
            float  y      = chartProperty.y;
            float  height = chartProperty.height;
            float  width  = chartProperty.width;

            IChart chart = slide.Shapes.AddChart(ChartType.Pie3D, x, y, width, height);

            // Setting chart Title
            chart.ChartTitle.AddTextFrameForOverriding(title);
            chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
            chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.WrapText   = NullableBool.False;
            chart.ChartTitle.Height = 30;
            chart.HasTitle          = true;

            int defaultWorksheetIndex = 0;

            int index = 0;
            // Getting the chart data worksheet
            IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;

            // Delete default generated series and categories
            chart.ChartData.Series.Clear();
            chart.ChartData.Categories.Clear();


            // Adding new series
            IChartSeries series = chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, title), chart.Type);
            //chart.ChartData.SeriesGroups[0].IsColorVaried = true;


            var sortedData = from objDic in data orderby objDic.Value descending select objDic;
            var sum        = sortedData.Select(item => item.Value).Sum();

            foreach (KeyValuePair <string, decimal> pair in sortedData)
            {
                // Adding new categories
                double percent    = (double)(pair.Value / sum);
                string percentStr = percent.ToString("0.%");
                chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, index, 0, pair.Key + ":" + percentStr));
                // Now populating series data
                series.DataPoints.AddDataPointForPieSeries(fact.GetCell(defaultWorksheetIndex, index, 1, pair.Value));
                index++;
            }


            // Create custom labels for each of categories for new series
            for (int i = 0; i < series.DataPoints.Count; i++)
            {
                Color dataPointColor = getDataPointColorByIndex(i);
                series.DataPoints[i].Format.Fill.FillType             = FillType.Solid;
                series.DataPoints[i].Format.Fill.SolidFillColor.Color = dataPointColor;
                IDataLabel lbl = series.DataPoints[i].Label;
                lbl.TextFrameForOverriding.TextFrameFormat.WrapText = NullableBool.False;
                lbl.DataLabelFormat.Position         = LegendDataLabelPosition.InsideEnd;
                lbl.DataLabelFormat.ShowCategoryName = false;
                lbl.DataLabelFormat.ShowPercentage   = true;
                lbl.DataLabelFormat.ShowLeaderLines  = false;
                lbl.DataLabelFormat.Separator        = ",";
            }

            // no showing Legend key in chart
            series.Chart.HasLegend = true;
            // Set Legend Properties
            chart.Legend.Width  = 350 / chart.Width;
            chart.Legend.Height = 60 / chart.Height;

            // Showing Leader Lines for Chart
            series.Labels.DefaultDataLabelFormat.ShowLeaderLines = false;
            series.Labels.DefaultDataLabelFormat.TextFormat.PortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
            series.Labels.DefaultDataLabelFormat.TextFormat.PortionFormat.FillFormat.FillType             = FillType.Solid;
            chart.Legend.Position = LegendPositionType.Bottom;
        }
        public static void Run()
        {
            //ExStart:SettingCalloutForDataLabel
            string             dataDir  = RunExamples.GetDataDir_Charts();
            Presentation       pres     = new Presentation(dataDir + "testc.pptx");
            ISlide             slide    = pres.Slides[0];
            IChart             chart    = slide.Shapes.AddChart(ChartType.Doughnut, 10, 10, 500, 500, false);
            IChartDataWorkbook workBook = chart.ChartData.ChartDataWorkbook;

            chart.ChartData.Series.Clear();
            chart.ChartData.Categories.Clear();
            chart.HasLegend = false;
            int seriesIndex = 0;

            while (seriesIndex < 15)
            {
                IChartSeries series = chart.ChartData.Series.Add(workBook.GetCell(0, 0, seriesIndex + 1, "SERIES " + seriesIndex), chart.Type);
                series.Explosion = 0;
                series.ParentSeriesGroup.DoughnutHoleSize = (byte)20;
                series.ParentSeriesGroup.FirstSliceAngle  = 351;
                seriesIndex++;
            }
            int categoryIndex = 0;

            while (categoryIndex < 15)
            {
                chart.ChartData.Categories.Add(workBook.GetCell(0, categoryIndex + 1, 0, "CATEGORY " + categoryIndex));
                int i = 0;
                while (i < chart.ChartData.Series.Count)
                {
                    IChartSeries    iCS       = chart.ChartData.Series[i];
                    IChartDataPoint dataPoint = iCS.DataPoints.AddDataPointForDoughnutSeries(workBook.GetCell(0, categoryIndex + 1, i + 1, 1));
                    dataPoint.Format.Fill.FillType                        = FillType.Solid;
                    dataPoint.Format.Line.FillFormat.FillType             = FillType.Solid;
                    dataPoint.Format.Line.FillFormat.SolidFillColor.Color = Color.White;
                    dataPoint.Format.Line.Width     = 1;
                    dataPoint.Format.Line.Style     = LineStyle.Single;
                    dataPoint.Format.Line.DashStyle = LineDashStyle.Solid;
                    if (i == chart.ChartData.Series.Count - 1)
                    {
                        IDataLabel lbl = dataPoint.Label;
                        lbl.TextFormat.TextBlockFormat.AutofitType                                   = TextAutofitType.Shape;
                        lbl.DataLabelFormat.TextFormat.PortionFormat.FontBold                        = NullableBool.True;
                        lbl.DataLabelFormat.TextFormat.PortionFormat.LatinFont                       = new FontData("DINPro-Bold");
                        lbl.DataLabelFormat.TextFormat.PortionFormat.FontHeight                      = 12;
                        lbl.DataLabelFormat.TextFormat.PortionFormat.FillFormat.FillType             = FillType.Solid;
                        lbl.DataLabelFormat.TextFormat.PortionFormat.FillFormat.SolidFillColor.Color = Color.LightGray;
                        lbl.DataLabelFormat.Format.Line.FillFormat.SolidFillColor.Color              = Color.White;
                        lbl.DataLabelFormat.ShowValue        = false;
                        lbl.DataLabelFormat.ShowCategoryName = true;
                        lbl.DataLabelFormat.ShowSeriesName   = false;
                        //lbl.DataLabelFormat.ShowLabelAsDataCallout = true;
                        lbl.DataLabelFormat.ShowLeaderLines        = true;
                        lbl.DataLabelFormat.ShowLabelAsDataCallout = false;
                        chart.ValidateChartLayout();
                        lbl.AsILayoutable.X = (float)lbl.AsILayoutable.X + (float)0.5;
                        lbl.AsILayoutable.Y = (float)lbl.AsILayoutable.Y + (float)0.5;
                    }
                    i++;
                }
                categoryIndex++;
            }
            pres.Save("chart.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
        }