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

    For Unit code types, the CodeText property of a function must contain the full compile unit text. The unit must include the class "Calculator" in the UserFunction namespace, and must implement the ISimpleFunction interface.

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim code As String = _
    "Namespace UserFunction" & vbNewLine & _
    "  Class Calculator" & vbNewLine & _
    "    Implements ISimpleFunction" & vbNewLine & _
      "    Public Function Calculate(x As Double) As Double _" & vbNewLine & _
      "      Implements ISimpleFunction.Calculate" & vbNewLine & _
      "      Dim x2 As Double = x*x" & vbNewLine & _
      "      if( x<0)" & vbNewLine & _
      "        return -x" & vbNewLine & _
      "      else" & vbNewLine & _
      "        return -0.5*x2" & vbNewLine & _
      "      End If" & vbNewLine & _
      "    End Function" & vbNewLine & _
      "  End Class" & vbNewLine & _
      "End Namespace"
     
    Dim yf As C1.Win.C1Chart.YFunction = New C1.Win.C1Chart.YFunction()
     
    yf.CodeType = C1.Win.C1Chart.FunctionCodeTypeEnum.Unit
    yf.CodeLanguage = C1.Win.C1Chart.FunctionCodeLanguageEnum.VB
    yf.CodeText = code
     
    yf.MinX = -5
    yf.MaxX = 5
    yf.LineStyle.Color = Color.Green
    yf.LineStyle.Thickness = 3
     
    C1Chart1.ChartGroups(0).ChartData.FunctionsList.Add(yf)
    

    To write code in C#

    C#
    Copy Code
    string code =
    "namespace UserFunction" +
    "{" +
    "  class Calculator : ISimpleFunction" + 
    "  {" +
    "    public double Calculate(double x)" + 
    "    {" + 
    "      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.Unit;
    yf.CodeText = code;
     
    yf.MinX = -5;
    yf.MaxX = 5;
    yf.LineStyle.Color = Color.Green;
    yf.LineStyle.Thickness = 2;
     
    c1Chart1.ChartGroups[0].ChartData.FunctionsList.Add( yf);
    
    See Also