2D Chart for WinForms | ComponentOne
Charting Data / Plotting Functions / Using a Code String to Define a Function / Method Code Types
In This Topic
    Method Code Types
    In This Topic

    For Method code types, the CodeText property of a function class must contain the body of a method that calculates the value of function and explicitly returns the value. The expected return value is of type Double. Note that VB syntax requires the inclusion of vbNewLines at the end of each statement in the code.

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim code As String = _        
     "Dim x2 As Double = x*x" & vbNewLine & _
     "if x<0 then " & vbNewLine & _    
     "  return x" & vbNewLine & _   
     "else" & vbNewLine & _    
     " return 0.5*x2" & vbNewLine & _     
     " End If"                 
    Dim yf As C1.Win.C1Chart.YFunction = New C1.Win.C1Chart.YFunction()                 
    yf.CodeType = C1.Win.C1Chart.FunctionCodeTypeEnum.Method        
    yf.CodeLanguage = C1.Win.C1Chart.FunctionCodeLanguageEnum.VB        
    yf.CodeText = code                 
    yf.MinX = -5        
    yf.MaxX = 5       
    yf.LineStyle.Color = Color.Blue        
    yf.LineStyle.Thickness = 3                 
    C1Chart1.ChartGroups(0).ChartData.FunctionsList.Add(yf)
    

    To write code in C#

    C#
    Copy Code
    string code = "double x2 = x*x;" + "if( x<0)" + "  return x;" + "else" + " return 0.5*x2;";                 
    C1.Win.C1Chart.YFunction yf = new C1.Win.C1Chart.YFunction();                
    yf.CodeType = C1.Win.C1Chart.FunctionCodeTypeEnum.Method;       
    yf.CodeText = code;        
    yf.MinX = -5;        
    yf.MaxX = 5;        
    yf.LineStyle.Color = Color.Blue;        
    yf.LineStyle.Thickness = 2;                
    c1Chart1.ChartGroups[0].ChartData.FunctionsList.Add(yf);
    
    See Also