翻译|使用教程|编辑:李显亮|2021-02-03 10:28:16.917|阅读 401 次
概述:图表用于汇总和直观表示PowerPoint演示文稿中的数据。因此,PowerPoint提供了多种图表类型以可视化数据。在本文中,将学习如何使用C#在PowerPoint演示文稿中创建这些流行的图表类型。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
图表用于汇总和直观表示PowerPoint演示文稿中的数据。因此,PowerPoint提供了多种图表类型以可视化数据。其中,最常用的图表类型包括饼图,折线图,条形图,直方图,股票图等。在本文中,将学习如何使用C#在PowerPoint演示文稿中创建这些流行的图表类型。
Aspose.Slides for .NET是一个C#类库,可让您从.NET应用程序中创建和处理PowerPoint演示文稿。此外,API允许您无缝创建图表并将其添加到演示文稿中。
>>你可以点击这里下载Aspose.Slides v21.1测试体验。
在本节中,将学习如何创建柱形图以及如何添加类别和系列以填充该图。以下是执行此操作的步骤。
为了演示,下面的代码示例演示如何使用C#在PowerPoint演示文稿中创建柱形图。
// 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.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 = System.Drawing.Color.Blue; // 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.Orange; // 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("column-chart.pptx", SaveFormat.Pptx);
以下是结果柱形图的屏幕截图。
以下是使用C#在PowerPoint演示文稿中创建分散图表的步骤。
下面的代码示例演示如何使用C#在PowerPoint演示文稿中创建分散的图表。
// 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.ScatterWithSmoothLines, 0, 0, 400, 400); // Getting the default chart data worksheet index int defaultWorksheetIndex = 0; // Getting the chart data worksheet IChartDataWorkbook fact = chart.ChartData.ChartDataWorkbook; // Delete demo series chart.ChartData.Series.Clear(); // Add new series chart.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 series IChartSeries 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 series series.Type = ChartType.ScatterWithStraightLinesAndMarkers; // Changing the chart series marker series.Marker.Size = 10; series.Marker.Symbol = MarkerStyleType.Star; // Take second chart series series = 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 marker series.Marker.Size = 10; series.Marker.Symbol = MarkerStyleType.Circle; // Save presentation with chart pres.Save("scattered-chart.pptx", SaveFormat.Pptx);
以下屏幕截图显示了生成的分散图表。
以下是使用C#在PowerPoint演示文稿中创建饼图的步骤。
下面的代码示例演示如何使用C#在PowerPoint演示文稿中创建饼图。
// 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.Orange; // 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.BlueViolet; // 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.YellowGreen; // 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("pie-chart.pptx", SaveFormat.Pptx);
以下是生成的饼图的屏幕截图。
以下是使用Aspose.Slides for .NET在PowerPoint演示文稿中创建直方图的步骤。
下面的代码示例演示如何使用C#创建直方图。
// Load or create presentation using (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); }
以下是创建的直方图的屏幕截图。
股票图表也是PowerPoint演示文稿中常用的图表类型之一。以下是创建股票图表的步骤。
下面的代码示例演示如何使用C#将股票图表添加到PowerPoint演示文稿中。
// Load or create presentation using (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); }
以下是创建的股票图表的屏幕截图。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn