Horizontally Centred Dynamic Images in SSRS

Posted on May 9, 2008. Filed under: SSRS | Tags: , , |

Centring Dynamic Logos in SSRS

This was an interesting problem I ran into the other day, basically I have a report that displays data on a range of businesses. The report then needed a dynamic header to display the logos for each of the businesses that feature in the report. The number of businesses could vary anywhere from 1 to 11 depending on the parameters selected.

In my case I was using images stored along side the reports but you could achieve exactly the same thing using images from a database.

Getting the images on the report

The image to display was derived from one of the other columns in my dataset so I added a derived field called ImageURI;



As tables are only capable of grouping in a vertical direction and I wanted to display the logos across the top of the page I opted for a Matrix. After adding a Matrix to the report and setting the appropriate dataset the next step was to add a column grouping for our ImageURI field;



I then dragged an image control into the header cell of the column (click finish to get out of the wizard) and then in the properties pane set the source to be external and the value to be the ImageURI field;



Now when we run the report we get all the appropriate logos displayed across the top of the page, the next step is to evenly space them to fill the entire page width.



Centring the Images

Now the options for modifying the layout of a Matrix are reasonably limited, primarily because we have no idea how many columns there are going to until run time and cells will automatically re-size to fit whatever data is thrown at them.

Based on this I used the “Padding” attribute of the image to force each cell to grow enough to fill the width of the page. I targeted the left padding of each image which will result in adding a bunch of white space to the left of each cell and pushing the image to the right hand side.
This is the original equation I used;

=(460 / (CountDistinct(Fields!ImageURI.Value, "Products")) - 80).ToString & "pt"
Note: 460 is the total width of my page and 80 is the width of each image



Now all I needed to do was get rid of the padding on the first image, to do this I modified my expression to only apply the padding if this was not the first grouping in the matrix, I achieved this using the RunningValue function as follows;

=iif(RunningValue(1, Sum, "matrix2 ") > 1,((380 / (CountDistinct(Fields!ImageURI.Value, "Products") - 1)) - 80).ToString & "pt", "0pt")

The result is nicely centred images as required. Obviously this approach only works if you have fixed width images (80px in my case), if not you will need to take the width of each image into account in the padding formula, a project for another day.



About these ads

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

    About

    Rowan works as a Program Manager for the ADO.NET Entity Framework team at Microsoft. He speaks at technical conferences and blogs at romiller.com. Rowan lives in Seattle, Washington with his wife Athalie. Prior to moving to the US he resided in the small state of Tasmania in Australia. Outside of technology Rowan's passions include snowboarding, mountain biking, horse riding, rock climbing and pretty much anything else that involves being active. The primary focus of his life, however, is to follow Jesus.

    RSS

    Subscribe Via RSS

    • Subscribe with Bloglines
    • Add your feed to Newsburst from CNET News.com
    • Subscribe in Google Reader
    • Add to My Yahoo!
    • Subscribe in NewsGator Online
    • The latest comments to all posts in RSS

    Meta

Liked it here?
Why not try sites on the blogroll...

Follow

Get every new post delivered to your Inbox.

Join 147 other followers

%d bloggers like this: