Get Equation Text of Chart Trendline with JavaScript via C++
Contents
[
Hide
]
You can retrieve the equation text of a chart trendline using Aspose.Cells for JavaScript via C++. Aspose.Cells provides the DataLabels.text[https://reference.aspose.com/cells/javascript-cpp/datalabels/#text–] property, which returns the equation text of a chart trendline. To use this property, you must first call the Chart.calculate()[https://reference.aspose.com/cells/javascript-cpp/chart/#calculate–] method.
The following screenshot shows the chart with a trendline, and its equation text is shown in red color. We will retrieve this text using the DataLabels.text[https://reference.aspose.com/cells/javascript-cpp/datalabels/#text–] property in the following sample code.

JavaScript code to get equation text of chart trendline
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Read Trendline Equation Example</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");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access the first worksheet
const worksheet = workbook.worksheets.get(0);
// Access the first chart inside the worksheet
const chart = worksheet.charts.get(0);
// Calculate the chart to get the equation text of the trendline
chart.calculate();
// Access the trendline
const trendLine = chart.nSeries.get(0).trendLines.get(0);
// Read the equation text of the trendline
const equationText = trendLine.dataLabels.text;
document.getElementById('result').innerHTML = `<p>Equation Text: ${equationText}</p>`;
});
</script>
</html>
Output generated by the sample code
This is the console output of the above sample code.
Equation Text: y = 8.1333x + 5