diff --git a/Charting.ps1 b/Charting.ps1 index 95d841d..f7fab3a 100644 --- a/Charting.ps1 +++ b/Charting.ps1 @@ -1,12 +1,15 @@ function DoChart { - param( - $targetData, + param( + $targetData, $title, - $chartType + [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 + $chart = New-ExcelChart -YRange "A1:A$($targetData.count)" -Title $title -ChartType $ChartType } else { $xyRange = Get-XYRange $targetData @@ -16,7 +19,8 @@ function DoChart { $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 + $chart = New-ExcelChart -XRange $xRange -YRange $yRange -Title $title -ChartType $ChartType ` + -NoLegend:$NoLegend -ShowCategory:$ShowCategory -ShowPercent:$ShowPercent } $xlFile = (New-TemporaryFile).fullname -replace "tmp","xlsx" @@ -24,26 +28,59 @@ function DoChart { } - function BarChart { - param($targetData,$title) +function BarChart { + param( + $targetData, + $title, + [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="BarStacked", + [Switch]$NoLegend, + [Switch]$ShowCategory, + [Switch]$ShowPercent + ) + + DoChart $targetData $title -ChartType $ChartType ` + -NoLegend:$NoLegend -ShowCategory:$ShowCategory -ShowPercent:$ShowPercent - DoChart $targetData $title BarStacked } - function PieChart { - param($targetData,$title) +function PieChart { + param( + $targetData, + $title, + [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="PieExploded3D", + [Switch]$NoLegend, + [Switch]$ShowCategory, + [Switch]$ShowPercent + ) - DoChart $targetData $title Pie + DoChart $targetData $title -ChartType $ChartType ` + -NoLegend:$NoLegend -ShowCategory:$ShowCategory -ShowPercent:$ShowPercent } - function LineChart { - param($targetData,$title) +function LineChart { + param( + $targetData, + $title, + [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="Line", + [Switch]$NoLegend, + [Switch]$ShowCategory, + [Switch]$ShowPercent + ) - DoChart $targetData $title Line - } + DoChart $targetData $title -ChartType $ChartType ` + -NoLegend:$NoLegend -ShowCategory:$ShowCategory -ShowPercent:$ShowPercent +} - function ColumnChart { - param($targetData,$title) +function ColumnChart { + param( + $targetData, + $title, + [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="ColumnStacked", + [Switch]$NoLegend, + [Switch]$ShowCategory, + [Switch]$ShowPercent + ) - DoChart $targetData $title ColumnStacked - } + DoChart $targetData $title -ChartType $ChartType ` + -NoLegend:$NoLegend -ShowCategory:$ShowCategory -ShowPercent:$ShowPercent +} \ No newline at end of file diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 329ae63..1a1514b 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -31,6 +31,9 @@ function Export-Excel { [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="Pie", [Switch]$IncludePivotTable, [Switch]$IncludePivotChart, + [Switch]$NoLegend, + [Switch]$ShowCategory, + [Switch]$ShowPercent, [Switch]$AutoSize, [Switch]$Show, [Switch]$NoClobber, @@ -270,6 +273,12 @@ function Export-Excel { if($IncludePivotChart) { $chart = $wsPivot.Drawings.AddChart("PivotChart", $ChartType, $pivotTable) + + $chart.DataLabel.ShowCategory=$ShowCategory + $chart.DataLabel.ShowPercent=$ShowPercent + + if($NoLegend) { $chart.Legend.Remove() } + $chart.SetPosition(1, 0, 6, 0) $chart.SetSize(600, 400) } @@ -304,6 +313,14 @@ function Export-Excel { $ChartName = "Chart"+(Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.','' $chart = $ws.Drawings.AddChart($ChartName, $chartDef.ChartType) $chart.Title.Text = $chartDef.Title + + if($chartDef.NoLegend) { + $chart.Legend.Remove() + } + #$chart.Datalabel.ShowLegendKey = $true + $chart.Datalabel.ShowCategory = $chartDef.ShowCategory + $chart.Datalabel.ShowPercent = $chartDef.ShowPercent + $chart.SetPosition($chartDef.Row, $chartDef.RowOffsetPixels,$chartDef.Column, $chartDef.ColumnOffsetPixels) $chart.SetSize($chartDef.Width, $chartDef.Height) diff --git a/New-ExcelChart.ps1 b/New-ExcelChart.ps1 index f427949..05591b0 100644 --- a/New-ExcelChart.ps1 +++ b/New-ExcelChart.ps1 @@ -10,7 +10,10 @@ function New-ExcelChart { $Row=0, $RowOffSetPixels=10, $Column=6, - $ColumnOffSetPixels=5 + $ColumnOffSetPixels=5, + [Switch]$NoLegend, + [Switch]$ShowCategory, + [Switch]$ShowPercent ) [PSCustomObject]@{ @@ -21,10 +24,12 @@ function New-ExcelChart { YRange=$YRange Width=$Width Height=$Height - Row=$Row RowOffSetPixels=$RowOffSetPixels Column=$Column ColumnOffSetPixels=$ColumnOffSetPixels - } + NoLegend = if($NoLegend) {$true} else {$false} + ShowCategory = if($ShowCategory) {$true} else {$false} + ShowPercent = if($ShowPercent) {$true} else {$false} + } } \ No newline at end of file