Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Created April 25, 2017 08:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IISResetMe/106fb20a5004b4b90b6f516da7710649 to your computer and use it in GitHub Desktop.
Save IISResetMe/106fb20a5004b4b90b6f516da7710649 to your computer and use it in GitHub Desktop.
Charting tool for MeasureScript v2
using namespace System.Windows.Forms.DataVisualization.Charting
function New-PSChart
{
param(
[Parameter(Mandatory,ValueFromPipeline)]
[PSTypeName('ScriptLineMeasurement')]
[psobject[]]$TimeLine,
[Parameter()]
[switch]$Invoke,
[Parameter()]
[int]$Maximum
)
begin {
Add-Type -AssemblyName System.Windows.Forms.DataVisualization
# Create new "Timeline" chart
$Chart = [Chart]::new()
$Chart.Width = 600
$Chart.Height = 400
$Chart.BackColor = 'White'
[void]$Chart.Titles.Add('Timeline')
# Create the actual ChartArea
$ChartArea = [ChartArea]::new('MyChartArea')
$ChartArea.AxisY.MajorGrid.LineWidth = 0
$ChartArea.AxisX.MajorGrid.LineWidth = 0
$ChartArea.AxisY.Title = 'Time Taken (ms)'
# Maximum Y-axis bound set by user?
if($Maximum)
{
$ChartArea.AxisY.Maximum = $Maximum
$ChartArea.AxisY.Minimum = 0
}
$ChartArea.AxisX.Title = 'Iteration (x10)'
$ChartArea.AxisY.Interval = 1000
$ChartArea.AxisX.Interval = 10
$Chart.ChartAreas.Add($ChartArea)
# Create Legend
$Legend = [Legend]::new('MyLegend')
$Chart.Legends.Add($Legend)
$SeriesIndex = 1
}
process {
foreach($line in $TimeLine){
# Create Chart series for each line
$Series = $Chart.Series.Add($line.LineNo)
$Series.ChartType = [SeriesChartType]::Line
$Series.IsVisibleInLegend = $true
$Series.BorderWidth = 3
$Series.ChartArea = 'MyChartArea'
$Series.Legend = 'MyLegend'
$Series.Color = @('#C0C0C0','#808080','#000000','#FF0000','#800000','#FFFF00','#808000','#00FF00','#008000','#00FFFF','#008080','#0000FF','#000080','#FF00FF','#800080')[$SeriesIndex % 16]
$XIndex = 1
# Populate series with datapoints
foreach($point in $Line.TimeLine.TimeSpans.Ticks){
# Sample every 10th with an offset to avoid the first (usually slow) measurement
if($XIndex % 10 -eq 3){
[void]$Series.Points.AddXY((($XIndex + 7) / 50), $point)
}
$XIndex++
}
$SeriesIndex++
}
}
end {
# Allocate temp file and rename to png
$tempFilePath = [System.IO.Path]::GetTempFileName()
$tempFile = Get-Item $tempFilePath |Rename-Item -Path $tempFilePath -NewName {"$($_.BaseName).png"} -PassThru
# Save chart to png
$Chart.SaveImage($tempFile,[ChartImageFormat]::Png)
# Open image file or return FileInfo object
if($Invoke){
Invoke-Item $tempFile
}
else{
return $tempFile
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment