Initial commit [charting]

This commit is contained in:
dfinke
2015-12-18 12:58:42 -05:00
parent 7f67047fa2
commit 652255ca9d
8 changed files with 239 additions and 1 deletions

49
Charting.ps1 Normal file
View File

@@ -0,0 +1,49 @@
function DoChart {
param(
$targetData,
$title,
$chartType
)
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
}
$xlFile = (New-TemporaryFile).fullname -replace "tmp","xlsx"
$targetData | Export-Excel $xlFile -ExcelChartDefinition $chart -Show -AutoSize
}
function BarChart {
param($targetData,$title)
DoChart $targetData $title BarStacked
}
function PieChart {
param($targetData,$title)
DoChart $targetData $title Pie
}
function LineChart {
param($targetData,$title)
DoChart $targetData $title Line
}
function ColumnChart {
param($targetData,$title)
DoChart $targetData $title ColumnStacked
}

22
Get-ExcelColumnName.ps1 Normal file
View File

@@ -0,0 +1,22 @@
function Get-ExcelColumnName {
param(
[Parameter(ValueFromPipeline=$true)]
$columnNumber=1
)
Process {
$dividend = $columnNumber
$columnName = @()
while($dividend -gt 0) {
$modulo = ($dividend - 1) % 26
$columnName += [char](65 + $modulo)
$dividend = [int](($dividend -$modulo)/26)
}
[PSCustomObject] @{
ColumnNumber = $columnNumber
ColumnName = $columnName -join ''
}
}
}

26
Get-XYRange.ps1 Normal file
View File

@@ -0,0 +1,26 @@
function Get-XYRange {
param($targetData)
$record = $targetData| select -First 1
$p=$record.psobject.Properties.name
$infer = for ($idx = 0; $idx -lt $p.Count; $idx++) {
$name = $p[$idx]
$value = $record.$name
$result=Invoke-AllTests $value -OnlyPassing -FirstOne
[PSCustomObject]@{
Name = $name
Value = $value
DataType = $result.DataType
ExcelColumn = (Get-ExcelColumnName ($idx+1)).ColumnName
}
}
[PSCustomObject]@{
XRange = $infer | ? {$_.datatype -match 'string'} | select -First 1 excelcolumn, name
YRange = $infer | ? {$_.datatype -match 'int|double'} |select -First 1 excelcolumn, name
}
}

View File

@@ -4,6 +4,12 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll"
. $PSScriptRoot\New-ConditionalFormattingIconSet.ps1
. $PSScriptRoot\Export-ExcelSheet.ps1
. $PSScriptRoot\New-ExcelChart.ps1
. $PSScriptRoot\Invoke-Sum.ps1
. $PSScriptRoot\InferData.ps1
. $PSScriptRoot\Get-ExcelColumnName.ps1
. $PSScriptRoot\Get-XYRange.ps1
. $PSScriptRoot\Charting.ps1
. $PSScriptRoot\New-PSItem.ps1
function Import-Excel {
param(

95
InferData.ps1 Normal file
View File

@@ -0,0 +1,95 @@
function Test-String{
param($p)
[PSCustomObject]@{
Test=$p -is [string]
DataType = "string"
}
}
function Test-Date {
param($p)
[datetime]$result = [datetime]::MinValue
[PSCustomObject]@{
Test=[datetime]::TryParse($p, [ref]$result)
DataType = "datetime"
}
}
function Test-Boolean {
param($p)
#[bool]$result = [bool]::FalseString
[bool]$result = $false
[PSCustomObject]@{
Test=[bool]::TryParse($p, [ref]$result)
DataType = "bool"
}
}
function Test-Number {
param($p)
[double]$result = [double]::MinValue
[PSCustomObject]@{
Test=[double]::TryParse($p, [ref]$result)
DataType = "double"
}
}
function Test-Integer {
param($p)
[int]$result = [int]::MinValue
[PSCustomObject]@{
Test=[int]::TryParse($p, [ref]$result)
DataType = "int"
}
}
$tests = [ordered]@{
TestBoolean = Get-Command Test-Boolean
TestInteger = Get-Command Test-Integer
TestNumber = Get-Command Test-Number
TestDate = Get-Command Test-Date
TestString = Get-Command Test-String
}
function Invoke-AllTests {
param(
$target,
[Switch]$OnlyPassing,
[Switch]$FirstOne
)
$resultCount=0
$tests.GetEnumerator() | ForEach {
$result=& $_.Value $target
$testResult = [PSCustomObject]@{
Test = $_.Key
Target = $target
Result = $result.Test
DataType= $result.DataType
}
if(!$OnlyPassing) {
$testResult
} elseif ($result.Test -eq $true) {
if($FirstOne) {
if($resultCount -ne 1) {
$testResult
$resultCount+=1
}
} else {
$testResult
}
}
}
}

View File

@@ -1,6 +1,6 @@
param([string]$InstallDirectory)
$fileList = echo EPPlus.dll ImportExcel.psd1 ImportExcel.psm1 Export-Excel.ps1 New-ConditionalFormattingIconSet.ps1 Export-ExcelSheet.ps1 New-ExcelChart.ps1
$fileList = echo EPPlus.dll ImportExcel.psd1 ImportExcel.psm1 Export-Excel.ps1 New-ConditionalFormattingIconSet.ps1 Export-ExcelSheet.ps1 New-ExcelChart.ps1 Invoke-Sum.ps1 InferData.ps1 Get-ExcelColumnName.ps1 Get-XYRange.ps1 Charting.ps1 New-PSItem.ps1
if ('' -eq $InstallDirectory)
{

17
Invoke-Sum.ps1 Normal file
View File

@@ -0,0 +1,17 @@
function Invoke-Sum {
param($data,$dimension,$measure)
$h=@{}
foreach ($item in $data){
$h.$($item.$dimension)+=$item.$measure
}
foreach ($entry in $h.GetEnumerator()){
[PSCustomObject]@{
Name=$entry.key
$measure=$entry.value
}
}
}

23
New-PSItem.ps1 Normal file
View File

@@ -0,0 +1,23 @@
function New-PSItem {
$totalArgs = $args.Count
if($args[-1] -is [array]) {
$script:PSItemHeader=$args[-1]
$totalArgs-=1
}
$h=[ordered]@{}
for ($idx = 0; $idx -lt $totalArgs; $idx+=1) {
if($PSItemHeader) {
$key = $PSItemHeader[$idx]
} else {
$key = "P$($idx+1)"
}
$h.$key=$args[$idx]
}
[PSCustomObject]$h
}