# calculating average value of a spline in an given interval

1. ##### Mikk
8 posts
Member since:
May 2013

Posted 03 May 2013 Link to this post

Hello everyone,

maybe thats more a mathematical question but i have a spline series for which i want to calculate the average value for in an given interval. what would be the best way to do this?
is there for example an way to get the interpolated value of the series at a certain position?

like for example i have the datapoints (x,y):
(13:15, 200), (13:57, 154),..., (18:31, 177)
now i like to know whats the average value between 15:30 and 17:30 o'clock .. which can be an interval in which i might have no 'real' values at all but only interpolated ones..

2. ##### Petar Kirov
425 posts

Posted 08 May 2013 Link to this post

Hi Mikk,

The RadChartView does not provide a built-in way to calculate the average value of a given interval, because the chart gives the WPF framework a set of control points and then the framework draws the actual Spline segments between them, over which you can't  have control.

One way you could go about this is to create a simple method that iterates over the chart ItemsSource and calculates the average value, like this:
`public ``class ``PlotInfo`
`{`
`    ``public ``DateTime X { ``get``; ``set``; }`
`    ``public ``double ``Y { ``get``; ``set``; }`
`}`

`public ``double ``CalculateAverage(List<PlotInfo> data, DateTime start, DateTime end)`
`{`
`    ``double ``sum = 0;`
`    ``int ``count = data.Count;`

`    ``int ``firstIndex = 0, lastIndex = data.Count - 1;`

`    ``while ``(data[firstIndex].X < start)`
`        ``firstIndex++;`
`    ``while ``(data[lastIndex].X > end)`
`        ``lastIndex++;`

`    ``if ``(firstIndex >= lastIndex)`
`    ``{`
`        ``sum = data[firstIndex].Y + data[lastIndex].Y;`
`        ``count = 2;`
`    ``}`

`    ``for ``(``int ``i = firstIndex; i <= lastIndex; i++)`
`    ``{`
`        ``sum += data[i].Y;`
`    ``}`

`    ``return ``sum / count;`
`}`

Or if you need to display an interpolated value at certain location, as a result of user interaction, you can use the ChartView conversion API which allows you to convert the mouse position to RadCartesianChart coordinates. I have attached a sample project demonstrating that. In the lower left corner you can see mouse position in RadCartesianChart coordinates.
The project is built in Silverlight, but since the API of the control is the same between WPF and SL you can just copy and paste the code to a WPF project.

I hope this helps.

Regards,
Petar Kirov
the Telerik team

3. ##### Mikk
8 posts
Member since:
May 2013

Posted 09 May 2013 Link to this post

Hello,

thanks for the reply already. But i think this will not help me. I agree that i can calculate the average for an list of values like this in some conditions. This would work surely on graphs with many even distributed values. But in my case this is not given. Especially i will have to calculate averages of time intervals in which not a single plotinfo value is available.

I would need something like your second part of the answer. But i need to get the interpolated values by code .. not via a mouse position.
Then i can get a value for every 5 minutes or so and then use the way you posted to calculate the average.

simple example:
Point1 8:00 - 100
Point2 12:00 - 200

now i need to get the value for time 10:00 (=150 in this example).
is there a way in the conversion api for that? I can not find such in the documentation..
4. ##### Petar Kirov
425 posts

Posted 14 May 2013 Link to this post

Hi Mikk,

I am afraid that RadChartView does not provide an API for getting the actual spline value at a given x position.

We are sorry for the inconvenience caused.

Regards,
Petar Kirov
the Telerik team

5. ##### Mikk
8 posts
Member since:
May 2013

Posted 17 May 2013 Link to this post

hmm thats bad news .. we would need this really.

can you give us a hint about how/where the spline values itself are calculated? maybe we can copy the functionality from the sources to calculate any value on demand by our own in an extra method?
6. ##### Petar Kirov
425 posts

Posted 22 May 2013 Link to this post

Hi Mikk,

Could you please open a formal support ticket, so we can provide you the details about the Spline
rendering algorithm in a private conversation.

Thanks for the understanding.

Regards,
Petar Kirov
Telerik

7. ##### Mikk
8 posts
Member since:
May 2013

Posted 22 May 2013 Link to this post

Hi Petar,

thank you for the offer. Meanwhile i found already another solution by just using my "own" interpolation algorithm and calculating a datapoint per minute to fill the chartline with it. The performance is absolutely fine and i have everything i need for additional calculations as well.

thanks a lot for your support.
Mikk