图表坐标轴
Contents
[
Hide
]
获取图表垂直坐标轴的最大值
Aspose.Slides for C++ 允许您获取垂直坐标轴上的最小值和最大值。请按照以下步骤操作:
- 创建一个 Presentation 类的实例。
- 访问第一张幻灯片。
- 添加一个带有默认数据的图表。
- 获取坐标轴上的实际最大值。
- 获取坐标轴上的实际最小值。
- 获取坐标轴上的实际主要单位。
- 获取坐标轴上的实际次要单位。
- 获取坐标轴上的实际主要单位刻度。
- 获取坐标轴上的实际次要单位刻度。
下面的示例代码展示了如何在 C++ 中获取所需的值:
auto pres = System::MakeObject<Presentation>();
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
auto chart = System::ExplicitCast<Chart>(shapes->AddChart(ChartType::Area, 100.0f, 100.0f, 500.0f, 350.0f));
chart->ValidateChartLayout();
auto axes = chart->get_Axes();
double maxValue = axes->get_VerticalAxis()->get_ActualMaxValue();
double minValue = axes->get_VerticalAxis()->get_ActualMinValue();
double majorUnit = axes->get_HorizontalAxis()->get_ActualMajorUnit();
double minorUnit = axes->get_HorizontalAxis()->get_ActualMinorUnit();
// 保存演示文稿
pres->Save(u"ErrorBars_out.pptx", SaveFormat::Pptx);
交换坐标轴之间的数据
Aspose.Slides 允许您快速交换坐标轴之间的数据——在垂直坐标轴 (y 轴) 上表示的数据移动到水平坐标轴 (x 轴),反之亦然。
这段 C++ 代码展示了如何在图表上执行坐标轴间的数据交换任务:
// 创建空演示文稿
auto pres = System::MakeObject<Presentation>();
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
auto chart = shapes->AddChart(ChartType::ClusteredColumn, 100.0f, 100.0f, 400.0f, 300.0f);
// 切换行和列
chart->get_ChartData()->SwitchRowColumn();
// 保存演示文稿
pres->Save(u"SwitchChartRowColumns_out.pptx", SaveFormat::Pptx);
禁用折线图的垂直坐标轴
这段 C++ 代码展示了如何隐藏折线图的垂直坐标轴:
auto pres = System::MakeObject<Presentation>();
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
auto chart = shapes->AddChart(ChartType::Line, 100.0f, 100.0f, 400.0f, 300.0f);
chart->get_Axes()->get_VerticalAxis()->set_IsVisible(false);
pres->Save(u"chart.pptx", SaveFormat::Pptx);
禁用折线图的水平坐标轴
这段代码展示了如何隐藏折线图的水平坐标轴:
auto pres = System::MakeObject<Presentation>();
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
auto chart = shapes->AddChart(ChartType::Line, 100.0f, 100.0f, 400.0f, 300.0f);
chart->get_Axes()->get_HorizontalAxis()->set_IsVisible(false);
pres->Save(u"chart.pptx", SaveFormat::Pptx);
更改类别轴
使用 set_CategoryAxisType() 方法,您可以指定首选的类别轴类型 (日期 或 文本)。以下 C++ 代码演示了此操作:
auto presentation = System::MakeObject<Presentation>(u"ExistingChart.pptx");
auto chart = System::AsCast<IChart>(presentation->get_Slides()->idx_get(0)->get_Shapes()->idx_get(0));
auto horizontalAxis = chart->get_Axes()->get_HorizontalAxis();
horizontalAxis->set_CategoryAxisType(CategoryAxisType::Date);
horizontalAxis->set_IsAutomaticMajorUnit(false);
horizontalAxis->set_MajorUnit(1);
horizontalAxis->set_MajorUnitScale(TimeUnitType::Months);
presentation->Save(u"ChangeChartCategoryAxis_out.pptx", SaveFormat::Pptx);
设置类别轴值的日期格式
Aspose.Slides for C++ 允许您设置类别轴值的日期格式。此操作在以下 C++ 代码中演示:
auto pres = System::MakeObject<Presentation>();
auto chart = pres->get_Slides()->idx_get(0)->get_Shapes()->AddChart(ChartType::Area, 50.0f, 50.0f, 450.0f, 300.0f);
auto wb = chart->get_ChartData()->get_ChartDataWorkbook();
wb->Clear(0);
chart->get_ChartData()->get_Series()->Clear();
auto areaCategories = chart->get_ChartData()->get_Categories();
areaCategories->Clear();
areaCategories->Add(wb->GetCell(0, u"A2", ObjectExt::Box<double>(DateTime(2015, 1, 1).ToOADate())));
areaCategories->Add(wb->GetCell(0, u"A3", ObjectExt::Box<double>(DateTime(2016, 1, 1).ToOADate())));
areaCategories->Add(wb->GetCell(0, u"A4", ObjectExt::Box<double>(DateTime(2017, 1, 1).ToOADate())));
areaCategories->Add(wb->GetCell(0, u"A5", ObjectExt::Box<double>(DateTime(2018, 1, 1).ToOADate())));
auto series = chart->get_ChartData()->get_Series()->Add(ChartType::Line);
auto dataPoints = series->get_DataPoints();
dataPoints->AddDataPointForLineSeries(wb->GetCell(0, u"B2", ObjectExt::Box<int32_t>(1)));
dataPoints->AddDataPointForLineSeries(wb->GetCell(0, u"B3", ObjectExt::Box<int32_t>(2)));
dataPoints->AddDataPointForLineSeries(wb->GetCell(0, u"B4", ObjectExt::Box<int32_t>(3)));
dataPoints->AddDataPointForLineSeries(wb->GetCell(0, u"B5", ObjectExt::Box<int32_t>(4)));
auto horizontalAxis = chart->get_Axes()->get_HorizontalAxis();
horizontalAxis->set_CategoryAxisType(CategoryAxisType::Date);
horizontalAxis->set_IsNumberFormatLinkedToSource(false);
horizontalAxis->set_NumberFormat(u"yyyy");
pres->Save(u"test.pptx", SaveFormat::Pptx);
设置图表坐标轴标题的旋转角度
Aspose.Slides for C++ 允许您设置图表坐标轴标题的旋转角度。以下 C++ 代码演示了此操作:
auto pres = System::MakeObject<Presentation>();
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
auto chart = shapes->AddChart(ChartType::ClusteredColumn, 50.0f, 50.0f, 450.0f, 300.0f);
auto verticalAxis = chart->get_Axes()->get_VerticalAxis();
verticalAxis->set_HasTitle(true);
verticalAxis->get_Title()->get_TextFormat()->get_TextBlockFormat()->set_RotationAngle(90.0f);
pres->Save(u"test.pptx", SaveFormat::Pptx);
在类别轴或值轴中设置位置轴
Aspose.Slides for C++ 允许您在类别轴或值轴中设置位置轴。以下 C++ 代码展示了如何执行此任务:
auto pres = System::MakeObject<Presentation>();
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
auto chart = shapes->AddChart(ChartType::ClusteredColumn, 50.0f, 50.0f, 450.0f, 300.0f);
chart->get_Axes()->get_HorizontalAxis()->set_AxisBetweenCategories(true);
pres->Save(u"AsposeScatterChart.pptx", SaveFormat::Pptx);
在图表值轴上启用显示单位标签
Aspose.Slides for C++ 允许您配置图表以在其值轴上显示单位标签。以下 C++ 代码演示了此操作:
auto pres = System::MakeObject<Presentation>(u"Test.pptx");
auto shapes = pres->get_Slides()->idx_get(0)->get_Shapes();
auto chart = shapes->AddChart(ChartType::ClusteredColumn, 50.0f, 50.0f, 450.0f, 300.0f);
chart->get_Axes()->get_VerticalAxis()->set_DisplayUnit(DisplayUnitType::Millions);
pres->Save(u"Result.pptx", SaveFormat::Pptx);