Added ShowPercent, ShowCategory, NoLegend

This commit is contained in:
dfinke
2015-12-26 14:50:57 -05:00
parent c17a272f1e
commit 0cee3a5576
3 changed files with 81 additions and 22 deletions

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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}
}
}