Aspose.Words for C++使用教程:从Scratch创建OOXML图表(上)

Aspose.Words for C++不依赖Microsoft Word,可在任何C++应用程序中生成和操作Word格式文档。本文将与大家分享如何插入柱形图到文档中。

Aspose.Words 下载https://www.evget.com/product/4114提供了 InsertChart 方法,该方法已添加到 DocumentBuilder 类中。那么,让我们看看如何使用 DocumentBuilder-> InsertChart 方法将简单的柱形图插入到文档中:

如何插入柱形图。

下面的示例代码显示了如何插入柱形图。

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);

// Add chart with default data. You can specify different chart types and sizes.
System::SharedPtr<Shape> shape = builder->InsertChart(ChartType::Column, 432, 252);

// Chart property of Shape contains all chart related options.
System::SharedPtr<Chart> chart = shape->get_Chart();

// Get chart series collection.
System::SharedPtr<ChartSeriesCollection> seriesColl = chart->get_Series();
// Check series count.
std::cout << seriesColl->get_Count() << std::endl;

// Delete default generated series.
seriesColl->Clear();

// Create category names array, in this example we have two categories.
System::ArrayPtr<System::String> categories = System::MakeArray<System::String>({u"AW Category 1", u"AW Category 2"});

// Adding new series. Please note, data arrays must not be empty and arrays must be the same size.
seriesColl->Add(u"AW Series 1", categories, System::MakeArray<double>({1, 2}));
seriesColl->Add(u"AW Series 2", categories, System::MakeArray<double>({3, 4}));
seriesColl->Add(u"AW Series 3", categories, System::MakeArray<double>({5, 6}));
seriesColl->Add(u"AW Series 4", categories, System::MakeArray<double>({7, 8}));
seriesColl->Add(u"AW Series 5", categories, System::MakeArray<double>({9, 10}));

System::String outputPath = dataDir + GetOutputFilePath(u"CreateColumnChart.InsertSimpleColumnChart.doc");
doc->Save(outputPath);

该代码会产生以下结果:

Add系列方法有四种不同的重载,它们涵盖了所有图表类型的所有可能的数据源变体:

System::SharedPtr<Document> doc = System::MakeObject<Document>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);

// Insert Column chart.
System::SharedPtr<Shape> shape = builder->InsertChart(ChartType::Column, 432, 252);
System::SharedPtr<Chart> chart = shape->get_Chart();

// Use this overload to add series to any type of Bar, Column, Line and Surface charts.
chart->get_Series()->Add(u"AW Series 1", System::MakeArray<System::String>({u"AW Category 1", u"AW Category 2"}), System::MakeArray<double>({1, 2}));

System::String outputPath = dataDir + GetOutputFilePath(u"CreateColumnChart.InsertColumnChart.doc");
doc->Save(outputPath);

该代码会产生以下结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值