We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

calculatina linear regression function based on a scatter chart (i.e scatter chart values)

Hello! (VS.NET 2005; SyncF 4.4.0.51)

Right now, i'm a little confused; I need to "port" an old access chart (done with some Microsoft chart control libary or so.) to our .net application using your control studio. However this control library offers me to do a scatter chart AND draw the linear regression function for this scatter on the chart area. However it seems that your chartcontrol/chart library doesn't have any similar functionality (i.e calculate the function formula -> calculating slope and Y-intercept, and display the line on the cart)

I know that you have supported functions for these calculation in *other* libraries (calculating and grid) However it seems that i need to create a lot of extra objects or classes to consume these functions (extra class that implements (ICalcData interface and i think either a gridmodel object or a spreadsheet object); So i would like to know:: Am i right with my assumption, that right now, there are no function inside the chart namespace that would allow me to calculate the formula for a linear regession, or have i overseen something?

If i am right, could you please think about this and implement basic functions to calculate these values to the chart namespace.

2 Replies

AD Administrator Syncfusion Team February 6, 2007 07:40 AM UTC

Hi Christian,

My strong apologies for the delay in responding to you.

If you intention is to draw a Scatter Chart with TrendLine then it can done as shown in the code snippet below,

private void IntializeChartData( )
{
//Creates a new series
ChartSeries series1 = this.chartControl1.Model.NewSeries("Birds Age Record", ChartSeriesType.Scatter);
series1.Text = series1.Name;

//Add method is used to add points to the series
series1.Points.Add( 1.5, 3.0 );//series1.Points.Add(Xvalue,Yvalue)
series1.Points.Add( 2.5, 4.0 );
series1.Points.Add( 3.2, 8.0 );
series1.Points.Add( 4.5, 9.0 );
series1.Points.Add( 5.2, 10.0 );
series1.Points.Add( 6.8, 12.0 );
series1.Points.Add( 7.1, 14.0 );

//Series created is added to the chartcontrol
this.chartControl1.Series.Add( series1 );
this.chartControl1.Series[0].Style.Symbol.Color = Color.Orange;

//To set the Range of PrimaryYAxis
this.chartControl1.PrimaryYAxis.RangeType = ChartAxisRangeType.Set;

//Range of PrimaryYAxis
this.chartControl1.PrimaryYAxis.Range = new MinMaxInfo( 0, 15, 2 );

//Title to be displayed along PrimaryXAxis
this.chartControl1.PrimaryXAxis.Title = "Size of Wings of a Bird";

//Title to be displayed along PrimaryYAxis
this.chartControl1.PrimaryYAxis.Title = "Age in Days";

//Visibility of Grid of PrimaryXAxis and PrimaryYAxis
this.chartControl1.PrimaryXAxis.DrawGrid = false;
this.chartControl1.PrimaryYAxis.DrawGrid = false;

this.chartControl1.PrimaryXAxis.ForeColor = Color.Green;
this.chartControl1.PrimaryYAxis.ForeColor = Color.Green;
}

private void DrawTrendLineGraph( )
{
ChartSeries trend = this.chartControl1.Model.NewSeries( "Linear Trendline", ChartSeriesType.Line );
trend.Text = trend.Name;
count = this.chartControl1.Series[0].Points.Count;

double x1 = this.chartControl1.Series[0].Points[0].X;
double x2 = this.chartControl1.Series[0].Points[count - 1].X;

for (int i = 0; i < this.chartControl1.Series[0].Points.Count; i++)
AddPoint(this.chartControl1.Series[0].Points[i].X, this.chartControl1.Series[0].Points[i].YValues[0]);

m = (count * XY - (X * Y)) / (count * Xsqr - X * X);
b = (Y - m * X) / count;

trend.Points.Add(x1, ((m * x1) + b));
trend.Points.Add(x2, ((m * x2) + b));

this.chartControl1.Series.Add( trend );
}

Also kindly take a look at the attached sample.

Let me know if I am wrong in getting your requirement.

Thanks & Regards,
Ramya.

TrendLine.zip


CF Christian Fleischhacker February 7, 2007 04:00 PM UTC

Hi Ramya!
Yes I wanted to draw a Trendline to the scatter graph; Thanks for the code and the example.

Loader.
Live Chat Icon For mobile
Up arrow icon