Powershell图表

时间:2016-07-20 14:30:24

标签: powershell powershell-v2.0 powershell-v3.0

我尝试根据每个版本为多个SQL实例创建一个饼图。例如,2012年 - 3个实例,2008年 - 2个实例,2005年 - 1个实例。任何人都可以帮助我如何在饼图中实现这一目标。

代码如下:

Add-Type -AssemblyName "System.Windows.Forms.DataVisualization"
$scriptpath = Split-Path -parent $MyInvocation.MyCommand.Definition
#frame
$MemoryUsageChart2 = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$MemoryUsageChart2.Width = 500
$MemoryUsageChart2.Height = 400
$MemoryUsageChart2.BackColor = [System.Drawing.Color]::White
#header
[void]$MemoryUsageChart2.Titles.Add("No.SQL Instances")
$MemoryUsageChart2.Titles[0].Font = "segoeuilight,20pt"
$MemoryUsageChart2.Titles[0].Alignment = "Bottomright"

$chartarea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$chartarea.Name = "ChartArea1"
$MemoryUsageChart2.ChartAreas.Add($chartarea)
[void]$MemoryUsageChart2.Series.Add("data2")

$MemoryUsageChart2.Series["data2"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Pie
$SQL= @{SQLA=2012; SQLB=2012; SQLC=2008; SQLD=2005;SQLE=2005 ;SQLF=2000}

1 个答案:

答案 0 :(得分:0)

我已经使用以下代码管理解决它,我们必须对哈希表进行分组并获取每个实例的计数并将其加载到X轴和Y轴以获得饼图:

    $MemoryUsageChart2.Series["data2"][“PieLabelStyle”] = “Outside” 
    $MemoryUsageChart2.Series["data2"][“PieLineColor”] = “Black” 
    $MemoryUsageChart2.Series["data2"][“PieDrawingStyle”] = "Concave”         
 $ht = $SQL.Values | group
            $x = $ht | select -ExpandProperty Name
            $y = $ht | select -ExpandProperty Count
            $i = 0
    $x | Foreach-object {$MemoryUsageChart2.Series["data2"].Points.AddXY("$($_ +(-$y[$i]))", $y[$i]); $i++ }
               $MemoryUsageChart2.SaveImage("C:\Virtual_Memory_Usage.png","png")