Crear gráficos PPTX en Java

Use Aspose.Slides for Java para crear y dar formato a gráficos en archivos de presentación sin Microsoft PowerPoint.

Crear gráficos en archivos PPTX con Java

Use Aspose.Slides for Java para crear y dar formato a gráficos en presentaciones PPTX. La API ofrece objetos de gráfico como IChart, IChartDataWorkbook, IChartSeries e IDataLabel. Puede instalar el paquete aspose-slides en un proyecto basado en Maven agregando el repositorio y la dependencia siguientes.

Repositorio

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependencia

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>26.8</version>
    <classifier>jdk16</classifier>
</dependency>

Crear un gráfico en PPTX – Java

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IChart chart = slide.getShapes().addChart(ChartType.ClusteredColumn, 0, 0, 500, 500);

    chart.getChartTitle().addTextFrameForOverriding("Sales by quarter");
    chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True);
    chart.getChartTitle().setHeight(20);
    chart.setTitle(true);

    chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);

    int defaultWorksheetIndex = 0;
    IChartDataWorkbook chartDataWorkbook = chart.getChartData().getChartDataWorkbook();

    chart.getChartData().getSeries().clear();
    chart.getChartData().getCategories().clear();

    chart.getChartData().getSeries().add(chartDataWorkbook.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
    chart.getChartData().getSeries().add(chartDataWorkbook.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType());

    chart.getChartData().getCategories().add(chartDataWorkbook.getCell(defaultWorksheetIndex, 1, 0, "Category 1"));
    chart.getChartData().getCategories().add(chartDataWorkbook.getCell(defaultWorksheetIndex, 2, 0, "Category 2"));
    chart.getChartData().getCategories().add(chartDataWorkbook.getCell(defaultWorksheetIndex, 3, 0, "Category 3"));

    IChartSeries firstSeries = chart.getChartData().getSeries().get_Item(0);
    firstSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 1, 1, 20));
    firstSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 2, 1, 50));
    firstSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 3, 1, 30));
    firstSeries.getFormat().getFill().setFillType(FillType.Solid);
    firstSeries.getFormat().getFill().getSolidFillColor().setColor(java.awt.Color.RED);

    IChartSeries secondSeries = chart.getChartData().getSeries().get_Item(1);
    secondSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 1, 2, 30));
    secondSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 2, 2, 10));
    secondSeries.getDataPoints().addDataPointForBarSeries(chartDataWorkbook.getCell(defaultWorksheetIndex, 3, 2, 60));
    secondSeries.getFormat().getFill().setFillType(FillType.Solid);
    secondSeries.getFormat().getFill().getSolidFillColor().setColor(java.awt.Color.GREEN);

    IDataLabel firstDataLabel = secondSeries.getDataPoints().get_Item(0).getLabel();
    firstDataLabel.getDataLabelFormat().setShowCategoryName(true);

    IDataLabel secondDataLabel = secondSeries.getDataPoints().get_Item(1).getLabel();
    secondDataLabel.getDataLabelFormat().setShowSeriesName(true);

    IDataLabel thirdDataLabel = secondSeries.getDataPoints().get_Item(2).getLabel();
    thirdDataLabel.getDataLabelFormat().setShowValue(true);
    thirdDataLabel.getDataLabelFormat().setShowSeriesName(true);
    thirdDataLabel.getDataLabelFormat().setSeparator("/");

    presentation.save("chart-presentation.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Cómo crear gráficos en archivos PPTX en Java

Cree gráficos en archivos PPTX con Aspose.Slides for Java.

  1. Cree un objeto Presentation.
  2. Obtenga la primera ISlide de la presentación.
  3. Agregue un objeto IChart con datos predeterminados.
  4. Establezca el título del gráfico y las opciones de las etiquetas de datos.
  5. Obtenga el objeto IChartDataWorkbook y borre las series y categorías predeterminadas.
  6. Agregue nuevas series y categorías.
  7. Rellene los datos de las series y establezca sus colores de relleno.
  8. Cree etiquetas de datos personalizadas para la serie del gráfico.
  9. Guarde la presentación con el gráfico.

Requisitos del sistema

Aspose.Slides for Java es compatible con las principales plataformas de escritorio y servidor. Asegúrese de que su proyecto cumpla estos requisitos previos.

  • Microsoft Windows, Linux, macOS u otro sistema operativo con un entorno de ejecución de Java compatible.
  • Obtenga la versión más reciente de Aspose.Slides for Java desde Maven .

Creado con Aspose.Slides

Crear gráficos PPTX en línea

La aplicación gratuita Aspose.Slides Chart está creada con Aspose.Slides for Java, la misma API que el código anterior. Ábrala y descubra lo que la API puede hacer con sus propios archivos PPTX.

  • Funciona en su navegador, sin instalar nada.
  • Vea cómo Aspose.Slides crea gráficos antes de escribir código.
  • Cree un gráfico a partir de datos de Excel o de una tabla.
  • Descárguelo como archivo PPTX o como imagen.

Qué es PPTX Formato de archivo

Un archivo PPTX es un archivo de presentación de Microsoft PowerPoint en formato Open XML. Almacena diapositivas, texto, imágenes, gráficos, formato, animaciones y otro contenido de presentación en un paquete comprimido basado en XML.

Leer más

Otros formatos compatibles para gráficos

Con Java, también puede crear gráficos en otros formatos.