趋势线是用来表示数据列中的斜率(或趋势)的附加线,又称为最佳拟合线。
因为趋势线可以根据当前数据预测未来值,所以在分析数据时可能会有所帮助。
用户可以为其图表创建6种不同类型的趋势线:包括线性,指数,对数,多项式,幂和移动平均值。
Linear: 最佳拟合直线。
Exponential: 用指数曲线说明数据值增大或减小然后稳定。
Logarithmic: 用对数曲线说明数据值快速增大或减小然后稳定。
Polynomial: 基于多项式的波动曲线。
Power: 以特定速率增加的曲线。
MovingAverage: 移动平均值曲线。
趋势线支持如下的图表类型:
Column
Bar
Line
Scatter
Area
你可以通过如下示例代码添加一条线性趋势线。
自定制
order: 在多项式趋势线中指定项数,取2到6之间的整数。
intercept: 指定“线性”,“指数”,“多项式”趋势线的截距。
displayEquation & displayRSquared: 指定是否显示趋势线的公式和R方。
forward & backward: 向前或向后预测数据。
(displayEquation,displayRSquared,forward,backward属性支持“线性”,“多项式”,“指数”,“对数”,“幂” 趋势线。)
style: 指定趋势线的样式,包括颜色,宽度和线型。
name: 指定趋势线的名称。 如果未指定名称,则将使用内置名称。
Period: 指定MovingAverage的周期,即是2到数据计数减1之间的整数。
import { Component, NgModule, enableProdMode } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { SpreadSheetsModule } from '@grapecity-software/spread-sheets-angular';
import GC from '@grapecity-software/spread-sheets';
import '@grapecity-software/spread-sheets-charts';
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
import './styles.css';
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],
];
@Component({
selector: 'app-component',
templateUrl: 'src/app.component.html'
})
export class AppComponent {
spread: GC.Spread.Sheets.Workbook;
hostStyle = {
width: '100%',
height: '100%',
overflow: 'hidden',
float: 'left'
};
initSpread($event: any) {
this.spread = $event.spread;
let spread = this.spread;
this.spread = spread;
spread.setSheetCount(2);
this.initSheet1();
this.initSheet2();
}
initSheet1() {
let sheet1 = this.spread.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
let chart1 = sheet1.charts.add("chart1", GC.Spread.Sheets.Charts.ChartType.xyScatter, 130, 5, 500, 350, "A1:B13", GC.Spread.Sheets.Charts.RowCol.columns);
let 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);
let linearTrendline = {
type: GC.Spread.Sheets.Charts.TrendlineType.linear,
style: {
color: 'red',
width: 2
}
};
targetSeries.trendlines = [linearTrendline];
chart1.series().set(targetSeriesIndex, targetSeries);
let 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);
let 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);
}
initSheet2() {
// More settings
let sheet2 = this.spread.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
let chart3 = sheet2.charts.add("chart3", GC.Spread.Sheets.Charts.ChartType.xyScatter, 130, 5, 500, 350, "A1:B13", GC.Spread.Sheets.Charts.RowCol.columns);
let 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);
let 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.
let 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);
let 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);
}
}
@NgModule({
imports: [BrowserModule, SpreadSheetsModule],
declarations: [AppComponent],
exports: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
enableProdMode();
// Bootstrap application with hash style navigation and global services.
platformBrowserDynamic().bootstrapModule(AppModule);
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/zh/angular/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- Polyfills -->
<script src="$DEMOROOT$/zh/angular/node_modules/core-js/client/shim.min.js"></script>
<script src="$DEMOROOT$/zh/angular/node_modules/zone.js/dist/zone.min.js"></script>
<!-- SystemJS -->
<script src="$DEMOROOT$/zh/angular/node_modules/systemjs/dist/system.js"></script>
<script src="systemjs.config.js"></script>
<script>
// workaround to load 'rxjs/operators' from the rxjs bundle
System.import('rxjs').then(function(m) {
System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators));
System.import('$DEMOROOT$/zh/lib/angular/license.ts');
System.import('./src/app.component');
});
</script>
</head>
<body>
<app-component></app-component>
</body>
</html>
<div class="sample-tutorial">
<gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="initSpread($event)">
<gc-worksheet></gc-worksheet>
</gc-spread-sheets>
</div>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
(function(global) {
System.config({
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
},
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'core-js': 'npm:core-js/client/shim.min.js',
'zone': 'npm:zone.js/dist/zone.min.js',
'rxjs': 'npm:rxjs/bundles/rxjs.umd.min.js',
'@angular/core': 'npm:@angular/core/bundles/core.umd.min.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.min.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.min.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.min.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.min.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.min.js',
'@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.min.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.min.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.min.js',
'jszip': 'npm:jszip/dist/jszip.min.js',
'typescript': 'npm:typescript/lib/typescript.js',
'ts': 'npm:plugin-typescript/lib/plugin.js',
'css': 'npm:systemjs-plugin-css/css.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
'systemjs-babel-build': 'npm:systemjs-plugin-babel/systemjs-babel-browser.js',
'@grapecity-software/spread-sheets-shapes': 'npm:@grapecity-software/spread-sheets-shapes/index.js',
'@grapecity-software/spread-sheets-charts': 'npm:@grapecity-software/spread-sheets-charts/index.js',
'@grapecity-software/spread-sheets': 'npm:@grapecity-software/spread-sheets/index.js',
'@grapecity-software/spread-sheets-resources-zh': 'npm:@grapecity-software/spread-sheets-resources-zh/index.js',
'@grapecity-software/spread-sheets-angular': 'npm:@grapecity-software/spread-sheets-angular/bundles/grapecity-software-spread-sheets-angular.umd.js',
'@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/jsob-test-dependency-package/react-components/index.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
},
}
});
})(this);