Show every second label on the SSRS chart X axis

Applies to: Nevron Chart for Reporting Services (SSRS 2005 and 2008)

How to show every second label on the SSRS chart X axis?

In Nevron Chart for SSRS you can use Custom Code injection to change the X or Y axis labels display sequence.



The following code example will display labels at every fourth tick for the X axis and every second tick for the Y axis:

[C#]
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)
            {
                  NChart chart = context.Document.Charts[0];
                   
                  NStandardScaleConfigurator scaleX = chart.Axis(StandardAxis.PrimaryX).ScaleConfigurator as NStandardScaleConfigurator;
                  scaleX.NumberOfTicksPerLabel = 4;
 
                  NStandardScaleConfigurator scaleY = chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator as NStandardScaleConfigurator;
                  scaleY.NumberOfTicksPerLabel = 2;
            }
      }
}

Article ID: 182, Created On: 3/8/2011, Modified: 3/9/2011

Comments (1)

Nick Minchin

I have added this code to my chart object, however am getting the following error when I go to preview the report:
NChart Rendering Failed. Exception was: An exception occured in Your Custom Code. Exception was: Object reference not set to an instance of an object. Stack Trace: at MyNamespace.MyClass.RSMain (NRSChartCodeContext context)

Any ideas?

I've since found my problem: I was using two categories which this code doesn't cater for. On that note, how would I modify this to cater for two groups? (or more?)

2/12/2013 at 5:32 AM
Christo Bahchevanov

When you have two categories, the scale will become Hierarchical scale. The following code shows how to remove every other label from the leaf labels of a hierarchical axis:

using System;
using System.Collections.Generic;
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)
{
NChart chart = context.Document.Charts[0];
NHierarchicalScaleConfigurator hScale = chart.Axis(StandardAxis.PrimaryX).ScaleConfigurator as NHierarchicalScaleConfigurator;

if (hScale == null)
{
return;
}

List<NHierarchicalScaleNode> leafNodes = new List<NHierarchicalScaleNode>();
AccumulateLeafNodes(hScale.Nodes, leafNodes);

for (int i = 1; i < leafNodes.Count; i += 2)
{
leafNodes[i].Text = string.Empty;
}
}

internal static void AccumulateLeafNodes(NHierarchicalScaleNodeCollection nodes, List<NHierarchicalScaleNode> leafNodes)
{
int count = nodes.Count;

for (int i = 0; i < count; i++)
{
NHierarchicalScaleNode node = (NHierarchicalScaleNode)nodes[i];

if (node.ChildNodes.Count == 0)
{
leafNodes.Add(node);
}
else
{
AccumulateLeafNodes(node.ChildNodes, leafNodes);
}
}
}
}
}

This will also preserve the ticks for the removed labels.

2/15/2013 at 11:51 AM