通过 C++ 使用 Node.js 操作图表的位置、大小和设计图
图表位置和大小
有时你需要改变工作表中新建或现有图表的位置或大小。Aspose.Cells 提供 Chart.getChartObject() 属性实现此功能。你可以使用其子属性重新调整图表的高度和宽度,或以新的 X 和 Y 坐标重新定位。
控制图表的位置和大小
要更改图表的位置(X、Y坐标)或大小(高度,宽度),请使用以下属性:
以下示例说明了上述 API 的用法;它加载包含图表的现有工作簿,然后使用 Aspose.Cells 重新调整图表的大小和位置。
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, "chart.xls");
// Loads the workbook which contains the chart
const workbook = new AsposeCells.Workbook(filePath);
const worksheet = workbook.getWorksheets().get(1);
// Load the chart from source worksheet
const chart = worksheet.getCharts().get(0);
// Resize the chart
chart.getChartObject().setWidth(400);
chart.getChartObject().setHeight(300);
// Reposition the chart
chart.getChartObject().setX(250);
chart.getChartObject().setY(150);
// Output the file
workbook.save(path.join(dataDir, "chart.out.xls"));
操作设计图表
有时你需要操作或修改设计模板文件中的图表。Aspose.Cells 完全支持操控设计图表内容和元素。数据、图表内容、背景图片和格式都能精准保存。
在模板文件中操纵设计图表
要操作模板文件中的设计图表,使用图表相关的 API。例如,可以使用 Worksheet.charts 属性获取模板文件中的现有图表集合。
创建图表
以下示例显示了如何创建金字塔图表。稍后我们将操纵此图表。
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Adding a new worksheet to the Excel object
const sheetIndex = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding sample values to cells
worksheet.getCells().get("A1").putValue(50);
worksheet.getCells().get("A2").putValue(100);
worksheet.getCells().get("A3").putValue(150);
worksheet.getCells().get("B1").putValue(4);
worksheet.getCells().get("B2").putValue(20);
worksheet.getCells().get("B3").putValue(50);
// Adding a chart to the worksheet
const chartIndex = worksheet.getCharts().add(AsposeCells.ChartType.Pyramid, 5, 0, 15, 5);
// Accessing the instance of the newly added chart
const chart = worksheet.getCharts().get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.getNSeries().add("A1:B3", true);
// Saving the Excel file
workbook.save(path.join(dataDir, "book1.out.xls"));
操作图表
以下示例显示了如何操纵现有的图表。在此示例中,我们修改了上面创建的图表。请注意,在生成的输出中,一个数据点的日期标签已设置为“United Kingdom, 30K”。
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, "piechart.xls");
// Loads the existing file.
const workbook = new AsposeCells.Workbook(filePath);
// Get the designer chart in the second sheet.
const sheet = workbook.getWorksheets().get(1);
const chart = sheet.getCharts().get(0);
// Get the data labels in the data series of the third data point.
const dataLabels = chart.getNSeries().get(0).getPoints().get(2).getDataLabels();
// Change the text of the label.
dataLabels.setText("Unided Kingdom, 400K ");
// Save the excel file.
workbook.save(path.join(dataDir, "output.xls"));
在设计师模板中操作折线图
在这个例子中,我们将操作一个折线图。我们将向现有图表添加一些数据系列,并改变它们的线条颜色。
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Open the existing file.
const workbook = new AsposeCells.Workbook(path.join(dataDir, "sample.xlsx"));
// Get the designer chart in the first worksheet.
const chart = workbook.getWorksheets().get(0).getCharts().get(0);
// Add the third data series to it.
chart.getNSeries().add("{60, 80, 10}", true);
// Add another data series (fourth) to it.
chart.getNSeries().add("{0.3, 0.7, 1.2}", true);
// Plot the fourth data series on the second axis.
chart.getNSeries().get(3).setPlotOnSecondAxis(true);
// Change the Border color of the second data series.
chart.getNSeries().get(1).getBorder().setColor(AsposeCells.Color.Green);
// Change the Border color of the third data series.
chart.getNSeries().get(2).getBorder().setColor(AsposeCells.Color.Red);
// Make the second value axis visible.
chart.getSecondValueAxis().setIsVisible(true);
// Save the excel file.
workbook.save(path.join(dataDir, "output.xls"));