C# Excel 爲圖表添加趨勢線、偏差線

Excel圖表可以將數據可視化,在圖表中另行添加趨勢線和偏差線,可對數據進行進一步的數據分析和統計的可視化處理。Excel中的趨勢線可用於趨勢預測/迴歸分析,共6中類型:指數(X),線性(L),對數(0),多項式(P),冪(W),移動平均(M)。偏差線可用於顯示潛在的偏差或相對於系列中每一個數據標誌的不肯定程度。Excel中可設置偏差線的顯示方向:正負誤差,負誤差,正誤差;以及設置偏差類型及偏差量:固定值,百分比,標準誤差,標準偏差,自定義類型。html

本篇文章主要介紹,使用免費版的Free Spire.XLSC#中獨立建立Excel文檔,生成折線圖、柱狀圖,並添加趨勢線和偏差線。spa

需添加的命名空間3d

using Spire.Xls;
using System.Drawing;
 

步驟詳解:
excel

步驟一:獨立建立Excel文件和表單。code

            Workbook workbook =  new Workbook();
            workbook.CreateEmptySheets( 1);
            Worksheet sheet = workbook.Worksheets[ 0];
 

步驟二:爲Excel單元格添加示例數據。htm

            sheet.Name =  " 偏差線和趨勢線演示 ";
            sheet.Range[ " A1 "].Value =  " 月份 ";
            sheet.Range[ " A2 "].Value =  " 一月 ";
            sheet.Range[ " A3 "].Value =  " 二月 ";
            sheet.Range[ " A4 "].Value =  " 三月 ";
            sheet.Range[ " A5 "].Value =  " 四月 ";
            sheet.Range[ " A6 "].Value =  " 五月 ";
            sheet.Range[ " A7 "].Value =  " 六月 ";
            sheet.Range[ " B1 "].Value =  " 計劃量 ";
            sheet.Range[ " B2 "].NumberValue =  3.3;
            sheet.Range[ " B3 "].NumberValue =  2.5;
            sheet.Range[ " B4 "].NumberValue =  2.0;
            sheet.Range[ " B5 "].NumberValue =  3.7;
            sheet.Range[ " B6 "].NumberValue =  4.5;
            sheet.Range[ " B7 "].NumberValue =  4.0;
            sheet.Range[ " C1 "].Value =  " 實際量 ";
            sheet.Range[ " C2 "].NumberValue =  3.8;
            sheet.Range[ " C3 "].NumberValue =  3.2;
            sheet.Range[ " C4 "].NumberValue =  1.7;
            sheet.Range[ " C5 "].NumberValue =  3.5;
            sheet.Range[ " C6 "].NumberValue =  4.5;
            sheet.Range[ " C7 "].NumberValue =  4.3
 

步驟三:生成折線圖,爲其添加趨勢線和偏差線。blog

             // 生成折線圖,設置位置
            Chart chart = sheet.Charts.Add(ExcelChartType.Line);
            chart.DataRange = sheet.Range[ " B1:B7 "];
            chart.SeriesDataFromRange =  false;
            chart.TopRow =  6;
            chart.BottomRow =  25;
            chart.LeftColumn =  2;
            chart.RightColumn =  9;
            chart.ChartTitle =  " 百分比正誤差偏差線和對數趨勢線示例 ";
            chart.ChartTitleArea.IsBold =  true;
            chart.ChartTitleArea.Size =  12;
            Spire.Xls.Charts.ChartSerie cs1 = chart.Series[ 0];      
            cs1.CategoryLabels = sheet.Range[ " A2:A7 "];
             // 添加對數趨勢線
            cs1.TrendLines.Add(TrendLineType.Logarithmic);
             // 添加10%正誤差偏差線
            cs1.ErrorBar( true, ErrorBarIncludeType.Plus, ErrorBarType.Percentage, 10);

 

步驟四:生成柱狀圖,併爲其添加趨勢線和偏差線。
            // 生成柱狀圖做爲對照組
            Chart chart2 = sheet.Charts.Add(ExcelChartType.ColumnClustered);
            chart2.DataRange = sheet.Range[ " B1:C7 "];
            chart2.SeriesDataFromRange =  false;
            chart2.TopRow =  6;
            chart2.BottomRow =  25;
            chart2.LeftColumn =  10;
            chart2.RightColumn =  17;
            chart2.ChartTitle =  " 正負標準偏差偏差線和指數趨勢線示例 ";
            chart2.ChartTitleArea.IsBold =  true;
            chart2.ChartTitleArea.Size =  12;
            Spire.Xls.Charts.ChartSerie cs2 = chart2.Series[ 0];
            cs2.CategoryLabels = sheet.Range[ " A2:A7 "];
             // 添加標準偏差負偏差0.3的偏差線
            cs2.ErrorBar( true, ErrorBarIncludeType.Minus, ErrorBarType.StandardError,  0.3);
             // 添加指數趨勢線
            cs2.TrendLines.Add(TrendLineType.Exponential);
            Spire.Xls.Charts.ChartSerie cs3 = chart2.Series[ 1];
             // 添加標準偏差正負偏差0.5的偏差線
            cs3.ErrorBar( true, ErrorBarIncludeType.Both, ErrorBarType.StandardError,  0.5);

 

