2D Chart for WinForms | ComponentOne
Charting Data / Plotting Functions / Calculating the Value for Functions / Calculating the Function Value Using the Calculate Method
In This Topic
    Calculating the Function Value Using the Calculate Method
    In This Topic

    As an alternative to the use of either a code string or events, the programmer may specify an instance of an object that implements C1.Win.C1Chart.ISimpleFunction interface. Such an object must inherit from the interface and implement a public function name, Calculate. The method, Calculate takes the independent variable (Double) as a parameter and returns the dependent variable (Double).

    For the YFunction class objects, the CustomFunction property must be set to the ISimpleFunction implementation object. For the ParametricFunction class object, the CustomFunctionX and CustomFunctionY properties must be set to appropriate objects implementing the ISimpleFunction interface.

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub Button_Click(ByVal sender As System.Object, _   
     ByVal e As System.EventArgs) Handles Button.Click        
      Dim yf As C1.Win.C1Chart.YFunction = New C1.Win.C1Chart.YFunction()      
      yf.CustomFunction = New CustomFunction()     
      yf.MinX = -5       
      yf.MaxX = 5       
      yf.LineStyle.Color = Color.DarkGreen        
      yf.LineStyle.Thickness = 3       
      C1Chart1.ChartGroups(0).ChartData.FunctionsList.Add(yf)      
    End Sub     
    Public Class CustomFunction     
      Implements C1.Win.C1Chart.ISimpleFunction   
      Public Function Calculate(ByVal x As Double) As Double _    
       Implements C1.Win.C1Chart.ISimpleFunction.Calculate    
        Return -x * x * x     ' y = - x*x*x   
      End Function   
    End Class
    

    To write code in C#

    C#
    Copy Code
    private void button_Click(object sender, System.EventArgs e)       
    {      
      C1.Win.C1Chart.YFunction yf = new C1.Win.C1Chart.YFunction();     
      yf.MinX = -5;      
      yf.MaxX = 5;     
      yf.LineStyle.Color = Color.DarkGreen;      
      yf.LineStyle.Thickness = 2;     
      yf.CustomFunction = new CustomFunction();    
      c1Chart1.ChartGroups[0].ChartData.FunctionsList.Add( yf);     
    }     
    class CustomFunction : C1.Win.C1Chart.ISimpleFunction     
    {     
      public double Calculate( double x)    
      {   
        return -x*x*x; // y = -x*x*x   
      }    
    }
    
    See Also