图表坐标轴
获取图表垂直轴上的最大值
Aspose.Slides for Node.js via Java 允许您获取垂直轴上的最小值和最大值。请按照以下步骤操作:
- 创建 Presentation 类的实例。
- 访问第一张幻灯片。
- 添加一个带默认数据的图表。
- 获取轴上的实际最大值。
- 获取轴上的实际最小值。
- 获取轴的实际主单位。
- 获取轴的实际次单位。
- 获取轴的实际主单位比例。
- 获取轴的实际次单位比例。
此示例代码——上述步骤的实现——展示了如何在 JavaScript 中获取所需的值:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Area, 100, 100, 500, 350);
chart.validateChartLayout();
var maxValue = chart.getAxes().getVerticalAxis().getActualMaxValue();
var minValue = chart.getAxes().getVerticalAxis().getActualMinValue();
var majorUnit = chart.getAxes().getHorizontalAxis().getActualMajorUnit();
var minorUnit = chart.getAxes().getHorizontalAxis().getActualMinorUnit();
// 保存演示文稿
pres.save("MaxValuesVerticalAxis_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
在轴之间交换数据
Aspose.Slides 允许您快速在轴之间交换数据——垂直轴(y 轴)上的数据会移动到水平轴(x 轴),反之亦然。
此 JavaScript 代码展示了如何在图表上执行轴间数据交换任务:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 100, 100, 400, 300);
// 切换行和列
chart.getChartData().switchRowColumn();
// 保存演示文稿
pres.save("SwitchChartRowColumns_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
禁用折线图的垂直轴
此 JavaScript 代码展示了如何隐藏折线图的垂直轴:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Line, 100, 100, 400, 300);
chart.getAxes().getVerticalAxis().setVisible(false);
pres.save("chart.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
禁用折线图的水平轴
此代码展示了如何隐藏折线图的水平轴:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Line, 100, 100, 400, 300);
chart.getAxes().getHorizontalAxis().setVisible(false);
pres.save("chart.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
更改分类轴
使用 CategoryAxisType 属性,您可以指定首选的分类轴类型(date 或 text)。下面的 JavaScript 代码演示了此操作:
var presentation = new aspose.slides.Presentation("ExistingChart.pptx");
try {
var chart = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
chart.getAxes().getHorizontalAxis().setCategoryAxisType(aspose.slides.CategoryAxisType.Date);
chart.getAxes().getHorizontalAxis().setAutomaticMajorUnit(false);
chart.getAxes().getHorizontalAxis().setMajorUnit(1);
chart.getAxes().getHorizontalAxis().setMajorUnitScale(aspose.slides.TimeUnitType.Months);
presentation.save("ChangeChartCategoryAxis_out.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (presentation != null) {
presentation.dispose();
}
}
设置分类轴值的日期格式
Aspose.Slides for Node.js via Java 允许您为分类轴值设置日期格式。此操作在以下 JavaScript 代码中演示:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.Area, 50, 50, 450, 300);
var wb = chart.getChartData().getChartDataWorkbook();
wb.clear(0);
chart.getChartData().getCategories().clear();
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().add(wb.getCell(0, "A2", convertToOADate(java.newInstanceSync("GregorianCalendar", 2015, 1, 1))));
chart.getChartData().getCategories().add(wb.getCell(0, "A3", convertToOADate(java.newInstanceSync("GregorianCalendar", 2016, 1, 1))));
chart.getChartData().getCategories().add(wb.getCell(0, "A4", convertToOADate(java.newInstanceSync("GregorianCalendar", 2017, 1, 1))));
chart.getChartData().getCategories().add(wb.getCell(0, "A5", convertToOADate(java.newInstanceSync("GregorianCalendar", 2018, 1, 1))));
var series = chart.getChartData().getSeries().add(aspose.slides.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(aspose.slides.CategoryAxisType.Date);
chart.getAxes().getHorizontalAxis().setNumberFormatLinkedToSource(false);
chart.getAxes().getHorizontalAxis().setNumberFormat("yyyy");
pres.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
const dayjs = require('dayjs');
function convertToOADate(date) {
const baseDate = dayjs('1899-12-30');
const days = date.diff(baseDate, 'day');
const fractionalDay = (date.hour() / 24) +
(date.minute() / (60 * 24)) +
(date.second() / (60 * 24 * 60));
const oaDate = days + fractionalDay;
return String(oaDate);
}
设置图表轴标题的旋转角度
Aspose.Slides for Node.js via Java 允许您设置图表轴标题的旋转角度。此 JavaScript 代码演示了该操作:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 50, 50, 450, 300);
chart.getAxes().getVerticalAxis().setTitle(true);
chart.getAxes().getVerticalAxis().getTitle().getTextFormat().getTextBlockFormat().setRotationAngle(90);
pres.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
在分类轴或数值轴中设置位置轴
Aspose.Slides for Node.js via Java 允许您在分类轴或数值轴中设置位置轴。此 JavaScript 代码展示了如何完成此任务:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 50, 50, 450, 300);
chart.getAxes().getHorizontalAxis().setAxisBetweenCategories(true);
pres.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
在图表数值轴上启用显示单位标签
Aspose.Slides for Node.js via Java 允许您配置图表,以在其数值轴上显示单位标签。此 JavaScript 代码演示了该操作:
var pres = new aspose.slides.Presentation();
try {
var chart = pres.getSlides().get_Item(0).getShapes().addChart(aspose.slides.ChartType.ClusteredColumn, 50, 50, 450, 300);
chart.getAxes().getVerticalAxis().setDisplayUnit(aspose.slides.DisplayUnitType.Millions);
pres.save("output.pptx", aspose.slides.SaveFormat.Pptx);
} finally {
if (pres != null) {
pres.dispose();
}
}
常见问题
如何设置一个轴交叉另一个轴的值(轴交叉)?
轴提供了交叉设置:您可以选择在零点、最大分类/数值处或特定数值处交叉。这对于上下移动 X 轴或强调基线非常有用。
如何相对于轴定位刻度标签(旁边、外部、内部)?
将label位置设置为 “cross”、“outside” 或 “inside”。这会影响可读性,并有助于节省空间,尤其是在小型图表上。