How do I get interpolated y-value between 2 data points?


#1

Here is a screenshot from my application http://take.ms/SDFyl

My x-axis is a SChartDateTimeAxis with weekly data points. My y-axis is a normal SChartNumberAxis. The interpolated values look alright on the chart.

I’d like to get a y-value given any x-value (date/time) at any point on the chart programmatically without relying on a crosshair. How do I do it?


#2

Hi,

You should be able to achieve this using the SChartPixelToPointMapper.

// Your known date on the x axis
NSDate *givenDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 5];
    
// Get the pixel value of this data value on the x axis (in terms of the point on the plot area view)
CGFloat pixelValue = [chart.xAxis pixelValueForDataValue:givenDate];
    
// Convert this point to be in terms of the chart as a whole (as this is what the pixel to point mapper expects)
CGPoint pointOnChart = [chart convertPoint:CGPointMake(pixelValue, 0) fromView:chart.canvas.glView];
    
// Now create the mapper that will allow us to retrieve the interpolated data point
SChartPixelToPointMapper *mapper = [[SChartPixelToPointMapper alloc] init];
    
SChartDataPoint *interpolatedDataPoint = [mapper mappingForPoint:pointOnChart onSeries:chart.series[0] onChart:chart].dataPoint;

I hope that helps!

Sam


#3

Yes! That works.

Thanks a lot Sam!


#4

Great, glad to have been able to help  smiley grin

Sam


#5

This problem is common enough. I think it should be posted somewhere more visible.

I’ve been struggling with it for 3 months.

Btw, the Search feature on this site is totally broken, always return no results. I can’t even search for simple keyword like ‘chart’ or ‘interpolate’.


#6

Hi toringuyen,

I’m sorry the search feature on our developer is currently down.

We have been doing some work behind the scenes and this seems to have been unintentioanlly affected.

We are currently working on fixing this issue and getting the search feature back online so forum posts like this one go back to being easy to find.

Let me know if you have any questions.

Kind regards,
Andrew Polkinghorn