Napravite tortni grafikon u programu PowerPoint s C# REST API-jem

Ovaj vas članak vodi kako stvoriti tortni grafikon u PowerPoint s C# REST API-jem. Naučit ćete kako izraditi tortni grafikon u PowerPointu uz C# RESTful Service koristeći Cloud SDK temeljen na .NET-u. Svi se koraci izvode u oglednom kodu zajedno s opisnim komentarima.

Preduvjet

Koraci za izradu kružnog grafikona u programu PowerPoint s API-jem temeljenim na C# .NET

  1. Inicijalizirajte API klijent s ID-om i KEY-om pomoću klase SlidesApi
  2. Pročitajte lokalnu prezentacijsku datoteku i prenesite je u pohranu u oblaku
  3. Napravite tortni grafikon definiranjem njegovog položaja, veličine i vrste za konfiguraciju
  4. Dodajte oznake grafikonu i stvorite niz podatkovnih točaka za grafikon
  5. Dodajte niz u grafikon i dodajte ovaj grafikon na ciljni slajd u učitanoj prezentaciji metodom CreateShape()
  6. Preuzmite prezentaciju u lokalnu datoteku

Ovi koraci opisuju kako napraviti tortni grafikon u PowerPointu uz C# RESTful Service. Instancirajte objekt grafikona, konfigurirajte njegova svojstva, dodajte kategorije i stvorite niz podatkovnih točaka za dodavanje u tortni grafikon. Na kraju dodajte tortni grafikon na ciljni slajd u prezentaciji i spremite ga na lokalni disk.

Kod za dodavanje kružnog grafikona u PowerPoint s C# RESTful uslugom

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'.");
}
}
}

Gornji kôd pokazuje kako stvoriti tortni grafikon u PPT-u s API-jem temeljenim na C# .NET. Možete osigurati da dodate onoliko nizova prikupljanja podataka koliko je kategorija dodano na grafikonu. Također imajte na umu da je učitavanje prezentacije na početku obavezno. U suprotnom možete dobiti iznimke tijekom izvršavanja koda bez učitavanja izvorne prezentacije.

Ovaj nas je članak naučio dodavanju kružnog grafikona u prezentaciju. Za dodavanje trakastog grafikona pogledajte članak na Stvorite trakasti grafikon u programu PowerPoint s C# REST API-jem.

 Hrvatski