Formato de gráficos de presentación en PHP
Descripción general
Este artículo explica cómo dar formato a los gráficos en presentaciones de PowerPoint mediante Aspose.Slides. Muestra cómo personalizar los elementos clave del gráfico, como ejes, líneas de cuadrícula, títulos, leyendas, el área de trazado y los rellenos de paredes, para mejorar la apariencia y la legibilidad de los datos del gráfico.
También demuestra cómo establecer propiedades de fuente para el texto del gráfico, aplicar formatos numéricos predefinidos a los datos del gráfico y habilitar esquinas redondeadas para el área del gráfico. En conjunto, estos ejemplos muestran cómo controlar tanto el estilo visual como la presentación de datos de los gráficos en una presentación.
Formatear entidades de gráfico
Use Gráfico para formatear los títulos de los gráficos, los ejes, las líneas de cuadrícula, las leyendas y el área de trazado. Este ejemplo crea un gráfico de líneas con marcadores y datos predeterminados, luego aplica estilos de texto y de línea separados a sus ejes de valores y de categorías. Los tamaños de fuente y los anchos de línea se especifican en puntos; las etiquetas de categoría se rotan 45 grados.
El eje de valores usa setNumberFormat de forma independiente de las celdas de origen. Su escala va de -2 a 15, con intervalos mayores de 2 y intervalos menores de 0,5. El ejemplo también combina el formato de porcentaje con setDisplayUnit establecido en miles, por lo que adapte estos ajustes al significado y la magnitud de sus datos. La leyenda permanece fuera del área de trazado porque setOverlay se llama con false. Los rellenos de la pared trasera y del suelo se configuran mediante getBackWall y getFloor, pero estas superficies solo son visibles en gráficos 3D.
use aspose\slides\ChartType;
use aspose\slides\DisplayUnitType;
use aspose\slides\FillType;
use aspose\slides\FontData;
use aspose\slides\LineDashStyle;
use aspose\slides\NullableBool;
use aspose\slides\Presentation;
use aspose\slides\PresetColor;
use aspose\slides\SaveFormat;
use aspose\slides\TickLabelPositionType;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::LineWithMarkers, 50, 50, 500, 400);
$chart->setTitle(true);
$chart->getChartTitle()->addTextFrameForOverriding("");
$chartTitle = $chart->getChartTitle()->getTextFrameForOverriding()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
$chartTitle->setText("Sample Chart");
$chartTitle->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$chartTitle->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
$chartTitle->getPortionFormat()->setFontHeight(20);
$chartTitle->getPortionFormat()->setFontBold(NullableBool::True);
$chartTitle->getPortionFormat()->setFontItalic(NullableBool::True);
$chart->getAxes()->getVerticalAxis()->getMajorGridLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$chart->getAxes()->getVerticalAxis()->getMajorGridLinesFormat()->getLine()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLUE);
$chart->getAxes()->getVerticalAxis()->getMajorGridLinesFormat()->getLine()->setWidth(5);
$chart->getAxes()->getVerticalAxis()->getMajorGridLinesFormat()->getLine()->setDashStyle(LineDashStyle::DashDot);
$chart->getAxes()->getVerticalAxis()->getMinorGridLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$chart->getAxes()->getVerticalAxis()->getMinorGridLinesFormat()->getLine()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->RED);
$chart->getAxes()->getVerticalAxis()->getMinorGridLinesFormat()->getLine()->setWidth(3);
$chart->getAxes()->getVerticalAxis()->setNumberFormatLinkedToSource(false);
$chart->getAxes()->getVerticalAxis()->setDisplayUnit(DisplayUnitType::Thousands);
$chart->getAxes()->getVerticalAxis()->setNumberFormat("0.0%");
$chart->getAxes()->getVerticalAxis()->setAutomaticMajorUnit(false);
$chart->getAxes()->getVerticalAxis()->setAutomaticMaxValue(false);
$chart->getAxes()->getVerticalAxis()->setAutomaticMinorUnit(false);
$chart->getAxes()->getVerticalAxis()->setAutomaticMinValue(false);
$chart->getAxes()->getVerticalAxis()->setMaxValue(15);
$chart->getAxes()->getVerticalAxis()->setMinValue(-2);
$chart->getAxes()->getVerticalAxis()->setMinorUnit(0.5);
$chart->getAxes()->getVerticalAxis()->setMajorUnit(2.0);
$valueAxisText = $chart->getAxes()->getVerticalAxis()->getTextFormat()->getPortionFormat();
$valueAxisText->setFontBold(NullableBool::True);
$valueAxisText->setFontHeight(16);
$valueAxisText->setFontItalic(NullableBool::True);
$valueAxisText->getFillFormat()->setFillType(FillType::Solid);
$valueAxisText->getFillFormat()->getSolidFillColor()->setPresetColor(PresetColor::DarkGreen);
$valueAxisText->setLatinFont(new FontData("Times New Roman"));
$chart->getAxes()->getVerticalAxis()->setTitle(true);
$chart->getAxes()->getVerticalAxis()->getTitle()->addTextFrameForOverriding("");
$valueAxisTitle = $chart->getAxes()->getVerticalAxis()->getTitle()->getTextFrameForOverriding()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
$valueAxisTitle->setText("Primary Axis");
$valueAxisTitle->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$valueAxisTitle->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
$valueAxisTitle->getPortionFormat()->setFontHeight(20);
$valueAxisTitle->getPortionFormat()->setFontBold(NullableBool::True);
$valueAxisTitle->getPortionFormat()->setFontItalic(NullableBool::True);
$chart->getAxes()->getHorizontalAxis()->getMajorGridLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$chart->getAxes()->getHorizontalAxis()->getMajorGridLinesFormat()->getLine()->getFillFormat()->getSolidFillColor()->setColor(new Java("java.awt.Color", 0, 128, 0));
$chart->getAxes()->getHorizontalAxis()->getMajorGridLinesFormat()->getLine()->setWidth(5);
$chart->getAxes()->getHorizontalAxis()->getMinorGridLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$chart->getAxes()->getHorizontalAxis()->getMinorGridLinesFormat()->getLine()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->YELLOW);
$chart->getAxes()->getHorizontalAxis()->getMinorGridLinesFormat()->getLine()->setWidth(3);
$categoryAxisText = $chart->getAxes()->getHorizontalAxis()->getTextFormat()->getPortionFormat();
$categoryAxisText->setFontBold(NullableBool::True);
$categoryAxisText->setFontHeight(16);
$categoryAxisText->setFontItalic(NullableBool::True);
$categoryAxisText->getFillFormat()->setFillType(FillType::Solid);
$categoryAxisText->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLUE);
$categoryAxisText->setLatinFont(new FontData("Arial"));
$chart->getAxes()->getHorizontalAxis()->setTitle(true);
$chart->getAxes()->getHorizontalAxis()->getTitle()->addTextFrameForOverriding("");
$categoryAxisTitle = $chart->getAxes()->getHorizontalAxis()->getTitle()->getTextFrameForOverriding()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
$categoryAxisTitle->setText("Sample Category");
$categoryAxisTitle->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$categoryAxisTitle->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
$categoryAxisTitle->getPortionFormat()->setFontHeight(20);
$categoryAxisTitle->getPortionFormat()->setFontBold(NullableBool::True);
$categoryAxisTitle->getPortionFormat()->setFontItalic(NullableBool::True);
$chart->getAxes()->getHorizontalAxis()->setTickLabelPosition(TickLabelPositionType::Low);
$chart->getAxes()->getHorizontalAxis()->setTickLabelRotationAngle(45);
$legendText = $chart->getLegend()->getTextFormat()->getPortionFormat();
$legendText->setFontBold(NullableBool::True);
$legendText->setFontHeight(16);
$legendText->setFontItalic(NullableBool::True);
$legendText->getFillFormat()->setFillType(FillType::Solid);
$legendText->getFillFormat()->getSolidFillColor()->setPresetColor(PresetColor::DarkRed);
$chart->getLegend()->setOverlay(false);
$chart->getBackWall()->setThickness(1);
$chart->getBackWall()->getFormat()->getFill()->setFillType(FillType::Solid);
$chart->getBackWall()->getFormat()->getFill()->getSolidFillColor()->setColor(new Java("java.awt.Color", 255, 165, 0));
$chart->getFloor()->getFormat()->getFill()->setFillType(FillType::Solid);
$chart->getFloor()->getFormat()->getFill()->getSolidFillColor()->setColor(java("java.awt.Color")->RED);
$chart->getPlotArea()->getFormat()->getFill()->setFillType(FillType::Solid);
$chart->getPlotArea()->getFormat()->getFill()->getSolidFillColor()->setPresetColor(PresetColor::LightCyan);
$presentation->save("FormattedChart_out.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Establecer propiedades de fuente para un gráfico
Establezca el tamaño de fuente predeterminado para el texto del gráfico mediante Chart::getTextFormat. El ejemplo crea un gráfico de columnas agrupadas con una fuente de 20 puntos y habilita las etiquetas de valor para la primera serie mediante setShowValue. Los elementos de texto con sus propias configuraciones de fuente explícitas pueden sobrescribir el valor predeterminado del gráfico.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::ClusteredColumn, 100, 100, 500, 400);
$chart->getTextFormat()->getPortionFormat()->setFontHeight(20);
$chart->getChartData()->getSeries()->get_Item(0)->getLabels()->getDefaultDataLabelFormat()->setShowValue(true);
$presentation->save("FontPropertiesForChart.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Establecer el formato numérico
Utilice ChartDataCell::setPresetNumberFormat para aplicar un formato numérico incorporado a las celdas del libro de trabajo del gráfico. Este ejemplo crea un gráfico de columnas agrupadas con datos predeterminados respaldados por celdas y aplica el índice de formato 10 (0.00%) a cada celda de valor de serie. Esto cambia la forma en que los valores se muestran en el libro de trabajo sin modificar los números subyacentes; por ejemplo, 0,25 se muestra como 25,00 %. Los formatos de eje y de etiqueta de datos pueden configurarse por separado.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::ClusteredColumn, 50, 50, 500, 400);
$series = $chart->getChartData()->getSeries();
for ($i = 0; $i < java_values($series->size()); $i++) {
$chartSeries = $series->get_Item($i);
$dataPoints = $chartSeries->getDataPoints();
for ($j = 0; $j < java_values($dataPoints->size()); $j++) {
$dataPoint = $dataPoints->get_Item($j);
$dataPoint->getValue()->getAsCell()->setPresetNumberFormat(10); // 0,00%
}
}
$presentation->save("PresetNumberFormat_out.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
La siguiente tabla enumera los índices de formatos numéricos incorporados con formatos representativos de Excel para la configuración regional Inglés (Estados Unidos). Las cadenas de formato exactas y su visualización pueden variar según la aplicación y la configuración regional, particularmente para moneda y fechas. Algunos formatos de Excel también difieren de los valores predeterminados en el estándar OOXML, como se describe en Notas de implementación del formato numérico de Microsoft.
| Index | Format |
|---|---|
| 0 | General |
| 1 | 0 |
| 2 | 0.00 |
| 3 | #,##0 |
| 4 | #,##0.00 |
| 5 | "$"#,##0_);("$"#,##0) |
| 6 | "$"#,##0_);[Red]("$"#,##0) |
| 7 | "$"#,##0.00_);("$"#,##0.00) |
| 8 | "$"#,##0.00_);[Red]("$"#,##0.00) |
| 9 | 0% |
| 10 | 0.00% |
| 11 | 0.00E+00 |
| 12 | # ?/? |
| 13 | # ??/?? |
| 14 | m/d/yyyy |
| 15 | d-mmm-yy |
| 16 | d-mmm |
| 17 | mmm-yy |
| 18 | h:mm AM/PM |
| 19 | h:mm:ss AM/PM |
| 20 | h:mm |
| 21 | h:mm:ss |
| 22 | m/d/yyyy h:mm |
| 37 | #,##0_);(#,##0) |
| 38 | #,##0_);[Red](#,##0) |
| 39 | #,##0.00_);(#,##0.00) |
| 40 | #,##0.00_);[Red](#,##0.00) |
| 41 | _(* #,##0_);_(* (#,##0);_(* "-"_);_(@_) |
| 42 | _("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_) |
| 43 | _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_) |
| 44 | _("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_) |
| 45 | mm:ss |
| 46 | [h]:mm:ss |
| 47 | mm:ss.0 |
| 48 | ##0.0E+0 |
| 49 | @ |
Establecer bordes redondeados del área del gráfico
Llama a Chart::setRoundedCorners con true para redondear las esquinas del área del gráfico. El ejemplo crea un gráfico de columnas agrupadas y utiliza getLineFormat para dar a su contorno un color gris sólido y un estilo de línea única antes de guardar la presentación.
use aspose\slides\ChartType;
use aspose\slides\FillType;
use aspose\slides\LineStyle;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try {
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::ClusteredColumn, 20, 100, 600, 400);
$chart->getLineFormat()->getFillFormat()->setFillType(FillType::Solid);
$chart->getLineFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
$chart->getLineFormat()->setStyle(LineStyle::Single);
$chart->setRoundedCorners(true);
$presentation->save("out.pptx", SaveFormat::Pptx);
} finally {
$presentation->dispose();
}
Editar anotaciones de gráficos existentes
Un gráfico puede contener formas personalizadas, como una anotación de texto que explique el estado de un informe. Acceda a estas formas mediante Chart::getUserShapes. La colección devuelta por getShapes contiene las formas propiedad del gráfico.
Un cuadro de texto ordinario colocado sobre un gráfico pertenece a la diapositiva, aunque parezca parte del gráfico. Recorrer Slide::getShapes encuentra el gráfico y las formas ordinarias de la diapositiva, pero no enumera las formas individuales dentro del gráfico. Los títulos de los gráficos y las etiquetas de datos son elementos de gráfico independientes, no anotaciones personalizadas en esta colección.
Descargue chart-annotations.pptx y colóquelo en el directorio de trabajo de la aplicación. Su primera diapositiva contiene un gráfico con una anotación sin agrupar que dice “Resultados preliminares”, una línea propiedad del gráfico y un cuadro de texto ordinario de la diapositiva que dice “Superposición de diapositiva”. El ejemplo encuentra el primer gráfico en esa diapositiva y cambia la anotación a “Resultados finales” cuando el informe está listo para publicarse. Comprueba si la colección está vacía y solo edita un AutoShape con un marco de texto no nulo devuelto por getTextFrame y con el texto esperado.
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation("chart-annotations.pptx");
try {
$slide = $presentation->getSlides()->get_Item(0);
$chartType = new JavaClass("com.aspose.slides.IChart");
$autoShapeType = new JavaClass("com.aspose.slides.IAutoShape");
$chart = null;
for ($i = 0; $i < java_values($slide->getShapes()->size()); $i++) {
$shape = $slide->getShapes()->get_Item($i);
if (java_instanceof($shape, $chartType)) {
$chart = $shape;
break;
}
}
if ($chart === null || java_values($chart->getUserShapes()->getShapes()->size()) == 0) {
echo "No chart or chart-owned shapes found." . PHP_EOL;
return;
}
$updated = false;
$userShapes = $chart->getUserShapes()->getShapes();
for ($i = 0; $i < java_values($userShapes->size()); $i++) {
$shape = $userShapes->get_Item($i);
if (java_instanceof($shape, $autoShapeType)) {
$annotation = $shape;
if (!java_is_null($annotation->getTextFrame()) && java_values($annotation->getTextFrame()->getText()) === "Preliminary results") {
$annotation->getTextFrame()->setText("Final results");
$updated = true;
break;
}
}
}
if ($updated) {
$presentation->save("chart-annotations-updated.pptx", SaveFormat::Pptx);
} else {
echo "No matching text annotation found." . PHP_EOL;
}
} finally {
$presentation->dispose();
}
La línea y el cuadro de texto de la diapositiva permanecen sin cambios. Las siguientes imágenes muestran la entrada y el resultado guardado y reabierto:
| Before editing | After editing |
|---|---|
![]() |
![]() |
Note
El ejemplo inspecciona solo las formas de nivel superior propiedad del gráfico. Si una anotación está dentro de un GroupShape, inspeccione también la colección getShapes de ese grupo. Repita esto recursivamente para los grupos anidados, aplicando las mismas comprobaciones de texto a sus formas hijas. Un grupo en sí es un contenedor; no lo trate como un cuadro de texto.Preguntas frecuentes
¿Puedo establecer rellenos semitransparentes para columnas/áreas manteniendo el borde opaco?
Sí. La transparencia del relleno y el contorno se configuran por separado. Esto es útil para mejorar la legibilidad de la cuadrícula y los datos en visualizaciones densas.
¿Cómo puedo gestionar las etiquetas de datos cuando se superponen?
Reduzca el tamaño de la fuente, desactive componentes de etiqueta no esenciales (por ejemplo, categorías), ajuste la posición de la etiqueta, muestre etiquetas solo para los puntos seleccionados si es necesario, o cambie el formato a “valor + leyenda”.
¿Puedo aplicar rellenos de degradado o patrón a las series?
Sí. Las series admiten rellenos sólidos, degradados y de patrón. En la práctica, use degradados con moderación y evite combinaciones que reduzcan el contraste con la cuadrícula y el texto.

