使用 C++ 管理图表中的形状
向图表添加标签控件
标签提供了一种向用户提供有关电子表格内容信息的方式。 Aspose.Cells允许您甚至在图表中添加和操作标签。
Aspose::Cells::Drawing::ShapeCollection 类提供了一个名为 AddLabelInChart 的方法,用于向图表添加标签控件。以下是该方法所用参数的列表:
- top – 以图表区域的1/4000单位为垂直偏移的标签。
- left – 以图表区域的1/4000单位为水平偏移的标签。
- height – 标签的高度,以图表区域的1/4000单位为单位。
- width – 标签的宽度,单位为图表区域的1/4000。
该方法返回 Aspose::Cells::Drawing::Label 对象。Label 类代表图表中的标签。它具有一些重要成员:
以下示例演示如何向图表添加标签。示例使用了一个设计文件(exp_piechart.xls),其中包含一个图表。我们使用此文件向图表插入标签。以下是添加标签到图表的原始代码。执行该代码时生成以下输出。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Drawing;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"chart.xls";
// Path of output excel file
U16String outputFilePath = outDir + u"chart.out.xls";
// Create workbook
Workbook workbook(inputFilePath);
// Get the designer chart in the second sheet
Worksheet sheet = workbook.GetWorksheets().Get(1);
Aspose::Cells::Charts::Chart chart = sheet.GetCharts().Get(0);
// Add a new label to the chart
Label label = chart.GetShapes().AddLabelInChart(100, 100, 350, 900);
// Set the caption of the label
label.SetText(u"A Label In Chart");
// Set the Placement Type, the way the Label is attached to the cells
label.SetPlacement(PlacementType::FreeFloating);
// Save the excel file
workbook.Save(outputFilePath);
std::cout << "Label added to chart successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
将文本框控件添加到图表
突出报告中的重要信息的一种方法是使用文本框。例如,输入文本以突出显示公司名称或指示销售最高的地区。Aspose::Cells::Drawing::ShapeCollection 类提供了一个名为 AddTextBoxInChart 的方法,用于向图表添加文本框控件。以下是该方法所用参数的列表:
- top – 文本框与图表区域左上角的垂直偏移量,单位为图表区域的1/4000。
- left – 文本框距离图表区域左上角的垂直偏移量,单位为图表区域的1/4000。
- height – 文本框的高度,单位为图表区域的1/4000。
- width – 文本框的宽度,单位为图表区域的1/4000。
该方法返回 Aspose::Cells::Drawing::TextBox 对象。TextBox 类代表图表中的文本框。
以下示例显示了如何向图表添加文本框。该示例使用之前的设计文件(exp_piechart.xls),其中包含一个图表。我们使用这个文件向图表中插入文本框以显示图表标题。以下是添加文本框到图表的原始代码。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Drawing;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"chart.xls";
// Path of output excel file
U16String outputFilePath = outDir + u"chart.out.xls";
// Create workbook
Workbook workbook(inputFilePath);
// Get the designer chart in the second sheet
Worksheet sheet = workbook.GetWorksheets().Get(1);
Aspose::Cells::Charts::Chart chart = sheet.GetCharts().Get(0);
// Add a new textbox to the chart
TextBox textbox0 = chart.GetShapes().AddTextBoxInChart(100, 1100, 350, 2550);
// Fill the text
textbox0.SetText(u"Sales By Region");
// Set the font color
textbox0.GetFont().SetColor(Color::Maroon());
// Set the font to bold
textbox0.GetFont().SetIsBold(true);
// Set the font size
textbox0.GetFont().SetSize(14);
// Set font attribute to italic
textbox0.GetFont().SetIsItalic(true);
// Get the fill format of the textbox
FillFormat fillformat = textbox0.GetFill();
// Get the line format type of the textbox
LineFormat lineformat = textbox0.GetLine();
// Set the line weight
lineformat.SetWeight(2);
// Set the dash style to solid
lineformat.SetDashStyle(MsoLineDashStyle::Solid);
// Save the excel file
workbook.Save(outputFilePath);
std::cout << "Textbox added to chart successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
向图表添加图片
Aspose.Cells 允许您将图像插入到图表中。例如,添加一张图片以强调或赋予图表或其内容更多的含义,或者插入一个品牌图片文件。
Aspose::Cells::Drawing::ShapeCollection 类提供了一个名为 AddPictureInChart 的方法,用于向图表添加图片对象。以下是该方法所用参数的列表:
- top – 图片与图表区域左上角的垂直偏移量,单位为图表区域的1/4000。
- left – 图片与图表区域左上角的水平偏移量,单位为图表区域的1/4000。
- stream - 一个包含图像数据的流对象。
- widthScale - 图像宽度的比例值,以百分比表示。
- heightScale - 图像高度的比例值,以百分比表示。
该方法返回一个 Aspose::Cells::Drawing::Picture 对象。Picture 类代表图表中的图片对象。
以下示例显示了如何向图表添加图片。该示例利用之前的设计文件(exp_piechart.xls),其中包含一个图表。我们使用这个文件向图表中插入图片。以下是添加图片到图表的原始代码。
#include <iostream>
#include <fstream>
#include <vector>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Charts;
using namespace Aspose::Cells::Drawing;
std::vector<uint8_t> ReadFileData(const U16String& filePath) {
std::ifstream file(filePath.ToUtf8(), std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> buffer(size);
if (!file.read(reinterpret_cast<char*>(buffer.data()), size)) {
throw std::runtime_error("Error reading file");
}
return buffer;
}
int main() {
Aspose::Cells::Startup();
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
Workbook workbook(srcDir + u"chart.xls");
std::vector<uint8_t> imageData = ReadFileData(srcDir + u"logo.jpg");
Worksheet sheet = workbook.GetWorksheets().Get(0);
Chart chart = sheet.GetCharts().Get(0);
Vector<uint8_t> data(imageData.data(), static_cast<int32_t>(imageData.size()));
Picture pic0 = chart.GetShapes().AddPictureInChart(50, 50, data, 40, 40);
LineFormat lineFormat = pic0.GetLine();
lineFormat.SetDashStyle(MsoLineDashStyle::Solid);
lineFormat.SetWeight(4);
workbook.Save(outDir + u"chart.out.xls");
std::cout << "Chart modified successfully." << std::endl;
Aspose::Cells::Cleanup();
return 0;
}
在图表中添加复选框
Aspose.Cells 允许您通过使用 MsoDrawingType 枚举向图表工作表中插入复选框。以下示例演示了向图表工作表添加复选框。
以下图像显示了输出文件中带有复选框的图表工作表。
以下代码片段生成的输出文件已附上,供您参考。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Drawing;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create a new workbook
Workbook workbook;
// Add a chart sheet to the workbook
int32_t index = workbook.GetWorksheets().Add(SheetType::Chart);
// Get the newly added chart sheet
Worksheet sheet = workbook.GetWorksheets().Get(index);
// Add a floating chart to the sheet
sheet.GetCharts().AddFloatingChart(ChartType::Column, 0, 0, 1024, 960);
// Add data series to the chart
sheet.GetCharts().Get(0).GetNSeries().Add(U16String(u"{1,2,3}"), false);
// Add a checkbox to the chart
sheet.GetCharts().Get(0).GetShapes().AddShapeInChart(MsoDrawingType::CheckBox, PlacementType::Move, 400, 400, 1000, 600);
// Set text for the checkbox
sheet.GetCharts().Get(0).GetShapes().Get(0).SetText(U16String(u"CheckBox 1"));
// Save the workbook
workbook.Save(outDir + u"InsertCheckboxInChartSheet_out.xlsx");
std::cout << "Checkbox added to chart sheet successfully!" << std::endl;
Aspose::Cells::Cleanup();
}