趋势线

趋势线是图表上附加的线,用来显示数据的斜率或趋势。

趋势线是用来表示数据列中的斜率(或趋势)的附加线,又称为最佳拟合线。 因为趋势线可以根据当前数据预测未来值,所以在分析数据时可能会有所帮助。 用户可以为其图表创建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之间的整数。
var 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], ]; var 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], ]; window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 2 }); initSheet1(spread); initSheet2(spread); }; function initSheet1(spread) { var sheet1 = spread.getSheet(0); sheet1.name('Basic'); sheet1.setArray(0, 0, advData); sheet1.setArray(18, 0, salesData); for (var 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 var chart1 = sheet1.charts.add("chart1", GC.Spread.Sheets.Charts.ChartType.xyScatter, 130, 5, 500, 350, "A1:B13", GC.Spread.Sheets.Charts.RowCol.columns); var 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); var targetSeriesIndex = 0; var targetSeries = chart1.series().get(targetSeriesIndex); var linearTrendline = { type: GC.Spread.Sheets.Charts.TrendlineType.linear, style: { color: 'red', width: 2 } }; targetSeries.trendlines = [ linearTrendline ]; chart1.series().set(targetSeriesIndex, targetSeries); var chart2 = sheet1.charts.add("chart2", GC.Spread.Sheets.Charts.ChartType.columnClustered, 130, 360, 500, 350, "A19:B31", GC.Spread.Sheets.Charts.RowCol.columns); var targetSeriesIndex = 0; var targetSeries = chart2.series().get(targetSeriesIndex); var 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); } function initSheet2(spread) { // More settings var sheet2 = spread.getSheet(1); sheet2.name('Advance'); sheet2.setArray(0, 0, advData); sheet2.setArray(18, 0, salesData); for (var 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 var chart3 = sheet2.charts.add("chart3", GC.Spread.Sheets.Charts.ChartType.xyScatter, 130, 5, 500, 350, "A1:B13", GC.Spread.Sheets.Charts.RowCol.columns); var 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); var targetSeriesIndex = 0; var targetSeries = chart3.series().get(targetSeriesIndex); var 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. var chart4 = sheet2.charts.add("chart4", GC.Spread.Sheets.Charts.ChartType.columnClustered, 130, 360, 500, 350, "A19:B31", GC.Spread.Sheets.Charts.RowCol.columns); var targetSeriesIndex = 0; var targetSeries = chart4.series().get(targetSeriesIndex); var 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); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-charts/dist/gc.spread.sheets.charts.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> </div> </div> </body> </html>
.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; }