在 PHP 中格式化演示文稿图表
格式图表实体
Aspose.Slides for PHP via Java 允许开发人员从头向幻灯片添加自定义图表。本文介绍如何格式化不同的图表实体,包括图表类别轴和数值轴。
Aspose.Slides for PHP via Java 提供了一个简单的 API,用于管理不同的图表实体并使用自定义值进行格式化:
- 创建一个 Presentation 类的实例。
- 通过索引获取幻灯片的引用。
- 添加一个带有默认数据的图表,并指定所需类型(本示例中我们使用 ChartType::LineWithMarkers)。
- 访问图表的数值轴并设置以下属性:
- 为数值轴主网格线设置 线格式。
- 为数值轴次网格线设置 线格式。
- 为数值轴设置 数字格式。
- 为数值轴设置 最小值、最大值、主单位和次单位。
- 为数值轴数据设置 文本属性。
- 为数值轴设置 标题。
- 为数值轴设置 线格式。
- 访问图表的类别轴并设置以下属性:
- 为类别轴主网格线设置 线格式。
- 为类别轴次网格线设置 线格式。
- 为类别轴数据设置 文本属性。
- 为类别轴设置 标题。
- 为类别轴设置 标签定位。
- 为类别轴标签设置 旋转角度。
- 访问图表图例并为其设置 文本属性。
- 设置显示图表图例而不与图表重叠。
- 访问图表的 次要数值轴 并设置以下属性:
- 启用次要 数值轴。
- 为次要数值轴设置 线格式。
- 为次要数值轴设置 数字格式。
- 为次要数值轴设置 最小值、最大值、主单位和次单位。
- 现在在次要数值轴上绘制第一条图表系列。
- 设置图表后墙的填充颜色。
- 设置图表绘图区域的填充颜色。
- 将修改后的演示文稿写入 PPTX 文件。
# 创建 Presentation 类的实例
$pres = new Presentation();
try {
# 访问第一张幻灯片
$slide = $pres->getSlides()->get_Item(0);
# 添加示例图表
$chart = $slide->getShapes()->addChart(ChartType::LineWithMarkers, 50, 50, 500, 400);
# 设置图表标题
$chart->hasTitle();
$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()->isNumberFormatLinkedToSource();
$chart->getAxes()->getVerticalAxis()->setDisplayUnit(DisplayUnitType::Thousands);
$chart->getAxes()->getVerticalAxis()->setNumberFormat("0.0%");
# 设置图表的最大值、最小值
$chart->getAxes()->getVerticalAxis()->isAutomaticMajorUnit();
$chart->getAxes()->getVerticalAxis()->isAutomaticMaxValue();
$chart->getAxes()->getVerticalAxis()->isAutomaticMinorUnit();
$chart->getAxes()->getVerticalAxis()->isAutomaticMinValue();
$chart->getAxes()->getVerticalAxis()->setMaxValue(15.0);
$chart->getAxes()->getVerticalAxis()->setMinValue(-2.0);
$chart->getAxes()->getVerticalAxis()->setMinorUnit(0.5);
$chart->getAxes()->getVerticalAxis()->setMajorUnit(2.0);
# 设置数值轴文本属性
$txtVal = $chart->getAxes()->getVerticalAxis()->getTextFormat()->getPortionFormat();
$txtVal->setFontBold(NullableBool::True);
$txtVal->setFontHeight(16);
$txtVal->setFontItalic(NullableBool::True);
$txtVal->getFillFormat()->setFillType(FillType::Solid);
$txtVal->getFillFormat()->getSolidFillColor()->setColor(new java("java.awt.Color", PresetColor->DarkGreen));
$txtVal->setLatinFont(new FontData("Times New Roman"));
# 设置数值轴标题
$chart->getAxes()->getVerticalAxis()->hasTitle();
$chart->getAxes()->getVerticalAxis()->getTitle()->addTextFrameForOverriding("");
$valtitle = $chart->getAxes()->getVerticalAxis()->getTitle()->getTextFrameForOverriding()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
$valtitle->setText("Primary Axis");
$valtitle->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$valtitle->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
$valtitle->getPortionFormat()->setFontHeight(20);
$valtitle->getPortionFormat()->setFontBold(NullableBool::True);
$valtitle->getPortionFormat()->setFontItalic(NullableBool::True);
# 为类别轴设置主网格线格式
$chart->getAxes()->getHorizontalAxis()->getMajorGridLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$chart->getAxes()->getHorizontalAxis()->getMajorGridLinesFormat()->getLine()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GREEN);
$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);
# 设置类别轴文本属性
$txtCat = $chart->getAxes()->getHorizontalAxis()->getTextFormat()->getPortionFormat();
$txtCat->setFontBold(NullableBool::True);
$txtCat->setFontHeight(16);
$txtCat->setFontItalic(NullableBool::True);
$txtCat->getFillFormat()->setFillType(FillType::Solid);
$txtCat->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLUE);
$txtCat->setLatinFont(new FontData("Arial"));
# 设置类别轴标题
$chart->getAxes()->getHorizontalAxis()->hasTitle();
$chart->getAxes()->getHorizontalAxis()->getTitle()->addTextFrameForOverriding("");
$catTitle = $chart->getAxes()->getHorizontalAxis()->getTitle()->getTextFrameForOverriding()->getParagraphs()->get_Item(0)->getPortions()->get_Item(0);
$catTitle->setText("Sample Category");
$catTitle->getPortionFormat()->getFillFormat()->setFillType(FillType::Solid);
$catTitle->getPortionFormat()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
$catTitle->getPortionFormat()->setFontHeight(20);
$catTitle->getPortionFormat()->setFontBold(NullableBool::True);
$catTitle->getPortionFormat()->setFontItalic(NullableBool::True);
# 设置类别轴标签位置
$chart->getAxes()->getHorizontalAxis()->setTickLabelPosition(TickLabelPositionType::Low);
# 设置类别轴标签旋转角度
$chart->getAxes()->getHorizontalAxis()->setTickLabelRotationAngle(45);
# 设置图例文本属性
$txtleg = $chart->getLegend()->getTextFormat()->getPortionFormat();
$txtleg->setFontBold(NullableBool::True);
$txtleg->setFontHeight(16);
$txtleg->setFontItalic(NullableBool::True);
$txtleg->getFillFormat()->setFillType(FillType::Solid);
$txtleg->getFillFormat()->getSolidFillColor()->setColor(new java("java.awt.Color", PresetColor->DarkRed));
# 设置显示图例且不覆盖图表
$chart->getLegend()->setOverlay(true);
# chart.ChartData.Series[0].PlotOnSecondAxis=true;
$chart->getChartData()->getSeries()->get_Item(0)->setPlotOnSecondAxis(true);
# 设置次要数值轴
$chart->getAxes()->getSecondaryVerticalAxis()->isVisible();
$chart->getAxes()->getSecondaryVerticalAxis()->getFormat()->getLine()->setStyle(LineStyle->ThickBetweenThin);
$chart->getAxes()->getSecondaryVerticalAxis()->getFormat()->getLine()->setWidth(20);
# 设置次要数值轴数字格式
$chart->getAxes()->getSecondaryVerticalAxis()->isNumberFormatLinkedToSource();
$chart->getAxes()->getSecondaryVerticalAxis()->setDisplayUnit(DisplayUnitType::Hundreds);
$chart->getAxes()->getSecondaryVerticalAxis()->setNumberFormat("0.0%");
# 设置图表的最大值、最小值
$chart->getAxes()->getSecondaryVerticalAxis()->isAutomaticMajorUnit();
$chart->getAxes()->getSecondaryVerticalAxis()->isAutomaticMaxValue();
$chart->getAxes()->getSecondaryVerticalAxis()->isAutomaticMinorUnit();
$chart->getAxes()->getSecondaryVerticalAxis()->isAutomaticMinValue();
$chart->getAxes()->getSecondaryVerticalAxis()->setMaxValue(20.0);
$chart->getAxes()->getSecondaryVerticalAxis()->setMinValue(-5.0);
$chart->getAxes()->getSecondaryVerticalAxis()->setMinorUnit(0.5);
$chart->getAxes()->getSecondaryVerticalAxis()->setMajorUnit(2.0);
# 设置图表后墙颜色
$chart->getBackWall()->setThickness(1);
$chart->getBackWall()->getFormat()->getFill()->setFillType(FillType::Solid);
$chart->getBackWall()->getFormat()->getFill()->getSolidFillColor()->setColor(java("java.awt.Color")->ORANGE);
$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()->setColor(new java("java.awt.Color", PresetColor->LightCyan));
# 保存演示文稿
$pres->save("FormattedChart.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
设置图表的字体属性
Aspose.Slides for PHP via Java 提供了对图表字体相关属性的支持。请按照以下步骤为图表设置字体属性。
- 实例化 Presentation 类对象。
- 在幻灯片上添加图表。
- 设置字体高度。
- 保存修改后的演示文稿。
下面提供示例代码。
# 创建 Presentation 类的实例
$pres = new Presentation();
try {
$chart = $pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::ClusteredColumn, 100, 100, 500, 400);
$chart->getTextFormat()->getPortionFormat()->setFontHeight(20);
$chart->getChartData()->getSeries()->get_Item(0)->getLabels()->getDefaultDataLabelFormat()->setShowValue(true);
$pres->save("FontPropertiesForChart.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
设置数字格式
Aspose.Slides for PHP via Java 提供了一个简单的 API,用于管理图表数据格式:
- 创建一个 Presentation 类的实例。
- 通过索引获取幻灯片的引用。
- 添加一个带有默认数据的图表,并指定所需类型(本示例使用 ChartType::ClusteredColumn)。
- 从可能的预设值中设置预设数字格式。
- 遍历每个图表系列中的图表数据单元格并设置图表数据数字格式。
- 保存演示文稿。
- 设置自定义数字格式。
- 遍历每个图表系列中的图表数据单元格并设置不同的图表数据数字格式。
- 保存演示文稿。
# 创建 Presentation 类的实例
$pres = new Presentation();
try {
# 访问第一张幻灯片
$slide = $pres->getSlides()->get_Item(0);
# 添加默认的簇状柱形图
$chart = $slide->getShapes()->addChart(ChartType::ClusteredColumn, 50, 50, 500, 400);
# 访问图表系列集合
$series = $chart->getChartData()->getSeries();
# 遍历每个图表系列
foreach($series as $ser) {
# 遍历系列中的每个数据单元格
foreach($ser->getDataPoints() as $cell) {
# 设置数字格式
$cell->getValue()->getAsCell()->setPresetNumberFormat(10);// 0.00%
}
}
# 保存演示文稿
$pres->save("PresetNumberFormat.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
以下给出了可能的预设数字格式值以及它们的预设索引:
| 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/yy |
| 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/yy h:mm |
| 37 | #,##0;-#,##0 |
| 38 | #,##0;Red-#,##0 |
| 39 | #,##0.00;-#,##0.00 |
| 40 | #,##0.00;Red-#,##0.00 |
| 41 | _ * #,##0_ ;_ * “_ ;_ @_ |
| 42 | _ $* #,##0_ ;_ $* “_ ;_ @_ |
| 43 | _ * #,##0.00_ ;_ * “??_ ;_ @_ |
| 44 | _ $* #,##0.00_ ;_ $* “??_ ;_ @_ |
| 45 | mm:ss |
| 46 | h:mm:ss |
| 47 | mm:ss.0 |
| 48 | ##0.0E+00 |
| 49 | @ |
设置图表区域圆角边框
Aspose.Slides for PHP via Java 提供了对图表区域的设置支持。已在 Chart 类中添加了方法 hasRoundedCorners 和 setRoundedCorners。
- 实例化 Presentation 类对象。
- 在幻灯片上添加图表。
- 设置图表的填充类型和填充颜色。
- 将圆角属性设置为 True。
- 保存修改后的演示文稿。
下面提供示例代码。
# 创建 Presentation 类的实例
$pres = new Presentation();
try {
$slide = $pres->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::ClusteredColumn, 20, 100, 600, 400);
$chart->getLineFormat()->getFillFormat()->setFillType(FillType::Solid);
$chart->getLineFormat()->setStyle(LineStyle->Single);
$chart->setRoundedCorners(true);
$pres->save("output.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
常见问题
Can I set semi-transparent fills for columns/areas while keeping the border opaque?
是的。填充透明度和轮廓是分别配置的。这对于在密集可视化中提高网格和数据的可读性非常有用。
How can I deal with data labels when they overlap?
可以缩小字体大小,禁用非必要的标签组件(例如类别),设置标签偏移/位置,必要时仅为选定点显示标签,或将格式切换为 “数值 + 图例”。
Can I apply gradient or pattern fills to series?
可以。通常同时提供纯色和渐变/图案填充。实际使用中,请谨慎使用渐变,避免与网格和文本的对比度降低。