趋势线
Contents
[
Hide
]
添加趋势线
Aspose.Slides for C++ 提供了一个简单的 API 用于管理不同的图表趋势线:
- 创建一个 Presentation 类的实例。
- 通过索引获取幻灯片的引用。
- 添加一个带有默认数据的图表以及任何所需类型(本示例使用 ChartType.ClusteredColumn)。
- 为图表系列 1 添加指数趋势线。
- 为图表系列 1 添加线性趋势线。
- 为图表系列 2 添加对数趋势线。
- 为图表系列 2 添加移动平均趋势线。
- 为图表系列 3 添加多项式趋势线。
- 为图表系列 3 添加幂趋势线。
- 将修改后的演示文稿写入 PPTX 文件。
以下代码用于创建带有趋势线的图表。
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-slides/Aspose.Slides-for-C | |
// The path to the documents directory. | |
const String outPath = u"../out/ChartTrendLines_out.pptx"; | |
//Instantiate Presentation class that represents PPTX file | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(); | |
//Access first slide | |
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0); | |
// Add chart with default data | |
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500); | |
// Adding ponential trend line for chart series 1 | |
SharedPtr<ITrendline> tredLinep = chart->get_ChartData()->get_Series()->idx_get(0)->get_TrendLines()->Add(Aspose::Slides::Charts::TrendlineType::Exponential); | |
tredLinep->set_DisplayEquation (false); | |
tredLinep->set_DisplayRSquaredValue( false); | |
// Adding Linear trend line for chart series 1 | |
SharedPtr<ITrendline> tredLineLin = chart->get_ChartData()->get_Series()->idx_get(0)->get_TrendLines()->Add(Aspose::Slides::Charts::TrendlineType::Linear); | |
tredLineLin->set_TrendlineType(TrendlineType::Linear); | |
tredLineLin->get_Format()->get_Line()->get_FillFormat()->set_FillType(FillType::Solid); | |
tredLineLin->get_Format()->get_Line()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Red()); | |
// Adding Logarithmic trend line for chart series 2 | |
SharedPtr<ITrendline> tredLineLog = chart->get_ChartData()->get_Series()->idx_get(1)->get_TrendLines()->Add(Aspose::Slides::Charts::TrendlineType::Logarithmic); | |
tredLineLog->set_TrendlineType(TrendlineType::Logarithmic); | |
tredLineLog->AddTextFrameForOverriding(u"New log trend line"); | |
// Adding MovingAverage trend line for chart series 2 | |
SharedPtr<ITrendline> tredLineMovAvg = chart->get_ChartData()->get_Series()->idx_get(1)->get_TrendLines()->Add(Aspose::Slides::Charts::TrendlineType::MovingAverage); | |
tredLineMovAvg->set_TrendlineType(TrendlineType::MovingAverage); | |
tredLineMovAvg->set_Period(3); | |
tredLineMovAvg->set_TrendlineName(u"New TrendLine Name"); | |
// Adding Polynomial trend line for chart series 3 | |
SharedPtr<ITrendline> tredLinePol = chart->get_ChartData()->get_Series()->idx_get(2)->get_TrendLines()->Add(Aspose::Slides::Charts::TrendlineType::Polynomial); | |
tredLinePol->set_TrendlineType(TrendlineType::Polynomial); | |
tredLinePol->set_Forward (1); | |
tredLinePol->set_Order (3); | |
// Adding Power trend line for chart series 3 | |
SharedPtr<ITrendline> tredLinePower = chart->get_ChartData()->get_Series()->idx_get(1)->get_TrendLines()->Add(Aspose::Slides::Charts::TrendlineType::Power); | |
tredLinePower->set_TrendlineType( TrendlineType::Power); | |
tredLinePower->set_Backward( 1); | |
// Write the presentation file to disk | |
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |
添加自定义线
Aspose.Slides for C++ 提供了一个简单的 API 来在图表中添加自定义线。要向演示文稿的选定幻灯片添加一条简单的直线,请按照以下步骤操作:
- 创建一个 Presentation 类的实例
- 通过使用其索引获得幻灯片的引用
- 使用 Shapes 对象暴露的 AddChart 方法创建一个新图表
- 使用 Shapes 对象暴露的 AddAutoShape 方法添加一个类型为线的 AutoShape
- 设置形状线条的颜色。
- 将修改后的演示文稿写入 PPTX 文件
以下代码用于创建带有自定义线的图表。
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
// The path to the documents directory. | |
const String outPath = u"../out/AddCustomLines.pptx"; | |
// Load the desired the presentation | |
SharedPtr<Presentation> pres = MakeObject<Presentation>(); | |
SharedPtr<IChart> chart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddChart(ChartType::ClusteredColumn, 100, 100, 500, 400); | |
SharedPtr<IAutoShape> shape = chart->get_UserShapes()->get_Shapes()->AddAutoShape(ShapeType::Line, 0, chart->get_Height() / 2, chart->get_Width(), 0); | |
shape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::Solid); | |
shape->get_LineFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(Color::get_Red()); | |
//Write the PPTX to Disk | |
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx); | |