Chart / Code Embedding
In This Topic
    Code Embedding
    In This Topic

    The component allows you to modify every aspect of the visualized chart image through custom C# code. This feature is useful when the settings exposed through the designer are not enough to produce the desired chart appearance. This topic describes how to take advantage of this feature. Note that the code used here uses the Nevron Chart for .NET API, so some familiarity with it is recommended.

    The component designer contains a tab called "Code" that allows you to type code, configure custom parameters and add referenced assemblies that can be used by your code. The following sections describe how to achieve these goals. The sample code below assumes that your are working with the XY Scatter Point example shipped with the control.

    Adding Code with Entry Point

    The easiest way to start using the code embedding feature is to load an entry point template:

    1. Go to the Code tab

    2. Select File\New

    3. Select "Entry Point" from the templates list and click Ok.

    The designer will load a simple C# code that contains an entry point that the component will call at runtime:

    C#
    Copy Code
    using System;
    using System.Drawing;
    using Nevron.GraphicsCore;
    using Nevron.Chart;
    using Nevron.ReportingServices.Template;
    namespace MyNamespace
    {
        /// <summary>
        /// Sample class
        /// </summary>
        public class MyClass
        {
            /// <summary>
            /// Main entry point
            /// </summary>
            /// <param name="context"></param>
            public static void RSMain(NRSChartCodeContext context)
            {
            }
        }
    }
    

    Adding Custom Parameters

    Custom parameters are name/value pairs that allow you to associate a name with a SSRS expression. The expression is evaluated at runtime and this allows for a convenient usage of the SSRS expression power inside custom code you write. To add a custom code parameter follow these steps:

    1. On the Code tab select the Build\Code Parameters menu item. It shows the "Code Parameters" editor. There you can type Name, Value pairs. The Value represents an SSRS expression that will be evaluated at runtime.

    2. Add two new pairs:

    Name Value
    AverageX =Avg(Fields!Temperature.Value)
    AverageY =Avg(Fields!Time.Value)

    Extending the Code

    1. Extend the code with the following:

    C#
    Copy Code
    using System;
    using System.Drawing;
    using Nevron.GraphicsCore;
    using Nevron.Chart;
    using Nevron.ReportingServices;
    namespace MyNamespace
    {
        /// <summary>
        /// Sample class
        /// </summary>
        public class MyClass
        {
            /// <summary>
            /// Main entry point
            /// </summary>
            /// <param name="context"></param>
            public static void RSMain(NRSChartCodeContext context)
            {
                if (context.Document.Charts.Count == 0)
                    return;
                // get the first chart in the document
                NChart chart = context.Document.Charts[0];
                if (chart.Series.Count == 0)
                    return;
                NPointSeries point = chart.Series[0] as NPointSeries;
                if (point == null)
                    return;
    
                double averageX = context.GetDoubleParameter("AverageX");
                double averageY = context.GetDoubleParameter("AverageY");
    
                // add const lines for the average x and y values
                NAxis xAxis = chart.Axis((int)StandardAxis.PrimaryX);
                NAxis yAxis = chart.Axis((int)StandardAxis.PrimaryY);
                // Add a constline for the left axis
                NAxisConstLine averageYConstLine = chart.Axis(StandardAxis.PrimaryY).ConstLines.Add();
                averageYConstLine.Value = averageY;
                // Add a constline for the bottom axis
                NAxisConstLine averageXConstLine = chart.Axis(StandardAxis.PrimaryX).ConstLines.Add();
                averageXConstLine.Value = averageX;
            }
        }
    }
    

    The "context" parameter passed to the main entry point has a property called Document. This property allows you to access the chart document generated by the SSRS chart template. The document fully exposes the Nevron Chart for .NET API and allows for full control over the chart appearance.

    Note that the code above gets the computed values for AverageX and AverageY using the GetDoubleParamer function of the context. The following table shows the the functions you can use to get typed parameters from SSRS:

    Function Description
    GetBooleanParameter Returns a named parameter, converted to boolean
    GetInt32Parameter Returns a named parameter, converted to Int32
    GetInt64Parameter Returns a named parameter, converted to Int64
    GetSingleParameter Returns a named parameter, converted to Single
    GetDoubleParameter Returns a named parameter, converted to Double
    GetStringParameter Returns a named parameter, converted to String