Code Injection / Chart Code Example
In This Topic
    Chart Code Example
    In This Topic

    Lets assumes that the you are displaying a XY scatter plot with the Point Chart Type. Lets also assume that your data source has two columns - X and Y - defining the data points X and Y coordinates. Also let the chart has a single category grouping expression:=Fields![RowIndex] (e.g. group by row).

    The code example creates X and Y constant lines that are positioned at the average X and average Y coordinates. The code example colorizes the points according to average quadrant to which they belong.

    To get the average values for the X and Y columns we use two code parameters:

    1. Name: AverageX, Expression: =AVG(Fields!X)
    2. Name: AverageY, Expression: =AVG(Fields!Y)

    The following code adds the constant lines to the first chart area and colorizes the points in the first point series according to the quadrant to which they belong.

    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;
    
                // get the first point series in the chart
                NPointSeries point = chart.Series[0] as NPointSeries;
                if (point == null)
                    return;
    
                // get the evaluated average X and Y
                double averageX = context.GetDoubleParameter("AverageX");
                double averageY = context.GetDoubleParameter("AverageY");
    
                // initialize quadrant colors
                Color[] quadrantColors = new Color[4];
                quadrantColors[0] = Color.Red;
                quadrantColors[1] = Color.Green;
                quadrantColors[2] = Color.Blue;
                quadrantColors[3] = Color.Yellow;
    
                // get the X and Y primary axes
                NAxis xAxis = chart.Axis(StandardAxis.PrimaryX);
                NAxis yAxis = chart.Axis(StandardAxis.PrimaryY);
    
                // add a constline for the left axis
                NAxisConstLine averageYConstLine = yAxis.ConstLines.Add();
                averageYConstLine.Value = averageY;
    
                // add a constline for the bottom axis
                NAxisConstLine averageXConstLine = xAxis.ConstLines.Add();
                averageXConstLine.Value = averageX;
    
                // colorize the data points according to quadrant
                int count = point.Values.Count;
                for (int i = 0; i < count; i++)
                {
                    int quadrant = 0;
                    double yValue = (double)point.Values[i];
                    double xValue = (double)point.XValues[i];
    
                    // determine quadrant
                    if (yValue > averageY)
                    {
                        if (xValue > averageX)
                        {
                            quadrant = 0;
                        }
                        else
                        {
                            quadrant = 1;
                        }
                    }
                    else
                    {
                        if (xValue > averageX)
                        {
                            quadrant = 3;
                        }
                        else
                        {
                            quadrant = 2;
                        }
                    }
    
                    // set point color depending on quadrant
                    point.FillStyles[i] = new NColorFillStyle(quadrantColors[quadrant]);
                }
            }
        }
    }