PPT处理控件Aspose.Slides功能演示:使用C#在PowerPoint演示文稿中创建图表

图表用于汇总和直观表示PowerPoint演示文稿中的数据。因此,PowerPoint提供了多种图表类型以可视化数据。在本文中,将学习如何使用C#在PowerPoint演示文稿中创建这些流行的图表类型。

图表用于汇总和直观表示PowerPoint演示文稿中的数据。因此,PowerPoint提供了多种图表类型以可视化数据。其中,最常用的图表类型包括饼图,折线图,条形图,直方图,股票图等。在本文中,将学习如何使用C#在PowerPoint演示文稿中创建这些流行的图表类型。

  • 使用C#在PowerPoint中创建柱形图
  • 使用C#在PowerPoint中创建散点图
  • 使用C#在PowerPoint中添加饼图
  • 使用C#在PowerPoint中添加直方图
  • 使用C#在PowerPoint中创建股票图表

Aspose.Slides for .NET是一个C#类库,可让您从.NET应用程序中创建和处理PowerPoint演示文稿。此外,API允许您无缝创建图表并将其添加到演示文稿中。

>>你可以点击这里下载Aspose.Slides v21.1测试体验。

整合所有格式API处理控件Aspose.Total永久授权火热促销中,联系客服立马1分钟了解全部!


使用C#在PowerPoint演示文稿中创建柱形图

在本节中,将学习如何创建柱形图以及如何添加类别和系列以填充该图。以下是执行此操作的步骤。

  • 首先,创建Presentation 类的实例 。
  • 在ISlide对象中获取幻灯片的引用。
  • 添加具有默认数据的ClusteredColumn图表,并在IChart对象中获取其引用。
  • 使用IChart.ChartTitle.AddTextFrameForOverriding(String)方法设置图表标题并设置其属性。
  • 将图表数据工作簿访问到IChartDataWorkbook对象中。
  • 分别使用IChart.ChartData.Series.Clear()和IChart.ChartData.Categories.Clear()方法从图表数据中清除所有默认系列和类别。
  • 添加新的系列和类别。
  • 将每个图表系列访问到IChartSeries对象中,并向其添加数据点。
  • 为图表系列添加填充颜色并设置标签。
  • 最后,使用Presentation.Save(String,SaveFormat)方法保存演示文稿。

为了演示,下面的代码示例演示如何使用C#在PowerPoint演示文稿中创建柱形图。

// Instantiate Presentation class that represents PPTX filePresentation pres = new Presentation();// Access first slideISlide sld = pres.Slides[0];// Add chart with default dataIChart chart = sld.Shapes.AddChart(ChartType.ClusteredColumn, 0, 0, 500, 500);// Setting chart Titlechart.ChartTitle.AddTextFrameForOverriding("Sample Title");chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;chart.ChartTitle.Height = 20;chart.HasTitle = true;// Set first series to Show Valueschart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;// Setting the index of chart data sheetint defaultWorksheetIndex = 0;// Getting the chart data worksheetIChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;// Delete default generated series and categorieschart.ChartData.Series.Clear();chart.ChartData.Categories.Clear();int s = chart.ChartData.Series.Count;s = chart.ChartData.Categories.Count;// Adding new serieschart.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 categorieschart.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 seriesIChartSeries series = chart.ChartData.Series[0];// Now populating series dataseries.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 seriesseries.Format.Fill.FillType = FillType.Solid;series.Format.Fill.SolidFillColor.Color = System.Drawing.Color.Blue;// Take second chart seriesseries = chart.ChartData.Series[1];// Now populating series dataseries.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 seriesseries.Format.Fill.FillType = FillType.Solid;series.Format.Fill.SolidFillColor.Color = Color.Orange;// First label will be show Category nameIDataLabel lbl = series.DataPoints[0].Label;lbl.DataLabelFormat.ShowCategoryName = true;lbl = series.DataPoints[1].Label;lbl.DataLabelFormat.ShowSeriesName = true;// Show value for third labellbl = series.DataPoints[2].Label;lbl.DataLabelFormat.ShowValue = true;lbl.DataLabelFormat.ShowSeriesName = true;lbl.DataLabelFormat.Separator = "/";// Save presentation with chartpres.Save("column-chart.pptx", SaveFormat.Pptx);

以下是结果柱形图的屏幕截图。

PPT处理控件Aspose.Slides功能演示:使用C#在PowerPoint演示文稿中创建图表

使用C#在PowerPoint中创建散点图

以下是使用C#在PowerPoint演示文稿中创建分散图表的步骤。

  • 使用Presentation 类创建一个新的演示 文稿。
  • 获取ISlide对象中幻灯片的引用。
  • 添加具有默认数据的ScatterWithSmoothLines图表类型,并在IChart对象中获取其引用。
  • 将图表数据工作簿访问到IChartDataWorkbook对象中,并清除默认系列。
  • 将新系列添加到图表数据。
  • 将每个系列访问IChartSeries对象,并将数据点添加到该系列。
  • 使用IChartSeries.Marker属性设置系列的标记。
  • 使用Presentation.Save(String,SaveFormat)方法保存演示文稿。

