Line Charts do not respect chart options

Product: PowerShell Universal
Version: 4.2.1

Issue

There seems to be an issue where if a chart, is set to -Type 'line', it ignores the Options that are supplied and uses it’s own.

Example Setup

$Data = @(
    [PSCustomObject]@{
        Date = 'Oct 2019'
        Total = 54
    },
    [PSCustomObject]@{
        Date = 'Nov 2019'
        Total = 15
    },
    [PSCustomObject]@{
        Date = 'Dec 2019'
        Total = 5
    }
)

$DataSet = New-UDChartJSDataset -DataProperty 'Total' -AdditionalOptions @{
    fill = $false
}

$ChartSplat = @{
    Type          = 'line'
    Data          = $Data
    Dataset       = $DataSet
    LabelProperty = 'Date'
    Options = @{
        plugins               = @{
            legend = @{  
                display = $false
            }
        }
        maintainAspectRatio = $false
        responsive          = $true
    }
}

New-UDChartJS @ChartSplat

If we just inspect the hashtable that the command produces, we can see that our supplied options have been dropped in favour of different options

Name                           Value
----                           -----
data                           {[datasets, System.Collections.Hashtable[]],
chartType                      line
assetId                        
loadData                       
type                           ud-chartjs
onClick                        
id                             b2b7ca50-a2c2-4a48-9eeb-e934e6c50905
options                        {[tension, 0.4], [fill, True]}
isPlugin                       True

Simply changing the chart type to ‘bar’ and inspecting the hashtable again, we see:

Name                           Value
----                           -----
data                           {[datasets, System.Collections.Hashtable[]], 
chartType                      bar
assetId                        
loadData                       
type                           ud-chartjs
onClick                        
id                             c8b59f19-3366-47e3-8578-482e1e3f7d63
options                        {[maintainAspectRatio, False], [plugins, Syste
isPlugin                       True

Note: This happens whether a dataset is used or not

The below line from the Cmdlet definition looks sus, but I’m supplying options, and I should be in the ‘Datasets’ parameterset when using a dataset.

f ($PSCmdlet.ParameterSetName -eq 'Simple') {
        if (-not $DatasetLabel) {
            $DatasetLabel = $DataProperty
        }

        if (-not $Options -and $Type -eq 'Line') {
            $Options = @{
                fill    = $true
                tension = 0.4
            }
        }

        $Dataset += New-UDChartJSDataset -Data $Data -DataProperty $DataProperty -Label $DatasetLabel -BackgroundColor $BackgroundColor -BorderColor $BorderColor -HoverBackgroundColor $HoverBackgroundColor -HoverBorderColor $HoverBorderColor -BorderWidth $BorderWidth -AdditionalOptions $Options
    }

I change my suspicion to be the below from the New-UDChartJS cmdlet definition:

if (-not ($MyInvocation.BoundParameters.ContainsKey('LoadData'))) {
    Foreach ($datasetDef in $Dataset) {
        if ($Type -eq 'Line' -and $datasetDef.Options -eq $null) {
            $Options = @{
                fill    = $true
                tension = 0.4
            }
        }

        if ($datasetDef.DataProperty) {
            $datasetDef.data = @($Data | ForEach-Object { $_.($datasetDef.DataProperty) })
        } 
    }
}
  1. $null should be on the left hand side of that equality check - but that’s not the issue.

  2. The $datasetDef.Options is not being set if it’s null here - the main $Options of the chart are instead being overwritten.