I've written a program that calculates the line of best fit (intercept/slope) given several input values from the user. I've plotted each of the individual values, however unsure of the code to plot the line given the slope and y-intercept.
This is the slope:
double m = ( aXY.Sum() -
((levels.Sum() * scores.Sum()) / 5)) / (newaX.Sum() - ((powLevels) / 5));
The Intercept
double b = meanY - (m * meanX);
Plotting of points
for (int i = 0; i < levels.GetLength(0); i++)
{
chart1.Series["Series1"].Points
.AddXY(levels.GetValue(i), scores.ToArray().GetValue(i));
}
Any ideas? I am by no means an expert and getting this far took a fair bit of experimenting..
Assuming your data are plotted as a scattergraph using the
ChartType.Points
the simplest way to add a line is to add one extraSeries
withChartType.Line
and set two points there.There are other ways to create a line on a
Chart
, like drawing it or creating aLineAnnotation
, but they are much more complicated!Following this example to the letter here is an implementation:
Note that after creating the series for the line of best fit the thing you were looking for are just the last two lines..:
If you want to you can add the first line point right at the y-axis:
In this case you may want to set the minimum x-values shown in the chart to prevent it from including
-1
, maybe like this: