Udostępnij za pośrednictwem


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:

=Code.GetColor(Fields!Series.Value)

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

CustomColorPalette.rdl

Comments

  • Anonymous
    June 28, 2005
    A frequently asked question is, 'how does one customise the colours in a chart?'  Below is a link...
  • Anonymous
    October 11, 2005
    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.
  • Anonymous
    February 09, 2006
    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?
  • Anonymous
    February 09, 2006
    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.....
    Kewl

  • Anonymous
    February 14, 2006
    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?
    cheers
  • Anonymous
    April 02, 2006
    Same here, actually when I preview the report it works perfect, when I deploy the report the colors only work in the ledgend.

  • Anonymous
    May 29, 2006
    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?
  • Anonymous
    July 16, 2006
    The comment has been removed
  • Anonymous
    August 30, 2006
    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.
  • Anonymous
    June 08, 2007
    I am unable to view the sample report. The link is not working. Can you help? Thanks.
  • Anonymous
    June 09, 2007
    I've re-attached the sample report.
  • Anonymous
    August 14, 2007
    Steven, 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
  • Anonymous
    August 14, 2007
    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.