Tutorial 44: Trend lines in XY charts
« on: February 05, 2016, 01:23:33 AM »
This sample will plot a X/Y (scatter) chart with a trend line.
 
You may also obtain the equation factors for the trend line calling CalculationTrendLine property which will return a TrendLine object containing the values of the formula y = bx + a.
 
Steps to run this sample:

1. Create a new Windows application project (VB.NET)
2. Add Crossing Charts Library to the toolbox pallete and drag a chart to Form1.
3. Add a Label control below the chart control.
4. Copy and paste de following code:

Code: [Select]
Imports CrossingChartsLibrary
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim sf As New SeriesFactory
  sf.AddPoint(1, 2)
  sf.AddPoint(1.7, 4)
  sf.AddPoint(4.2, 6)
  sf.AddPoint(5, 8)
  sf.AddPoint(6, 10)
  sf.AddPoint(8.2, 12)
  With CrossingCharts1
    .Title = "Trend Line Sample"
    .Style = STYLE2D3D.STYLE2D_XY
    ' Assign data
    .Series = sf.Series
    .ShowValues = False
    ' Setup y-axis
    .YAxisScaleMode = SCALEMODE.SCALEMODE_FIXED
    .YAxisScaleMinimum = 0
    .YAxisScaleMaximum = 14
    .YAxisNumericFormat = "0"
    ' Setup x-axis
    .XAxisScaleMode = SCALEMODE.SCALEMODE_FIXED
    .XAxisScaleMinimum = 0
    .XAxisScaleMaximum = 10
    .XAxisNumericFormat = "0"
    .ShowDividersX = True
    .ShowXAxis = True
    ' Look and feel
    .WallsBackStyle = STYLEWALLBACKGROUND.STYLEWALLBACKGROUND_GRADIENT
    .WallsBackColorPrincipal = Color.LightGray
    .WallsBackColorSecondary = Color.LightGray
    .SeriesColor = New Color() {Color.Black}
    .ShowDataTable = False
    .ShowLegend = False
    ' Draw trend line
    .Calculation = CALCULATION.CALCULATION_AVERAGE
    .CalculationLineStyle = New Pen(Color.Blue, 2)
    ' Refresh chart
    .RefreshChart()
    ' Print trend line equation below chart
    Dim trendLine As Trendline = .CalculationTrendLine(0)
    Label1.Text = "Equation: y = " & trendLine.b & " x + " & trendLine.a
End With
End Sub
End Class

Results:

« Last Edit: February 16, 2016, 11:15:03 AM by CrossingCharts »