简体   繁体   English

SQL SSRS颜色代码/根据SSRS中的Tablix创建热图(基于值的Tablix行中的颜色)

[英]SQL SSRS Color Code / Create a heatmap out of a tablix in SSRS (color in tablix rows based on value)

so I created this simple tablix that looks like this 所以我创建了一个看起来像这样的简单tablix

https://i.imgur.com/njzI19Y.png

Is there any way to add an expression to this tablix so that the rows are color coded based on a base value. 有什么方法可以向此Tablix添加表达式,以便基于基值对行进行颜色编码。

So the way that I am envisioning it is, the base value is the column Avg Value and I will set the background color of that column grey. 因此,我设想的方式是,基本值是列“平均值”,我将设置该列的背景色为灰色。

Then I would want every Running AOV to be color coded according to its difference from that base value. 然后,我希望每个正在运行的AOV根据与该基本值的差异进行颜色编码。 So for example, in that tablix row 5, the base value would be $88. 因此,例如,在该Tablix行5中,基值为$ 88。

Then the RunningAOV1st is $77 so it would be a red color. 那么RunningAOV1st是$ 77,所以它将是红色。 RunningAOVTwo is $84 so it would be a very slightly light red. RunningAOVTwo是$ 84,所以它会是非常浅的红色。 Next, RunningAOVThree is $87 so it is even more lighter version of red. 接下来,RunningAOVThree的价格为87美元,因此它的红色版本更轻。 If it was something like $90, it would be a very light shade of green. 如果价格是90美元,那将是非常浅的绿色阴影。

Is this possible? 这可能吗? I'm just assuming I need to put some expression for the background color of the cell, but I am not sure what the expression would be. 我只是假设需要为单元格的背景色添加一些表达式,但是我不确定该表达式是什么。

This answer take a slightly different approach to yours. 这个答案与您的答案略有不同。 I had a similar scenario and opted to do the colour calculation in SQL. 我有一个类似的情况,选择在SQL中进行颜色计算。 I just found it easier that way, although you could convert this to a custom code function in your report quite easily I think. 我认为这样做比较容易,尽管我认为您可以很轻松地将其转换为报表中的自定义代码功能。

Here's a sample of a dataset from a report that coloured cells based on the age of some data. 这是报告中的数据集示例,该报告根据某些数据的使用期限对单元格进行了着色。 The age has already bee precaluclated so the AGe column referenced here is just a number. 这个年龄已经被人们预知了,所以这里引用的AGe栏只是一个数字。 If the age was over 90 days then it would always show as solid red. 如果年龄超过90天,它将始终显示为纯红色。

There's part of the dataset query 数据集查询的一部分

SELECT 
     lp.*
    , 255 as Red-- Red: always 255
    , 255 - (Age/3) AS Green -- Green: gives range from 255 - 225 for 0 to 90 days. Anything over 90 will be set to 255 in next statement
    , 254 - (Age * (254/90)) AS Blue -- Blue: Give range of 254 - 0 for 0 to 90 days
    , cast(NULL as varchar (7)) AS HexColour
INTO #t 
 FROM #d lp
    JOIN Dim.Geography g ON lp.CountryID = g.CountryID


UPDATE #t SET HexColour =  [fn].[ConvertRGBValuesToHexColour] (Red, CASE WHEN Green <225 THEN 225 ELSE Green END , Blue) 

SELECT * FROM #t

The above calculated values for red green and blue based on the age column, we also add a column to stored the final hex value and update that based on a function (below). 上面根据age列计算出的红色,绿色和蓝色值,我们还添加了一个列来存储最终的十六进制值,并根据函数(如下)对其进行更新。 The idea is that the HexColor column can be referenced directly as the color property in the report design. 这个想法是可以在报表设计中直接将HexColor列作为color属性引用。

Below is the function to convert the RGB values into a hex value for consumption in the report. 以下是将RGB值转换为十六进制值以供报告使用的函数。

CREATE FUNCTION [fn].[ConvertRGBValuesToHexColour] (@R int, @G int, @B int)

RETURNS varchar(7)
AS
BEGIN
    RETURN '#' + RIGHT(CONVERT(VARCHAR(11), CONVERT(BINARY(1),@R,0) + CONVERT(BINARY(1),@G,0) +CONVERT(BINARY(1),@B,0) , 1),6)
END

You could right click on the text-box for that data and open the Properties. 您可以右键单击该数据的文本框,然后打开“属性”。 Then on the Fill tab click the "fx" and open the Expression box. 然后在“填充”选项卡上单击“ fx”,然后打开“表达式”框。 Then set the background color based on an IIF statement for the value you wish. 然后根据IIF语句设置所需的背景色。 在此处输入图片说明

I would suggest to have a table to define the color coding based on a min and max value. 我建议有一个表格来定义基于最小值和最大值的颜色编码。 Created a temp table to show how it could be done, i didn't get all the combinations you have, but using a sample dataset 创建了一个临时表来显示它是如何完成的,我没有获得所有的组合,但是使用了一个样本数据集

