Mettre à jour le graphique
Contents
[
Hide
]
Mettre à jour le graphique
Aspose.Slides pour C++ a fourni l’API la plus simple pour mettre à jour des graphiques de la manière la plus facile. Pour mettre à jour un graphique sur une diapositive :
- Ouvrez une instance de la classe Presentation contenant le graphique.
- Obtenez la référence d’une diapositive en utilisant son Index.
- Parcourez toutes les formes pour trouver le graphique souhaité.
- Accédez à la feuille de données du graphique.
- Modifiez les données de la série de données du graphique en changeant les valeurs de la série.
- Ajoutez une nouvelle série et remplissez les données à l’intérieur.
- Écrivez la présentation modifiée en tant que fichier PPTX.
Les exemples de code qui suivent montrent comment mettre à jour un graphique.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C | |
// The path to the documents directory. | |
const String templatePath = u"../templates/ExistingChart.pptx"; | |
const String outPath = u"../out/ExistingChart_out.pptx"; | |
//Instantiate Presentation class that represents PPTX file | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(templatePath); | |
//Access first slide | |
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0); | |
// Add chart with default data | |
SharedPtr<Chart> chart = DynamicCast<Aspose::Slides::Charts::Chart>(slide->get_Shapes()->idx_get(0)); | |
// Setting the index of chart data sheet | |
int defaultWorksheetIndex = 0; | |
// Getting the chart data worksheet | |
SharedPtr<IChartDataWorkbook> fact = chart->get_ChartData()->get_ChartDataWorkbook(); | |
// Changing chart Category Name | |
fact->GetCell(defaultWorksheetIndex, 1, 0, ObjectExt::Box<String>(u"Modified Category 1")); | |
fact->GetCell(defaultWorksheetIndex, 2, 0, ObjectExt::Box<String>(u"Modified Category 2")); | |
// Take first chart series | |
SharedPtr<IChartSeries> series = chart->get_ChartData()->get_Series()->idx_get(0); | |
// Now updating series data | |
fact->GetCell(defaultWorksheetIndex, 0, 1, ObjectExt::Box<String>(u"New_Series1"));// Modifying series name | |
series->get_DataPoints()->idx_get(0)->get_Value()->set_Data(System::ObjectExt::Box<double>(90)); | |
series->get_DataPoints()->idx_get(1)->get_Value()->set_Data(System::ObjectExt::Box<double>(123)); | |
series->get_DataPoints()->idx_get(2)->get_Value()->set_Data(System::ObjectExt::Box<double>(44)); | |
// Take Second chart series | |
series = chart->get_ChartData()->get_Series()->idx_get(1); | |
// Now updating series data | |
fact->GetCell(defaultWorksheetIndex, 0, 2, ObjectExt::Box<System::String>(u"New_Series2"));// Modifying series name | |
series->get_DataPoints()->idx_get(0)->get_Value()->set_Data(System::ObjectExt::Box<double>(20)); | |
series->get_DataPoints()->idx_get(1)->get_Value()->set_Data(System::ObjectExt::Box<double>(67)); | |
series->get_DataPoints()->idx_get(2)->get_Value()->set_Data(System::ObjectExt::Box<double>(99)); | |
// Now, Adding a new serie | |
chart->get_ChartData()->get_Series()->Add(fact->GetCell(defaultWorksheetIndex, 0, 3, ObjectExt::Box<System::String>(u"Series 3")), chart->get_Type()); | |
// Take 3rd chart series | |
series = chart->get_ChartData()->get_Series()->idx_get(2); | |
// Now populating series data | |
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 1, 3, ObjectExt::Box<double>(20))); | |
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 2, 3, ObjectExt::Box<double>(50))); | |
series->get_DataPoints()->AddDataPointForBarSeries(fact->GetCell(defaultWorksheetIndex, 3, 3, ObjectExt::Box<double>(30))); | |
chart->set_Type(Aspose::Slides::Charts::ChartType::ClusteredCylinder); | |
// Write the presentation file to disk | |
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |