使用 ChartGlobalizationSettings 类在 C++ 中设置图表组件的不同语言
Contents
[
Hide
]
可能的使用场景
Aspose.Cells API 已经暴露了 ChartGlobalizationSettings 类,以处理用户希望将图表组件设置为不同语言的场景。电子表格中自定义小计的标签。
ChartGlobalizationSettings类介绍
ChartGlobalizationSettings 类目前提供以下8个可被重写的方法,用于自定义类中实现如轴标题名、轴单位名、图表标题名等的不同语言翻译。
- GetAxisTitleName:获取轴的标题名称。
- GetAxisUnitName:获取轴单位的名称。
- GetChartTitleName:获取图表标题的名称。
- GetLegendDecreaseName:获取图例减少的名称。
- GetLegendIncreaseName:获取图例增加的名称。
- GetLegendTotalName:获取图例的总名称。
- GetOtherName:获取图表中“其他”标签的名称。
- GetSeriesName:获取图表中系列的名称。
自定义语言翻译
在这里,我们将根据以下数据创建瀑布图。图表中将以英语显示图表组件的名称。我们将使用土耳其语示例来展示如何在图表中显示图表标题、图例增加/减少名称、总计名称和轴标题。
示例代码
以下示例代码加载了示例Excel文件。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Charts;
class TurkeyChartGlobalizationSettings : public ChartGlobalizationSettings
{
public:
TurkeyChartGlobalizationSettings() : ChartGlobalizationSettings() {}
U16String GetChartTitleName() override
{
return u"Grafik Başlığı"; // Chart Title
}
U16String GetLegendIncreaseName() override
{
return u"Artış"; // Increase
}
U16String GetLegendDecreaseName() override
{
return u"Düşüş"; // Decrease
}
U16String GetLegendTotalName() override
{
return u"Toplam"; // Total
}
U16String GetAxisTitleName() override
{
return u"Eksen Başlığı"; // Axis Title
}
};
void ChartGlobalizationSettingsTest()
{
// Create an instance of existing Workbook
U16String pathName = u"input.xlsx";
Workbook workbook(pathName);
// Set custom chartGlobalizationSettings, here is TurkeyChartGlobalizationSettings
TurkeyChartGlobalizationSettings* globalizationSettings = new TurkeyChartGlobalizationSettings();
workbook.GetSettings().GetGlobalizationSettings()->SetChartSettings(globalizationSettings);
// Get the worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Load the chart from source worksheet
ChartCollection chartCollection = worksheet.GetCharts();
Chart chart = chartCollection.Get(0);
// Chart Calculate
chart.Calculate();
// Get the chart title
Title title = chart.GetTitle();
// Output the name of the Chart title
std::cout << "\nWorkbook chart title: " << title.GetText().ToUtf8() << std::endl;
// Get the legend labels
Vector<U16String> legendEntriesLabels = chart.GetLegend().GetLegendLabels();
// Output the name of the Legend
for (int i = 0; i < legendEntriesLabels.GetLength(); i++)
{
std::cout << "\nWorkbook chart legend: " << legendEntriesLabels[i].ToUtf8() << std::endl;
}
// Output the name of the Axis title
Title categoryAxisTitle = chart.GetCategoryAxis().GetTitle();
std::cout << "\nWorkbook category axis title: " << categoryAxisTitle.GetText().ToUtf8() << std::endl;
delete globalizationSettings;
}
int main()
{
Aspose::Cells::Startup();
ChartGlobalizationSettingsTest();
Aspose::Cells::Cleanup();
return 0;
}
示例代码生成的输出
这是上述示例代码的控制台输出。
Workbook chart title: Grafik Başlığı
Workbook chart legend: Artış
Workbook chart legend: Düşüş
Workbook chart legend: Toplam
Workbook category axis tile: Eksen Başlığı