Este artículo le guiará sobre cómo crear un gráfico circular en PowerPoint con la API REST de C#. Aprenderá cómo hacer un gráfico circular en PowerPoint con el servicio RESTful de C# usando un SDK de nube basado en .NET. Todos los pasos se realizan en el código de muestra junto con los comentarios descriptivos.
Requisito previo
Descargar Aspose.Slides Cloud SDK for Dotnet to make a Pie chart
Configure el proyecto C# con el SDK anterior para trabajar con gráficos circulares en una diapositiva de presentación.
Pasos para hacer un gráfico circular en PowerPoint con API basada en C# .NET
- Inicialice el cliente API con ID y CLAVE usando la clase SlidesApi
- Lea el archivo de presentación local y cárguelo en el almacenamiento en la nube.
- Cree el gráfico circular definiendo su posición, tamaño y tipo para configurarlo
- Agregue etiquetas al gráfico y cree una serie de puntos de datos para el gráfico
- Agregue series al gráfico y agregue este gráfico a la diapositiva de destino en la presentación cargada con el método CreateShape()
- Descargue la presentación a un archivo local
Estos pasos describen cómo se hace un gráfico circular en PowerPoint con el servicio RESTful de C#. Cree una instancia del objeto del gráfico, configure sus propiedades, agregue categorías y cree series de puntos de datos para agregarlos al gráfico circular. Finalmente, agregue el gráfico circular a la diapositiva de destino en la presentación y guárdelo en el disco local.
Código para agregar un gráfico circular en PowerPoint con el servicio RESTful de 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'."); | |
} | |
} | |
} |
El código anterior muestra cómo crear un gráfico circular en PPT con API basada en C# .NET. Puede asegurarse de agregar tantas series de recopilación de datos como el número de categorías agregadas en el gráfico. También tenga en cuenta que es necesario cargar la presentación al principio; de lo contrario, puede obtener excepciones al ejecutar el código sin cargar la presentación fuente.
Este artículo nos ha enseñado a agregar un gráfico circular en una presentación. Para agregar un gráfico de barras, consulte el artículo sobre Cree un gráfico de barras en PowerPoint con la API REST de C#.