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

    Instead of adding source code to the CodeText, CodeTextX or CodeTextY properties, an event delegate can be used to specify an event method to calculate the appropriate function value.

    When using event delegates, the function value is calculated in event CalculateY for the YFunction class objects. For ParametricFunction class objects, two events, CalculateX and CalculateY must be set, one for each calculated coordinate. Non-null event delegates indicate that the event should be used to calculate the corresponding function value, even if the corresponding CodeText property is also specified in either the YFunction or ParametricFunction class objects. In order to use events, the programmer must provide the appropriate event handlers.

    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()  
      AddHandler yf.CalculateY, AddressOf Function_Calculate    
      yf.MinX = -5    
      yf.MaxX = 5   
      yf.LineStyle.Color = Color.DarkBlue   
      yf.LineStyle.Thickness = 3         
      C1Chart1.ChartGroups(0).ChartData.FunctionsList.Add(yf)   
    End Sub     
        
    Private Sub Function_Calculate(ByVal sender As Object, _  ByVal e As C1.Win.C1Chart.CalculateFunctionEventArgs)    
      e.Result = e.Parameter * e.Parameter * e.Parameter    ' y = x*x*x   
    End Sub
    

    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.DarkBlue;    
      yf.LineStyle.Thickness = 2;    
      yf.CalculateY += new C1.Win.C1Chart.CalculateFunctionEventHandler( Function_Calculate);         
      c1Chart1.ChartGroups[0].ChartData.FunctionsList.Add( yf);    
    }
             
    private void Function_Calculate( object sender, C1.Win.C1Chart.CalculateFunctionEventArgs e)   
    {   
      e.Result = e.Parameter * e.Parameter * e.Parameter; // y = x*x*x    
    }
    
    See Also