Articles in this section
Category / Section

How to add a custom trend line in WinForms Chart?

5 mins read
TrendLines are used to draw lines within the ChartArea to visually represent trends in data in Winforms Chart. By utilizing mouse events, users can interactively draw multiple trend lines on the chart. Each line can be customized with different colors to enhance clarity and distinguish between various trends.


C#

//Assign the X and Y axes
this.chartControl1.PrimaryXAxis.ValueType = ChartValueType.DateTime;
this.chartControl1.PrimaryYAxis.ValueType = ChartValueType.Double;

//Configure the chart series
ChartSeries chartSeries = new ChartSeries("Sales");
foreach (var data in dataSource)
{
    chartSeries.Points.Add(data.Date, data.Sales);
}
chartSeries.Type = ChartSeriesType.Line;
this.chartControl1.Series.Add(chartSeries);

var trendLines = new[]
{
    new { Points = new[] { (new DateTime(2000, 1, 1), 10.0), (new DateTime(2002, 1, 1), 15.0) }, Color = Color.Red },
    new { Points = new[] { (new DateTime(2003, 1, 1), 20.0), (new DateTime(2005, 1, 1), 30.0) }, Color = Color.Green },
    new { Points = new[] { (new DateTime(2006, 1, 1), 35.0), (new DateTime(2008, 1, 1), 45.0) }, Color = Color.Red },
    new { Points = new[] { (new DateTime(2007, 1, 1), 20.0), (new DateTime(2010, 1, 1), 40.0) }, Color = Color.Green }
};

// Add all trend lines
foreach (var trend in trendLines)
{
    AddTrendLine(trend.Points, trend.Color);
}

void AddTrendLine((DateTime, double)[] points, Color color)
{
    ChartSeries trendSeries = new ChartSeries();
    trendSeries.Type = ChartSeriesType.Line;
    foreach (var point in points)
    {
        trendSeries.Points.Add(point.Item1, point.Item2);
    }
    trendSeries.Style.Interior = new Syncfusion.Drawing.BrushInfo(color);
    chartControl1.Series.Add(trendSeries);
}

VB

'Assign the X And Y axes
columnChart.PrimaryXAxis.ValueType = ChartValueType.DateTime
columnChart.PrimaryYAxis.ValueType = ChartValueType.Double

'Configure the chart series
For Each item In viewModel.PlantDetails
        ChartSeries1.Points.Add(item.Date, item.YValue)
Next
ChartSeries1.Type = ChartSeriesType.Line
columnChart.Series.Add(ChartSeries1)

Dim trendLines = {
    (Points:={(New DateTime(2000, 1, 1), 10.0), (New DateTime(2002, 1, 1), 15.0)}, Color:=Color.Red),
    (Points:={(New DateTime(2003, 1, 1), 20.0), (New DateTime(2005, 1, 1), 30.0)}, Color:=Color.Green),
    (Points:={(New DateTime(2006, 1, 1), 35.0), (New DateTime(2008, 1, 1), 45.0)}, Color:=Color.Red),
    (Points:={(New DateTime(2007, 1, 1), 20.0), (New DateTime(2010, 1, 1), 40.0)}, Color:=Color.Green)
}

' Add all trend lines
For Each trend In trendLines
Dim trendSeries = New ChartSeries()
trendSeries.Type = ChartSeriesType.Line
trendSeries.Style.Interior = New Syncfusion.Drawing.BrushInfo(trend.Color)
trendSeries.Style.Border.Width = 2
    For Each pt In trend.Points
          trendSeries.Points.Add(pt.Item1, pt.Item2)
     Next
     columnChart.Series.Add(trendSeries)
Next

Output:

add a custom trend line


Conclusion

I hope you enjoyed learning about how to add a custom trend line in WinForms Chart.

You can refer to our WinForms Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms Chart examples to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you! 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied