Trendline is an additional line that indicates the slope (or trend) in a particular data series and is also known as a line of best fit.
Trendlines can be helpful when you are analyzing data because they can forecast future values based upon your current data.
Users can create 6 different types of trendlines for their charts: including linear, exponential, logarithmic, polynomial, power, and moving average.
Linear: a best fit straight line for simple linear data sets.
Exponential: a best-fit curved line that illustrates how data values increase or decrease and then level out.
Logarithmic: a best-fit curved line that illustrates the data increases or decreases quickly and then levels out.
Polynomial: a curved line illustrating fluctuations in the data values based on the order property.
Power: a curved line to compare measurements that increase at a specific rate.
MovingAverage: averages a specific number of data points, and uses the value as a point in the line.
Trendline supports the following chart types:
Column
Bar
Line
Scatter
Area
You can create a linear trendline using the following code:
Customize
order: Specify the number of terms in the Polynomial equation. The order is a integer with range 2 to 6.
intercept: Specify the intercept for linear, exponential, polynomial trendline.
displayEquation & displayRSquared: Specify whether to use equation or R squared for the trendline.
forward & backward: use forward or backward to project the data.
The displayEquation, displayRSquared, forward, backward supports linear, exponential, logarithmic, polynomial, power trendline.
style: Specify the line style of the trendline, including color, width, and dash line.
name: Specify the name of the trendline. The built-in name will be used if no name is provided.
Period: Specify the period of the MovingAverage Trendline. The order is a integer with range 2 to data set count minus 1.
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
<gc-worksheet />
<gc-worksheet />
<gc-worksheet />
</gc-spread-sheets>
</div>
</template>
<script setup>
import { ref } from 'vue';
import '@mescius/spread-sheets-vue';
import GC from '@mescius/spread-sheets';
import '@mescius/spread-sheets-shapes';
import '@mescius/spread-sheets-charts';
const spread = ref(null);
const advData = [
['Advertising', 'Items sold'],
[28, 17],
[34, 19],
[41, 18],
[47, 20],
[52, 24],
[59, 26],
[65, 29],
[72, 31],
[80, 34],
[87, 39],
[94, 40],
[102, 42],
];
const salesData = [
['', 'Sales'],
['Jan', 54],
['Feb', 60],
['Mar', 86],
['Apr', 92],
['May', 112],
['Jun', 157],
['Jul', 202],
['Aug', 195],
['Sep', 187],
['Oct', 194],
['Nov', 238],
['Dec', 289],
];
const initSpread = (spreadInstance) => {
spread.value = spreadInstance;
spread.value.setSheetCount(2);
initSheet1();
initSheet2();
};
const initSheet1 = () => {
const sheet1 = spread.value.getSheet(0);
sheet1.name('Basic');
sheet1.setArray(0, 0, advData);
sheet1.setArray(18, 0, salesData);
for (let i = 0; i < 12; i++) {
sheet1.getCell(i + 1, 0).formatter('$#,##0');
}
// Choose a suitable trendline type from GC.Spread.Sheets.Charts.TrendlineType to fit your chart
const chart1 = sheet1.charts.add("chart1", GC.Spread.Sheets.Charts.ChartType.xyScatter, 130, 5, 500, 350, "A1:B13", GC.Spread.Sheets.Charts.RowCol.columns);
const axes = chart1.axes();
axes.primaryValue.title.text = 'Items sold';
axes.primaryCategory.title.text = 'Advertising';
axes.primaryCategory.majorGridLine.visible = true;
axes.primaryCategory.majorUnit = 10;
chart1.axes(axes);
let targetSeriesIndex = 0;
let targetSeries = chart1.series().get(targetSeriesIndex);
const linearTrendline = {
type: GC.Spread.Sheets.Charts.TrendlineType.linear,
style: {
color: 'red',
width: 2
}
};
targetSeries.trendlines = [linearTrendline];
chart1.series().set(targetSeriesIndex, targetSeries);
const chart2 = sheet1.charts.add("chart2", GC.Spread.Sheets.Charts.ChartType.columnClustered, 130, 360, 500, 350, "A19:B31", GC.Spread.Sheets.Charts.RowCol.columns);
targetSeriesIndex = 0;
targetSeries = chart2.series().get(targetSeriesIndex);
const exponentialTrendline = {
type: GC.Spread.Sheets.Charts.TrendlineType.exponential,
style: {
color: 'orange',
width: 2,
dashStyle: GC.Spread.Sheets.Charts.LineDashStyle.dash
}
};
targetSeries.trendlines = [exponentialTrendline];
chart2.series().set(targetSeriesIndex, targetSeries);
};
const initSheet2 = () => {
// More settings
const sheet2 = spread.value.getSheet(1);
sheet2.name('Advance');
sheet2.setArray(0, 0, advData);
sheet2.setArray(18, 0, salesData);
for (let i = 0; i < 12; i++) {
sheet2.getCell(i + 1, 0).formatter('$#,##0');
}
// Change the order(the highest power for the independent variable) of polynomial trendline to adjust R-squared value
// Also you could show the equation and R-squared value in chart area if you want
const chart3 = sheet2.charts.add("chart3", GC.Spread.Sheets.Charts.ChartType.xyScatter, 130, 5, 500, 350, "A1:B13", GC.Spread.Sheets.Charts.RowCol.columns);
const axes = chart3.axes();
axes.primaryValue.title.text = 'Items sold';
axes.primaryCategory.title.text = 'Advertising';
axes.primaryCategory.majorGridLine.visible = true;
axes.primaryCategory.majorUnit = 10;
chart3.axes(axes);
let targetSeriesIndex = 0;
let targetSeries = chart3.series().get(targetSeriesIndex);
const polynomialTrendline = {
type: GC.Spread.Sheets.Charts.TrendlineType.polynomial,
order: 4,
displayEquation: true,
displayRSquared: true,
style: {
color: 'red',
width: 2
}
};
targetSeries.trendlines = [polynomialTrendline];
chart3.series().set(targetSeriesIndex, targetSeries);
// Set a value in the Forward and Backward fields to project your data into the future.
const chart4 = sheet2.charts.add("chart4", GC.Spread.Sheets.Charts.ChartType.columnClustered, 130, 360, 500, 350, "A19:B31", GC.Spread.Sheets.Charts.RowCol.columns);
targetSeriesIndex = 0;
targetSeries = chart4.series().get(targetSeriesIndex);
const exponentialTrendline = {
type: GC.Spread.Sheets.Charts.TrendlineType.exponential,
forward: 3,
style: {
color: 'orange',
width: 2,
dashStyle: GC.Spread.Sheets.Charts.LineDashStyle.dash
}
};
targetSeries.trendlines = [exponentialTrendline];
chart4.series().set(targetSeriesIndex, targetSeries);
};
</script>
<style scoped>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: 100%;
height: 100%;
overflow: hidden;
float: left;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
#app {
height: 100%;
}
</style>
<!DOCTYPE html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>SpreadJS VUE</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/en/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/en/vue3/node_modules/systemjs/dist/system.src.js"></script>
<script src="./systemjs.config.js"></script>
<script src="./compiler.js" type="module"></script>
<script>
var System = SystemJS;
System.import("./src/app.js");
System.import('$DEMOROOT$/en/lib/vue3/license.js');
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>
(function (global) {
SystemJS.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
packageConfigPaths: [
'./node_modules/*/package.json',
"./node_modules/@mescius/*/package.json",
"./node_modules/@babel/*/package.json",
"./node_modules/@vue/*/package.json"
],
map: {
'vue': "npm:vue/dist/vue.esm-browser.js",
'tiny-emitter': 'npm:tiny-emitter/index.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
"systemjs-babel-build": "npm:systemjs-plugin-babel/systemjs-babel-browser.js",
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-resources-en': 'npm:@mescius/spread-sheets-resources-en/index.js',
'@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js',
'@mescius/spread-sheets-shapes': 'npm:@mescius/spread-sheets-shapes/index.js',
'@mescius/spread-sheets-charts': 'npm:@mescius/spread-sheets-charts/index.js',
},
meta: {
'*.css': { loader: 'systemjs-plugin-css' },
'*.vue': { loader: "../plugin-vue/index.js" }
}
});
})(this);