Conditionally including scales in the -Options section for a chart

I am using a loop to pass a collection of data for charts on my dashboard’s home page. I am struggling to figure out how to conditionally include the scales portion below only when $Metric.ChartType is equal to ‘Bar’ (i.e. so it gets excluded from other charts such as Doughnut charts). Most PowerShell code, such as an IF statement, placed in the -Options section throws an error as it appears to be looking for UD DSL. Below the code is a sample of the data being passed. Currently the XenApp section shows total connections but I’d like to break it down to Active and Disconnected, stacked in a single bar per server (i.e. the data passed will change once I can conditionally include/exclude the scale\stacked section).

   New-UDLayout -Columns 4 {
        foreach ($Metric in $AllMetrics) {
            $Data = $ServerMetrics | Where-Object {$_.Name -eq $Metric.Name}
            New-UDRow -Columns {
                New-UDColumn -Size 12 -Content {
                    New-UDChart -Title $Metric.Name -Type $Metric.ChartType  -Endpoint {
                        $Data | Out-UDChartData -LabelProperty "Label" -Dataset @(
                            $newUDChartDatasetSplat = @{
                                Label                = 'Label'
                                DataProperty         = 'Value'
                                BackgroundColor      = $Data.Color
                                HoverBackgroundColor = "#00B2A9"
                            }
                            New-UDChartDataset @newUDChartDatasetSplat 
                        )
                    } -Options @{  
                        legend = @{  
                            display = $false
                        }
	                         scales = @{
						    xAxes = @(
							  @{
								  stacked = $true
							  }
						    )
						    yAxes = @(
							  @{
								  stacked = $true
							  }
						  )
					  }
                    }
                }
            }
        }
    }
}

Data sample:

Name            ChartType Color      Label           Value
----            --------- -----      -----           -----
CPU             Doughnut  Red        90-100%		 0
CPU             Doughnut  Yellow     80-89%          0
CPU             Doughnut  LightGreen 30-79%          8
CPU             Doughnut  Green      0-29%           98
RAM             Doughnut  Red        90-100%         0
RAM             Doughnut  Yellow     80-89%          0
RAM             Doughnut  LightGreen 30-79%          8
RAM             Doughnut  Green      0-29%           98
XenApp Sessions Bar       Green      01N05           3
XenApp Sessions Bar       Green      01N06           6
XenApp Sessions Bar       Green      01N09           6
XenApp Sessions Bar       Green      01N10           5
XenApp Sessions Bar       Green      01N11           6
XenApp Sessions Bar       Green      01N12           6
XenApp Sessions Bar       Green      01N13           6
XenApp Sessions Bar       Green      01N20           8
XenApp Sessions Bar       Green      01N21           7

Would something like this work? Do the conditional logic upfront and build the options in a $ChartOptions variable to be used when calling the New-UDChart cmdlet?

if ($Metric.ChartType -eq 'Bar') {
    $ChartOptions = @{
        legend = @{  
            display = $false
        }
                scales = @{
            xAxes = @(
                @{
                    stacked = $true
                }
            )
            yAxes = @(
                @{
                    stacked = $true
                }
            )
        }
    }
} else {
    $ChartOptions = @{}
}

New-UDLayout -Columns 4 {
    foreach ($Metric in $AllMetrics) {
        $Data = $ServerMetrics | Where-Object {$_.Name -eq $Metric.Name}
        New-UDRow -Columns {
            New-UDColumn -Size 12 -Content {
                New-UDChart -Title $Metric.Name -Type $Metric.ChartType  -Endpoint {
                    $Data | Out-UDChartData -LabelProperty "Label" -Dataset @(
                        $newUDChartDatasetSplat = @{
                            Label                = 'Label'
                            DataProperty         = 'Value'
                            BackgroundColor      = $Data.Color
                            HoverBackgroundColor = "#00B2A9"
                        }
                        New-UDChartDataset @newUDChartDatasetSplat 
                    )
                } -Options $ChartOptions
            }
        }
    }
}
2 Likes

That worked perfectly. Thanks!

Awesome, great to hear. :+1: