Crear o actualizar gráficos de presentación de PowerPoint en JavaScript
Descripción general
Este artículo describe cómo crear gráficos de presentación de PowerPoint en Java. También puedes actualizar los gráficos en JavaScript. Cubre los siguientes temas.
Gráfico: Normal
Gráfico: Disperso
- Java Crear Dispersed Chart
- Java Crear PowerPoint Dispersed Chart
- Java Crear PowerPoint Presentation Dispersed Chart
Gráfico: Circular
Gráfico: Mapa de árbol
- Java Crear Tree Map Chart
- Java Crear PowerPoint Tree Map Chart
- Java Crear PowerPoint Presentation Tree Map Chart
Gráfico: Acciones
- Java Crear Stock Chart
- Java Crear PowerPoint Stock Chart
- Java Crear PowerPoint Presentation Stock Chart
Gráfico: Caja y bigote
- Java Crear Box and Whisker Chart
- Java Crear PowerPoint Box and Whisker Chart
- Java Crear PowerPoint Presentation Box and Whisker Chart
Gráfico: Embudo
- Java Crear Funnel Chart
- Java Crear PowerPoint Funnel Chart
- Java Crear PowerPoint Presentation Funnel Chart
Gráfico: Explosión radial
- Java Crear Sunburst Chart
- Java Crear PowerPoint Sunburst Chart
- Java Crear PowerPoint Presentation Sunburst Chart
Gráfico: Histograma
- Java Crear Histogram Chart
- Java Crear PowerPoint Histogram Chart
- Java Crear PowerPoint Presentation Histogram Chart
Gráfico: Radar
- Java Crear Radar Chart
- Java Crear PowerPoint Radar Chart
- Java Crear PowerPoint Presentation Radar Chart
Gráfico: Multi categoría
- Java Crear Multi Category Chart
- Java Crear PowerPoint Multi Category Chart
- Java Crear PowerPoint Presentation Multi Category Chart
Gráfico: Mapa
Acción: Actualizar gráfico
- Java Actualizar PowerPoint Chart
- Java Actualizar Presentation Chart
- Java Actualizar PowerPoint Presentation Chart
Crear gráfico
Los gráficos ayudan a las personas a visualizar datos rápidamente y a obtener ideas, lo que puede no ser evidente a simple vista en una tabla o hoja de cálculo.
¿Por qué crear gráficos?
Al usar gráficos, puedes
- agregar, condensar o resumir grandes cantidades de datos en una sola diapositiva de una presentación
- exponer patrones y tendencias en los datos
- deducir la dirección y el impulso de los datos a lo largo del tiempo o respecto a una unidad de medida específica
- detectar valores atípicos, aberraciones, desviaciones, errores, datos sin sentido, etc.
- comunicar o presentar datos complejos
En PowerPoint, puedes crear gráficos mediante la función de inserción, que ofrece plantillas para diseñar muchos tipos de gráficos. Usando Aspose.Slides, puedes crear gráficos regulares (basados en tipos de gráficos populares) y gráficos personalizados.
Creación de gráficos normales
Pasos: Crear gráfico
- Pasos: Crear PowerPoint Chart en JavaScript
- Pasos: Crear Presentation Chart en JavaScript
- Pasos: Crear PowerPoint Presentation Chart en JavaScript
Pasos de código:
- Crear una instancia de la clase Presentation.
- Obtener la referencia de una diapositiva mediante su índice.
- Añadir un gráfico con algunos datos y especificar el tipo de gráfico preferido.
- Añadir un título al gráfico.
- Acceder a la hoja de datos del gráfico.
- Eliminar todas las series y categorías predeterminadas.
- Añadir nuevas series y categorías.
- Añadir nuevos datos al gráfico para las series.
- Añadir un color de relleno para las series.
- Añadir etiquetas para las series.
- Guardar la presentación modificada como archivo PPTX.
Este código JavaScript muestra cómo crear un gráfico normal:
// Instancia una clase de presentación que representa un archivo PPTX
var pres = new aspose.slides.Presentation();
try {
// Accede a la primera diapositiva
var sld = pres.getSlides().get_Item(0);
// Agrega un gráfico con sus datos predeterminados
var chart = sld.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 0, 0, 500, 500);
// Establece el título del gráfico
chart.getChartTitle().addTextFrameForOverriding("Sample Title");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(aspose.slides.NullableBool.True);
chart.getChartTitle().setHeight(20);
chart.hasTitle();
// Configura la primera serie para mostrar valores
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
// Establece el índice para la hoja de datos del gráfico
var defaultWorksheetIndex = 0;
// Obtiene la hoja de datos del gráfico
var fact = chart.getChartData().getChartDataWorkbook();
// Elimina las series y categorías generadas por defecto
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
var s = chart.getChartData().getSeries().size();
s = chart.getChartData().getCategories().size();
// Agrega nuevas series
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType());
// Agrega nuevas categorías
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"));
// Obtiene la primera serie del gráfico
var series = chart.getChartData().getSeries().get_Item(0);
// Ahora rellena los datos de la serie
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));
// Establece el color de relleno para la serie
series.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
series.getFormat().getFill().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
// Obtiene la segunda serie del gráfico
series = chart.getChartData().getSeries().get_Item(1);
// Rellena los datos de la serie
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 2, 30));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 10));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 2, 60));
// Establece el color de relleno para la serie
series.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
series.getFormat().getFill().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GREEN"));
// Crea etiquetas personalizadas para cada categoría de la nueva serie
// Establece la primera etiqueta para mostrar el nombre de la categoría
var lbl = series.getDataPoints().get_Item(0).getLabel();
lbl.getDataLabelFormat().setShowCategoryName(true);
lbl = series.getDataPoints().get_Item(1).getLabel();
lbl.getDataLabelFormat().setShowSeriesName(true);
// Muestra el valor para la tercera etiqueta
lbl = series.getDataPoints().get_Item(2).getLabel();
lbl.getDataLabelFormat().setShowValue(true);
lbl.getDataLabelFormat().setShowSeriesName(true);
lbl.getDataLabelFormat().setSeparator("/");
// Guarda la presentación con el gráfico
pres.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Creación de gráficos dispersos
Los gráficos dispersos (también conocidos como diagramas de dispersión o gráficos x‑y) se utilizan a menudo para comprobar patrones o demostrar correlaciones entre dos variables.
Puedes querer usar un gráfico disperso cuando
- tienes datos numéricos emparejados
- tienes 2 variables que se relacionan bien entre sí
- deseas determinar si 2 variables están relacionadas
- tienes una variable independiente que tiene múltiples valores para una variable dependiente
Pasos: Crear Scattered Chart en JavaScript | Pasos: Crear PowerPoint Scattered Chart en JavaScript | Pasos: Crear PowerPoint Presentation Scattered Chart en JavaScript
- Por favor, sigue los pasos mencionados en Creación de gráficos normales
- En el tercer paso, añade un gráfico con algunos datos y especifica tu tipo de gráfico como uno de los siguientes
- ChartType.ScatterWithMarkers - Representa un gráfico de dispersión.
- ChartType.ScatterWithSmoothLinesAndMarkers - Representa un gráfico de dispersión conectado por curvas, con marcadores de datos.
- ChartType.ScatterWithSmoothLines - Representa un gráfico de dispersión conectado por curvas, sin marcadores de datos.
- ChartType.ScatterWithStraightLinesAndMarkers - Representa un gráfico de dispersión conectado por líneas, con marcadores de datos.
- ChartType.ScatterWithStraightLines - Representa un gráfico de dispersión conectado por líneas, sin marcadores de datos.
Este código JavaScript muestra cómo crear gráficos dispersos con diferentes series de marcadores:
// Instancia una clase de presentación que representa un archivo PPTX
var pres = new aspose.slides.Presentation();
try {
// Accede a la primera diapositiva
var slide = pres.getSlides().get_Item(0);
// Crea el gráfico predeterminado
var chart = slide.getShapes().addChart(aspose.slides.ChartType.ScatterWithSmoothLines, 0, 0, 400, 400);
// Obtiene el índice de la hoja de datos del gráfico predeterminada
var defaultWorksheetIndex = 0;
// Obtiene la hoja de datos del gráfico
var fact = chart.getChartData().getChartDataWorkbook();
// Elimina la serie de demostración
chart.getChartData().getSeries().clear();
// Agrega nuevas series
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());
// Obtiene la primera serie del gráfico
var series = chart.getChartData().getSeries().get_Item(0);
// Agrega un nuevo punto (1:3) a la serie
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 1), fact.getCell(defaultWorksheetIndex, 2, 2, 3));
// Agrega un nuevo punto (2:10)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 2), fact.getCell(defaultWorksheetIndex, 3, 2, 10));
// Cambia el tipo de serie
series.setType(aspose.slides.ChartType.ScatterWithStraightLinesAndMarkers);
// Cambia el marcador de la serie del gráfico
series.getMarker().setSize(10);
series.getMarker().setSymbol(aspose.slides.MarkerStyleType.Star);
// Obtiene la segunda serie del gráfico
series = chart.getChartData().getSeries().get_Item(1);
// Agrega un nuevo punto (5:2) allí
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 5), fact.getCell(defaultWorksheetIndex, 2, 4, 2));
// Agrega un nuevo punto (3:1)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 3, 3), fact.getCell(defaultWorksheetIndex, 3, 4, 1));
// Agrega un nuevo punto (2:2)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 4, 3, 2), fact.getCell(defaultWorksheetIndex, 4, 4, 2));
// Agrega un nuevo punto (5:1)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 5, 3, 5), fact.getCell(defaultWorksheetIndex, 5, 4, 1));
// Cambia el marcador de la serie del gráfico
series.getMarker().setSize(10);
series.getMarker().setSymbol(aspose.slides.MarkerStyleType.Circle);
pres.save("AsposeChart_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Creación de gráficos circulares
Los gráficos circulares son ideales para mostrar la relación parte‑todo en los datos, especialmente cuando los datos contienen etiquetas categóricas con valores numéricos. Sin embargo, si tus datos tienen muchas partes o etiquetas, podrías considerar usar un gráfico de barras.
Pasos: Crear Pie Chart en JavaScript | Pasos: Crear PowerPoint Pie Chart en JavaScript | Pasos: Crear PowerPoint Presentation Pie Chart en JavaScript
- Crear una instancia de la clase Presentation.
- Obtener la referencia de una diapositiva mediante su índice.
- Añadir un gráfico con datos predeterminados junto con el tipo deseado (en este caso, ChartType.Pie).
- Acceder a los datos del gráfico mediante ChartDataWorkbook.
- Eliminar las series y categorías predeterminadas.
- Añadir nuevas series y categorías.
- Añadir nuevos datos al gráfico para las series.
- Añadir nuevos puntos al gráfico y colores personalizados para los sectores del gráfico circular.
- Definir etiquetas para las series.
- Definir líneas guía para las etiquetas de series.
- Establecer el ángulo de rotación para las diapositivas del gráfico circular.
- Guardar la presentación modificada como archivo PPTX.
Este código JavaScript muestra cómo crear un gráfico circular:
// Instancia una clase de presentación que representa un archivo PPTX
var pres = new aspose.slides.Presentation();
try {
// Accede a la primera diapositiva
var slides = pres.getSlides().get_Item(0);
// Agrega un gráfico con datos predeterminados
var chart = slides.getShapes().addChart(aspose.slides.ChartType.Pie, 100, 100, 400, 400);
// Establece el título del gráfico
chart.getChartTitle().addTextFrameForOverriding("Sample Title");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(aspose.slides.NullableBool.True);
chart.getChartTitle().setHeight(20);
chart.setTitle(true);
// Establece la primera serie para mostrar valores
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
// Establece el índice para la hoja de datos del gráfico
var defaultWorksheetIndex = 0;
// Obtiene la hoja de datos del gráfico
var fact = chart.getChartData().getChartDataWorkbook();
// Elimina las series y categorías generadas por defecto
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
// Agrega nuevas categorías
chart.getChartData().getCategories().add(fact.getCell(0, 1, 0, "First Qtr"));
chart.getChartData().getCategories().add(fact.getCell(0, 2, 0, "2nd Qtr"));
chart.getChartData().getCategories().add(fact.getCell(0, 3, 0, "3rd Qtr"));
// Agrega nuevas series
var series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 1, "Series 1"), chart.getType());
// Rellena los datos de la serie
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
series.getDataPoints().addDataPointForPieSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 30));
// No funciona en la nueva versión
// Agregando nuevos puntos y estableciendo el color del sector
// series.IsColorVaried = true;
chart.getChartData().getSeriesGroups().get_Item(0).setColorVaried(true);
var point = series.getDataPoints().get_Item(0);
point.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
point.getFormat().getFill().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "CYAN"));
// Establece el borde del sector
point.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
point.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "GRAY"));
point.getFormat().getLine().setWidth(3.0);
point.getFormat().getLine().setStyle(aspose.slides.LineStyle.ThinThick);
point.getFormat().getLine().setDashStyle(aspose.slides.LineDashStyle.DashDot);
var point1 = series.getDataPoints().get_Item(1);
point1.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
point1.getFormat().getFill().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "ORANGE"));
// Establece el borde del sector
point1.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
point1.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "BLUE"));
point1.getFormat().getLine().setWidth(3.0);
point1.getFormat().getLine().setStyle(aspose.slides.LineStyle.Single);
point1.getFormat().getLine().setDashStyle(aspose.slides.LineDashStyle.LargeDashDot);
var point2 = series.getDataPoints().get_Item(2);
point2.getFormat().getFill().setFillType(java.newByte(aspose.slides.FillType.Solid));
point2.getFormat().getFill().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "YELLOW"));
// Establece el borde del sector
point2.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
point2.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(java.getStaticFieldValue("java.awt.Color", "RED"));
point2.getFormat().getLine().setWidth(2.0);
point2.getFormat().getLine().setStyle(aspose.slides.LineStyle.ThinThin);
point2.getFormat().getLine().setDashStyle(aspose.slides.LineDashStyle.LargeDashDotDot);
// Crea etiquetas personalizadas para cada categoría de la nueva serie
var lbl1 = series.getDataPoints().get_Item(0).getLabel();
// lbl.ShowCategoryName = true;
lbl1.getDataLabelFormat().setShowValue(true);
var lbl2 = series.getDataPoints().get_Item(1).getLabel();
lbl2.getDataLabelFormat().setShowValue(true);
lbl2.getDataLabelFormat().setShowLegendKey(true);
lbl2.getDataLabelFormat().setShowPercentage(true);
var lbl3 = series.getDataPoints().get_Item(2).getLabel();
lbl3.getDataLabelFormat().setShowSeriesName(true);
lbl3.getDataLabelFormat().setShowPercentage(true);
// Muestra líneas de guía para el gráfico
series.getLabels().getDefaultDataLabelFormat().setShowLeaderLines(true);
// Establece el ángulo de rotación para los sectores del gráfico circular
chart.getChartData().getSeriesGroups().get_Item(0).setFirstSliceAngle(180);
// Guarda la presentación con un gráfico
pres.save("PieChart_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Creación de gráficos de líneas
Los gráficos de líneas (también conocidos como diagramas de líneas) son ideales cuando deseas mostrar cambios en el valor a lo largo del tiempo. Con un gráfico de líneas, puedes comparar muchos datos a la vez, seguir cambios y tendencias a lo largo del tiempo, resaltar anomalías en series de datos, etc.
- Crear una instancia de la clase Presentation.
- Obtener la referencia de una diapositiva mediante su índice.
- Añadir un gráfico con datos predeterminados junto con el tipo deseado (en este caso,
ChartType.Line). - Acceder a los datos del gráfico mediante IChartDataWorkbook.
- Eliminar las series y categorías predeterminadas.
- Añadir nuevas series y categorías.
- Añadir nuevos datos al gráfico para las series.
- Guardar la presentación modificada como archivo PPTX.
Este código JavaScript muestra cómo crear un gráfico de líneas:
var pres = new aspose.slides.Presentation();
try {
var lineChart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Line, 10, 50, 600, 350);
pres.save("lineChart.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Por defecto, los puntos en un gráfico de líneas están unidos por líneas rectas continuas. Si deseas que los puntos se unan mediante trazos en lugar de líneas sólidas, puedes especificar tu tipo de trazo preferido así:
var lineChart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Line, 10, 50, 600, 350);
for (let i = 0; i < lineChart.getChartData().getSeries().size(); i++) {
let series = lineChart.getChartData().getSeries().get_Item(i);
series.getFormat().getLine().setDashStyle(aspose.slides.LineDashStyle.Dash);
});
Creación de gráficos de mapa de árbol
Los gráficos de mapa de árbol son ideales para datos de ventas cuando deseas mostrar el tamaño relativo de categorías de datos y, al mismo tiempo, llamar rápidamente la atención sobre los elementos que son grandes contribuyentes a cada categoría.
Pasos: Crear Tree Map Chart en JavaScript | Pasos: Crear PowerPoint Tree Map Chart en JavaScript | Pasos: Crear PowerPoint Presentation Tree Map Chart en JavaScript
- Crear una instancia de la clase Presentation .
- Obtener la referencia de una diapositiva mediante su índice.
- Añadir un gráfico con datos predeterminados junto con el tipo deseado (en este caso, ChartType.TreeMap).
- Acceder a los datos del gráfico mediante ChartDataWorkbook.
- Eliminar las series y categorías predeterminadas.
- Añadir nuevas series y categorías.
- Añadir nuevos datos al gráfico para las series.
- Guardar la presentación modificada como archivo PPTX.
Este código JavaScript muestra cómo crear un gráfico de mapa de árbol:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Treemap, 50, 50, 500, 400);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
var wb = chart.getChartData().getChartDataWorkbook();
wb.clear(0);
// rama 1
var leaf = chart.getChartData().getCategories().add(wb.getCell(0, "C1", "Leaf1"));
leaf.getGroupingLevels().setGroupingItem(1, "Stem1");
leaf.getGroupingLevels().setGroupingItem(2, "Branch1");
chart.getChartData().getCategories().add(wb.getCell(0, "C2", "Leaf2"));
leaf = chart.getChartData().getCategories().add(wb.getCell(0, "C3", "Leaf3"));
leaf.getGroupingLevels().setGroupingItem(1, "Stem2");
chart.getChartData().getCategories().add(wb.getCell(0, "C4", "Leaf4"));
// rama 2
leaf = chart.getChartData().getCategories().add(wb.getCell(0, "C5", "Leaf5"));
leaf.getGroupingLevels().setGroupingItem(1, "Stem3");
leaf.getGroupingLevels().setGroupingItem(2, "Branch2");
chart.getChartData().getCategories().add(wb.getCell(0, "C6", "Leaf6"));
leaf = chart.getChartData().getCategories().add(wb.getCell(0, "C7", "Leaf7"));
leaf.getGroupingLevels().setGroupingItem(1, "Stem4");
chart.getChartData().getCategories().add(wb.getCell(0, "C8", "Leaf8"));
var series = chart.getChartData().getSeries().add(aspose.slides.ChartType.Treemap);
series.getLabels().getDefaultDataLabelFormat().setShowCategoryName(true);
series.getDataPoints().addDataPointForTreemapSeries(wb.getCell(0, "D1", 4));
series.getDataPoints().addDataPointForTreemapSeries(wb.getCell(0, "D2", 5));
series.getDataPoints().addDataPointForTreemapSeries(wb.getCell(0, "D3", 3));
series.getDataPoints().addDataPointForTreemapSeries(wb.getCell(0, "D4", 6));
series.getDataPoints().addDataPointForTreemapSeries(wb.getCell(0, "D5", 9));
series.getDataPoints().addDataPointForTreemapSeries(wb.getCell(0, "D6", 9));
series.getDataPoints().addDataPointForTreemapSeries(wb.getCell(0, "D7", 4));
series.getDataPoints().addDataPointForTreemapSeries(wb.getCell(0, "D8", 3));
series.setParentLabelLayout(aspose.slides.ParentLabelLayoutType.Overlapping);
pres.save("Treemap.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Creación de gráficos de acciones
Pasos: Crear Stock Chart en JavaScript | Pasos: Crear PowerPoint Stock Chart en JavaScript | Pasos: Crear PowerPoint Presentation Stock Chart en JavaScript
- Crear una instancia de la clase Presentation .
- Obtener la referencia de una diapositiva mediante su índice.
- Añadir un gráfico con datos predeterminados junto con el tipo deseado (ChartType.OpenHighLowClose).
- Acceder a los datos del gráfico mediante ChartDataWorkbook.
- Eliminar las series y categorías predeterminadas.
- Añadir nuevas series y categorías.
- Añadir nuevos datos al gráfico para las series.
- Especificar el formato HiLowLines.
- Guardar la presentación modificada como archivo PPTX.
Ejemplo de código JavaScript usado para crear un gráfico de acciones:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.OpenHighLowClose, 50, 50, 600, 400);
var wb = chart.getChartData().getChartDataWorkbook();
chart.getChartData().getCategories().add(wb.getCell(0, 1, 0, "A"));
chart.getChartData().getCategories().add(wb.getCell(0, 2, 0, "B"));
chart.getChartData().getCategories().add(wb.getCell(0, 3, 0, "C"));
chart.getChartData().getSeries().add(wb.getCell(0, 0, 1, "Open"), chart.getType());
chart.getChartData().getSeries().add(wb.getCell(0, 0, 2, "High"), chart.getType());
chart.getChartData().getSeries().add(wb.getCell(0, 0, 3, "Low"), chart.getType());
chart.getChartData().getSeries().add(wb.getCell(0, 0, 4, "Close"), chart.getType());
var series = chart.getChartData().getSeries().get_Item(0);
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 1, 1, 72));
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 2, 1, 25));
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 3, 1, 38));
series = chart.getChartData().getSeries().get_Item(1);
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 1, 2, 172));
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 2, 2, 57));
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 3, 2, 57));
series = chart.getChartData().getSeries().get_Item(2);
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 1, 3, 12));
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 2, 3, 12));
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 3, 3, 13));
series = chart.getChartData().getSeries().get_Item(3);
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 1, 4, 25));
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 2, 4, 38));
series.getDataPoints().addDataPointForStockSeries(wb.getCell(0, 3, 4, 50));
chart.getChartData().getSeriesGroups().get_Item(0).getUpDownBars().setUpDownBars(true);
chart.getChartData().getSeriesGroups().get_Item(0).getHiLowLinesFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.Solid));
for (let i = 0; i < chart.getChartData().getSeries().size(); i++) {
let ser = chart.getChartData().getSeries().get_Item(i);
ser.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
}
pres.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Creación de gráficos de caja y bigote
Pasos: Crear Box and Whisker Chart en JavaScript | Pasos: Crear PowerPoint Box and Whisker Chart en JavaScript | Pasos: Crear PowerPoint Presentation Box and Whisker Chart en JavaScript
- Crear una instancia de la clase Presentation .
- Obtener la referencia de una diapositiva mediante su índice.
- Añadir un gráfico con datos predeterminados junto con el tipo deseado (ChartType.BoxAndWhisker).
- Acceder a los datos del gráfico mediante ChartDataWorkbook.
- Eliminar las series y categorías predeterminadas.
- Añadir nuevas series y categorías.
- Añadir nuevos datos al gráfico para las series.
- Guardar la presentación modificada como archivo PPTX.
Este código JavaScript muestra cómo crear un gráfico de caja y bigote:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.BoxAndWhisker, 50, 50, 500, 400);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
var wb = chart.getChartData().getChartDataWorkbook();
wb.clear(0);
chart.getChartData().getCategories().add(wb.getCell(0, "A1", "Category 1"));
chart.getChartData().getCategories().add(wb.getCell(0, "A2", "Category 1"));
chart.getChartData().getCategories().add(wb.getCell(0, "A3", "Category 1"));
chart.getChartData().getCategories().add(wb.getCell(0, "A4", "Category 1"));
chart.getChartData().getCategories().add(wb.getCell(0, "A5", "Category 1"));
chart.getChartData().getCategories().add(wb.getCell(0, "A6", "Category 1"));
var series = chart.getChartData().getSeries().add(aspose.slides.ChartType.BoxAndWhisker);
series.setQuartileMethod(aspose.slides.QuartileMethodType.Exclusive);
series.setShowMeanLine(true);
series.setShowMeanMarkers(true);
series.setShowInnerPoints(true);
series.setShowOutlierPoints(true);
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B1", 15));
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B2", 41));
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B3", 16));
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B4", 10));
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B5", 23));
series.getDataPoints().addDataPointForBoxAndWhiskerSeries(wb.getCell(0, "B6", 16));
pres.save("BoxAndWhisker.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Creación de gráficos de embudo
Pasos: Crear Funnel Chart en JavaScript | Pasos: Crear PowerPoint Funnel Chart en JavaScript | Pasos: Crear PowerPoint Presentation Funnel Chart en JavaScript
- Crear una instancia de la clase Presentation .
- Obtener la referencia de una diapositiva mediante su índice.
- Añadir un gráfico con datos predeterminados junto con el tipo deseado (ChartType.Funnel).
- Guardar la presentación modificada como archivo PPTX.
El código JavaScript muestra cómo crear un gráfico de embudo:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Funnel, 50, 50, 500, 400);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
var wb = chart.getChartData().getChartDataWorkbook();
wb.clear(0);
chart.getChartData().getCategories().add(wb.getCell(0, "A1", "Category 1"));
chart.getChartData().getCategories().add(wb.getCell(0, "A2", "Category 2"));
chart.getChartData().getCategories().add(wb.getCell(0, "A3", "Category 3"));
chart.getChartData().getCategories().add(wb.getCell(0, "A4", "Category 4"));
chart.getChartData().getCategories().add(wb.getCell(0, "A5", "Category 5"));
chart.getChartData().getCategories().add(wb.getCell(0, "A6", "Category 6"));
var series = chart.getChartData().getSeries().add(aspose.slides.ChartType.Funnel);
series.getDataPoints().addDataPointForFunnelSeries(wb.getCell(0, "B1", 50));
series.getDataPoints().addDataPointForFunnelSeries(wb.getCell(0, "B2", 100));
series.getDataPoints().addDataPointForFunnelSeries(wb.getCell(0, "B3", 200));
series.getDataPoints().addDataPointForFunnelSeries(wb.getCell(0, "B4", 300));
series.getDataPoints().addDataPointForFunnelSeries(wb.getCell(0, "B5", 400));
series.getDataPoints().addDataPointForFunnelSeries(wb.getCell(0, "B6", 500));
pres.save("Funnel.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Creación de gráficos de explosión radial
Pasos: Crear Sunburst Chart en JavaScript | Pasos: Crear PowerPoint Sunburst Chart en JavaScript | Pasos: Crear PowerPoint Presentation Sunburst Chart en JavaScript
- Crear una instancia de la clase Presentation .
- Obtener la referencia de una diapositiva mediante su índice.
- Añadir un gráfico con datos predeterminados junto con el tipo deseado (en este caso, ChartType.sunburst).
- Guardar la presentación modificada como archivo PPTX.
Este código JavaScript muestra cómo crear un gráfico de explosión radial:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Sunburst, 50, 50, 500, 400);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
var wb = chart.getChartData().getChartDataWorkbook();
wb.clear(0);
// rama 1
var leaf = chart.getChartData().getCategories().add(wb.getCell(0, "C1", "Leaf1"));
leaf.getGroupingLevels().setGroupingItem(1, "Stem1");
leaf.getGroupingLevels().setGroupingItem(2, "Branch1");
chart.getChartData().getCategories().add(wb.getCell(0, "C2", "Leaf2"));
leaf = chart.getChartData().getCategories().add(wb.getCell(0, "C3", "Leaf3"));
leaf.getGroupingLevels().setGroupingItem(1, "Stem2");
chart.getChartData().getCategories().add(wb.getCell(0, "C4", "Leaf4"));
// rama 2
leaf = chart.getChartData().getCategories().add(wb.getCell(0, "C5", "Leaf5"));
leaf.getGroupingLevels().setGroupingItem(1, "Stem3");
leaf.getGroupingLevels().setGroupingItem(2, "Branch2");
chart.getChartData().getCategories().add(wb.getCell(0, "C6", "Leaf6"));
leaf = chart.getChartData().getCategories().add(wb.getCell(0, "C7", "Leaf7"));
leaf.getGroupingLevels().setGroupingItem(1, "Stem4");
chart.getChartData().getCategories().add(wb.getCell(0, "C8", "Leaf8"));
var series = chart.getChartData().getSeries().add(aspose.slides.ChartType.Sunburst);
series.getLabels().getDefaultDataLabelFormat().setShowCategoryName(true);
series.getDataPoints().addDataPointForSunburstSeries(wb.getCell(0, "D1", 4));
series.getDataPoints().addDataPointForSunburstSeries(wb.getCell(0, "D2", 5));
series.getDataPoints().addDataPointForSunburstSeries(wb.getCell(0, "D3", 3));
series.getDataPoints().addDataPointForSunburstSeries(wb.getCell(0, "D4", 6));
series.getDataPoints().addDataPointForSunburstSeries(wb.getCell(0, "D5", 9));
series.getDataPoints().addDataPointForSunburstSeries(wb.getCell(0, "D6", 9));
series.getDataPoints().addDataPointForSunburstSeries(wb.getCell(0, "D7", 4));
series.getDataPoints().addDataPointForSunburstSeries(wb.getCell(0, "D8", 3));
pres.save("Sunburst.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Creación de gráficos de histograma
Pasos: Crear Histogram Chart en JavaScript | Pasos: Crear PowerPoint Histogram Chart en JavaScript | Pasos: Crear PowerPoint Presentation Histogram Chart en JavaScript
- Crear una instancia de la clase Presentation .
- Obtener la referencia de una diapositiva mediante su índice.
- Añadir un gráfico con datos predeterminados junto con el tipo deseado (ChartType.Histogram).
- Acceder a los datos del gráfico mediante ChartDataWorkbook.
- Eliminar las series y categorías predeterminadas.
- Añadir nuevas series y categorías.
- Guardar la presentación modificada como archivo PPTX.
Este código JavaScript muestra cómo crear un gráfico de histograma:
var pres = new aspose.slides.Presentation();
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Histogram, 50, 50, 500, 400);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
var wb = chart.getChartData().getChartDataWorkbook();
wb.clear(0);
var series = chart.getChartData().getSeries().add(aspose.slides.ChartType.Histogram);
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A1", 15));
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A2", -41));
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A3", 16));
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A4", 10));
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A5", -23));
series.getDataPoints().addDataPointForHistogramSeries(wb.getCell(0, "A6", 16));
chart.getAxes().getHorizontalAxis().setAggregationType(aspose.slides.AxisAggregationType.Automatic);
Creación de gráficos de radar
Pasos: Crear Radar Chart en JavaScript | Pasos: Crear PowerPoint Radar Chart en JavaScript | Pasos: Crear PowerPoint Presentation Radar Chart en JavaScript
- Crear una instancia de la clase Presentation .
- Obtener la referencia de una diapositiva mediante su índice.
- Añadir un gráfico con algunos datos y especificar tu tipo de gráfico preferido (
ChartType.Radaren este caso). - Guardar la presentación modificada como archivo PPTX.
Este código JavaScript muestra cómo crear un gráfico de radar:
var pres = new aspose.slides.Presentation();
try {
pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Radar, 20, 20, 400, 300);
pres.save("Radar-chart.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Creación de gráficos multi categoría
Pasos: Crear Multi Category Chart en JavaScript | Pasos: Crear PowerPoint Multi Category Chart en JavaScript | Pasos: Crear PowerPoint Presentation Multi Category Chart en JavaScript
- Crear una instancia de la clase Presentation .
- Obtener la referencia de una diapositiva mediante su índice.
- Añadir un gráfico con datos predeterminados junto con el tipo deseado (ChartType.ClusteredColumn).
- Acceder a los datos del gráfico mediante ChartDataWorkbook.
- Eliminar las series y categorías predeterminadas.
- Añadir nuevas series y categorías.
- Añadir nuevos datos al gráfico para las series.
- Guardar la presentación modificada como archivo PPTX.
Este código JavaScript muestra cómo crear un gráfico multicatálogo:
var pres = new aspose.slides.Presentation();
try {
var ch = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 100, 100, 600, 450);
ch.getChartData().getSeries().clear();
ch.getChartData().getCategories().clear();
var fact = ch.getChartData().getChartDataWorkbook();
fact.clear(0);
var defaultWorksheetIndex = 0;
var category = ch.getChartData().getCategories().add(fact.getCell(0, "c2", "A"));
category.getGroupingLevels().setGroupingItem(1, "Group1");
category = ch.getChartData().getCategories().add(fact.getCell(0, "c3", "B"));
category = ch.getChartData().getCategories().add(fact.getCell(0, "c4", "C"));
category.getGroupingLevels().setGroupingItem(1, "Group2");
category = ch.getChartData().getCategories().add(fact.getCell(0, "c5", "D"));
category = ch.getChartData().getCategories().add(fact.getCell(0, "c6", "E"));
category.getGroupingLevels().setGroupingItem(1, "Group3");
category = ch.getChartData().getCategories().add(fact.getCell(0, "c7", "F"));
category = ch.getChartData().getCategories().add(fact.getCell(0, "c8", "G"));
category.getGroupingLevels().setGroupingItem(1, "Group4");
category = ch.getChartData().getCategories().add(fact.getCell(0, "c9", "H"));
// Añadiendo series
var series = ch.getChartData().getSeries().add(fact.getCell(0, "D1", "Series 1"), aspose.slides.ChartType.ClusteredColumn);
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D2", 10));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D3", 20));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D4", 30));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D5", 40));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D6", 50));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D7", 60));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D8", 70));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, "D9", 80));
// Save presentation with chart
pres.save("AsposeChart_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Creación de gráficos de mapa
Un gráfico de mapa es una visualización de un área que contiene datos. Los gráficos de mapa son ideales para comparar datos o valores entre regiones geográficas.
Pasos: Crear Map Chart en JavaScript | Pasos: Crear PowerPoint Map Chart en JavaScript | Pasos: Crear PowerPoint Presentation Map Chart en JavaScript
Este código JavaScript muestra cómo crear un gráfico de mapa:
let pres = new aspose.slides.Presentation();
try {
let chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Map, 50, 50, 500, 400);
pres.save("mapChart.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Creación de gráficos combinados
Un gráfico combinado (o gráfico combo) combina dos o más tipos de gráficos en un solo diagrama. Este gráfico te permite resaltar, comparar o examinar diferencias entre dos o más conjuntos de datos, ayudándote a identificar relaciones entre ellos.

El siguiente código JavaScript muestra cómo crear el gráfico combinado mostrado arriba en una presentación de PowerPoint:
function createComboChart() {
let presentation = new aspose.slides.Presentation();
let slide = presentation.getSlides().get_Item(0);
try {
let chart = createChartWithFirstSeries(slide);
addSecondSeriesToChart(chart);
addThirdSeriesToChart(chart);
setPrimaryAxesFormat(chart);
setSecondaryAxesFormat(chart);
presentation.save("combo-chart.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
presentation.dispose();
}
}
function createChartWithFirstSeries(slide) {
let chart = slide.getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 50, 50, 600, 400);
// Establecer el título del gráfico.
chart.setTitle(true);
chart.getChartTitle().addTextFrameForOverriding("Chart Title");
chart.getChartTitle().setOverlay(false);
let titleParagraph = chart.getChartTitle().getTextFrameForOverriding().getParagraphs().get_Item(0);
let titleFormat = titleParagraph.getParagraphFormat().getDefaultPortionFormat();
titleFormat.setFontBold(java.newByte(aspose.slides.NullableBool.False));
titleFormat.setFontHeight(18);
// Establecer la leyenda del gráfico.
chart.getLegend().setPosition(aspose.slides.LegendPositionType.Bottom);
chart.getLegend().getTextFormat().getPortionFormat().setFontHeight(12);
// Eliminar las series y categorías generadas por defecto.
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
const worksheetIndex = 0;
let workbook = chart.getChartData().getChartDataWorkbook();
// Agregar nuevas categorías.
chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 1, 0, "Category 1"));
chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 2, 0, "Category 2"));
chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 3, 0, "Category 3"));
chart.getChartData().getCategories().add(workbook.getCell(worksheetIndex, 4, 0, "Category 4"));
// Agregar la primera serie.
let seriesNameCell = workbook.getCell(worksheetIndex, 0, 1, "Series 1");
let series = chart.getChartData().getSeries().add(seriesNameCell, chart.getType());
series.getParentSeriesGroup().setOverlap(java.newByte(-25));
series.getParentSeriesGroup().setGapWidth(220);
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 1, 1, 4.3));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 2, 1, 2.5));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 3, 1, 3.5));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 4, 1, 4.5));
return chart;
}
function addSecondSeriesToChart(chart) {
let workbook = chart.getChartData().getChartDataWorkbook();
const worksheetIndex = 0;
let seriesNameCell = workbook.getCell(worksheetIndex, 0, 2, "Series 2");
let series = chart.getChartData().getSeries().add(seriesNameCell, aspose.slides.ChartType.ClusteredColumn);
series.getParentSeriesGroup().setOverlap(java.newByte(-25));
series.getParentSeriesGroup().setGapWidth(220);
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 1, 2, 2.4));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 2, 2, 4.4));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 3, 2, 1.8));
series.getDataPoints().addDataPointForBarSeries(workbook.getCell(worksheetIndex, 4, 2, 2.8));
}
function addThirdSeriesToChart(chart) {
let workbook = chart.getChartData().getChartDataWorkbook();
const worksheetIndex = 0;
let seriesNameCell = workbook.getCell(worksheetIndex, 0, 3, "Series 3");
let series = chart.getChartData().getSeries().add(seriesNameCell, aspose.slides.ChartType.Line);
series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 1, 3, 2.0));
series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 2, 3, 2.0));
series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 3, 3, 3.0));
series.getDataPoints().addDataPointForLineSeries(workbook.getCell(worksheetIndex, 4, 3, 5.0));
series.setPlotOnSecondAxis(true);
}
function setPrimaryAxesFormat(chart) {
// Establecer el eje horizontal.
let horizontalAxis = chart.getAxes().getHorizontalAxis();
horizontalAxis.getTextFormat().getPortionFormat().setFontHeight(12);
horizontalAxis.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
setAxisTitle(horizontalAxis, "X Axis");
// Establecer el eje vertical.
let verticalAxis = chart.getAxes().getVerticalAxis();
verticalAxis.getTextFormat().getPortionFormat().setFontHeight(12);
verticalAxis.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
setAxisTitle(verticalAxis, "Y Axis 1");
// Establecer el color de las líneas de cuadrícula principales verticales.
let majorGridLinesFormat = verticalAxis.getMajorGridLinesFormat().getLine().getFillFormat();
majorGridLinesFormat.setFillType(java.newByte(aspose.slides.FillType.Solid));
majorGridLinesFormat.getSolidFillColor().setColor(java.newInstanceSync("java.awt.Color", 217, 217, 217));
}
function setSecondaryAxesFormat(chart) {
// Establecer el eje horizontal secundario.
let secondaryHorizontalAxis = chart.getAxes().getSecondaryHorizontalAxis();
secondaryHorizontalAxis.setPosition(aspose.slides.AxisPositionType.Bottom);
secondaryHorizontalAxis.setCrossType(aspose.slides.CrossesType.Maximum);
secondaryHorizontalAxis.setVisible(false);
secondaryHorizontalAxis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
secondaryHorizontalAxis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
// Establecer el eje vertical secundario.
let secondaryVerticalAxis = chart.getAxes().getSecondaryVerticalAxis();
secondaryVerticalAxis.setPosition(aspose.slides.AxisPositionType.Right);
secondaryVerticalAxis.getTextFormat().getPortionFormat().setFontHeight(12);
secondaryVerticalAxis.getFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
secondaryVerticalAxis.getMajorGridLinesFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
secondaryVerticalAxis.getMinorGridLinesFormat().getLine().getFillFormat().setFillType(java.newByte(aspose.slides.FillType.NoFill));
setAxisTitle(secondaryVerticalAxis, "Y Axis 2");
}
function setAxisTitle(axis, axisTitle) {
axis.setTitle(true);
axis.getTitle().setOverlay(false);
let titleParagraph = axis.getTitle().addTextFrameForOverriding(axisTitle).getParagraphs().get_Item(0);
let titleFormat = titleParagraph.getParagraphFormat().getDefaultPortionFormat();
titleFormat.setFontBold(java.newByte(aspose.slides.NullableBool.False));
titleFormat.setFontHeight(12);
}
Actualización de gráficos
Pasos: Actualizar PowerPoint Chart en JavaScript | Pasos: Actualizar Presentation Chart en JavaScript | Pasos: Actualizar PowerPoint Presentation Chart en JavaScript
- Instanciar una clase Presentation que represente la presentación que contiene el gráfico que deseas actualizar.
- Obtener la referencia de una diapositiva mediante su índice.
- Recorrer todas las formas para encontrar el gráfico deseado.
- Acceder a la hoja de datos del gráfico.
- Modificar los datos de las series del gráfico cambiando los valores de las series.
- Añadir una nueva serie y rellenar los datos en ella.
- Guardar la presentación modificada como archivo PPTX.
Este código JavaScript muestra cómo actualizar un gráfico:
var pres = new aspose.slides.Presentation();
try {
// Acceder al primer marcador de diapositiva
var sld = pres.getSlides().get_Item(0);
// Obtener gráfico con datos predeterminados
var chart = sld.getShapes().get_Item(0);
// Establecer el índice de la hoja de datos del gráfico
var defaultWorksheetIndex = 0;
// Obtener la hoja de datos del gráfico
var fact = chart.getChartData().getChartDataWorkbook();
// Cambiar el nombre de la categoría del gráfico
fact.getCell(defaultWorksheetIndex, 1, 0, "Modified Category 1");
fact.getCell(defaultWorksheetIndex, 2, 0, "Modified Category 2");
// Tomar la primera serie del gráfico
var series = chart.getChartData().getSeries().get_Item(0);
// Ahora actualizando los datos de la serie
fact.getCell(defaultWorksheetIndex, 0, 1, "New_Series1");// Modificando el nombre de la serie
series.getDataPoints().get_Item(0).getValue().setData(90);
series.getDataPoints().get_Item(1).getValue().setData(123);
series.getDataPoints().get_Item(2).getValue().setData(44);
// Tomar la segunda serie del gráfico
series = chart.getChartData().getSeries().get_Item(1);
// Ahora actualizando los datos de la serie
fact.getCell(defaultWorksheetIndex, 0, 2, "New_Series2");// Modificando el nombre de la serie
series.getDataPoints().get_Item(0).getValue().setData(23);
series.getDataPoints().get_Item(1).getValue().setData(67);
series.getDataPoints().get_Item(2).getValue().setData(99);
// Ahora, añadiendo una nueva serie
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 3, "Series 3"), chart.getType());
// Tomar la tercera serie del gráfico
series = chart.getChartData().getSeries().get_Item(2);
// Ahora rellenando los datos de la serie
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 1, 3, 20));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 50));
series.getDataPoints().addDataPointForBarSeries(fact.getCell(defaultWorksheetIndex, 3, 3, 30));
chart.setType(aspose.slides.ChartType.ClusteredCylinder);
// Guardar la presentación con el gráfico
pres.save("AsposeChartModified_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Establecer rango de datos para gráficos
Para establecer el rango de datos de un gráfico, haz lo siguiente:
- Instanciar una clase Presentation que represente la presentación que contiene el gráfico.
- Obtener la referencia de una diapositiva mediante su índice.
- Recorrer todas las formas para encontrar el gráfico deseado.
- Acceder a los datos del gráfico y establecer el rango.
- Guardar la presentación modificada como archivo PPTX.
Este código JavaScript muestra cómo establecer el rango de datos para un gráfico:
var pres = new aspose.slides.Presentation();
try {
var slide = pres.getSlides().get_Item(0);
var chart = slide.getShapes().get_Item(0);
chart.getChartData().setRange("Sheet1!A1:B4");
pres.save("SetDataRange_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
Uso de marcadores predeterminados en gráficos
Cuando utilizas un marcador predeterminado en los gráficos, cada serie del gráfico obtiene automáticamente símbolos de marcador diferentes.
Este código JavaScript muestra cómo establecer automáticamente un marcador de serie de gráfico:
var pres = new aspose.slides.Presentation();
try {
var slide = pres.getSlides().get_Item(0);
var chart = slide.getShapes().addChart(aspose.slides.ChartType.LineWithMarkers, 10, 10, 400, 400);
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
var fact = chart.getChartData().getChartDataWorkbook();
chart.getChartData().getSeries().add(fact.getCell(0, 0, 1, "Series 1"), chart.getType());
var series = chart.getChartData().getSeries().get_Item(0);
chart.getChartData().getCategories().add(fact.getCell(0, 1, 0, "C1"));
series.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 1, 1, 24));
chart.getChartData().getCategories().add(fact.getCell(0, 2, 0, "C2"));
series.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 2, 1, 23));
chart.getChartData().getCategories().add(fact.getCell(0, 3, 0, "C3"));
series.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 3, 1, -10));
chart.getChartData().getCategories().add(fact.getCell(0, 4, 0, "C4"));
series.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 4, 1, null));
chart.getChartData().getSeries().add(fact.getCell(0, 0, 2, "Series 2"), chart.getType());
// Tomar la segunda serie del gráfico
var series2 = chart.getChartData().getSeries().get_Item(1);
// Ahora rellenando los datos de la serie
series2.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 1, 2, 30));
series2.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 2, 2, 10));
series2.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 3, 2, 60));
series2.getDataPoints().addDataPointForLineSeries(fact.getCell(0, 4, 2, 40));
chart.setLegend(true);
chart.getLegend().setOverlay(false);
pres.save("DefaultMarkersInChart.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
FAQ
¿Qué tipos de gráficos admite Aspose.Slides?
Aspose.Slides admite una amplia gama de tipos de gráficos, incluidos barras, líneas, circulares, áreas, dispersión, histograma, radar y muchos más. Esta flexibilidad te permite elegir el tipo de gráfico más adecuado para tus necesidades de visualización de datos.
¿Cómo añado un nuevo gráfico a una diapositiva?
Para añadir un gráfico, primero creas una instancia de la clase Presentation , recuperas la diapositiva deseada mediante su índice y luego llamas al método para añadir un gráfico, especificando el tipo de gráfico y los datos iniciales. Este proceso integra el gráfico directamente en tu presentación.
¿Cómo puedo actualizar los datos mostrados en un gráfico?
Puedes actualizar los datos de un gráfico accediendo a su libro de datos (ChartDataWorkbook), borrando cualquier serie y categoría predeterminada, y luego añadiendo tus datos personalizados. Esto te permite refrescar el gráfico programáticamente para reflejar los datos más recientes.
¿Es posible personalizar la apariencia del gráfico?
Sí, Aspose.Slides ofrece amplias opciones de personalización. Puedes modificar colores, fuentes, etiquetas, leyendas y otros elementos de formato para adaptar la apariencia del gráfico a tus requisitos de diseño específicos.