代码之家  ›  专栏  ›  技术社区  ›  Nathan DeWitt

如何在ReportingServices2005中保持图表之间的颜色一致?

  •  2
  • Nathan DeWitt  · 技术社区  · 15 年前

    我为图表创建了一个自定义调色板,使用 a technique described 在TeTNET上。

    我还有一系列钻取柱形图,在其中单击一列,它将一个参数传递给下一个图表,依此类推,给出钻取的外观。

    我的图表由三种类型的人工组成,在主图表上有三种颜色。当我向下钻取到下一个图表时,其中一些类别并没有主类别所拥有的全部三种类型的人工。所以调色板中的第一种颜色被分配给系列,即使它是上一个图表中的第二种颜色。如果可能的话,我想避免这种情况。

    因此,数据值在第一个图表上是绿色的(颜色顺序中的第二个),在下一个图表上是黄色的(颜色顺序中的第一个)。如何使图表“记住”第一个图表中的序列组总数?

    这是Reporting Services 2005。

    4 回复  |  直到 15 年前
        1
  •  2
  •   jimconstable    15 年前

    您不能使用自定义调色板修复此问题。

    您可以在数据库中为人工类型指定颜色(使用十六进制最简单)。然后在数据集中传递。然后将颜色属性设置为十六进制值。

        2
  •  1
  •   JonH    15 年前

    不幸的是,这是不可能的。我找这个已经很久了…

        3
  •  0
  •   Nathan DeWitt    15 年前

    我之所以能够解决这个问题,是因为我使用的是一个自定义调色板,它被实现为一个哈希表。我基本上序列化了这些信息,并将其传递给子报表上的隐藏参数,然后重新展开数据结构。

    这不是完美的,但它现在有效。

    ' Define some globals, including the color palette '
    Private colorPalette As String() = _
        {"#FFF8A3", "#A9CC8F", "#B2C8D9", "#BEA37A", "#F3AA79", "#B5B5A9", "#E6A5A4", _
         "#F8D753", "#5C9746", "#3E75A7", "#7A653E", "#E1662A", "#74796F", "#C4384F", _
         "#F0B400", "#1E6C0B", "#00488C", "#332600", "#D84000", "#434C43", "#B30023"}
         ' color palette pulled from SAP guidelines '
         ' http://www.sapdesignguild.org/resources/diagram_guidelines/color_palettes.html '
    Private count As Integer = 0
    Private colorMapping As New System.Collections.Hashtable()
    ' Create a custom color palette '
    Public Function GetColor(ByVal groupingValue As String) As String
        If colorMapping.ContainsKey(groupingValue) Then
            Return colorMapping(groupingValue)
        End If
        Dim c As String = colorPalette(count Mod colorPalette.Length)
        count = count + 1
        colorMapping.Add(groupingValue, c)
        Return c
    End Function
    
    ' In custom actions of the data value, set the results of this '
    ' function to the mapping parameter in the next report '
    Public Function PassColorMapping() As String
        If colorMapping.Count = 0 Then
            Return Nothing
        End If
        Try
            ' convert the hashtable to an array so it can be serialized '
            Dim objHash As Object()() = ToJaggedArray(colorMapping)
    
            ' serialize the colorMapping variable '
            Dim outStream As New System.IO.StringWriter()
            Dim s As New System.Xml.Serialization.XmlSerializer(GetType(Object()()))
            s.Serialize(outStream, objHash)
    
            ' move on to the next report '
            Return outStream.ToString()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Function
    

    我遇到了一个问题,我找不到与报告的onload事件等效的内容。因为我不知道把这个充气代码放在哪里,所以我把它贴在绘图区的背景色上。因此我总是返回“白烟”。如果我能找到合适的地方放,我会把这个换掉的。

    ' Call this function when the report loads to get the series groups '
    ' that have already been loaded into the custom color palette '
    ' Pass in the parameter used to store the color mapping '
    Public Function InflateParamMapping(ByVal paramMapping As Parameter) As String
        Try
            If paramMapping.Value Is Nothing Then
                Return "WhiteSmoke"
            ElseIf colorMapping.Count = 0 Then      
                Dim pXmlized As String = paramMapping.Value
                ' deserialize the mapping parameter '
                Dim s As New System.Xml.Serialization.XmlSerializer(GetType(Object()()))
    
                ' get the jagged array and convert to hashtable '
                Dim objHash As Object()() = DirectCast(s.Deserialize(New System.IO.StringReader(pXmlized)), Object()())
                ' stick the result in the global colorMapping hashtable '
                colorMapping = ToHashTable(objHash)
                count = colorMapping.Count
            End If
        Catch ex As Exception
    '       MsgBox(ex.Message) '
        End Try
        Return "WhiteSmoke"
    End Function
    

    ToJaggedArray() ToHashTable() 是帮助程序函数,因为哈希表在实现 IDictionary . 我当时很着急,所以我很快就把它们转换成一个数组。代码来自 Collection Serialization in ASP.NET Web Services 马克·里奇曼写的文章。我将代码从C转换为vb.net以在报告中使用。

    Public Function ToJaggedArray(ByVal ht As System.Collections.HashTable) As Object()()
        Dim oo As Object()() = New Object(ht.Count - 1)() {}
        Dim i As Integer = 0
        For EAch key As Object in ht.Keys
            oo(i) = New Object() {key, ht(key)}
            i += 1
        Next
        Return oo
    End Function
    
    Public Function ToHashTable(ByVal oo As Object()()) As System.Collections.HashTable
        Dim ht As New System.Collections.HashTable(oo.Length)
        For Each pair As Object() In oo
            Dim key As Object = pair(0)
            Dim value As Object = pair(1)
            ht(key) = value
        Next
        Return ht
    End Function
    

    现在在报告本身中,您需要做一些事情。

    • 添加对的引用 System.Xml 在里面 报表属性 在两份报告中。
    • 行动 对于父报表,将包含数据结构的参数设置为 =Code.PassColorMapping()
    • 绘图区 将此表达式作为背景: =Code.InflateParamMapping(Parameters!colorMapping)
    • 当然,在 填充 为了你的数据 系列风格 在这两个图表上都放置此表达式: =Code.GetColor(Fields!Type.Value)

    您可以根据需要为任意多的子报表继续这样做-我目前有3个级别的钻取,并且工作正常。

        4
  •  0
  •   Janus007    15 年前

    我很容易就解决了。

    在我的父报表中,我假设有12个系列字段,每个字段在图表中都有自己的颜色,在我的子报表中,我只保留图表中的所有系列,例如使用向下钻取从柱形图转到折线图,但我控制它们的可见性…

    因此,在系列属性中的子报表中->可见性中,我只添加了一个表达式: =(场)!contentType.value<gt;参数!contenttype.value)

    这样,报表只保留单击值的可见性,并隐藏所有其他值,颜色保持不变:)

    推荐文章