Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

How to Create an Index Line Chart in SQL Server Reporting Services

$
0
0

By: Koen Verbeeck || Related Tips: > Reporting Services Charts

Problem

I want to create a line chart in SQL Server Reporting Services. I have two series I need to plot on the chart, but one series has high values while the other series has much lower values. The second series is being pushed flat against the X-axis by the first series. How can I plot both series without losing detail in both series? I’d like to compare the growth or change between the series.

Solution

When plotting two-line series with a big difference in absolute values on the same chart, the series with the lowest values will lose detail, as you can see in the following chart:


How to Create an Index Line Chart in SQL Server Reporting Services

Here the line for the sales amount of the Accessories category are plotted against the X-axis, since the Y-axis has a very large scale. This makes it seem the Accessories sales approach zero, while in reality they are not. They are multiple possible solutions:

Adding a scale break, although this is not considered to be a best practice Plotting the two line series on two separate charts. It’s easy to compare the two lines with each other, both it takes up more space on your report. Using a dual axis on the same chart, which can lead to misleading interpretations Reformatting the data into an indexed chart. Here the data is indexed using the first data point. It allows to easily compare the trends of two data sets.

In this tip, we’ll focus on the last option while also showing the alternatives.

Creating an Index Line Chart Test Data

In this tip we’ll use the AdventureWorks 2017 data warehouse, which can be downloaded from Github . We’ll retrieve the online sales for the categories Bikes and Accessories of the year 2013. Bikes have very high revenue (since the price is much higher), while the revenue of accessories is considerably lower. The following query satisfies our requirement:

SELECT
[Category] = c.[EnglishProductCategoryName]
,[YearMonth] = YEAR([OrderDate]) * 100 + MONTH([OrderDate])
,[SalesAmount] = SUM([SalesAmount])
FROM [dbo].[FactInternetSales] f
JOIN [dbo].[DimProduct] p ON .[ProductKey] = [f].[ProductKey]
JOIN [dbo].[DimProductSubcategory] s ON [s].[ProductSubcategoryKey] = .[ProductSubcategoryKey]
JOIN [dbo].[DimProductCategory] c ON [c].[ProductCategoryKey] = [s].[ProductCategoryKey]
WHERE c.[EnglishProductCategoryName] IN ('Bikes','Accessories')
AND YEAR([OrderDate]) = 2013
GROUP BY YEAR([OrderDate]) * 100 + MONTH([OrderDate])
,[c].[EnglishProductCategoryName]
ORDER BY [YearMonth], [Category];

The result set is as follows:


How to Create an Index Line Chart in SQL Server Reporting Services
Creating a Simple Line Chart

Let’s start by turning this data into a line chart. Create a new report, with a connection to the AdventureWorks data warehouse and add a new data set using the query from the previous section. Right-click on the report canvas and choose Insert > Chart .


How to Create an Index Line Chart in SQL Server Reporting Services

In the dialog, choose the line chart type.


How to Create an Index Line Chart in SQL Server Reporting Services

Use the following configuration for the chart data:


How to Create an Index Line Chart in SQL Server Reporting Services

With default settings (and a decent title), we get the line chart from the introduction:


How to Create an Index Line Chart in SQL Server Reporting Services

On the horizontal axis, the interval is changed from Auto to 1 . This makes sure that all months are displayed on the axis.


How to Create an Index Line Chart in SQL Server Reporting Services

If you want to enable a scale break, right-click on the vertical axis and go to its properties. In the Axis Options pane, select the Enable scale breaks option.


How to Create an Index Line Chart in SQL Server Reporting Services

The chart now looks like this:


How to Create an Index Line Chart in SQL Server Reporting Services

The line series for the Accessories now shows all detail. However, the chart becomes more cluttered by the not-so-subtle scale break “rip”. Furthermore, viewers of the chart might be tempted to compare the two lines in absolute values. However, the horizontal axis is now quite misleading. In the bottom part, the gridlines are closer together and the difference between the minimum and maximum value is a bit over 40,000. In the top part, the gridlines have more space between and the difference between the min and max value is about 1,000,000! A huge difference although the two lines look the same.

Plotting the two lines on separate chart will also allow you to compare evolution, but it suffers from the same shortcomings since the axis will have the same scale issues.

Using a Dual Axis

Before we can create a chart with dual axis, we need to make some adjustments to the query, so we have two measures (one for Bikes, one for Accessories) for each month:

WITH CTE_source AS
(
SELECT
[YearMonth] = YEAR([OrderDate]) * 100 + MONTH([OrderDate])
,[SalesAmountAccessoires] = IIF(c.[EnglishProductCategoryName] = 'Bikes',NULL,[SalesAmount])
,[SalesAmountBikes] = IIF(c.[EnglishProductCategoryName] = 'Accessories',NULL,[SalesAmount])
FROM [dbo].[FactInternetSales] f
JOIN [dbo].[DimProduct] p ON .[ProductKey] = [f].[ProductKey]
JOIN [dbo].[DimProductSubcategory] s ON [s].[ProductSubcategoryKey] = .[ProductSubcategoryKey]
JOIN [dbo].[DimProductCategory] c ON [c].[ProductCategoryKey] = [s].[ProductCategoryKey]
WHERE c.[EnglishProductCategoryName] IN('Bikes','Accessories')
AND YEAR([OrderDate]) = 2013
)
SELECT
[YearMonth]
,[SalesAmountAccessoires] = SUM([SalesAmountAccessoires])
,[SalesAmountBikes] = SUM([SalesAmountBikes])
FROM [CTE_source]
GROUP BY [YearMonth];

First, we calculate the two measures in the inner query by setting the value to NULL if it’s one category or the other. In the outer query, we aggregate those measures by the month. The results look like this:


How to Create an Index Line Chart in SQL Server Reporting Services

Add a new data set to the report using this query. Add a new line chart and configure the chart data as in the following screenshot:


How to Create an Index Line Chart in SQL Server Reporting Services

Right-click the line of the Bike series and go to the Series Properties .


How to Create an Index Line Chart in SQL Server Reporting Services

In the Axes and Chart Area pane , set the vertical axis to Secondary .


How to Create an Index Line Chart in SQL Server Reporting Services

This results in the following chart:


How to Create an Index Line Chart in SQL Server Reporting Services
There are some issues with this type of

Viewing all articles
Browse latest Browse all 3160

Trending Articles