Este artigo orienta você sobre como criar um gráfico de pizza em PowerPoint com API REST C#. Você aprenderá como criar um gráfico de pizza no PowerPoint com C# RESTful Service usando um Cloud SDK baseado em .NET. Todas as etapas são executadas no código de amostra junto com os comentários descritivos.
Pré-requisito
Download Aspose.Slides Cloud SDK for Dotnet to make a Pie chart
Configure o projeto C# com o SDK acima para trabalhar com gráficos de pizza em um slide de apresentação
Etapas para fazer um gráfico de pizza no PowerPoint com API baseada em C# .NET
- Inicialize o cliente da API com ID e KEY usando a classe SlidesApi
- Leia o arquivo de apresentação local e carregue-o no armazenamento em nuvem
- Crie o gráfico de pizza definindo sua posição, tamanho e tipo para configurá-lo
- Adicione rótulos ao gráfico e crie uma série de pontos de dados para o gráfico
- Adicione séries ao gráfico e adicione este gráfico ao slide de destino na apresentação carregada com o método CreateShape()
- Baixe a apresentação para um arquivo local
Estas etapas descrevem como criar um gráfico de pizza no PowerPoint com C# RESTful Service. Instancie o objeto gráfico, configure suas propriedades, adicione categorias e crie séries de pontos de dados para adicionar ao gráfico de pizza. Por fim, adicione o gráfico de pizza ao slide de destino da apresentação e salve-o no disco local.
Código para adicionar gráfico de pizza no PowerPoint com serviço 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'."); | |
} | |
} | |
} |
O código acima mostra como criar um gráfico de pizza em PPT com API baseada em C# .NET. Você pode adicionar tantas séries de coleta de dados quanto o número de categorias adicionadas no gráfico. Observe também que o carregamento da apresentação no início é obrigatório, caso contrário você poderá obter exceções ao executar o código sem carregar a apresentação de origem.
Este artigo nos ensinou a adicionar um gráfico de pizza em uma apresentação. Para adicionar um gráfico de barras, consulte o artigo em Crie um gráfico de barras no PowerPoint com API REST C#.