从头开始创建OOXML图表
Contents
[
Hide
]
使用Aspose.Words从头开始创建OOXML图表
Aspose.Words提供添加到DocumentBuilder
类中的InsertChart
方法。 那么,让我们看看如何使用DocumentBuilder->InsertChart
方法将简单的柱形图插入到文档中:
如何插入柱形图
下面给出的代码示例演示了如何插入柱形图。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
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 = outputDataDir + u"CreateColumnChart.InsertSimpleColumnChart.doc"; | |
doc->Save(outputPath); |
代码产生以下结果:
Series Add方法有四种不同的重载,它公开以涵盖所有图表类型的数据源的所有可能变体:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
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 = outputDataDir + u"CreateColumnChart.InsertColumnChart.doc"; | |
doc->Save(outputPath); |
代码产生以下结果:
如何InsertScatterChart
下面给出的代码示例显示了如何插入散点图。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// The path to the documents directory. | |
System::String outputDataDir = GetOutputDataDir_WorkingWithCharts(); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Insert Scatter chart. | |
System::SharedPtr<Shape> shape = builder->InsertChart(ChartType::Scatter, 432, 252); | |
System::SharedPtr<Chart> chart = shape->get_Chart(); | |
// Use this overload to add series to any type of Scatter charts. | |
chart->get_Series()->Add(u"AW Series 1", System::MakeArray<double>({0.7, 1.8, 2.6}), System::MakeArray<double>({2.7, 3.2, 0.8})); | |
System::String outputPath = outputDataDir + u"InsertScatterChart.docx"; | |
doc->Save(outputPath); |
代码产生以下结果:
如何插入面积图
下面给出的代码示例显示了如何插入面积图。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// The path to the documents directory. | |
System::String outputDataDir = GetOutputDataDir_WorkingWithCharts(); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Insert Area chart. | |
System::SharedPtr<Shape> shape = builder->InsertChart(ChartType::Area, 432, 252); | |
System::SharedPtr<Chart> chart = shape->get_Chart(); | |
// Use this overload to add series to any type of Area, Radar and Stock charts. | |
chart->get_Series()->Add(u"AW Series 1", | |
System::MakeArray<System::DateTime>({System::DateTime(2002, 5, 1), System::DateTime(2002, 6, 1), System::DateTime(2002, 7, 1), System::DateTime(2002, 8, 1), System::DateTime(2002, 9, 1)}), | |
System::MakeArray<double>({32, 32, 28, 12, 15})); | |
System::String outputPath = outputDataDir + u"TestInsertAreaChart.docx"; | |
doc->Save(outputPath); |
代码产生以下结果:
如何插入气泡图
下面给出的代码示例演示了如何插入气泡图。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// The path to the documents directory. | |
System::String outputDataDir = GetOutputDataDir_WorkingWithCharts(); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(); | |
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc); | |
// Insert Bubble chart. | |
System::SharedPtr<Shape> shape = builder->InsertChart(ChartType::Bubble, 432, 252); | |
System::SharedPtr<Chart> chart = shape->get_Chart(); | |
// Use this overload to add series to any type of Bubble charts. | |
chart->get_Series()->Add(u"AW Series 1", | |
System::MakeArray<double>({0.7, 1.8, 2.6}), | |
System::MakeArray<double>({2.7, 3.2, 0.8}), | |
System::MakeArray<double>({10, 4, 8})); | |
System::String outputPath = outputDataDir + u"InsertBubbleChart.docx"; | |
doc->Save(outputPath); |
代码产生以下结果: