Skip to content

Commit c4fd6fc

Browse files
StartAutomatingStartAutomating
authored andcommitted
feat: Turtle.PieGraph ( Fixes PoshWeb#239 )
Fraction support
1 parent f688f51 commit c4fd6fc

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Turtle.types.ps1xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,9 +2133,49 @@ $Slices = @(
21332133
$dataPoint.Count
21342134
$richSlices = $true
21352135
}
2136+
elseif ($dataPoint -isnot [string]) {
2137+
foreach ($prop in $dataPoint.psobject.properties) {
2138+
if ($dataPoint.($prop.Name) | IsPrimitive) {
2139+
$Total += $dataPoint.($prop.Name) # add it to the total
2140+
$dataPoint.($prop.Name) -as [double] # and output that
2141+
}
2142+
}
2143+
$richSlices = $true
2144+
}
21362145
}
21372146
)
21382147

2148+
if ($Slices.Length -eq 1 -and -not $richSlices) {
2149+
2150+
# If we provide a single number, we will auto-slice the pie
2151+
# If the number is between 0 and 1, we want to show a fraction
2152+
if ($slices[0] -ge 0 -and $slices[0] -le 1) {
2153+
# Set the total to one
2154+
$total = 1
2155+
# and make two pie slices.
2156+
$slices = $slices[0], (1- $slices[0])
2157+
} else {
2158+
# Otherwise, we want mostly equal pie slices
2159+
# (mostly is in case of a decimal value)
2160+
# Get the floor of our slice,
2161+
$floor = [Math]::Floor($slices[0])
2162+
# and determine the remainder.
2163+
$remainder = $slices[0] - $floor
2164+
# Then create N equal slices.
2165+
$Slices = @(,1 * $floor)
2166+
# If there was a remainder
2167+
if ($remainder) {
2168+
# create a small slice.
2169+
$slices += $remainder
2170+
}
2171+
# Retotal our pie
2172+
$total = 0.0
2173+
foreach ($slice in $slices) {
2174+
$total += $slice
2175+
}
2176+
}
2177+
}
2178+
21392179
# Turn each numeric slice into a ratio
21402180
$relativeSlices =
21412181
foreach ($slice in $Slices) { $slice/ $total }

0 commit comments

Comments
 (0)