使用ChartGlobalizationSettings类设置图表组件的不同语言
Contents
[
Hide
]
可能的使用场景
Aspose.Cells API 已经暴露了 ChartGlobalizationSettings 类,以处理用户希望将图表组件设置为不同语言的场景。电子表格中自定义小计的标签。
ChartGlobalizationSettings类介绍
ChartGlobalizationSettings 类目前提供以下8个方法,可以在自定义类中进行重写,以将轴标题名称、轴单位名称、图表标题名称等翻译成不同的语言。
- GetAxisTitleName:获取轴的标题名称。
- GetAxisUnitName:获取轴单位的名称。
- GetChartTitleName:获取图表标题的名称。
- GetLegendDecreaseName:获取图例减少的名称。
- GetLegendIncreaseName:获取图例增加的名称。
- GetLegendTotalName:获取图例的总名称。
- GetOtherName:获取图表中“其他”标签的名称。
- GetSeriesName:获取图表中系列的名称。
自定义语言翻译
在这里,我们将根据以下数据创建瀑布图。图表中将以英语显示图表组件的名称。我们将使用土耳其语示例来展示如何在图表中显示图表标题、图例增加/减少名称、总计名称和轴标题。
示例代码
以下示例代码加载了示例Excel文件。
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
//Create a custom language class for chart component,here take Turkey for example, | |
//which will translate the chart element to specific language | |
public class TurkeyChartGlobalizationSettings : ChartGlobalizationSettings | |
{ | |
public override string GetChartTitleName() | |
{ | |
return "Grafik Başlığı";//Chart Title | |
} | |
public override string GetLegendIncreaseName() | |
{ | |
return "Artış";//Increase | |
} | |
public override string GetLegendDecreaseName() | |
{ | |
return "Düşüş";//Decrease; | |
} | |
public override string GetLegendTotalName() | |
{ | |
return "Toplam";//Total | |
} | |
public override string GetAxisTitleName() | |
{ | |
return "Eksen Başlığı";//Axis Title | |
} | |
} | |
public static void ChartGlobalizationSettingsTest() | |
{ | |
//Create an instance of existing Workbook | |
string pathName = "input.xlsx"; | |
Workbook workbook = new Workbook(pathName); | |
//Set custom chartGlobalizationSettings, here is TurkeyChartGlobalizationSettings | |
workbook.Settings.GlobalizationSettings.ChartSettings = new TurkeyChartGlobalizationSettings(); | |
//Get the worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
ChartCollection chartCollection = worksheet.Charts; | |
//Load the chart from source worksheet | |
Chart chart = chartCollection[0]; | |
//Chart Calculate | |
chart.Calculate(); | |
//Get the chart title | |
Title title = chart.Title; | |
//Output the name of the Chart title | |
Console.WriteLine("\nWorkbook chart title: " + title.Text); | |
string[] legendEntriesLabels = chart.Legend.GetLegendLabels(); | |
//Output the name of the Legend | |
for (int i = 0; i < legendEntriesLabels.Length; i++) | |
{ | |
Console.WriteLine("\nWorkbook chart legend: " + legendEntriesLabels[i]); | |
} | |
//Output the name of the Axis title | |
Title categoryAxisTitle = chart.CategoryAxis.Title; | |
Console.WriteLine("\nWorkbook category axis tile: " + categoryAxisTitle.Text); | |
} |
示例代码生成的输出
这是上述示例代码的控制台输出。
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ığı