Questo articolo ti guida su come creare un grafico a torta in PowerPoint con l’API REST C#. Imparerai come creare un grafico a torta in PowerPoint con il servizio RESTful C# utilizzando un Cloud SDK basato su .NET. Tutti i passaggi vengono eseguiti nel codice di esempio insieme ai commenti descrittivi.
Prerequisito
Scaricamento Aspose.Slides Cloud SDK for Dotnet to make a Pie chart
Configura il progetto C# con l’SDK precedente per lavorare con i grafici a torta in una diapositiva di presentazione
Passaggi per creare un grafico a torta in PowerPoint con API basata su C# .NET
- Inizializza il client API con ID e CHIAVE utilizzando la classe SlidesApi
- Leggi il file di presentazione locale e caricalo nell’archivio cloud
- Crea il grafico a torta definendone posizione, dimensione e tipo per configurarlo
- Aggiungi etichette al grafico e crea una serie di punti dati per il grafico
- Aggiungi serie al grafico e aggiungi questo grafico alla diapositiva di destinazione nella presentazione caricata con il metodo CreateShape()
- Scarica la presentazione in un file locale
Questi passaggi descrivono come creare un grafico a torta in PowerPoint con il servizio RESTful C#. Crea un’istanza dell’oggetto grafico, configura le sue proprietà, aggiungi categorie e crea serie di punti dati da aggiungere al grafico a torta. Infine, aggiungi il grafico a torta alla diapositiva di destinazione nella presentazione e salva sul disco locale.
Codice per aggiungere un grafico a torta in PowerPoint con il servizio RESTful C#
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'."); | |
} | |
} | |
} |
Il codice sopra mostra come creare un grafico a torta in PPT con API basata su C# .NET. Puoi assicurarti di aggiungere tante serie di raccolte dati quante sono le categorie aggiunte nel grafico. Tieni inoltre presente che è necessario caricare la presentazione all’inizio, altrimenti potresti ottenere eccezioni durante l’esecuzione del codice senza caricare la presentazione sorgente.
Questo articolo ci ha insegnato ad aggiungere un grafico a torta in una presentazione. Per aggiungere un grafico a barre, fai riferimento all’articolo su Crea un grafico a barre in PowerPoint con l’API REST C#.