Personalizar ejes de gráficos en presentaciones en Android
Obtener los valores máximos en el eje vertical en los gráficos
Aspose.Slides for Android mediante Java le permite obtener los valores mínimo y máximo en un eje vertical. Siga estos pasos:
- Cree una instancia de la clase Presentation.
- Acceda a la primera diapositiva.
- Agregue un gráfico con datos predeterminados.
- Obtenga el valor máximo real del eje.
- Obtenga el valor mínimo real del eje.
- Obtenga la unidad principal real del eje.
- Obtenga la unidad menor real del eje.
- Obtenga la escala de unidad principal real del eje.
- Obtenga la escala de unidad menor real del eje.
Este código de ejemplo—una implementación de los pasos anteriores—le muestra cómo obtener los valores requeridos en Java:
Presentation pres = new Presentation();
try {
Chart chart = (Chart)pres.getSlides().get_Item(0).getShapes().addChart(ChartType.Area, 100, 100, 500, 350);
chart.validateChartLayout();
double maxValue = chart.getAxes().getVerticalAxis().getActualMaxValue();
double minValue = chart.getAxes().getVerticalAxis().getActualMinValue();
double majorUnit = chart.getAxes().getHorizontalAxis().getActualMajorUnit();
double minorUnit = chart.getAxes().getHorizontalAxis().getActualMinorUnit();
// Guarda la presentación
pres.save("MaxValuesVerticalAxis_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Intercambiar los datos entre ejes
Aspose.Slides le permite intercambiar rápidamente los datos entre ejes: los datos representados en el eje vertical (eje y) se trasladan al eje horizontal (eje x) y viceversa.
Este código Java le muestra cómo realizar la tarea de intercambio de datos entre ejes en un gráfico:
Presentation pres = new Presentation();
try {
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 100, 100, 400, 300);
// Intercambia filas y columnas
chart.getChartData().switchRowColumn();
// Guarda la presentación
pres.save("SwitchChartRowColumns_out.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Desactivar el eje vertical para gráficos de líneas
Este código Java le muestra cómo ocultar el eje vertical en un gráfico de líneas:
Presentation pres = new Presentation();
try {
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.Line, 100, 100, 400, 300);
chart.getAxes().getVerticalAxis().setVisible(false);
pres.save("chart.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Desactivar el eje horizontal para gráficos de líneas
Este código le muestra cómo ocultar el eje horizontal en un gráfico de líneas:
Presentation pres = new Presentation();
try {
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.Line, 100, 100, 400, 300);
chart.getAxes().getHorizontalAxis().setVisible(false);
pres.save("chart.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Cambiar un eje de categoría
Con la propiedad CategoryAxisType, puede especificar el tipo de eje de categoría que prefiera (date o text). Este código en Java demuestra la operación:
Presentation presentation = new Presentation("ExistingChart.pptx");
try {
IChart chart = (IChart)presentation.getSlides().get_Item(0).getShapes().get_Item(0);
chart.getAxes().getHorizontalAxis().setCategoryAxisType(CategoryAxisType.Date);
chart.getAxes().getHorizontalAxis().setAutomaticMajorUnit(false);
chart.getAxes().getHorizontalAxis().setMajorUnit(1);
chart.getAxes().getHorizontalAxis().setMajorUnitScale(TimeUnitType.Months);
presentation.save("ChangeChartCategoryAxis_out.pptx", SaveFormat.Pptx);
} finally {
if (presentation != null) presentation.dispose();
}
Establecer el formato de fecha para los valores del eje de categoría
Aspose.Slides for Android mediante Java le permite establecer el formato de fecha para un valor del eje de categoría. La operación se demuestra en este código Java:
Presentation pres = new Presentation();
try {
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.Area, 50, 50, 450, 300);
IChartDataWorkbook wb = chart.getChartData().getChartDataWorkbook();
wb.clear(0);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().add(wb.getCell(0, "A2", convertToOADate(new GregorianCalendar(2015, 1, 1))));
chart.getChartData().getCategories().add(wb.getCell(0, "A3", convertToOADate(new GregorianCalendar(2016, 1, 1))));
chart.getChartData().getCategories().add(wb.getCell(0, "A4", convertToOADate(new GregorianCalendar(2017, 1, 1))));
chart.getChartData().getCategories().add(wb.getCell(0, "A5", convertToOADate(new GregorianCalendar(2018, 1, 1))));
IChartSeries series = chart.getChartData().getSeries().add(ChartType.Line);
series.getDataPoints().addDataPointForLineSeries(wb.getCell(0, "B2", 1));
series.getDataPoints().addDataPointForLineSeries(wb.getCell(0, "B3", 2));
series.getDataPoints().addDataPointForLineSeries(wb.getCell(0, "B4", 3));
series.getDataPoints().addDataPointForLineSeries(wb.getCell(0, "B5", 4));
chart.getAxes().getHorizontalAxis().setCategoryAxisType(CategoryAxisType.Date);
chart.getAxes().getHorizontalAxis().setNumberFormatLinkedToSource(false);
chart.getAxes().getHorizontalAxis().setNumberFormat("yyyy");
pres.save("output.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
public static String convertToOADate(GregorianCalendar date) throws ParseException
{
double oaDate;
SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
java.util.Date baseDate = myFormat.parse("30 12 1899");
Long days = TimeUnit.DAYS.convert(date.getTimeInMillis() - baseDate.getTime(), TimeUnit.MILLISECONDS);
oaDate = (double) days + ((double) date.get(Calendar.HOUR_OF_DAY) / 24) + ((double) date.get(Calendar.MINUTE) / (60 * 24)) + ((double) date.get(Calendar.SECOND) / (60 * 24 * 60));
return String.valueOf(oaDate);
}
Establecer un ángulo de rotación para el título de un eje del gráfico
Aspose.Slides for Android mediante Java le permite establecer el ángulo de rotación para el título de un eje del gráfico. Este código Java demuestra la operación:
Presentation pres = new Presentation();
try {
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 450, 300);
chart.getAxes().getVerticalAxis().setTitle(true);
chart.getAxes().getVerticalAxis().getTitle().getTextFormat().getTextBlockFormat().setRotationAngle(90);
pres.save("output.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Establecer la posición del eje en un eje de categoría o de valores
Aspose.Slides for Android mediante Java le permite establecer la posición del eje en un eje de categoría o de valores. Este código Java muestra cómo realizar la tarea:
Presentation pres = new Presentation();
try {
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 450, 300);
chart.getAxes().getHorizontalAxis().setAxisBetweenCategories(true);
pres.save("output.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Habilitar la etiqueta de unidad de visualización en el eje de valores del gráfico
Aspose.Slides for Android mediante Java le permite configurar un gráfico para que muestre una etiqueta de unidad en su eje de valores. Este código Java demuestra la operación:
Presentation pres = new Presentation();
try {
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 50, 50, 450, 300);
chart.getAxes().getVerticalAxis().setDisplayUnit(DisplayUnitType.Millions);
pres.save("output.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
FAQ
¿Cómo establezco el valor en el que un eje cruza al otro (cruce de ejes)?
Los ejes ofrecen una configuración de cruce: puede elegir cruzar en cero, en la categoría/valor máximo, o en un valor numérico específico. Esto es útil para desplazar el eje X hacia arriba o abajo o para enfatizar una línea base.
¿Cómo puedo posicionar las etiquetas de marcas respecto al eje (al lado, fuera, dentro)?
Establezca la posición de la etiqueta a “cross”, “outside” o “inside”. Esto afecta la legibilidad y ayuda a conservar espacio, especialmente en gráficos pequeños.