New-UDChart - Set the yAxes to start at zero

I tried to research the Chart.js documentation and found this:

https://www.chartjs.org/docs/latest/axes/cartesian/linear.html

I think that I am supposed to use the -Options parameter on the New-UDChart cmdlet, but I cannot determine how to construct a PowerShell hashtable that gives me a zero based bar chart. I tried something like the following:

$ChartOptions = @{
beginAtZero = $true
}

Am I even close? Thanks for any help!

-Jim

You need to tell ChartJS which axis to begin at zero. Here’s an example

$db = New-UDDashboard -Title "BeginAtZero" -Content {
    New-UDChart -Type Line -Endpoint {
        1..10 | % { 
            [PSCustomObject]@{
                Name = $_
                Random = Get-Random -Minimum 1000 -Maximum 10000
            } 
        }   | Out-UDChartData -DataProperty Random -LabelProperty Name
     
    } -Options @{
        scales = @{
            yAxes = @(
                @{
                    ticks = @{
                        beginAtZero = $true
                    }
                }
            )
        }
    }
}

Start-UDDashboard -Port 1111 -Dashboard $db

image

2 Likes

Thanks Adam. Your example works for sure. However, I think I uncovered a situation where it does not work. If you use the new “-Height” and “-Width” parameters on the New-UDChart cmdlet in version 2.1.0, then it seems to ignore the “beginAtZero” option.

1 Like

@adam, should I file an issue on Github?

@Jim Yes, please. That sounds like a bug.

would there be an equivalent option to set a maximum value (for instance if an API has a 10k call limit, set an upper bound on the y-axis

Edit: I just needed to go read the chartJS docs :stuck_out_tongue: this did it.

-Options @{scales = @{yAxes = @(@{ticks = @{beginAtZero = $true; suggestedMax=10000 } }) } }

1 Like