mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-15 07:43:23 +00:00
Added ShowPercent, ShowCategory, NoLegend
This commit is contained in:
71
Charting.ps1
71
Charting.ps1
@@ -2,11 +2,14 @@ function DoChart {
|
|||||||
param(
|
param(
|
||||||
$targetData,
|
$targetData,
|
||||||
$title,
|
$title,
|
||||||
$chartType
|
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType,
|
||||||
|
[Switch]$NoLegend,
|
||||||
|
[Switch]$ShowCategory,
|
||||||
|
[Switch]$ShowPercent
|
||||||
)
|
)
|
||||||
|
|
||||||
if($targetData[0] -is [System.ValueType]) {
|
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 {
|
} else {
|
||||||
$xyRange = Get-XYRange $targetData
|
$xyRange = Get-XYRange $targetData
|
||||||
|
|
||||||
@@ -16,7 +19,8 @@ function DoChart {
|
|||||||
$Y = $xyRange.YRange.ExcelColumn
|
$Y = $xyRange.YRange.ExcelColumn
|
||||||
$YRange = "{0}2:{0}{1}" -f $Y,($targetData.count+1)
|
$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"
|
$xlFile = (New-TemporaryFile).fullname -replace "tmp","xlsx"
|
||||||
@@ -24,26 +28,59 @@ function DoChart {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function BarChart {
|
function BarChart {
|
||||||
param($targetData,$title)
|
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 {
|
function PieChart {
|
||||||
param($targetData,$title)
|
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 {
|
function LineChart {
|
||||||
param($targetData,$title)
|
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 {
|
function ColumnChart {
|
||||||
param($targetData,$title)
|
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
|
||||||
|
}
|
||||||
@@ -31,6 +31,9 @@ function Export-Excel {
|
|||||||
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="Pie",
|
[OfficeOpenXml.Drawing.Chart.eChartType]$ChartType="Pie",
|
||||||
[Switch]$IncludePivotTable,
|
[Switch]$IncludePivotTable,
|
||||||
[Switch]$IncludePivotChart,
|
[Switch]$IncludePivotChart,
|
||||||
|
[Switch]$NoLegend,
|
||||||
|
[Switch]$ShowCategory,
|
||||||
|
[Switch]$ShowPercent,
|
||||||
[Switch]$AutoSize,
|
[Switch]$AutoSize,
|
||||||
[Switch]$Show,
|
[Switch]$Show,
|
||||||
[Switch]$NoClobber,
|
[Switch]$NoClobber,
|
||||||
@@ -270,6 +273,12 @@ function Export-Excel {
|
|||||||
|
|
||||||
if($IncludePivotChart) {
|
if($IncludePivotChart) {
|
||||||
$chart = $wsPivot.Drawings.AddChart("PivotChart", $ChartType, $pivotTable)
|
$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.SetPosition(1, 0, 6, 0)
|
||||||
$chart.SetSize(600, 400)
|
$chart.SetSize(600, 400)
|
||||||
}
|
}
|
||||||
@@ -304,6 +313,14 @@ function Export-Excel {
|
|||||||
$ChartName = "Chart"+(Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.',''
|
$ChartName = "Chart"+(Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.',''
|
||||||
$chart = $ws.Drawings.AddChart($ChartName, $chartDef.ChartType)
|
$chart = $ws.Drawings.AddChart($ChartName, $chartDef.ChartType)
|
||||||
$chart.Title.Text = $chartDef.Title
|
$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.SetPosition($chartDef.Row, $chartDef.RowOffsetPixels,$chartDef.Column, $chartDef.ColumnOffsetPixels)
|
||||||
$chart.SetSize($chartDef.Width, $chartDef.Height)
|
$chart.SetSize($chartDef.Width, $chartDef.Height)
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,10 @@ function New-ExcelChart {
|
|||||||
$Row=0,
|
$Row=0,
|
||||||
$RowOffSetPixels=10,
|
$RowOffSetPixels=10,
|
||||||
$Column=6,
|
$Column=6,
|
||||||
$ColumnOffSetPixels=5
|
$ColumnOffSetPixels=5,
|
||||||
|
[Switch]$NoLegend,
|
||||||
|
[Switch]$ShowCategory,
|
||||||
|
[Switch]$ShowPercent
|
||||||
)
|
)
|
||||||
|
|
||||||
[PSCustomObject]@{
|
[PSCustomObject]@{
|
||||||
@@ -21,10 +24,12 @@ function New-ExcelChart {
|
|||||||
YRange=$YRange
|
YRange=$YRange
|
||||||
Width=$Width
|
Width=$Width
|
||||||
Height=$Height
|
Height=$Height
|
||||||
|
|
||||||
Row=$Row
|
Row=$Row
|
||||||
RowOffSetPixels=$RowOffSetPixels
|
RowOffSetPixels=$RowOffSetPixels
|
||||||
Column=$Column
|
Column=$Column
|
||||||
ColumnOffSetPixels=$ColumnOffSetPixels
|
ColumnOffSetPixels=$ColumnOffSetPixels
|
||||||
|
NoLegend = if($NoLegend) {$true} else {$false}
|
||||||
|
ShowCategory = if($ShowCategory) {$true} else {$false}
|
||||||
|
ShowPercent = if($ShowPercent) {$true} else {$false}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user