Utilizzo della classe ChartGlobalizationSettings per impostare lingue diverse per i componenti del grafico con JavaScript via C++
Possibili Scenari di Utilizzo
Le API di Aspose.Cells hanno esposto la classe ChartGlobalizationSettings per gestire gli scenari in cui l’utente desidera impostare componenti del grafico in lingue diverse e etichette personalizzate per i subtotali in un foglio di calcolo.
Introduzione alla classe ChartGlobalizationSettings
La classe ChartGlobalizationSettings attualmente offre i seguenti 8 metodi che possono essere sovrascritti in una classe personalizzata per tradurre nomi come AxisTitle, AxisUnit, ChartTitle e altri in diverse lingue.
- ChartGlobalizationSettings.axisTitleName: Ottiene il nome del Titolo per l’Asse.
- ChartGlobalizationSettings.axisUnitName(DisplayUnitType): Ottiene il Nome dell’Unità di Asse.
- ChartGlobalizationSettings.chartTitleName: Ottiene il nome del Titolo del Grafico.
- ChartGlobalizationSettings.legendDecreaseName: Ottiene il nome di Diminuzione per la Leggenda.
- ChartGlobalizationSettings.legendIncreaseName: Ottiene il nome di Increase per la legenda.
- ChartGlobalizationSettings.legendTotalName: Ottiene il nome di Totale per la Leggenda.
- ChartGlobalizationSettings.otherName: Ottiene il nome delle etichette “Altro” per il Grafico.
- ChartGlobalizationSettings.seriesName: Ottiene il nome di Serie nel Grafico.
Traduzione personalizzata
Qui, creeremo un grafico a barre basato sui seguenti dati. I nomi dei componenti del grafico verranno visualizzati in inglese nel grafico. Useremo un esempio in lingua turca per mostrare come visualizzare il Titolo del Grafico, i nomi di Aumento/Diminuzione della Leggenda, il nome Totale e il Titolo dell’Asse in turco.

Codice di Esempio
Il seguente codice di esempio carica il file Excel di esempio.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Chart Globalization Settings Example</title>
</head>
<body>
<h1>Chart Globalization Settings Example (Turkey)</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
// Define TurkeyChartGlobalizationSettings by converting getXxx methods to properties
class TurkeyChartGlobalizationSettings extends AsposeCells.ChartGlobalizationSettings {
constructor() {
super();
this.chartTitleName = "Grafik Başlığı"; // Chart Title
this.legendIncreaseName = "Artış"; // Increase
this.legendDecreaseName = "Düşüş"; // Decrease
this.legendTotalName = "Toplam"; // Total
this.axisTitleName = "Eksen Başlığı"; // Axis Title
}
}
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = '';
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
// No try-catch: let errors propagate
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate Workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Set custom chartGlobalizationSettings (Turkey)
workbook.settings.globalizationSettings.chartSettings = new TurkeyChartGlobalizationSettings();
// Access the first worksheet and its charts
const worksheet = workbook.worksheets.get(0);
const chartCollection = worksheet.charts;
const chart = chartCollection.get(0);
// Calculate the chart
chart.calculate();
// Get the chart title text
const title = chart.title;
const titleText = title ? title.text : "(No Title)";
// Prepare output messages
const messages = [];
messages.push('<p style="color: green;">Operation completed successfully!</p>');
messages.push(`<p>Workbook chart title: ${titleText}</p>`);
// Get legend labels and output them
const legendEntriesLabels = chart.legend.legendLabels;
if (legendEntriesLabels && legendEntriesLabels.forEach) {
const legendItems = [];
legendEntriesLabels.forEach(label => {
legendItems.push(`<li>${label}</li>`);
});
if (legendItems.length) {
messages.push('<p>Workbook chart legend:</p>');
messages.push(`<ul>${legendItems.join('')}</ul>`);
} else {
messages.push('<p>(No legend labels found)</p>');
}
} else {
messages.push('<p>(No legend or legend labels available)</p>');
}
// Save modified workbook to allow download (optional)
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
resultDiv.innerHTML = messages.join('');
});
</script>
</html>
Output generato dal codice di esempio
Questo è l’output console del codice di esempio precedente.
Workbook chart title: Grafik Başlığı
Workbook chart legend: Artış
Workbook chart legend: Düşüş
Workbook chart legend: Toplam
Workbook category axis title: Eksen Başlığı