Développer un créateur de graphiques de dispersion en Java

Ce que nous visons

Cet article de blog vous permettra d’automatiser la création de graphiques par programmation. Nous essaierons de simplifier les choses en écrivant un extrait de code pour développer un créateur de graphiques de dispersion en Java. Aspose.Slides for Java est une API Java puissante mais légère pour créer et manipuler des graphiques. De plus, cette API de haut niveau offre un écosystème convivial pour les développeurs par rapport à d’autres API concurrentes. Alors, passons à l’essentiel.

Java API Pour les Graphiques - Installation

Vous pouvez envisager de visiter les détails d’installation ici. En résumé, vous pouvez télécharger ce fichier JAR ou utiliser les configurations Maven suivantes pour installer cette bibliothèque de graphiques Java :

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository> 
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>24.4</version>
    <classifier>jdk16</classifier>
</dependency>

Développer un Créateur de Graphiques de Dispersion - Extrait de Code

Nous écrivons maintenant les étapes à suivre :

  • Créez une instance de la classe Presentation.
  • Appelez la méthode get_Item pour accéder à la première diapositive.
  • Créez le graphique par défaut en appelant la méthode addChart.
  • Appelez la méthode getChartDataWorkbook pour obtenir la feuille de données du graphique.
  • Ajoutez une nouvelle catégorie de graphique en appelant la méthode add.
  • Ajoutez un nouveau point (1:3) en appelant la méthode addDataPointForScatterSeries.
  • Définissez le type de série en appelant la méthode setType.
  • Définissez la taille du marqueur dans le graphique en appelant la méthode setSize.
  • Appelez la méthode setSymbol pour définir le style du marqueur dans le graphique.
  • Enregistrez le fichier PPTX/PPT de sortie sur le disque en appelant la méthode save.

Voici un exemple de code montrant comment développer un créateur de graphiques de dispersion en Java par programmation :

package com.example;
import com.aspose.slides.*;
import java.io.File;
public class main
{
public static void main(String[] args)
{
// The path to the documents directory.
String dataDir = "/Desktop/";
// Create directory if it is not already present.
boolean IsExists = new File(dataDir).exists();
if (!IsExists)
new File(dataDir).mkdirs();
// Create an instance of the Presentation class.
Presentation pres = new Presentation();
// Invoke the get_Item method to access the first slide.
ISlide slide = pres.getSlides().get_Item(0);
// Creating the default chart by calling the addChart method.
IChart chart = slide.getShapes().addChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400);
// Getting the default chart data worksheet index.
int defaultWorksheetIndex = 0;
// Call the getChartDataWorkbook method to get the chart data worksheet.
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
// Delete demo series
chart.getChartData().getSeries().clear();
// Add new chart category by calling the add method.
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 1, "Series 1"), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 1, 3, "Series 2"), chart.getType());
// Take first chart series.
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
// Add new point (1:3) by calling the addDataPointForScatterSeries method.
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 1), fact.getCell(defaultWorksheetIndex, 2, 2, 3));
// Add new point (2:10)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 2), fact.getCell(defaultWorksheetIndex, 3, 2, 10));
// Set the type of series by calling the setType method.
series.setType(ChartType.ScatterWithStraightLinesAndMarkers);
// Set the marker size in the chart by calling the setSize method.
series.getMarker().setSize(10);
// Invoke the setSymbol method to set the marker style in the chart.
series.getMarker().setSymbol(MarkerStyleType.Star);
// Take second chart series
series = chart.getChartData().getSeries().get_Item(1);
// Add new point (5:2) there.
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 5), fact.getCell(defaultWorksheetIndex, 2, 4, 2));
// Add new point (3:1)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 3, 3), fact.getCell(defaultWorksheetIndex, 3, 4, 1));
// Add new point (2:2)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 4, 3, 2), fact.getCell(defaultWorksheetIndex, 4, 4, 2));
// Add new point (5:1)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 5, 3, 5), fact.getCell(defaultWorksheetIndex, 5, 4, 1));
// Changing the chart series marker
series.getMarker().setSize(10);
series.getMarker().setSymbol(MarkerStyleType.Circle);
// Save the output PPTX/PPT file on the disk by calling the save method.
pres.save(dataDir + "AsposeChart_out.pptx", SaveFormat.Pptx);
}
}
java-api-for-charts

Obtenez une Licence Gratuite

Vous pouvez obtenir une licence temporaire gratuite pour essayer cette bibliothèque de graphiques Java sans limitations d’évaluation.

En résumé

Bien que Aspose.Slides for Java offre une large gamme de fonctionnalités pour créer et manipuler des graphiques par programmation, une documentation complète est disponible. De plus, vous pouvez consulter les références de l’API et le dépôt GitHub pour commencer à développer un créateur de graphiques de dispersion en Java. De plus, vous pouvez visiter ce lien pour un aperçu rapide des fonctionnalités offertes.

N’hésitez pas à nous contacter

Vous pouvez nous faire part de vos questions ou demandes sur notre forum.

Voir aussi