This article guides on how to create bar chart in PowerPoint with C# REST API. You will learn how to create a bar graph in PowerPoint with C# RESTful Service using a .NET-based Cloud SDK. It contains complete information on adding a chart and setting data.
Prerequisite
- Create an account API credentials
- Download Aspose.Slides Cloud SDK for Dotnet to add a chart
- Setup C# project with the above SDK to work with graphs in a presentation
Steps to Add Column Chart in PowerPoint with C# .NET-based API
- Initialize the SlidesApi with a client ID and secret for adding a chart
- Upload the presentation file to the Cloud
- Create a new chart configuration object using the Chart class and set the chart type
- Add chart title and initialize categories for the chart
- Initialize the data series for the chart, create the data series, and add the first data series
- Initialize more data series for the chart if required
- Add the chart to the first slide of the presentation using the CreateShape() method and download it if required
These steps describe how to create bar chart in PowerPoint with C# .NET-based API. Create the SlidesApi object, upload the presentation, create a Chart object, set chart type, position, dimensions, title, categories and desired data series. Add the data series to the chart and add the chart to the first slide.
Code to Create Bar Graph in PowerPoint with C# REST Interface
using Aspose.Slides.Cloud.Sdk; | |
using Aspose.Slides.Cloud.Sdk.Model; | |
using System; | |
using System.IO; | |
namespace AsposeTestCodes | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // For adding a custom shape to a presentation | |
{ | |
// Initialize the Slides API using client ID and secret key | |
SlidesApi slidesApi = new SlidesApi("Client ID", "Secret"); | |
// Upload the presentation file to the cloud | |
var uploadResponse = slidesApi.UploadFile("MyPresentation.pptx", new MemoryStream(File.ReadAllBytes("MyPresentation.pptx"))); | |
// Create a new chart configuration object | |
Chart chartConfig = new Chart(); | |
// Define chart type as a stacked bar chart | |
chartConfig.ChartType = Chart.ChartTypeEnum.StackedBar; | |
// Set the position and dimensions of the chart on the slide | |
chartConfig.X = 150; // Horizontal position (pixels) | |
chartConfig.Y = 150; // Vertical position (pixels) | |
chartConfig.Width = 500; // Chart width (pixels) | |
chartConfig.Height = 350; // Chart height (pixels) | |
// Add a title to the chart | |
chartConfig.Title = new ChartTitle { Text = "Bar Chart Example" }; | |
// Initialize categories (x-axis labels) for the chart | |
chartConfig.Categories = new System.Collections.Generic.List<ChartCategory>(); | |
chartConfig.Categories.Add(new ChartCategory { Value = "Group A" }); // First category | |
chartConfig.Categories.Add(new ChartCategory { Value = "Group B" }); // Second category | |
chartConfig.Categories.Add(new ChartCategory { Value = "Group C" }); // Third category | |
chartConfig.Categories.Add(new ChartCategory { Value = "Group D" }); // Fourth category | |
// Initialize data series for the chart | |
chartConfig.Series = new System.Collections.Generic.List<Series>(); | |
// Create the first data series | |
OneValueSeries firstSeries = new OneValueSeries(); | |
firstSeries.DataPoints = new System.Collections.Generic.List<OneValueChartDataPoint>(); | |
firstSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 15 }); // Value for Group A | |
firstSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 40 }); // Value for Group B | |
firstSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 25 }); // Value for Group C | |
firstSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 10 }); // Value for Group D | |
// Add the first data series to the chart | |
chartConfig.Series.Add(firstSeries); | |
// Create the second data series | |
OneValueSeries secondSeries = new OneValueSeries(); | |
secondSeries.DataPoints = new System.Collections.Generic.List<OneValueChartDataPoint>(); | |
secondSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 35 }); // Value for Group A | |
secondSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 20 }); // Value for Group B | |
secondSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 45 }); // Value for Group C | |
secondSeries.DataPoints.Add(new OneValueChartDataPoint { Value = 15 }); // Value for Group D | |
// Add the second data series to the chart | |
chartConfig.Series.Add(secondSeries); | |
// Add the chart to the first slide of the presentation | |
Chart createdChart = (Chart)slidesApi.CreateShape("MyPresentation.pptx", 1, chartConfig); | |
// Print the number of categories in the chart to the console | |
Console.WriteLine(createdChart.Categories.Count); | |
// Download the modified presentation from the cloud | |
Stream stream = slidesApi.DownloadFile("MyPresentation.pptx"); | |
// Save the downloaded file to the local system | |
var fs = new FileStream("Downloaded.pptx", FileMode.Create, FileAccess.Write); | |
stream.CopyTo(fs); // Copy the stream data to the local file | |
} | |
} | |
} |
This code demonstrates the process of how to make a bar graph in PowerPoint with C# Low Code API. You may set different chart type such as ClusteredColumn, Column3D, ClusteredCone, and Line etc, using the ChartTypeEnum and other parameters in the Chart object including ShowDataLabelsOverMaximum, BackWall, SideWall, Floor, and Legend etc.
This article has taught us the creation of a bar chart in PowerPoint. To convert HTML to PowerPoint, refer to the article on Convert HTML to PowerPoint with NET REST API.