Functions are defined and calculated with the help of the NFunctionCalculator class. You have to create an instance of this class in order to use functions:
C# |
Copy Code
|
---|---|
NFunctionCalculator functionCalc = new NFunctionCalculator();
|
Visual Basic |
Copy Code
|
---|---|
Dim functionCalc As NFunctionCalculator = New NFunctionCalculator() |
The second step is to define the data sources (arguments) for the function, which actually contain the input data for the calculation. The arguments are ordinary NDataSeries objects - most often the "Values" data series of some existing chart series (for example Line or Bar). To inform the function calculator that an NDataSeries object will participate in the function calculation you have to add it to the Arguments collection. The following code demonstrates this, assuming that bar1 and bar2 are NBarSeries objects, which are already added to the chart.
C# |
Copy Code
|
---|---|
bar1.Values.Name = "Apples"; bar2.Values.Name = "Oranges"; functionCalc.Arguments.Add(bar1.Values); functionCalc.Arguments.Add(bar2.Values); |
Visual Basic |
Copy Code
|
---|---|
bar1.Values.Name = "Apples" bar2.Values.Name = "Oranges" functionCalc.Arguments.Add(bar1.Values) functionCalc.Arguments.Add(bar2.Values) |
The third step is to define the function that will be calculated. This is done with the help of the Expression property of the function calculator. It is a string, which contains the formula of the function. For this example we will use the ADD function to add the values of the two bar series:
C# |
Copy Code
|
---|---|
functionCalc.Expression = "ADD( Apples ; Oranges )";
|
Visual Basic |
Copy Code
|
---|---|
functionCalc.Expression = "ADD( Apples ; Oranges )"
|
Now the function calculator is ready and the only thing left is to call the Calculate method, which actually produces the result of the function. It returns an NDataSeries object, which contains the result values.
C# |
Copy Code
|
---|---|
NDataSeries result = functionCalc.Calculate(); |
Visual Basic |
Copy Code
|
---|---|
Dim result As NDataSeries = functionCalc.Calculate() |
After calling the Calculate method, the "result" data series contains the result of the calculation. The Calculate method can be called multiple times with the same arguments and expression - this is useful when the values in the source data series are changed repeatedly, and you need to calculate a function with the new input values each time.
In case you have to change the arguments for the function, don't forget to call the functionCalc.Arguments.Clear() method to remove the arguments for the previous calculations.
When the calculation is done, the result data series can be used in many ways but naturally the most common task is to present the result data into a chart.