Usar la clase ChartGlobalizationSettings para establecer un idioma diferente para el componente del gráfico
Escenarios de uso posibles
Las API de Aspose.Cells han expuesto la clase ChartGlobalizationSettings para tratar los escenarios en los que el usuario desea establecer el componente del gráfico en un idioma diferente, como etiquetas personalizadas para subtotales en una hoja de cálculo.
Introducción a la clase ChartGlobalizationSettings
La clase ChartGlobalizationSettings ofrece actualmente los siguientes 8 métodos que se pueden sobrescribir en una clase personalizada para traducir, por ejemplo, el nombre de AxisTitle, AxisUnit y ChartTitle a un idioma diferente.
- GetAxisTitleName: Obtiene el nombre del Título para el Eje.
- GetAxisUnitName: Obtiene el nombre de la Unidad del Eje.
- GetChartTitleName: Obtiene el nombre del Título del Gráfico.
- GetLegendDecreaseName: Obtiene el nombre de Disminución para la Leyenda.
- GetLegendIncreaseName: Obtiene el nombre de Aumento para la Leyenda.
- GetLegendTotalName: Obtiene el nombre de Total para la Leyenda.
- GetOtherName: Obtiene el nombre de las etiquetas “Otro” para el Gráfico.
- GetSeriesName: Obtiene el nombre de la Serie en el Gráfico.
Traducción personalizada de idioma
Aquí, crearemos un gráfico de cascada basado en los siguientes datos. Los nombres de los componentes del gráfico se mostrarán en inglés en el gráfico. Utilizaremos un ejemplo en idioma turco para mostrar cómo mostrar el Título del Gráfico, los nombres de Aumento/Disminución de la Leyenda, el nombre de Total y el Título del Eje en turco.
Código de muestra
El siguiente código de ejemplo carga el archivo de Excel de muestra.
//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); | |
} |
Resultado generado por el código de ejemplo
Este es el resultado de consola del código de ejemplo anterior.
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ığı