Initial pass at adding ChartTrendLine

This commit is contained in:
dfinke
2019-06-13 15:38:28 -04:00
parent bb1b413ada
commit 3ce485a144

View File

@@ -97,11 +97,12 @@
#> #>
[Alias("New-ExcelChart")] #This was the former name. The new name reflects that we are defining a chart, not making one in the workbook. [Alias("New-ExcelChart")] #This was the former name. The new name reflects that we are defining a chart, not making one in the workbook.
[cmdletbinding()] [cmdletbinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '',Justification='Does not change system State')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Does not change system State')]
param( param(
$Title = "Chart Title", $Title = "Chart Title",
$Header, $Header,
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = "ColumnStacked", [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = "ColumnStacked",
[OfficeOpenXml.Drawing.Chart.eTrendLine[]]$ChartTrendLine,
$XRange, $XRange,
$YRange, $YRange,
$Width = 500, $Width = 500,
@@ -138,11 +139,12 @@
$YMinValue, $YMinValue,
[OfficeOpenXml.Drawing.Chart.eAxisPosition]$YAxisPosition [OfficeOpenXml.Drawing.Chart.eAxisPosition]$YAxisPosition
) )
if ( $Header ) {Write-Warning "The header parameter is ignored."} #Nothing was done with it when creating a chart. if ( $Header ) { Write-Warning "The header parameter is ignored." } #Nothing was done with it when creating a chart.
#might be able to do [PSCustomObject]$PsboundParameters, the defaults here match those in Add-Excel Chart #might be able to do [PSCustomObject]$PsboundParameters, the defaults here match those in Add-Excel Chart
[PSCustomObject]@{ [PSCustomObject]@{
Title = $Title Title = $Title
ChartType = $ChartType ChartType = $ChartType
ChartTrendLine = $ChartTrendLine
XRange = $XRange XRange = $XRange
YRange = $YRange YRange = $YRange
Width = $Width Width = $Width
@@ -151,14 +153,14 @@
RowOffSetPixels = $RowOffSetPixels RowOffSetPixels = $RowOffSetPixels
Column = $Column Column = $Column
ColumnOffSetPixels = $ColumnOffSetPixels ColumnOffSetPixels = $ColumnOffSetPixels
LegendPosition = $LegendPosition LegendPosition = $LegendPosition
LegendSize = $LegendSize LegendSize = $LegendSize
Legendbold = $LegendBold Legendbold = $LegendBold
NoLegend = $NoLegend -as [Boolean] NoLegend = $NoLegend -as [Boolean]
ShowCategory = $ShowCategory -as [Boolean] ShowCategory = $ShowCategory -as [Boolean]
ShowPercent = $ShowPercent -as [Boolean] ShowPercent = $ShowPercent -as [Boolean]
SeriesHeader = $SeriesHeader SeriesHeader = $SeriesHeader
TitleBold = $TitleBold -as [Boolean] TitleBold = $TitleBold -as [Boolean]
TitleSize = $TitleSize TitleSize = $TitleSize
XAxisTitleText = $XAxisTitleText XAxisTitleText = $XAxisTitleText
XAxisTitleBold = $XAxisTitleBold -as [Boolean] XAxisTitleBold = $XAxisTitleBold -as [Boolean]
@@ -170,7 +172,7 @@
XMinValue = $XMinValue XMinValue = $XMinValue
XAxisPosition = $XAxisPosition XAxisPosition = $XAxisPosition
YAxisTitleText = $YAxisTitleText YAxisTitleText = $YAxisTitleText
YAxisTitleBold = $YAxisTitleBold -as [Boolean] YAxisTitleBold = $YAxisTitleBold -as [Boolean]
YAxisTitleSize = $YAxisTitleSize YAxisTitleSize = $YAxisTitleSize
YAxisNumberformat = $YAxisNumberformat YAxisNumberformat = $YAxisNumberformat
YMajorUnit = $YMajorUnit YMajorUnit = $YMajorUnit
@@ -326,24 +328,25 @@ function Add-ExcelChart {
and is marked off in units of 0.25 shown to two decimal places. and is marked off in units of 0.25 shown to two decimal places.
The key will for the chart will be at the bottom in 8 point bold type and the line will be named "Sin(x)". The key will for the chart will be at the bottom in 8 point bold type and the line will be named "Sin(x)".
#> #>
[cmdletbinding(DefaultParameterSetName='Worksheet')] [cmdletbinding(DefaultParameterSetName = 'Worksheet')]
[OutputType([OfficeOpenXml.Drawing.Chart.ExcelChart])] [OutputType([OfficeOpenXml.Drawing.Chart.ExcelChart])]
param( param(
[Parameter(ParameterSetName='Workshet',Mandatory=$true)] [Parameter(ParameterSetName = 'Workshet', Mandatory = $true)]
[OfficeOpenXml.ExcelWorksheet]$Worksheet, [OfficeOpenXml.ExcelWorksheet]$Worksheet,
[Parameter(ParameterSetName='PivotTable',Mandatory=$true)] [Parameter(ParameterSetName = 'PivotTable', Mandatory = $true)]
[OfficeOpenXml.Table.PivotTable.ExcelPivotTable]$PivotTable , [OfficeOpenXml.Table.PivotTable.ExcelPivotTable]$PivotTable ,
[String]$Title, [String]$Title,
#$Header, Not used but referenced previously #$Header, Not used but referenced previously
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = "ColumnStacked", [OfficeOpenXml.Drawing.Chart.eChartType]$ChartType = "ColumnStacked",
[OfficeOpenXml.Drawing.Chart.eTrendLine[]]$ChartTrendLine,
$XRange, $XRange,
$YRange, $YRange,
[int]$Width = 500, [int]$Width = 500,
[int]$Height = 350, [int]$Height = 350,
[int]$Row = 0, [int]$Row = 0,
[int]$RowOffSetPixels = 10, [int]$RowOffSetPixels = 10,
[int]$Column = 6, [int]$Column = 6,
[int]$ColumnOffSetPixels = 5, [int]$ColumnOffSetPixels = 5,
[OfficeOpenXml.Drawing.Chart.eLegendPosition]$LegendPosition, [OfficeOpenXml.Drawing.Chart.eLegendPosition]$LegendPosition,
$LegendSize, $LegendSize,
[Switch]$LegendBold, [Switch]$LegendBold,
@@ -372,11 +375,11 @@ function Add-ExcelChart {
$YMinValue, $YMinValue,
[OfficeOpenXml.Drawing.Chart.eAxisPosition]$YAxisPosition, [OfficeOpenXml.Drawing.Chart.eAxisPosition]$YAxisPosition,
[Switch]$PassThru [Switch]$PassThru
) )
try { try {
if ($PivotTable) { if ($PivotTable) {
$Worksheet = $PivotTable.WorkSheet $Worksheet = $PivotTable.WorkSheet
$chart = $Worksheet.Drawings.AddChart(("Chart" + $PivotTable.Name ),$ChartType,$PivotTable) $chart = $Worksheet.Drawings.AddChart(("Chart" + $PivotTable.Name ), $ChartType, $PivotTable)
} }
else { else {
$ChartName = 'Chart' + (Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.', '' $ChartName = 'Chart' + (Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.', ''
@@ -384,75 +387,83 @@ function Add-ExcelChart {
$chartDefCount = @($YRange).Count $chartDefCount = @($YRange).Count
if ($chartDefCount -eq 1) { if ($chartDefCount -eq 1) {
$Series = $chart.Series.Add($YRange, $XRange) $Series = $chart.Series.Add($YRange, $XRange)
if ($SeriesHeader) { $Series.Header = $SeriesHeader} if ($ChartType -notmatch "stacked|3D$|pie|Doughnut|Cone|Cylinder|Pyramid") {
else { $Series.Header = 'Series 1'} foreach ($trendLine in $ChartTrendLine) {
$null = $Series.TrendLines.Add($trendLine)
}
}
else {
Write-Warning "Chart trend line is not supported for chart type: $ChartType"
}
if ($SeriesHeader) { $Series.Header = $SeriesHeader }
else { $Series.Header = 'Series 1' }
} }
else { else {
for ($idx = 0; $idx -lt $chartDefCount; $idx += 1) { for ($idx = 0; $idx -lt $chartDefCount; $idx += 1) {
if ($Yrange.count -eq $xrange.count) { if ($Yrange.count -eq $xrange.count) {
$Series = $chart.Series.Add($YRange[$idx], $XRange[$idx]) $Series = $chart.Series.Add($YRange[$idx], $XRange[$idx])
} }
else { else {
$Series = $chart.Series.Add($YRange[$idx], $XRange) $Series = $chart.Series.Add($YRange[$idx], $XRange)
} }
if ($SeriesHeader.Count -gt 0) { if ($SeriesHeader.Count -gt 0) {
if ($SeriesHeader[$idx] -match '^=') {$Series.HeaderAddress = $SeriesHeader[$idx] -replace '^=',''} if ($SeriesHeader[$idx] -match '^=') { $Series.HeaderAddress = $SeriesHeader[$idx] -replace '^=', '' }
else {$Series.Header = $SeriesHeader[$idx] } else { $Series.Header = $SeriesHeader[$idx] }
} }
else { $Series.Header = "Series $($idx)"} else { $Series.Header = "Series $($idx)" }
} }
} }
} }
if ($Title) { if ($Title) {
$chart.Title.Text = $Title $chart.Title.Text = $Title
if ($TitleBold) {$chart.Title.Font.Bold = $true} if ($TitleBold) { $chart.Title.Font.Bold = $true }
if ($TitleSize) {$chart.Title.Font.Size = $TitleSize} if ($TitleSize) { $chart.Title.Font.Size = $TitleSize }
} }
if ($NoLegend) { $chart.Legend.Remove() } if ($NoLegend) { $chart.Legend.Remove() }
else { else {
if ($PSBoundParameters.ContainsKey('LegendPosition')) {$chart.Legend.Position = $LegendPosition} if ($PSBoundParameters.ContainsKey('LegendPosition')) { $chart.Legend.Position = $LegendPosition }
if ($PSBoundParameters.ContainsKey('LegendBold')) {$chart.Legend.Font.Bold = [boolean]$LegendBold} if ($PSBoundParameters.ContainsKey('LegendBold')) { $chart.Legend.Font.Bold = [boolean]$LegendBold }
if ($LegendSize) {$chart.Legend.Font.Size = $LegendSize} if ($LegendSize) { $chart.Legend.Font.Size = $LegendSize }
} }
if ($XAxisTitleText) { if ($XAxisTitleText) {
$chart.XAxis.Title.Text = $XAxisTitleText $chart.XAxis.Title.Text = $XAxisTitleText
if ($PSBoundParameters.ContainsKey('XAxisTitleBold')) { if ($PSBoundParameters.ContainsKey('XAxisTitleBold')) {
$chart.XAxis.Title.Font.Bold = [boolean]$XAxisTitleBold $chart.XAxis.Title.Font.Bold = [boolean]$XAxisTitleBold
} }
if ($XAxisTitleSize) {$chart.XAxis.Title.Font.Size = $XAxisTitleSize} if ($XAxisTitleSize) { $chart.XAxis.Title.Font.Size = $XAxisTitleSize }
} }
if ($XAxisPosition) {Write-Warning "X-axis position is not being set propertly at the moment, parameter ignored" } if ($XAxisPosition) { Write-Warning "X-axis position is not being set propertly at the moment, parameter ignored" }
#$chart.ChartXml.chartSpace.chart.plotArea.catAx.axPos.val = $XAxisPosition.ToString().substring(0,1)} #$chart.ChartXml.chartSpace.chart.plotArea.catAx.axPos.val = $XAxisPosition.ToString().substring(0,1)}
if ($XMajorUnit) {$chart.XAxis.MajorUnit = $XMajorUnit} if ($XMajorUnit) { $chart.XAxis.MajorUnit = $XMajorUnit }
if ($XMinorUnit) {$chart.XAxis.MinorUnit = $XMinorUnit} if ($XMinorUnit) { $chart.XAxis.MinorUnit = $XMinorUnit }
if ($null -ne $XMinValue) {$chart.XAxis.MinValue = $XMinValue} if ($null -ne $XMinValue) { $chart.XAxis.MinValue = $XMinValue }
if ($null -ne $XMaxValue) {$chart.XAxis.MaxValue = $XMaxValue} if ($null -ne $XMaxValue) { $chart.XAxis.MaxValue = $XMaxValue }
if ($XAxisNumberformat) {$chart.XAxis.Format = (Expand-NumberFormat $XAxisNumberformat)} if ($XAxisNumberformat) { $chart.XAxis.Format = (Expand-NumberFormat $XAxisNumberformat) }
if ($YAxisTitleText) { if ($YAxisTitleText) {
$chart.YAxis.Title.Text = $YAxisTitleText $chart.YAxis.Title.Text = $YAxisTitleText
if ($PSBoundParameters.ContainsKey('YAxisTitleBold')) { if ($PSBoundParameters.ContainsKey('YAxisTitleBold')) {
$chart.YAxis.Title.Font.Bold = [boolean]$YAxisTitleBold $chart.YAxis.Title.Font.Bold = [boolean]$YAxisTitleBold
} }
if ($YAxisTitleSize) {$chart.YAxis.Title.Font.Size = $YAxisTitleSize} if ($YAxisTitleSize) { $chart.YAxis.Title.Font.Size = $YAxisTitleSize }
} }
if ($YAxisPosition) {Write-Warning "Y-axis position is not being set propertly at the moment, parameter ignored" } if ($YAxisPosition) { Write-Warning "Y-axis position is not being set propertly at the moment, parameter ignored" }
#$chart.ChartXml.chartSpace.chart.plotArea.valAx.axPos.val= $YAxisPosition.ToString().substring(0,1)} #$chart.ChartXml.chartSpace.chart.plotArea.valAx.axPos.val= $YAxisPosition.ToString().substring(0,1)}
if ($YMajorUnit) {$chart.YAxis.MajorUnit = $YMajorUnit} if ($YMajorUnit) { $chart.YAxis.MajorUnit = $YMajorUnit }
if ($YMinorUnit) {$chart.YAxis.MinorUnit = $YMinorUnit} if ($YMinorUnit) { $chart.YAxis.MinorUnit = $YMinorUnit }
if ($null -ne $YMinValue){$chart.YAxis.MinValue = $YMinValue} if ($null -ne $YMinValue) { $chart.YAxis.MinValue = $YMinValue }
if ($null -ne $YMaxValue){$chart.YAxis.MaxValue = $YMaxValue} if ($null -ne $YMaxValue) { $chart.YAxis.MaxValue = $YMaxValue }
if ($YAxisNumberformat) {$chart.YAxis.Format = (Expand-NumberFormat $YAxisNumberformat)} if ($YAxisNumberformat) { $chart.YAxis.Format = (Expand-NumberFormat $YAxisNumberformat) }
if ($null -ne $chart.Datalabel) { if ($null -ne $chart.Datalabel) {
$chart.Datalabel.ShowCategory = [boolean]$ShowCategory $chart.Datalabel.ShowCategory = [boolean]$ShowCategory
$chart.Datalabel.ShowPercent = [boolean]$ShowPercent $chart.Datalabel.ShowPercent = [boolean]$ShowPercent
} }
$chart.SetPosition($Row, $RowOffsetPixels, $Column, $ColumnOffsetPixels) $chart.SetPosition($Row, $RowOffsetPixels, $Column, $ColumnOffsetPixels)
$chart.SetSize($Width, $Height) $chart.SetSize($Width, $Height)
if ($PassThru) {return $chart} if ($PassThru) { return $chart }
} }
catch {Write-Warning -Message "Failed adding Chart to worksheet '$($WorkSheet).name': $_"} catch { Write-Warning -Message "Failed adding Chart to worksheet '$($WorkSheet).name': $_" }
} }