Tutorial: Add a Bar Chart to Your Report (Report Builder)
A bar chart displays category data horizontally. This can help to:
Improve readability of long category names.
Improve understandability of times plotted as values.
Compare the relative value of multiple series.
The following illustration shows the bar chart that you will create, with sales for 2008 and 2009 for the top five salespeople, in alphabetical order.
What You Will Learn
In this tutorial you will learn how to do the following:
Note
In this tutorial, the steps for the wizard are consolidated into one procedure. For step-by-step instructions about how to browse to a report server, create a dataset, and choose a data source, see the first tutorial in this series: Tutorial: Creating a Basic Table Report (Report Builder).
Estimated time to complete this tutorial: 15 minutes.
Requirements
For more information about requirements, see Prerequisites for Tutorials (Report Builder).
1. Create a Chart Report from the Chart Wizard
From the Getting Started dialog box, create an embedded dataset, choose a shared data source, and create a bar chart by using the Chart Wizard.
Note
In this tutorial, the query contains the data values so that it does not need an external data source. This makes the query quite long. In a business environment, a query would not contain the data. This is for learning purposes only.
To create a new chart report
Click Start, point to Programs, point to Microsoft SQL Server 2012 Report Builder, and then click Report Builder.
The Getting Started dialog box appears.
Note
If the Getting Started dialog box does not appear, click the Report Builder button, and then click New.
In the left pane, verify that New Report is selected.
In the right pane, click Chart Wizard.
On the Choose a dataset page, click Create a dataset, and then click Next.
On the Choose a connection to a data source page, select an existing data source or browse to the report server and select a data source, and then click Next. You may need to enter a user name and password.
Note
The data source you choose is unimportant, as long as you have adequate permissions. You will not be getting data from the data source. For more information, see Alternative Ways to Get a Data Connection (Report Builder).
On the Design a query page, click Edit as Text.
Paste the following query into the query pane:
SELECT 'Luis' as FirstName, 'Alverca' as LastName, CAST(170000.00 AS money) AS SalesYear2009, CAST(150000. AS money) AS SalesYear2008 UNION SELECT 'Jeffrey' as FirstName, 'Zeng' as LastName, CAST(210000. AS money) AS SalesYear2009, CAST(190000. AS money) AS SalesYear2008 UNION SELECT 'Houman' as FirstName, 'Pournasseh' as LastName, CAST(150000. AS money) AS SalesYear2009, CAST(180000. AS money) AS SalesYear2008 UNION SELECT 'Robin' as FirstName, 'Wood' as LastName, CAST(75000. AS money) AS SalesYear2009, CAST(175000. AS money) AS SalesYear2008 UNION SELECT 'Daniela' as FirstName, 'Guaita' as LastName, CAST(170000. AS money) AS SalesYear2009, CAST(175000. AS money) AS SalesYear2008 UNION SELECT 'John' as FirstName, 'Yokim' as LastName, CAST(160000. AS money) AS SalesYear2009, CAST(195000. AS money) AS SalesYear2008 UNION SELECT 'Delphine' as FirstName, 'Ribaute' as LastName, CAST(180000. AS money) AS SalesYear2009, CAST(205000. AS money) AS SalesYear2008 UNION SELECT 'Robert' as FirstName, 'Hernady' as LastName, CAST(140000. AS money) AS SalesYear2009, CAST(180000. AS money) AS SalesYear2008 UNION SELECT 'Tanja' as FirstName, 'Plate' as LastName, CAST(150000. AS money) AS SalesYear2009, CAST(160000. AS money) AS SalesYear2008 UNION SELECT 'David' as FirstName, 'Bradley' as LastName, CAST(210000. AS money) AS SalesYear2009, CAST(180000. AS money) AS SalesYear2008 UNION SELECT 'Michal' as FirstName, 'Jaworski' as LastName, CAST(175000. AS money) AS SalesYear2009, CAST(220000. AS money) AS SalesYear2008 UNION SELECT 'Chris' as FirstName, 'Ashton' as LastName, CAST(195000. AS money) AS SalesYear2009, CAST(205000. AS money) AS SalesYear2008 UNION SELECT 'Pongsiri' as FirstName, 'Hirunyanitiwatna' as LastName, CAST(175000. AS money) AS SalesYear2009, CAST(215000. AS money) AS SalesYear2008 UNION SELECT 'Brian' as FirstName, 'Burke' as LastName, CAST(187000. AS money) AS SalesYear2009, CAST(207000. AS money) AS SalesYear2008
(Optional) Click the Run button (!) to see the data your chart will be based on.
Click Next.
2. Choose the Chart Type
You can choose from a variety of predefined chart types.
To add a column chart
On the Choose a chart type page, the column chart is the default chart type.
Click Bar, and then click Next.
On the Arrange chart fields page, there are four fields in the Available fields pane: FirstName, LastName, SalesYear2009, and SalesYear2008.
Drag LastName to the Categories pane.
Drag SalesYear2009 to the Values pane. SalesYear2009 represents the sales amount for each salesperson for the year 2009. The Values pane displays
[Sum(SalesYear2009)]
because the chart displays the aggregate for each product.Drag SalesYear2008 to the Values pane under SalesYear2009. SalesYear2008 represents the sales amount for each salesperson for the year 2008.
Click Next.
On the Choose a style page, in the Styles pane, select a style.
A style specifies a font style, a set of colors, and a border style. When you select a style, the Preview pane displays a sample of the chart with that style.
Click Finish.
The chart is added to the design surface.
Click the chart to display the chart handles. Drag the bottom-right corner of the chart to increase the size of the chart.
Click Run to preview the report.
The report displays the bar chart for sales for each sales person for the years 2008 and 2009. The length of the bar corresponds to the sales total.
3. Modify the Display of Names on the Vertical Axis
By default, only some of the values on the vertical axis appear. You can change the chart to display all categories.
To display all sales persons along the category axis of a bar chart
Switch to report design view.
Right-click the vertical axis, and then click Vertical Axis Properties.
Under Axis range and interval, in the Interval box, type 1.
Click OK.
Right-click the vertical Axis Title and clear the Show Axis Title check box.
Click Run to preview the report.
Note
If you cannot read the salesperson names on the vertical axis, you can make your chart taller or change the formatting options for the axis labels.
Display Last Name and First Name on Vertical Axis
You can change the category expression to include last name followed by first name of each sales person.
To change the category expression
Switch to report design view.
Double-click the chart to display the Chart Data pane.
In the Category Groups area, right-click [LastName], and then click Category Group Properties.
In Label, click the expression (Fx) button.
Type the following expression:
=Fields!LastName.Value & ", " & Fields!FirstName.Value
This expression concatenates the last name, a comma, and the first name.
Click OK.
Click OK.
Click Run to preview the report.
If the first names do not appear when you run the report, you can refresh the data manually. While still in preview mode, on the Run tab in the Navigation group, click Refresh.
Note
If you cannot read the salesperson names on the vertical axis, you can make your chart taller or change the formatting options for the axis labels.
4. Change the Sort Order for Names on the Vertical Axis
When you sort the data on a chart, you are changing the order of values on the category axis.
To sort the names in alphabetical order on the bar chart
Switch to report design view.
Double-click the chart to display the Chart Data pane.
In the Category Groups area, right-click [LastName], and then click Category Group Properties.
Click Sorting. The Change sorting options page displays a list of sort expressions. By default, this list has one sort expression that is the same as the original category group expression.
In Sort by, click the expression (Fx) button.
Type the following expression:
=Fields!LastName.Value & ", " & Fields!FirstName.Value
Click OK.
Back on the Category Group Properties page, in the Order drop-down list, select Z to A. This selects reverse alphabetical order so that the names appear in order from top to bottom.
Click OK.
Click Run to preview the report.
The names on the horizontal axis are sorted in reverse order, with Alerca at the top and Zeng at the bottom.
5. Move the Legend
To improve the readability of the chart values, you might want to move the chart legend. For example, in a bar chart where bars are shown horizontally, you can change the position of the legend so that it is above or below the chart area. This gives more horizontal space to the bars.
To display the legend below the chart area of a bar chart
Switch to report design view.
Right-click the legend on the chart.
Select Legend Properties.
For Legend position, select a different position. For example, set the position to the middle bottom option.
When the legend is placed at the top or bottom of a chart, the layout of the legend changes from vertical to horizontal. You can select a different layout from the Layout drop-down list.
Click OK.
Click Run to preview the report.
6. Title the Chart
To change the chart title above the chart area of a bar chart
Switch to report design view.
Select the words Chart Title at the top of the chart, and then type the following text: Sales for 2008 and 2009.
Click anywhere outside the text.
Click Run to preview the report.
7. Format and Label the Horizontal Axis
By default, the horizontal axis displays values in a general format that is automatically scaled to fit the size of the chart.
To format the numbers on the horizontal axis
Switch to report design view.
Click the horizontal axis along the bottom of the chart to select it.
On the ribbon, on the Home tab, in the Number group, click the Currency button. The horizontal axis labels change to currency.
(Optional) Remove the decimal digits. Near the Currency button, click the Decrease Decimal button twice.
Right-click the horizontal axis, and click Horizontal Axis Properties.
On the Number tab, select Show values in Thousands.
Click OK.
Right-click Axis Title and click Axis Title Properties.
In the Title text box, type Sales in thousands and click OK.
Click Run to preview the report.
The report displays the sales amount on the horizontal axis as currency in thousands, and has no decimal digits.
8. Add a Filter to Display the Top Five Values
You can add a filter to the chart to specify which data from the dataset to include or exclude in the chart.
To add a filter and display the top five values
Switch to report design view.
Double-click the chart to display the Chart Data pane.
In the Category Groups area, right-click the [LastName] field, and then click Category Group Properties.
Click Filters. The Change filters page can display a list of filter expressions. By default, this list is empty.
Click Add. A new blank filter appears.
In Expression, type [Sum(SalesYear2009)]. This creates the underlying expression
=Sum(Fields!SalesYear2009.Value)
, which you can see if you click the fx button.Verify that the data type is Text.
In Operator, select Top N from the drop-down list.
In Value, type the following expression: =5
Click OK.
Click Run to preview the report.
If the results are not filtered when you run the report, you can refresh the data manually. On the Run tab in the Navigation group, click Refresh.
The chart shows the top five salesperson names from the 2009 sales data.
9. Add a Report Title
To add a report title
On the design surface, click Click to add title.
Type Sales Bar Chart, press ENTER, and then type Top Five Sellers for 2009, so it looks like this:
Sales Bar Chart
Top Five Sellers for 2009
Select Sales Bar Chart, and click the Bold button.
Select Top Five Sellers for 2009, and in the Font section on the Home tab, set the font size to 10.
(Optional) You may need to make the Title text box taller to accommodate the two lines of text.
This title will appear at the top of the report. When there is no page header defined, items at the top of the report body are the equivalent of a report header.
Click Run to preview the report.
10. Save the Report
To save the report
Switch to report design view.
From the Report Builder button, click Save As.
In Name, type Sales Bar Chart.
Click Save.
Your report is saved on the report server.
Next Steps
You have successfully completed the Adding a Bar Chart to Your Report tutorial. To learn more about charts, see Charts (Report Builder and SSRS) and Sparklines and Data Bars (Report Builder and SSRS).
See Also
Tutorials (Report Builder)
Report Builder in SQL Server 2014