SQL used 使用SQL

        select * INTO #Data
        from (
        Select 1 AS Counts,75 AS Value,'Total Orders' AS Category UNION ALL
        Select 1 AS Counts,250 AS Value,'Avg Orders' AS Category UNION ALL
        Select 1 AS Counts,10 AS Value,'Avg Value' AS Category UNION ALL
        Select 1 AS Counts,13 AS Value,'Running 1st' AS Category UNION ALL
        Select 1 AS Counts,'' AS Value,'Running 2nd' AS Category UNION ALL
        Select 1 AS Counts,'' AS Value,'Running 3rd' AS Category UNION ALL
        Select 2 AS Counts,23 AS Value,'Total Orders' AS Category UNION ALL
        Select 2 AS Counts,46 AS Value,'Avg Orders' AS Category UNION ALL
        Select 2 AS Counts,30 AS Value,'Avg Value' AS Category UNION ALL
        Select 2 AS Counts,34 AS Value,'Running 1st' AS Category UNION ALL
        Select 2 AS Counts,'' AS Value,'Running 2nd' AS Category UNION ALL
        Select 2 AS Counts,'' AS Value,'Running 3rd' AS Category UNION ALL
        Select 3 AS Counts,'' AS Value,'Total Orders' AS Category UNION ALL
        Select 3 AS Counts,23 AS Value,'Avg Orders' AS Category UNION ALL
        Select 3 AS Counts,55 AS Value,'Avg Value' AS Category UNION ALL
        Select 3 AS Counts,67 AS Value,'Running 1st' AS Category UNION ALL
        Select 3 AS Counts,77 AS Value,'Running 2nd' AS Category UNION ALL
        Select 3 AS Counts,'' AS Value,'Running 3rd' AS Category  ) a

        select * INTO #Colors
        from (
        select 'Green' color, 1 min_value , 50 max_value  UNION ALL
        select 'Yellow' color, 51 min_value , 100 max_value   UNION ALL
        select 'Red' color, 101 min_value , 1000 max_value 
        ) b

        select  a.Category, a.Counts, a.Value, b.color
        from    #Data a
        left join   #Colors b
                on a.Value between b.min_value and b.max_value

        drop table #Colors
        drop table #Data

This will give the output with the color you want to have 这将为输出提供您想要的颜色

input dataset is 输入数据集为

在此处输入图片说明

output would look like this 输出看起来像这样

在此处输入图片说明

if you have a physical table you would have the flexibility to change color using the table. 如果您有物理表,则可以灵活地使用表更改颜色。

Design window 设计窗口

在此处输入图片说明

background expression added on the color value 在颜色值上添加了背景表达式

在此处输入图片说明

So I don't see any answers here based on an actual expression you can use to do this. 因此,根据可用于执行此操作的实际表达式,我在此处看不到任何答案。 I'll say up front, this is going to be somewhat complex and will probably require some tweaking on your end to get the values correct. 我先要说,这将有些复杂,可能需要对端进行一些调整才能使值正确。 You'll want to use a SWITCH to make this as simple as possible and simply specify a range that you want for each color. 您将希望使用SWITCH使其尽可能简单,并只需为每种颜色指定所需的范围。 Assuming your AOVRunning values are all populated from the same column in the DB, the following expression should work in the background color property. 假设所有AOVRunning值均从数据库的同一列中填充,则以下表达式应在背景色属性中起作用。

=SWITCH(Fields!AOV.Value > (Fields!Avg.Value + 10), "Green",
Fields!AOV.Value >= (Fields!Avg.Value + 5) AND Fields!AOV.Value <= (Fields!Avg.Value + 10), "LightGreen",
Fields!AOV.Value = Fields!Avg.Value, "Yellow",
Fields!AOV.Value <= (Fields!Avg.Value - 5) AND Fields!AOV.Value >= (Fields!Avg.Value -10), "LightRed",
Fields!AOV.Value < (Fields!Avg.Value - 10), "Red",
true, "White")

This is just comparing each AOV value to the average with ranges associated to that average. 这只是将每个AOV值与平均值以及与该平均值关联的范围进行比较。 Obviously, you can add as many colors as you like to this based on the spread you choose -- this was just an example. 显然,您可以根据选择的跨度向其中添加尽可能多的颜色-这只是一个示例。 The final true, "White" just catches any leftovers that don't meet any of the other conditional comparisons. 最终的true, "White"仅捕获不满足任何其他条件比较的所有剩菜。 You could also just add an IIF outside the SWITCH to eliminate those results before entering the SWITCH ... 您也可以在SWITCH外部添加一个IIF以消除这些结果,然后再进入SWITCH ...

=IIF(Fields!AOV.Value Is Nothing, "White", SWITCH(....))`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM