Chart.point标签不适用于2个图表,而不是1个

时间:2018-12-09 19:04:35

标签: powershell charts

在Powershell显示中处理图表是通过Windows ping到各种服务器的结果。 此代码在一张图表上效果很好。 它显示一个堆积的条形图,其中的ping失败显示为红色,显示的ping失败次数为红色,如果“响应时间”大于某个数字,则会将其变为黄色并标记响应时间。

#region setup chart
$Chart = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Size = '600,750'
#$Chart.BackColor = [system.drawing.color]::Transparent

$ChartArea = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.ChartArea
$ChartArea.AxisX.Title = 'Response times'
$ChartArea.AxisY.Title = 'Time in MS '
$ChartArea.AxisX.Interval = '1'
$ChartArea.AxisX.LabelStyle.Enabled = $true
$ChartArea.AxisX.LabelStyle.Angle = 90
$Chart.ChartAreas.Add($ChartArea)
$Chart.Series.Add('Good')
$Chart.Series['Good']["DrawingStyle"] = "Cylinder"
$Chart.Series['Good'].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::StackedBar
 $Chart.Series['Good'].Color = [system.drawing.color]::Green
 $Title = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Title
$Chart.Titles.Add($Title)
$Chart.Titles[0].Text = 'Ping Monitor (Active Pings)'

#endregion chart

function processPINGS {
  BEGIN {
         $curpoint=0
         $global:Chart.Series['Good'].points.clear()
    }#begin

Process {
ForEach($key in $global:status.keys)
{ 
         switch ($global:status[$key].FailedType){
              0 { 
                  $Value = $global:Chart.Series['Good'].Points.AddXY("$($global:status[$key].name)","$($global:status[$key].'Response Time')")
                 }   #end case 0 
              1 { $Value = $global:Chart.Series['Good'].Points.AddXY("$($global:status[$key].name)","150")
                           $global:Chart.Series['Good'].points[$curpoint].color = [system.drawing.color]::RED
                           $global:Chart.Series['Good'].points[$curpoint].label = "$($global:status[$key].'Failed Pings') Failed Pings"
                 }#end case 1
             2 {$Value=$global:Chart.Series['Good'].Points.AddXY("$($global:status[$key].name)","$($status[$key].'Response Time')")
                       $global:Chart.Series['Good'].points[$curpoint].color = [system.drawing.color]::Yellow
                       $global:Chart.Series['Good'].points[$curpoint].label = "$($global:status[$key].'Response Time') MS"
                }#end case 2
           }#end switch
       $curpoint++
    }#end foreach Key
   }#end of process block
}#end of function processPINGS

$PingCount = 0
Do {
    Ping-Machines
    processPINGS
   $Chart.SaveImage("$scriptpath\Chartgoodpings.png", "PNG")
   Start-Sleep -seconds 30
   }Until (PingCount -gt 120)

因此,现在我想制作2张图表,一张用于ping的计算机,一张用于不ping的计算机,以便某人可以快速查看所有未ping的计算机。我复制了以下图表设置

    $NoPingChart = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Chart
    $NoPingChart.Size = '600,750'
    $NoPingChartArea = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.ChartArea
    $NoPingChartArea.AxisX.Title = 'Response times'
    $NoPingChartArea.AxisY.Title = 'Time in MS '
    $NoPingChartArea.AxisX.Interval = '1'
    $NoPingChartArea.AxisX.LabelStyle.Enabled = $true
    $NoPingChartArea.AxisX.LabelStyle.Angle = 90
    $NoPingChart.ChartAreas.Add($NoPingChartArea)
    $NoPingChart.Series.Add('Good')
    $NoPingChart.Series['Good']["DrawingStyle"] = "Cylinder"
    $NoPingChart.Series['Good'].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::StackedBar
    $NoPingChart.Series['Good'].Color = [system.drawing.color]::RED
    $Title = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Title
   $NoPingChart.Titles.Add($Title)
   $NoPingChart.Titles[0].Text = 'Ping Monitor (Failed Pings)'  

然后我更改了processPings函数,以将不良ping的结果保存到新图表中,如下所示

function processPINGS {
  BEGIN {
         $curpoint=0
         $global:Chart.Series['Good'].points.clear()
    }#begin

Process {
ForEach($key in $global:status.keys)
{ 
         switch ($global:status[$key].FailedType){
              0 { 
                  $Value = $global:Chart.Series['Good'].Points.AddXY("$($global:status[$key].name)","$($global:status[$key].'Response Time')")
                 }   #end case 0 
              1 { $Value = $global:NoPingChart.Series['Good'].Points.AddXY("$($global:status[$key].name)","150")
                           $global:NoPingChart.Series['Good'].points[$curpoint].color = [system.drawing.color]::RED
                           $global:NoPingChart.Series['Good'].points[$curpoint].label = "$($global:status[$key].'Failed Pings') Failed Pings"
                 }#end case 1
             2 {$Value=$global:Chart.Series['Good'].Points.AddXY("$($global:status[$key].name)","$($status[$key].'Response Time')")
                       $global:Chart.Series['Good'].points[$curpoint].color = [system.drawing.color]::Yellow
                       $global:Chart.Series['Good'].points[$curpoint].label = "$($global:status[$key].'Response Time') MS"
                }#end case 2
           }#end switch
       $curpoint++
    }#end foreach Key
   }#end of process block
}#end of function processPINGS

然后我再次运行程序并进行了更改,一旦尝试在情况1或2中创建点,我会遇到以下错误。

  The property 'Label' cannot be found on this object. Verify that the property exists and can be set.
   At C:\ToolBox\PINGMonitor\pingChart-v3.ps1:94 char:87
    + ... nts[$curpoint].Label = "$($global:status[$key].'Failed Pings') Failed ...
       +                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
       + FullyQualifiedErrorId : PropertyNotFound

 The property 'Label' cannot be found on this object. Verify that the property exists and can be set.
At C:\ToolBox\PINGMonitor\pingChart-v3.ps1:94 char:87
+ ... nts[$curpoint].Label = "$($global:status[$key].'Failed Pings') Failed ...
+                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
   + FullyQualifiedErrorId : PropertyNotFound

  The property 'Color' cannot be found on this object. Verify that the property exists and can be set.
 At C:\ToolBox\PINGMonitor\pingChart-v3.ps1:99 char:23
+ ...             
   $global:GoodChart.Series['Good'].points[$curpoint].Color  ...
 + 
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
   + FullyQualifiedErrorId : PropertyNotFound

停止程序并查看图表属性

$global:NoPingChart.Series['bad'].points[$curpoint]
  XValue                      : 0
  YValues                     : {150}
  IsEmpty                     : False
  Name                        : DataPoint
  Label                       : 

然后手动设置标签

$global:NoPingChart.Series['bad'].points[$curpoint].label = "Test Label"
$global:NoPingChart.Series['bad'].points[$curpoint]
XValue                      : 0
YValues                     : {150}
IsEmpty                     : False
Name                        : DataPoint
Label                       : Test Label

您可以在那里找到标签,我可以手动更改它。 我现在正打在桌子上,试图看看发生了什么。 任何帮助将不胜感激

0 个答案:

没有答案