确定图表中存在的轴类型,使用Node.js通过C++
Contents
[
Hide
]
有时,用户需要知道某个特定轴是否存在于图表中。例如,他们想知道图表中是否存在次数值轴。有些图表,如饼图、爆炸饼图、饼果图、饼条图、3D饼图、3D爆炸饼图、圆环图、爆炸圆环等,没有轴。
Aspose.Cells提供了Chart.hasAxis(axisType, isPrimary)方法来判断图表是否具有特定的轴。
以下示例代码演示了如何使用Chart.hasAxis(axisType, isPrimary)判断样本图表是否具有主类别和数值轴以及次类别和数值轴。
Node.js代码判断图表中是否存在轴
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "source.xlsx");
// Create workbook object
const workbook = new AsposeCells.Workbook(filePath);
// Access the first worksheet
const worksheet = workbook.getWorksheets().get(0);
// Check if there are any charts before accessing
const charts = worksheet.getCharts();
if (charts.getCount() === 0) {
console.log("No charts found in the worksheet.");
return;
}
// Access the chart
const chart = charts.get(0);
// Determine which axis exists in chart
let ret = chart.hasAxis(AsposeCells.AxisType.Category, true);
console.log("Has Primary Category Axis: " + ret);
ret = chart.hasAxis(AsposeCells.AxisType.Category, false);
console.log("Has Secondary Category Axis: " + ret);
ret = chart.hasAxis(AsposeCells.AxisType.Value, true);
console.log("Has Primary Value Axis: " + ret);
ret = chart.hasAxis(AsposeCells.AxisType.Value, false);
console.log("Has Secondary Value Axis: " + ret);
示例代码生成的控制台输出
代码的控制台输出如下,显示主类别轴和数值轴为true,次类别轴和数值轴为false。
Has Primary Category Axis: True
Has Secondary Category Axis: False
Has Primary Value Axis: True
Has Secondary Value Axis: False