步驟五:保存Excel文檔,啓動查看效果。
            workbook.SaveToFile( " S3.xlsx ", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start( " S3.xlsx ");

 

效果圖:文檔

 

純圖表:get


 

完整代碼:數據分析

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Xls;
using System.Drawing;

namespace ConsoleApplication2
{
     class Program
    {
         static  void Main( string[] args)
        {
          
            Workbook workbook =  new Workbook();
            workbook.CreateEmptySheets( 1);
            Worksheet sheet = workbook.Worksheets[ 0];
            
            sheet.Name =  " 偏差線和趨勢線演示 ";
            sheet.Range[ " A1 "].Value =  " 月份 ";
            sheet.Range[ " A2 "].Value =  " 一月 ";
            sheet.Range[ " A3 "].Value =  " 二月 ";
            sheet.Range[ " A4 "].Value =  " 三月 ";
            sheet.Range[ " A5 "].Value =  " 四月 ";
            sheet.Range[ " A6 "].Value =  " 五月 ";
            sheet.Range[ " A7 "].Value =  " 六月 ";
            sheet.Range[ " B1 "].Value =  " 計劃量 ";
            sheet.Range[ " B2 "].NumberValue =  3.3;
            sheet.Range[ " B3 "].NumberValue =  2.5;
            sheet.Range[ " B4 "].NumberValue =  2.0;
            sheet.Range[ " B5 "].NumberValue =  3.7;
            sheet.Range[ " B6 "].NumberValue =  4.5;
            sheet.Range[ " B7 "].NumberValue =  4.0;
            sheet.Range[ " C1 "].Value =  " 實際量 ";
            sheet.Range[ " C2 "].NumberValue =  3.8;
            sheet.Range[ " C3 "].NumberValue =  3.2;
            sheet.Range[ " C4 "].NumberValue =  1.7;
            sheet.Range[ " C5 "].NumberValue =  3.5;
            sheet.Range[ " C6 "].NumberValue =  4.5;
            sheet.Range[ " C7 "].NumberValue =  4.3;      
            
            Chart chart = sheet.Charts.Add(ExcelChartType.Line);
            chart.DataRange = sheet.Range[ " B1:B7 "];
            chart.SeriesDataFromRange =  false;
            chart.TopRow =  6;
            chart.BottomRow =  25;
            chart.LeftColumn =  2;
            chart.RightColumn =  9;
            chart.ChartTitle =  " 百分比正誤差偏差線和對數趨勢線示例 ";
            chart.ChartTitleArea.IsBold =  true;
            chart.ChartTitleArea.Size =  12;
            Spire.Xls.Charts.ChartSerie cs1 = chart.Series[ 0];      
            cs1.CategoryLabels = sheet.Range[ " A2:A7 "];
            cs1.TrendLines.Add(TrendLineType.Logarithmic);
            cs1.ErrorBar( true, ErrorBarIncludeType.Plus, ErrorBarType.Percentage,  10);
            Chart chart2 = sheet.Charts.Add(ExcelChartType.ColumnClustered);
            chart2.DataRange = sheet.Range[ " B1:C7 "];
            chart2.SeriesDataFromRange =  false;
            chart2.TopRow =  6;
            chart2.BottomRow =  25;
            chart2.LeftColumn =  10;
            chart2.RightColumn =  17;
            chart2.ChartTitle =  " 正負標準偏差偏差線和指數趨勢線示例 ";
            chart2.ChartTitleArea.IsBold =  true;
            chart2.ChartTitleArea.Size =  12;
            Spire.Xls.Charts.ChartSerie cs2 = chart2.Series[ 0];
            cs2.CategoryLabels = sheet.Range[ " A2:A7 "];
            cs2.ErrorBar( true, ErrorBarIncludeType.Minus, ErrorBarType.StandardError,  0.3);
            cs2.TrendLines.Add(TrendLineType.Exponential);
            Spire.Xls.Charts.ChartSerie cs3 = chart2.Series[ 1];
            cs3.ErrorBar( true, ErrorBarIncludeType.Both, ErrorBarType.StandardError,  0.5);
            
            workbook.SaveToFile( " S3.xlsx ", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start( " S3.xlsx ");         }     } }
相關文章
相關標籤/搜索