<# .SYNOPSIS Creates a line chart from cvs data .DESCRIPTION Creates a fancy (more or less...) line chart reading a csv file (a _real_ 'comma separated values'-file, not the thing which is generated by excel. The chart is written to a png file. This thing has been created to automaticly report the utilization of our horizon view pools to my boss. .PARAMETER CsvPath Specifies full path to the csv file. .PARAMETER Caption Specifies the chart's caption. .PARAMETER MetricFields Specifies the fields within the csv file which contain the y-axis data .PARAMETER DateField Specifies the name of the field containing date .PARAMETER DateFieldFormat Specifies the input format for parsing the dare file. For a list of available format specifiers, see DateTimeFormatInfo Class .PARAMETER ImagePath Specifies the path where the image file is written. If the file exists, it will be overwritten. .PARAMETER ImageHeight Specifies the desired height of the image .PARAMETER ImageWidth Specifies the desired width of the image .PARAMETER AxisXIsLogarithmic If Present, the y-axis will be drawn logarithmic .NOTES Author: kurator | lagerhaus128.ch Changelog: Revision 0: Initial revision .EXAMPLE Convert-CsvToChart.ps1 -CsvPath "c:\temp\20160503_poolusage.csv" -Caption "VDI pool statistics 20160403-20160503" -MetricFields @("POOL01","POOL02","POOL03","POOL04","POOL05","POOL06") -DateField "Date" -DateFieldFormat "yyyyMMdd" -ImagePath "c:\temp\out.png" .COMPONENT #> #Requires -Version 4.0 param( [Parameter(Mandatory=$true)][string]$CsvPath, [Parameter(Mandatory=$true)][string]$Caption, [Parameter(Mandatory=$true)][array]$MetricFields, [Parameter(Mandatory=$true)][string]$DateField, [Parameter(Mandatory=$false)][string]$DateFieldFormat = "dd.MM.yyyy hh:mm", [Parameter(Mandatory=$true)][string]$ImagePath, [Parameter(Mandatory=$false)][int]$ImageHeight = 1500, [Parameter(Mandatory=$false)][int]$ImageWidth = 1000, [switch]$AxisYIsLogarithmic ) Try{ [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization") } catch { Write-Error "The assembly System.Windows.Forms.DataVisualization Assembly is not available." exit 1 } Try{ $data = Import-Csv $CsvPath $chart = New-Object System.Windows.Forms.DataVisualization.Charting.Chart $chart.width = $ImageHeight $chart.Height = $ImageWidth $chart.Left = 40 $chart.top = 30 $chart.Name = $Caption [void]$chart.Titles.Add($Caption) [void]$chart.ChartAreas.Add((New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea)) [void]$chart.Legends.Add((New-Object System.Windows.Forms.DataVisualization.Charting.Legend)) foreach ($Metric in $MetricFields) { [void]$chart.Series.Add($Metric) $chart.Series[$Metric].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Line $chart.Series[$Metric].XValueType = [System.Windows.Forms.DataVisualization.Charting.ChartValueType]::DateTime $chart.ChartAreas[0].AxisY.IsLogarithmic = $AxisYIsLogarithmic foreach ($row in $data) { [void]$chart.Series[$Metric].Points.Add((New-Object System.Windows.Forms.DataVisualization.Charting.DataPoint(($([datetime]::ParseExact([string]$row.$DateField, $DateFieldFormat, $null)).ToOADate()), [double]$($row.$Metric).Replace(',','.')))) } } $chart.SaveImage($ImagePath, "PNG") Remove-Variable chart,data } catch { Write-Error $_.Exception.Message }