下面的代码示例演示如何使用C#在PowerPoint演示文稿中创建分散的图表。

// Instantiate Presentation class that represents PPTX filePresentation pres = new Presentation();// Access first slideISlide sld = pres.Slides[0];// Add chart with default dataIChart chart = sld.Shapes.AddChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400);// Getting the default chart data worksheet indexint defaultWorksheetIndex = 0;// Getting the chart data worksheetIChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;// Delete demo serieschart.ChartData.Series.Clear();// Add new serieschart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.Type);chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 1, 3, "Series 2"), chart.Type);// Take first chart seriesIChartSeries series = chart.ChartData.Series[0];// Add new point (1:3) there.series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 1), fact.GetCell(defaultWorksheetIndex, 2, 2, 3));// Add new point (2:10)series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 2), fact.GetCell(defaultWorksheetIndex, 3, 2, 10));// Edit the type of seriesseries.Type = ChartType.ScatterWithStraightLinesAndMarkers;// Changing the chart series markerseries.Marker.Size = 10;series.Marker.Symbol = MarkerStyleType.Star;// Take second chart seriesseries = chart.ChartData.Series[1];// Add new point (5:2) there.series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 2, 3, 5), fact.GetCell(defaultWorksheetIndex, 2, 4, 2));// Add new point (3:1)series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 3, 3, 3), fact.GetCell(defaultWorksheetIndex, 3, 4, 1));// Add new point (2:2)series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 4, 3, 2), fact.GetCell(defaultWorksheetIndex, 4, 4, 2));// Add new point (5:1)series.DataPoints.AddDataPointForScatterSeries(fact.GetCell(defaultWorksheetIndex, 5, 3, 5), fact.GetCell(defaultWorksheetIndex, 5, 4, 1));// Changing the chart series markerseries.Marker.Size = 10;series.Marker.Symbol = MarkerStyleType.Circle;// Save presentation with chartpres.Save("scattered-chart.pptx", SaveFormat.Pptx);

以下屏幕截图显示了生成的分散图表。

PPT处理控件Aspose.Slides功能演示:使用C#在PowerPoint演示文稿中创建图表

使用C#在PowerPoint中创建饼图

以下是使用C#在PowerPoint演示文稿中创建饼图的步骤。

  • 使用Presentation 类创建一个新的演示 文稿。
  • 获取ISlide对象中幻灯片的引用。
  • 添加具有默认数据的饼图类型,并在IChart对象中获取其引用。
  • 使用IChart.ChartTitle.AddTextFrameForOverriding(String)方法设置图表标题并设置其属性。
  • 设置值的可见性。
  • 分别使用IChart.ChartData.Series.Clear()和IChart.ChartData.Categories.Clear()方法清除默认系列和类别。
  • 将图表数据工作簿访问到IChartDataWorkbook对象中。
  • 将新类别添加到图表数据到IChart.ChartData.Categories集合中。
  • 将新系列添加到图表数据到IChart.ChartData.Series集合中。
  • 将每个系列获取到IChartSeries对象中,并将数据点添加到该系列中。
  • 将每个数据点访问IChartDataPoint对象,并设置其格式。
  • 使用IDataLabel对象访问数据点中的数据标签并设置其格式。
  • 设置引导线和旋转角度。
  • 使用Presentation.Save(String,SaveFormat)方法保存演示文稿。

下面的代码示例演示如何使用C#在PowerPoint演示文稿中创建饼图。

// Instantiate Presentation class that represents PPTX filePresentation presentation = new Presentation();// Access first slideISlide slides = presentation.Slides[0];// Add chart with default dataIChart chart = slides.Shapes.AddChart(ChartType.Pie, 100, 100, 400, 400);// Setting chart Titlechart.ChartTitle.AddTextFrameForOverriding("Sample Title");chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;chart.ChartTitle.Height = 20;chart.HasTitle = true;// Set first series to Show Valueschart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;// Setting the index of chart data sheetint defaultWorksheetIndex = 0;// Getting the chart data worksheetIChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook;// Delete default generated series and categorieschart.ChartData.Series.Clear();chart.ChartData.Categories.Clear();// Adding new categorieschart.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 seriesIChartSeries series = chart.ChartData.Series.Add(fact.GetCell(0, 0, 1, "Series 1"), chart.Type);// Now populating series dataseries.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.Orange;// Setting Sector borderpoint.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.BlueViolet;// Setting Sector borderpoint1.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.YellowGreen;// Setting Sector borderpoint2.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 seriesIDataLabel 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 Sectorschart.ChartData.SeriesGroups[0].FirstSliceAngle = 180;// Save presentation with chartpresentation.Save("pie-chart.pptx", SaveFormat.Pptx);

以下是生成的饼图的屏幕截图。

PPT处理控件Aspose.Slides功能演示:使用C#在PowerPoint演示文稿中创建图表

使用C#在PowerPoint中添加直方图

