Page 1 of 1

Calculated serie

Posted: Tue Nov 02, 2010 1:19 pm
by 9533057
Hi,

How can I chart a serie, defined by a function based on others series ?

For example: I have 3 series (each serie does not have the same number of points).
I would like to chart Serie4 = (Serie1+(0.15*Serie2)-0.25*Serie3)/2
If series 1 2 3 are updated (new points added), Serie4 must reflect changes

Thanks for you help

Regards,

Guilz

Re: Calculated serie

Posted: Tue Nov 02, 2010 1:30 pm
by narcis
Hi Guilz,

You can use custom function for that. You'll find an example at All Features\Welcome!\Functions\Extended\Custom y=f(x) in the features demo available at TeeChart's program group.

Re: Calculated serie

Posted: Tue Nov 02, 2010 2:16 pm
by 9533057
Hi Narcis,

I am afraid the example is not very helpful - I like to do this by code and I do not find any example of creating custom function yet.
In the code given as example:

Code: Select all

Private Sub TChart1_OnFunctionCalculate(ByVal SeriesIndex As Long, ByVal X As Double, Y As Double)
  Y = Sin(X / 10)
End Sub
It is working if you have 1 serie but how to adapt this for 3 series ?
Must I specify my series as datasource like this:

Code: Select all

TChart1.Series(0).DataSource = "Series1, Series2, Series3"
A little project should be very appreciate Narcis because this subject is not very clear when you try to do this by code... :D

Regards,

Guilz

Re: Calculated serie

Posted: Tue Nov 02, 2010 3:55 pm
by 10050769
Hello Guilz,

I have made for you a simple example using 2 series. Could you please, tell us if next code, works as you want?

Code: Select all

Private Sub Form_Load()
  TChart1.Aspect.View3D = False
 With TChart1
  'Add 2 data Series
  .AddSeries scLine
  .AddSeries scLine
  ' Populate them with data (here random)
  .Series(0).FillSampleValues 25
  .Series(1).FillSampleValues 25
  .AddSeries scLine
  .Series(2).SetFunction tfCustom
  .Series(2).DataSource = "Series0,Series1"
End With
End Sub
Private Sub TChart1_OnFunctionCalculate(ByVal SeriesIndex As Long, ByVal X As Double, Y As Double)
 Y = X * 2
End Sub
If you want more information, you can see ActiveX Tutorials, concretely Tutorial 7: Working with Functions.

I hope will helps.
Thanks,

Re: Calculated serie

Posted: Tue Nov 02, 2010 4:21 pm
by 9533057
Hi Sandra,

I am sorry but your example is not what I am looking for (I would like to work with 3 series as input and create the result serie using the function : Serie0 = (Serie1+(0.15*Serie2)-0.25*Serie3)/2 in my example

Code: Select all

TChart1.Series(1).FillSampleValues 15
TChart1.Series(2).FillSampleValues 10
TChart1.Series(3).FillSampleValues 5
TChart1.Series(0).SetFunction tfCustom
TChart1.Series(0).FunctionType.asCustom.NumPoints = 15
TChart1.Series(0).DataSource = "Series1, Series2, Series3"
Here I would like to have in serie0 the result of my function ((Serie1+(0.15*Serie2)-0.25*Serie3)/2)

I read the tutorial 7 and at the end the Deriving custom functions is similar for what I am looking for BUT I use teechart ActiveX (not VCL) and so it can not be done like this...

Thanks a lot for your help and patience :D

Regards,

Guilz

Re: Calculated serie

Posted: Wed Nov 03, 2010 9:30 am
by narcis
Hi Guilz,

To achieve what you need custom function would need, at least, ValueIndex parameter in the OnCalculate event too. I'll add this to the wish-list (TV52015259) to be considered for inclusion in future releases.

In the meantime you can add a series without any function that plots its values from existing series values, for example:

Code: Select all

Private Sub Form_Load()
    With TChart1
        For i = 0 To 2
            .AddSeries scBar
            .Series(i).FillSampleValues 15
        Next
        
        .AddSeries scLine
        
        For j = 0 To .Series(0).Count - 1
            X = .Series(0).XValues.Value(j)
            Y = (.Series(0).YValues.Value(j) + (0.15 * .Series(2).YValues.Value(j)) - (0.25 * .Series(2).YValues.Value(j))) / 2
            .Series(3).AddXY X, Y, "", clTeeColor
        Next
    End With
End Sub