Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

c# - Plotting a line in a chart given the y intercept and slope

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..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming your data are plotted as a scattergraph using the ChartType.Points the simplest way to add a line is to add one extra Series with ChartType.Line and set two points there.

There are other ways to create a line on a Chart, like drawing it or creating a LineAnnotation, but they are much more complicated!

Following this example to the letter here is an implementation:

enter image description here

Note that after creating the series for the line of best fit the thing you were looking for are just the last two lines..:

private void button1_Click(object sender, EventArgs e)
{
    // create TWO series!
    chart1.Series.Clear();
    chart1.Series.Add("Data");
    chart1.Series.Add("Line of best fit");
    chart1.Series[0].ChartType = SeriesChartType.Point;
    chart1.Series[1].ChartType = SeriesChartType.Line;

    List<int> levels = new List<int>() { 8, 2, 11, 6, 5, 4, 12, 9, 6, 1};
    List<int> scores = new List<int>() { 3, 10, 3, 6, 8, 12, 1, 4, 9, 14};

    double minX = levels.ToList().Min();
    double maxX = levels.ToList().Max();
    double meanX = 1f * levels.Sum() / levels.Count;
    double meanY = 1f * scores.Sum() / scores.Count;

    double st = 0;
    double sb = 0;
    for (int i = 0; i < levels.Count; i++ )
    {
        st += (levels[i] - meanX) * (scores[i] - meanY);
        sb += (levels[i] - meanX) * (levels[i] - meanX);
    }
    double slope = st / sb;
    double y0 = meanY - slope * meanX;  // y-intercept or y-crossing

    for (int i = 0; i < levels.Count; i++)
    {
            chart1.Series[0].Points.AddXY(levels[i], scores[i]);
    }
    // this is the part that creates the line of best fit:
    chart1.Series[1].Points.AddXY(minX, y0 + minX * slope);
    chart1.Series[1].Points.AddXY(maxX, y0 + maxX * slope);
}

If you want to you can add the first line point right at the y-axis:

 chart1.Series[1].Points.AddXY(0, y0 );

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:

 chart1.ChartAreas[0].AxisX.Minimum = minX - 1;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...