0

Currently I have 204 items to display as legend in webchartcontrol, my current code can only support color for around 150 items. If more than that, then error "Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index". How can i get unlimited color to be store in list array ? Please guide me on this.

My Code

    Dim colors As List(Of String) = New List(Of String)()
    Dim colorNames As String() = System.Enum.GetNames(GetType(KnownColor))
    Dim whiteBrightness As Single = Color.FromKnownColor(KnownColor.NavajoWhite).GetBrightness()
    For Each colorName As String In colorNames
        Dim KnownColor As KnownColor = CType(System.Enum.Parse(GetType(KnownColor), colorName), KnownColor)
        Dim knownColorBrightness As Single = Color.FromKnownColor(KnownColor).GetBrightness()
        If (KnownColor > KnownColor.Transparent AndAlso knownColorBrightness < whiteBrightness AndAlso colorName.IndexOf("Gray") = -1) Then
            colors.Add(colorName)

        End If
    Next

   Dim lColor As Dictionary(Of String, String) = New Dictionary(Of String, String)


        Dim colorTracker As Integer = 0
        '''''''''''''''''''''''''
        lColor.Add("Remaining", "DarkGray")


        For Each dr In dt.Rows
            Dim series1 As New Series(dr("tool").ToString(), ViewType.StackedBar)
            series1.ValueScaleType = ScaleType.Numerical
            series1.Points.Add(New SeriesPoint(dr("ID").ToString(), dr("Process")))


            ' series1.ToolTipEnabled = DevExpress.Utils.DefaultBoolean.True
            'series1.ToolTipHintDataMember = dr("tooldata").ToString()
            series1.ToolTipPointPattern = "<span style='font-size:13px'>EQPID: {A} <br/> Recipe: {S} <br/> " + dr("tooldata").ToString() + "</span>"

            chart.Series.Add(series1)
            Dim myview1 As StackedBarSeriesView = CType(series1.View, StackedBarSeriesView)
            myview1.BarWidth = 0.5
            myview1.FillStyle.FillMode = FillMode.Solid



            'CHECK if same item, assigned same color, else assigned other color
            If lColor.ContainsKey(dr("tool").ToString()) Then
                series1.View.Color = Color.FromName(lColor(dr("tool").ToString()))

            Else

                \\error happens here in line series1.View.Color, there 
                 are 204 tool items, but the length returned for colors from  
                 knowncolor is 174, that is why the index 
                 error occurs.My problem is there any other properties that
                 contain unlimited color?

                series1.View.Color = Color.FromName(colors(colorTracker))
                lColor.Add(dr("tool").ToString(), colors(colorTracker))
                colorTracker = colorTracker + 1

            End If
            ''''''''''

        Next
Vanquisher
  • 35
  • 8
  • There is no such inherent limit on a `List`. If you're getting an `IndexOutOfRangeException` then it's because you're using an index that is out of range. You need to address that specifically. For that, we need to know exactly where the exception is thrown and the state of the application when it occurs. Of course, before you post anything about the issue here, you need to debug the code using breakpoints and determine EXACTLY how the behaviour differs from your expectations. – jmcilhinney Oct 09 '19 at 01:35
  • I doubt that this has anything to do with your issue but you should be using `AndAlso` rather than `And` there. You should pretty much ALWAYS use `AndAlso` and `OrElse` rather than `And` and `Or`. In the rare cases that think you need `And` and `Or`, you're probably doing something else wrong because requiring side-effects is not great practice. – jmcilhinney Oct 09 '19 at 01:37
  • Noted, my bad for didnt address the problem properly, I have edited the post. – Vanquisher Oct 09 '19 at 02:01
  • The `KnownColor` enum is just a list of colours for which the system has a standard name. Some of them are objective colours while others are system features for which the colour can be set by the user. You are not limited to those colours. Given that a `Color` value is defined by its `R`, `G` and `B` values (ignoring transparency), you can create 255*255*255 different `Color` values if you want. Have at it. You just need to put some thought into how you will generate the number you need. That's not a programming problem because you can do it without code. – jmcilhinney Oct 09 '19 at 02:10

1 Answers1

0

ChartControl and WebChartControl support the Series Colorizer feature (available in v18.1 and later versions) that allows to define the Series color schema from a predefined set of keys or data source values. You should be able to use the built-in functionality instead of initializing the Series color manually.

AlexK
  • 128
  • 5