FIND POINT ON MSCHART

# FIND POINT ON MSCHART

• Monday, March 05, 2012 8:10 AM

I have a spline curve graph having 10 points on MS CHART using vb.net:

(1,30), (2,35),(3,42),(4,47),(5,53),(6,49),(7,45),(8,43),(9,41),(10,37)

how can i find the X value where y = 51 ?

• Edited by Monday, March 05, 2012 9:29 AM
•

### All Replies

• Monday, March 05, 2012 10:29 PM

I don't think it is posible to get values of any of the MS Chart functions, unless anyone else knows different. Spline fits are normally used for getting equally spaced points rather than specific values, so fitting a single polynomial function might be more useful.

The only way I know of getting the approximate value from a spline is to use a maths library function rather than the ms chart option. The Math.Net library has a CubicSplineInterpolation function that does the job. Apologies for using c# but you can get the following,

With the following code that ignores the fact that y=51 gives 2 x values.

```using MathNet.Numerics.Interpolation.Algorithms;

public Form1()
{
InitializeComponent();
double[] xData = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double[] yData = new double[] { 30, 35, 42, 47, 53, 49, 45, 43, 41, 37 };
//store the spline
List<double> splineX = new List<double>();
List<double> splineY = new List<double>();
//generate the spline
CubicSplineInterpolation myInterp = new CubicSplineInterpolation(xData, yData);
double a = 1;
while (a < 10)
{
a = a + 0.05;
}
//get the Y value closest to required
var closest = splineY.Select(n => new { n, distance = Math.Abs(n - 51) })
.OrderBy(p => p.distance)
.First().n;
//get the X value
double myVal = splineX[splineY.IndexOf(closest)];
//Plot the chart
chart1.Series.Clear();
chart1.Legends.Clear();
Series actual = new Series("Actual");
actual.Points.DataBindXY(xData, yData);
actual.ChartType = SeriesChartType.Point;
actual.ChartArea = "ChartArea1";
//
Series smooth = new Series("Smooth");
smooth.Points.DataBindXY(splineX, splineY);
smooth.ChartType = SeriesChartType.Line;
smooth.ChartArea = "ChartArea1";