Perfect From Now On (Custom Chart Legends)

While the legend built into Reporting Services charts is relatively easy to use, it lacks some of the flexibility that you might like to have. One issue is that it takes up space within the chart boundaries, making the plot area smaller as the number of series grows. So how can you get more control over the legend? Roll your own!

The trick here is to replace the legend inside the chart with a table data region beside the chart bound to the same data set. First create a custom function to return a series color based on the group value. To be consistent, the function should use a hash table to map the same value to the same color:

Private colorPalette As String() = {"Green", "Blue", "Red", "Orange", "Aqua", "Teal", "Gold", "RoyalBlue", "#A59D93", "#B8341B", "#352F26", "#F1E7D6", "#E16C56", "#CFBA9B"}
Private count As Integer = 0
Private mapping As New System.Collections.Hashtable()

Public Function GetColor(ByVal groupingValue As String) As String
  If mapping.ContainsKey(groupingValue) Then
    Return mapping(groupingValue)
  End If
  Dim c As String = colorPalette(count Mod colorPalette.Length)
  count = count + 1
  mapping.Add(groupingValue, c)
  Return c
End Function

Create your chart and set the series value appropriately. Hide the legend in the chart properties dialog. Now create a table with with two columns and a single row grouped by the group expression used as the graph series. Set the color of the data point in the chart as well as the background color of the first column in the table to:


Thanks to Robert Bruckner for the work on this one. I have atttached a sample report using this technique using the Northwind sample database.


Comments (13)
  1. A frequently asked question is, ‘how does one customise the colours in a chart?’  Below is a link…

  2. Chad says:

    This is exactly what I was looking for. My graph is setup a little different than this one and my colors are not consistent. I built my graph with 5 different "values:", not one "series group:", and I’m not sure how to associate the color appropriately. But looking back at your example, I’m not sure if your colors are matching up correctly.

  3. Jennifer says:

    Looking at the example, I tried this and the colors in the table did nt match the ones in the grid… am I missing something?

  4. Jennifer - again says:

    Ok, so here’s the scoop…

    The sample code works, but not in ANY of the preview modes.

    You just have to deploy it to see it work…..


  5. Jackie says:

    I can get the colors to appear in the table legend but the pie chart does not display the appropriate colors in Report Design and Report Manager.

    Where do I set the color of the data point?


  6. Lou says:

    Same here, actually when I preview the report it works perfect, when I deploy the report the colors only work in the ledgend.

  7. Pini says:

    Is any one knows how to " Set the color of the data point in the chart " so they match the colors in the array of colors we filled?

  8. To be blunt, the legends that SQL Reporting Services 2000 offer out of the box are ***, they are…

  9. Sachin Punatar says:

    This example working fine with the table i have created for Series and it will implement colors as mentioned in the custom function.  But I am not able to set colors in each column of my chart. It has been said in this example that ‘Set the color of the data point in the chart’ but I really don’t have any idea where to place "=code.GetColor(…)" to set colors for each column. If anyone can help on this it will be gre8.

  10. Niketa Patel says:

    I am unable to view the sample report. The link is not working. Can you help? Thanks.

  11. bwelcker says:

    I’ve re-attached the sample report.

  12. karenros says:


     I am using ur custom code for Pie chart, but the colors on the pie chart and different than the colors in the legend. Can you please tell me how can i set the colors in the chart with the colors in the legend

  13. karenros says:

    Never mind i got it to work.. But now a pie chart is been divided as 40%,30%,30%

    It displays the same color for 30%.. how can i solve this.

Comments are closed.

Skip to main content