Setting line/bar colours based on Series name


https://www.nevron.com/Forum/Topic7715.aspx
Print Topic | Close Window

By Rishi Ghose - 11 Years Ago
Hi,

I'm having trouble setting the colour of a line or bar in a chart based on the series name. For example, I have a chart that plots sales for 4 products categorised by month. Each product needs to have specific colour assigned to it. How can I code this in?

I've attached an example of the chart if it helps.

Many thanks in advance,
Rishi
By Nevron Support - 11 Years Ago

Hi Rishi,

It looks like the attachment is missing from your post. Can you try posting it once again.

You can also take a look at the following KB topic: http://support.nevron.com/KB/a202/color-pie-chart-ssrs-according-to-color-each-row-the-dataset.aspx
By Rishi Ghose - 11 Years Ago

Thank you, I've added the Attachment.

The pie chart example has only one series, is it possible for you to give me an example with more than one series?

Thanks,
Rishi
By Rishi Ghose - 11 Years Ago

I came up with the following code for a Line Chart to solve the problem. However it does not seem to work, is there an obvious mistake on this?

using System;
using System.Drawing;
using System.Windows.Forms;
using Nevron.GraphicsCore;
using Nevron.Chart;
using Nevron.ReportingServices;

namespace MyNamespace
{
///
/// Sample class
///

public class MyClass
{
///
/// Main entry point
///

///
public static void RSMain(NRSChartCodeContext context)
{
NChart chart = context.Document.Charts[0];

for (int i = 0; i < chart.Series.Count; i++)
{
NSeriesBase series = chart.Series[i];

NLineSeries lineSeries = chart.Series[i] as NLineSeries;

if (series.Name.Contains("AGRALIX MONO")) {
lineSeries.FillStyle = new NColorFillStyle(Color.Red);
}

if (series.Name.Contains("CARDEX")) {
lineSeries.FillStyle = new NColorFillStyle(Color.Pink);
}

}
}
}
}
By Nevron Support - 11 Years Ago
Hi Rishi,

Yes - you need to modify the border style color for the line (fill is not used when line shape is line) - we tested with the following code and it was working OK:


using System;
using System.Drawing;
using System.Collections.Generic;
using Nevron.GraphicsCore;
using Nevron.Chart;
using Nevron.ReportingServices;

namespace MyNamespace
{
   ///
   /// Sample class
   ///

   public class MyClass
   {
      struct Name2Color
      {
         internal Name2Color(string name, Color color)
         {
            Name = name;
            Color = color;
         }
         
         internal string Name;
         internal Color Color;
      }
      ///
      /// Main entry point
      ///

      ///
      public static void RSMain(NRSChartCodeContext context)
      {
         NChart chart = context.Document.Charts[0];
         
         List name2ColorList =new List();
         
         name2ColorList.Add(new Name2Color("Revenue", Color.Blue));
         
         foreach (NLineSeries line in chart.Series)
         {
            foreach (Name2Color name2Color in name2ColorList)
            {
               if (line.Name.Contains(name2Color.Name))
               {
                  line.BorderStyle.Color = name2Color.Color;
                  break;
               }
            }
         }
      }
   }
}

Hope this helps - let us know if you meet any problems.
By Rishi Ghose - 11 Years Ago

I managed to figure this out, thanks to the pie chart example given above. The code above works for a bar chart..

For a line chart, please replace the line:

lineSeries.FillStyle = new NColorFillStyle(Color.Pink);

with

lineSeries.BorderStyle = new NStrokeStyle(2, Color.Pink);