Charting.ps1

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
function DoChart {
    param(
        $targetData,
        $title,
        [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType,
        [Switch]$NoLegend,
        [Switch]$ShowCategory,
        [Switch]$ShowPercent
     )

     if($targetData[0] -is [System.ValueType]) {
         $chart = New-ExcelChart -YRange "A1:A$($targetData.count)" -Title $title -ChartType $ChartType
     } else {
         $xyRange = Get-XYRange $targetData

         $X = $xyRange.XRange.ExcelColumn
         $XRange = "{0}2:{0}{1}" -f $X,($targetData.count+1)

         $Y = $xyRange.YRange.ExcelColumn
         $YRange = "{0}2:{0}{1}" -f $Y,($targetData.count+1)

         $chart = New-ExcelChart -XRange $xRange -YRange $yRange -Title $title -ChartType $ChartType `
            -NoLegend:$NoLegend -ShowCategory:$ShowCategory -ShowPercent:$ShowPercent
     }

     $xlFile = (New-TemporaryFile).fullname -replace "tmp","xlsx"
     $targetData | Export-Excel $xlFile -ExcelChartDefinition $chart -Show -AutoSize
}

function BarChart {
    param(
        [Parameter(ValueFromPipeline=$true)]
        $targetData,
        $title,
        [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="BarStacked",
        [Switch]$NoLegend,
        [Switch]$ShowCategory,
        [Switch]$ShowPercent
    )

    Begin   { $data = @() }
    Process { $data += $targetData}

    End {
        DoChart $data $title -ChartType $ChartType `
            -NoLegend:$NoLegend -ShowCategory:$ShowCategory -ShowPercent:$ShowPercent
    }
}

function PieChart {
    param(
        [Parameter(ValueFromPipeline=$true)]
        $targetData,
        $title,
        [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="PieExploded3D",
        [Switch]$NoLegend,
        [Switch]$ShowCategory,
        [Switch]$ShowPercent
    )

    Begin   { $data = @() }
    Process { $data += $targetData}

    End {
        DoChart $data $title -ChartType $ChartType `
            -NoLegend:$NoLegend -ShowCategory:$ShowCategory -ShowPercent:$ShowPercent
    }
 }

function LineChart {
    param(
        [Parameter(ValueFromPipeline=$true)]
        $targetData,
        $title,
        [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="Line",
        [Switch]$NoLegend,
        [Switch]$ShowCategory,
        [Switch]$ShowPercent
    )

    Begin   { $data = @() }
    Process { $data += $targetData}

    End {
        DoChart $data $title -ChartType $ChartType `
            -NoLegend:$NoLegend -ShowCategory:$ShowCategory -ShowPercent:$ShowPercent
    }
}

function ColumnChart {
    param(
        [Parameter(ValueFromPipeline=$true)]
        $targetData,
        $title,
        [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="ColumnStacked",
        [Switch]$NoLegend,
        [Switch]$ShowCategory,
        [Switch]$ShowPercent
    )

    Begin   { $data = @() }
    Process { $data += $targetData}

    End {
        DoChart $data $title -ChartType $ChartType `
            -NoLegend:$NoLegend -ShowCategory:$ShowCategory -ShowPercent:$ShowPercent
    }
}