在powershell中使用MSCharting的线图

时间:2015-02-27 03:41:33

标签: powershell graph

我正在尝试使用PowerShell中的MScharting绘制折线图。我希望X轴从第一点开始。目前我的代码从下一点开始。让第一个只为零。这是我的代码。 (我的X轴包含日期。)

[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.Datavisualization")


$chart = New-Object System.Windows.Forms.Datavisualization.charting.chart
$chart.width = 600
$chart.Height = 400
$chart.Top = 40
$chart.Left = 30

$chartArea = New-Object System.windows.Forms.Datavisualization.charting.chartArea
$chart.chartAreas.Add($chartArea)

[Void]$chart.Titles.Add("Peak Report")
$chartArea.AxisX.Title = "Date"
$chartArea.AxisY.Title = "Records"
$chart.Titles[0].Font = "Arial,13pt"
$chartArea.AxisX.TitleFont = "Arial,13pt"
$chartArea.AxisY.TitleFont = "Arial,13pt"
$chartArea.AxisY.Interval = 50
$chartArea.AxisX.Interval = 1

#Legend
$legend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend
$legend.name = "Legend"
$chart.Legends.Add($legend)

#$Chart.BackColor = [System.Drawing.Color]::White

[Array]$Info = Get-Content "C:\Users\Megharaj\Desktop\New Text Document.txt"
$x = $Null
$y = $Null
foreach ( $i in $Info )
{
    $data = $i -split "\s+"
    [Array]$x += $data[0]
    [Array]$y += $data[2]
}

# data series  
[void]$chart.Series.Add("Demand")  
$chart.Series["Demand"].ChartType = "Line"  
$chart.Series["Demand"].BorderWidth = 3  
$chart.Series["Demand"].IsVisibleInLegend = $true  
$chart.Series["Demand"].chartarea = "ChartArea1"  
$chart.Series["Demand"].Legend = "Legend"  
$chart.Series["Demand"].color = "#62B5CC"  

$chart.Series["Demand"].Points.DataBindXY($x, $y)

# display the chart on a form 
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left 
$Form = New-Object Windows.Forms.Form 
$Form.Text = "PowerShell Chart" 
$Form.Width = 650 
$Form.Height = 450 
$Form.controls.add($Chart) 
$Form.Add_Shown({$Form.Activate()}) 
$Form.ShowDialog()

$file = $Report + "_" +$dt

$Chart.SaveImage("C:\Users\Megharaj\Desktop\$file.jpeg", "JPEG")

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码

$Chartarea.AxisX.Minimum = 1
$Chartarea.AxisX.Maximum = #Whatever your last point is -1 to remove the empty space at the end.
$Chartarea.AxisY.IsStartedFromZero = 0 # Might be something else your interested in using

我知道这已经过时了,但我只想将其添加到记录中,因为它可能对某人有用。