以下是使用Aspose.Slides for .NET在PowerPoint演示文稿中创建直方图的步骤。

  • 创建Presentation 类的实例 。
  • 通过索引获取ISlide对象中幻灯片的引用。
  • 添加带有默认数据的直方图图表。
  • 清除默认系列和类别。
  • 访问IChartDataWorkbook对象中的图表数据工作簿。
  • 添加新系列,并在IChartSeries对象中获取其引用。
  • 将数据点添加到序列中。
  • 设置图表轴的聚合类型。
  • 使用Presentation.Save(String,SaveFormat)方法保存演示文稿。

下面的代码示例演示如何使用C#创建直方图。

// Load or create presentationusing (Presentation pres = new Presentation()){    // Add histogram chart    IChart chart = pres.Slides[0].Shapes.AddChart(ChartType.Histogram, 50, 50, 500, 400);    chart.ChartData.Categories.Clear();    chart.ChartData.Series.Clear();    // Access chart data workbook    IChartDataWorkbook wb = chart.ChartData.ChartDataWorkbook;    // Clear workbook    wb.Clear(0);    // Add chart series    IChartSeries series = chart.ChartData.Series.Add(ChartType.Histogram);    series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A1", 15));    series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A2", -41));    series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A3", 16));    series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A4", 10));    series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A5", -23));    series.DataPoints.AddDataPointForHistogramSeries(wb.GetCell(0, "A6", 16));    chart.Axes.HorizontalAxis.AggregationType = AxisAggregationType.Automatic;    // Save presentation    pres.Save("histogram-chart.pptx", SaveFormat.Pptx);}

以下是创建的直方图的屏幕截图。

PPT处理控件Aspose.Slides功能演示:使用C#在PowerPoint演示文稿中创建图表

使用C#在PowerPoint中创建股票图表

股票图表也是PowerPoint演示文稿中常用的图表类型之一。以下是创建股票图表的步骤。

  • 使用Presentation 类创建一个新的PowerPoint演示 文稿。
  • 使用幻灯片的索引在ISlide对象中获取幻灯片的引用。
  • 将OpenHighLowClose图表添加到幻灯片中,并在IChart对象中获取其引用。
  • 清除默认系列和类别。
  • 访问IChartDataWorkbook对象中的图表数据。
  • 将新系列和类别添加到图表。
  • 访问每个图表系列并添加数据点。
  • 指定HiLowLines格式。
  • 使用Presentation.Save(String,SaveFormat)方法保存演示文稿。

下面的代码示例演示如何使用C#将股票图表添加到PowerPoint演示文稿中。

// Load or create presentationusing (Presentation pres = new Presentation()){    // Add chart    IChart chart = pres.Slides[0].Shapes.AddChart(ChartType.OpenHighLowClose, 50, 50, 600, 400, false);    // Clear categories and series    chart.ChartData.Series.Clear();    chart.ChartData.Categories.Clear();    // Access chart data workbook    IChartDataWorkbook wb = chart.ChartData.ChartDataWorkbook;    // Add categories    chart.ChartData.Categories.Add(wb.GetCell(0, 1, 0, "A"));    chart.ChartData.Categories.Add(wb.GetCell(0, 2, 0, "B"));    chart.ChartData.Categories.Add(wb.GetCell(0, 3, 0, "C"));    // Add series    chart.ChartData.Series.Add(wb.GetCell(0, 0, 1, "Open"), chart.Type);    chart.ChartData.Series.Add(wb.GetCell(0, 0, 2, "High"), chart.Type);    chart.ChartData.Series.Add(wb.GetCell(0, 0, 3, "Low"), chart.Type);    chart.ChartData.Series.Add(wb.GetCell(0, 0, 4, "Close"), chart.Type);    // Add data points    IChartSeries series = chart.ChartData.Series[0];    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 1, 72));    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 1, 25));    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 1, 38));    series = chart.ChartData.Series[1];    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 2, 172));    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 2, 57));    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 2, 57));    series = chart.ChartData.Series[2];    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 3, 12));    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 3, 12));    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 3, 13));    series = chart.ChartData.Series[3];    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 1, 4, 25));    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 2, 4, 38));    series.DataPoints.AddDataPointForStockSeries(wb.GetCell(0, 3, 4, 50));    // Set whether chart has up/down bars    chart.ChartData.SeriesGroups[0].UpDownBars.HasUpDownBars = true;    // Specify hi/low line format    chart.ChartData.SeriesGroups[0].HiLowLinesFormat.Line.FillFormat.FillType = FillType.Solid;    foreach (IChartSeries ser in chart.ChartData.Series)    {        ser.Format.Line.FillFormat.FillType = FillType.NoFill;    }    // Save presentation    pres.Save("stock-chart.pptx", SaveFormat.Pptx);}

以下是创建的股票图表的屏幕截图。

PPT处理控件Aspose.Slides功能演示:使用C#在PowerPoint演示文稿中创建图表

还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询


标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月1日
下一篇 2021年1月1日

相关推荐

发表回复

登录后才能评论