Can I use a PSCustomObject to pass values to DataProperty of a Stacked Bar Chart?

Is it possible to pass a PSCustomObject containing values to the DataProperty of a stacked bar chart? Below is some sample data and my attempt to pass the data. There is no error, I just get an empty bar chart.

Name                   ChartType ChartOptions Color                          Label                           Value
----                   --------- ------------ -----                          -----                           -----
CPU                    Doughnut               Red                            90-100%                             0
CPU                    Doughnut               Yellow                         80-89%                              0
CPU                    Doughnut               LightGreen                     30-79%                              6
CPU                    Doughnut               Green                          0-29%                             101
RAM                    Doughnut               Red                            90-100%                             0
RAM                    Doughnut               Yellow                         80-89%                              0
RAM                    Doughnut               LightGreen                     30-79%                              6
RAM                    Doughnut               Green                          0-29%                             101
XenApp Sessions        Bar       Stacked      @{Color1=Green; Color2=Yellow} 01N05           @{Value1=0; Value2=3}
XenApp Sessions        Bar       Stacked      @{Color1=Green; Color2=Yellow} 01N06           @{Value1=1; Value2=5}
XenApp Sessions        Bar       Stacked      @{Color1=Green; Color2=Yellow} 01N09           @{Value1=4; Value2=0}
XenApp Sessions        Bar       Stacked      @{Color1=Green; Color2=Yellow} 01N10           @{Value1=3; Value2=0}
XenApp Sessions        Bar       Stacked      @{Color1=Green; Color2=Yellow} 01N11           @{Value1=3; Value2=1}
XenApp Sessions        Bar       Stacked      @{Color1=Green; Color2=Yellow} 01N12           @{Value1=2; Value2=1}
XenApp Sessions        Bar       Stacked      @{Color1=Green; Color2=Yellow} 01N13           @{Value1=3; Value2=1}
XenApp Sessions        Bar       Stacked      @{Color1=Green; Color2=Yellow} 01N20           @{Value1=3; Value2=1}
XenApp Sessions        Bar       Stacked      @{Color1=Green; Color2=Yellow} 01N21           @{Value1=2; Value2=2}


if ($Metric.ChartType -eq 'Bar' -and $Metric.ChartOptions -eq 'Stacked') {
    Write-Verbose "Setting Chart Data for stacked bar charts"
    $Dataset = @()
    $Dataset += @(
    	$newUDChartDatasetSplat = @{
    		Label                = 'Active'
    		DataProperty         = 'Value.Value1'
    		BackgroundColor      = $Data.Color.Color1
    		HoverBackgroundColor = "#00B2A9"
    	}
    	New-UDChartDataset @newUDChartDatasetSplat 
    )
    $Dataset += @(
    	$newUDChartDatasetSplat = @{
    		Label                = 'Disconnected'
    		DataProperty         = 'Value.Value2'
    		BackgroundColor      = $Data.Color.Color2
    		HoverBackgroundColor = "#00B2A9"
    	}
    	New-UDChartDataset @newUDChartDatasetSplat 
    )            
} else {
    $Dataset = @(
    	$newUDChartDatasetSplat = @{
    		Label                = 'Label'
    		DataProperty         = 'Value'
    		BackgroundColor      = $Data.Color
    		HoverBackgroundColor = "#00B2A9"
    	}
    	New-UDChartDataset @newUDChartDatasetSplat 
    )
}

You have different object variable names: $Metric and $Data Is that correct? Are you using something like $Metric = import-Csv -Path ./mydata.csv

See this snippet to help understand $Metric and $Data. $ServerMetrics contains the data listed in my original post (i.e. Name, ChartType, ChartOptions, Color, Label, Value).

$AllMetrics = $ServerMetrics | Select-Object Name, ChartType, ChartOptions | Sort-Object -Unique -Property Name
foreach ($Metric in $AllMetrics) {
    $Data = $ServerMetrics | Where-Object {$_.Name -eq $Metric.Name}
	# stuff
}

If that’s the case then shouldn’t your splat hashtable contain:

DataProperty = $Data.Value.Value1

etc…

I thought that too but when I tried that it returns an error that it is expecting a string:

New-UDPage : Exception calling "Invoke" with "0" argument(s): "Cannot process argument transformation on parameter ‘DataProperty’. Cannot convert value to type System.String."

I was going to mention this before, but I didn’t because it may be painful. I think you need split your actual data values from your formatting options. I think it will simplify things as far as logic goes (I know it confused me quite a bit). :slight_smile: But in the meantime, another suggestion is to move this logic inside the New-UDChart section. This helps you avoid building an array of UDChartDatasets. Also, try to set the Dataproperty and BackgroundColor values outside the splat hashtable to ensure they are the proper types.