Saturday, 15 September 2012

c# - Find the maximum distance between two series in a chart -



c# - Find the maximum distance between two series in a chart -

i have got chart 2 series. want find maximum distance between chats along x-axis in given interval. solve problem calculation of distatance on given x-point in image @ x=50 enough.

i have got next code:

public void maxspacing(object chart, int series1, int series2) { chart tmpchart = (chart)chart; double distance = 0; int positon = 0; (int = 0; < tmpchart.series[series1].points.count(); i++) { if ((math.abs(tmpchart.series[series1].points[i].yvalues[0] - tmpchart.series[series2].points[i].yvalues[0])) > distance) { distance = tmpchart.series[series1].points[i].yvalues[0] - tmpchart.series[series2].points[i].yvalues[0]; } }

the problem code is, uses datapoints of both series. if amount/interval of points in series1 , series2 differs calculation not work. looking vor y-values on given x-value calculate distance.

this assumes want sort of interpolation between points if 2 series not have same number of points. simple linear interpolation should work sufficiently big number of points, , whole algorithm (in pseudo-code):

double distance = 0; series series1 = tmpchart.series[series1]; series series2 = tmpchart.series[series2]; series seriestoenumerate = series1.points.count() >= series2.points.count() ? series1 : series2; (int = 0; < series1.count(); ++i) { datapoint point1 = series1.points[i]; datapoint point2 = series2.points[i]; if (point1.x == point2.x) { distance = math.abs(point1.y - point2.y) // if greater previous distance } else { // find 2 points in series2 x values surround point1.x, phone call them point3 , point4 // interpolate between point3 , point4 find y value @ x of point1 double slope = (point4.y - point3.y) / (point4.x - point3.x); double intercept = point4.y - slope * point4.x; double y2 = slope * point1.x + intercept; distance = math.abs(point1.y - y2); // if greater previous distance } }

that's simple illustration of algorithm. you'll want clean up, error checking, create more efficient, etc.

c# charts distance mschart series

No comments:

Post a Comment