Create a Pie Chart in PowerPoint with C# REST API

This article guides you on how to create a Pie chart in PowerPoint with C# REST API. You will learn how to make a Pie chart in PowerPoint with C# RESTful Service using a .NET-based Cloud SDK. All the steps are performed in the sample code along with the descriptive comments.

Prerequisite

Steps to Make a Pie Chart in PowerPoint with C# .NET-based API

  1. Initialize the API client with ID and KEY using the SlidesApi class
  2. Read the local presentation file and upload it to the Cloud storage
  3. Create the Pie chart by defining its position, size, and type for configuring it
  4. Add labels to the chart and create a series of data points for the chart
  5. Add series to the chart and add this chart to the target slide in the loaded presentation with the CreateShape() method
  6. Download the presentation to a local file

These steps describe how do you make a Pie chart in PowerPoint with C# RESTful Service. Instantiate the chart object, configure its properties, add categories, and create data points series for adding to the Pie chart. Finally, add the Pie chart to the target slide in the presentation and save on the local disk.

Code to Add Pie Graph in PowerPoint with C# RESTful Service

using SlidesCloud.SDK; // Importing the SDK for managing presentations
using SlidesCloud.SDK.Model; // Importing models used for creating and manipulating presentation components
using System;
using System.IO;
namespace PresentationManager // Custom namespace for this program
{
class ShapeAdder
{
static void Main(string[] args) // Main entry point of the program
{
// Initialize the API client with API key and secret
SlidesApi presentationApi = new SlidesApi("Client ID", "Secret");
// Upload the presentation file to the server
// Reads the local presentation file and uploads it to the cloud for processing
var uploadResult = presentationApi.UploadFile("SampleDeck.pptx", new MemoryStream(File.ReadAllBytes("SampleDeck.pptx")));
// Configure chart properties
// Creating a new chart object and defining its position, size, and type
Chart customChart = new Chart
{
ChartType = Chart.ChartTypeEnum.Pie, // Specifies the type of chart (Pie Chart)
X = 150, // X-coordinate of the chart's position on the slide
Y = 120, // Y-coordinate of the chart's position on the slide
Width = 450, // Width of the chart
Height = 350, // Height of the chart
Title = new ChartTitle { Text = "Pie Chart" } // Setting the chart title
};
// Adding categories (labels) to the chart
customChart.Categories = new System.Collections.Generic.List<ChartCategory>
{
new ChartCategory { Value = "Category A" }, // First category
new ChartCategory { Value = "Category B" }, // Second category
new ChartCategory { Value = "Category C" } // Third category
};
// Define data series and data points
// Creating a series of data points for the chart
OneValueSeries chartSeries = new OneValueSeries
{
IsColorVaried = true, // Allows different colors for each data point
DataPoints = new System.Collections.Generic.List<OneValueChartDataPoint>
{
new OneValueChartDataPoint { Value = 35 }, // Data point for Category A
new OneValueChartDataPoint { Value = 45 }, // Data point for Category B
new OneValueChartDataPoint { Value = 20 } // Data point for Category C
}
};
// Adding the series to the chart
customChart.Series = new System.Collections.Generic.List<Series> { chartSeries };
// Add the chart to the first slide of the presentation
Chart insertedChart = (Chart)presentationApi.CreateShape("SampleDeck.pptx", 1, customChart);
// Display the number of categories in the chart
Console.WriteLine("Number of Categories in the Chart: " + insertedChart.Categories.Count);
// Download the updated presentation file from the server
Stream updatedPresentationStream = presentationApi.DownloadFile("SampleDeck.pptx");
// Save the downloaded presentation to a local file
// Writing the downloaded stream to a new file on disk
using (var fileStream = new FileStream("UpdatedDeck.pptx", FileMode.Create, FileAccess.Write))
{
updatedPresentationStream.CopyTo(fileStream);
}
// Notify the user that the process is complete
Console.WriteLine("Updated presentation saved as 'UpdatedDeck.pptx'.");
}
}
}

The above code shows how to create Pie chart in PPT with C# .NET-based API. You may ensure that you add as many series of data collection as the number of categories added in the chart. Also note that loading the presentation in he beginning is must required otherwise you may get exceptions while executing code without uploading the source presentation.

This article has taught us adding a Pie chart in a presentation. To add a bar chart, refer to the article on Create Bar chart in PowerPoint with C# REST API.

 English