Spread WPF 18
Features / Charts / Chart Elements / Trendline
In This Topic
    Trendline
    In This Topic

    Trendline refers to a straight or curved line that is superimposed on the chart to inform the user about the direction of the data or the trend. It helps in predicting the future values.

    You can use the Add method of the ITrendlines interface to create a new trendline for a specific series. You need to bind the trend line to a data source, set other relevant properties and add it to the Series collection.

    Following is the list of various trendline types supported in the Spread WPF charts:

    Trendline Type Description
    Linear  Linear trendline is the straight line that most closely approximates the data in the chart. The data is linear, if the data pattern resembles a line.
    Polynomial  Polynomial trendlines are curved lines that are used with fluctuating data. They are useful for analyzing gains or losses over a large data set.
    Logarithmic  Logarithmic trendline is a best-fit curved line that is most useful when the rate of change in the data increases or decreases quickly and then levels out.
    MovingAverage Moving Average trendline is used to smooth out variations in data to show a trend clearly. It uses the average value of a specific amount of data as a point in the trendline.
    Power  Power trendline Power trendline is a curved line that is best used with data sets that compare measurements that increase at a specific rate.
    Exponential  Exponential trendline is a curved line that is most useful when data values rise or fall at increasingly higher rates. You cannot create an exponential trend line if your data contains zero or negative values.

    Refer to the following example code to add trendlines.

    Copy Code
    object[,] values = new object[,] { { 1, 3, 5, 2, 8, 12 }, { 2, 4, 5, 6, 2, 9 }, { 2, 4, 2, 1, 1, 2 }, { 0, 4, 2, 1, 0, 2 }, { 1, 3, 5, 0, 8, 12 } };
    var sheet1 = spreadSheet1.Workbook.ActiveSheet;
    sheet1.SetValue(0, 0, values, false);
    sheet1.Cells["A1:F3"].Select();
    sheet1.Shapes.AddChart(GrapeCity.Spreadsheet.Charts.ChartType.ColumnClustered, 100, 50, 600, 350, false);
    // Add trendline.
    sheet1.ChartObjects[0].Chart.Series[0].Trendlines.Add(TrendlineType.Polynomial, 3);
    sheet1.ChartObjects[0].Chart.Series[0].Trendlines[0].DisplayEquation = true;
    sheet1.ChartObjects[0].Chart.Series[0].Trendlines[0].DisplayRSquared = true;
    sheet1.ChartObjects[0].Chart.Series[1].Trendlines.Add(TrendlineType.Logarithmic);
    
    Copy Code
    Dim values = New Object(,) {
        {1, 3, 5, 2, 8, 12},
        {2, 4, 5, 6, 2, 9},
        {2, 4, 2, 1, 1, 2},
        {0, 4, 2, 1, 0, 2},
        {1, 3, 5, 0, 8, 12}}
    Dim sheet1 = spreadSheet1.Workbook.ActiveSheet
    sheet1.SetValue(0, 0, values, False)
    sheet1.Cells("A1:F3").[Select]()
    sheet1.Shapes.AddChart(GrapeCity.Spreadsheet.Charts.ChartType.ColumnClustered, 100, 50, 600, 350, False)
    ' Add trendline.
    sheet1.ChartObjects(0).Chart.Series(0).Trendlines.Add(TrendlineType.Polynomial, 3)
    sheet1.ChartObjects(0).Chart.Series(0).Trendlines(0).DisplayEquation = True
    sheet1.ChartObjects(0).Chart.Series(0).Trendlines(0).DisplayRSquared = True
    sheet1.ChartObjects(0).Chart.Series(1).Trendlines.Add(TrendlineType.Logarithmic)