-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathExport-MermaidXY.ps1
More file actions
74 lines (67 loc) · 2.25 KB
/
Export-MermaidXY.ps1
File metadata and controls
74 lines (67 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<#
.SYNOPSIS
Generates a Mermaid XY bar/line chart for the values of a series of properties.
.FUNCTIONALITY
Mermaid Diagrams
.INPUTS
System.Object with the properties requested.
.OUTPUTS
System.String containing Mermaid XY chart data.
.LINK
https://mermaid.js.org/syntax/xyChart.html
.EXAMPLE
Get-Item Save-*.ps1 |Export-MermaidXY.ps1 -Title "Save scripts" -Units bytes -LabelProperty Name -BarProperty Length
xychart-beta
title "Save scripts"
x-axis "" [Save-PodcastEpisodes.ps1, Save-Secret.ps1, Save-WebRequest.ps1]
y-axis "bytes" 0 --> 3239
bar "Length" [2754, 3239, 3112]
#>
#Requires -Version 7
[CmdletBinding()] Param(
# The property to use as labels on the X axis.
[string] $LabelProperty,
# Properties to use to render a line graph.
[string[]] $LineProperty = @(),
# Properties to use to render a bar graph.
[string[]] $BarProperty = @(),
# A title for the chart.
[string] $Title,
# The label for the X axis.
[Alias('Domain','Progression')][string] $XAxisLabel,
# The label for the Y axis.
[Alias('Range','Units')][string] $YAxisLabel,
# The objects with the specified properties.
[Parameter(ValueFromPipeline=$true)][psobject] $InputObject
)
Begin
{
filter Format-Values
{
[CmdletBinding()] Param(
[Parameter(Position=0,Mandatory=$true)][psobject[]] $ChartData,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][string] $Property,
[ValidateSet('bar','line')][string] $Type
)
$Local:OFS = ', '
return "$Type `"$Property`" [$($ChartData |Select-Object -ExpandProperty $Property)]"
}
}
End
{
$data = $input
$labels = $data |
Select-Object -ExpandProperty $LabelProperty |
ForEach-Object {$_ -match '\W' ? "`"$($_ -replace '"')`"" : $_}
$minmax = $LineProperty + $BarProperty |ForEach-Object {$data |Select-Object -ExpandProperty $_} |Measure-Object -Minimum -Maximum
$min = $minmax.Minimum -gt 0 ? 0 : $minmax.Minimum
$max = $minmax.Maximum
$Local:OFS = "`n"
return @"
xychart-beta
title "$Title"
x-axis "$XAxisLabel" [$($labels -join ', ')]
y-axis "$YAxisLabel" $min --> $max
$(@($LineProperty |Format-Values -ChartData $data -Type line) + @($BarProperty |Format-Values -ChartData $data -Type bar))
"@
}