翻譯|使用教程|編輯:李顯亮|2021-02-03 10:28:16.917|閱讀 517 次
概述:圖表用于匯總和直觀表示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@fc6vip.cn