Random Color Generator for Charts

Hi guys and gals,
I figured out how to make random colors show up for charts and graphs. I got tired of manually assigning colors, only to run out and have the pie chart or bar graph start showing a white color for the ones that didn’t have colors assigned to them. Below is an example that works quite well. It generates a random hex value for a color.

Hope its useful :slight_smile:

Row -Columns {               
            New-UDColumn -Size 12 -Content { }               
            New-UDColumn -Size 12 -Content {      
              New-UDChart -Title "Items in chart" -Type Doughnut -Height 300px -Width 1 -Endpoint { 
                 # Create an ArrayList
                 $RandomColors = New-Object System.Collections.ArrayList
                 # Loop through the collection.
                 foreach ($color in ( $list)) {
                   $colour = "#{0:X6}" -f (Get-Random -Maximum 0xFFFFFF)   
                   # Add to the ArrayList. 
                   [void]$RandomColors.Add($color)
                   # Call the array.
                   $RandomColors
                 }

                $list | Out-UDChartData  -DataProperty "Count" -Label "Item" -DatasetLabel "$list" -BackgroundColor @($RandomColors) -BorderColor 'black' -HoverBackgroundColor '#FF9F0D'                  
              } 
            }
          } 

image

4 Likes