2017-07-05 39 views
0

使用.Net圖表控件創建一些圖表,圖形。此刻,我能夠拯救那些圖表,圖表分別對每個圖像文件,如下所示:使用.Net圖表控件添加多個圖像

$mailHash = @{"Email Set"=$emailPer;"No Email"=$noEmailPer} 

$outFilePic= "C:\My Documents\Documents" 

Function drawPie ($hash){ 

    Add-Type -AssemblyName System.Windows.Forms 
    Add-Type -AssemblyName System.Windows.Forms.DataVisualization 
    $Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart 
    $ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea 
    $Series = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Series 
    $ChartTypes = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType] 

    $Series.ChartType = $ChartTypes::Pie 
    $Chart.Series.Add($Series) 
    $Chart.ChartAreas.Add($ChartArea) 
    $Chart.Series['Series1'].Points.DataBindXY($hash.keys, $hash.values) 
    $Chart.Series[‘Series1’][‘PieLabelStyle’] = ‘Disabled’ 

    $Legend = New-Object System.Windows.Forms.DataVisualization.Charting.Legend 
    $Legend.IsEquallySpacedItems = $True 
    $Legend.BorderColor = 'Black' 
    $Chart.Legends.Add($Legend) 
    $chart.Series["Series1"].LegendText = "#VALX (#VALY%)" 

    $Chart.Width = 700 
    $Chart.Height = 400 
    $Chart.Left = 10 
    $Chart.Top = 10 
    $Chart.BackColor = [System.Drawing.Color]::White 
    $Chart.BorderColor = 'Black' 
    $Chart.BorderDashStyle = 'Solid' 

    $ChartTitle = New-Object System.Windows.Forms.DataVisualization.Charting.Title 
    $ChartTitle.Text = 'Email ' 
    $Font = New-Object System.Drawing.Font @('Microsoft Sans Serif','12', [System.Drawing.FontStyle]::Bold) 
    $ChartTitle.Font =$Font 
    $Chart.Titles.Add($ChartTitle) 
    $Chart.SaveImage($outFilePic, 'jpeg') 
} 

以上是動態函數創建對餅圖。因此,也會創建許多不同的圖表。如何將這些保存在一個圖像中?

回答

1

你可以嘗試以下的版本,我已經添加了多個系列的傳說到同一個圖表對象(我做了一系列的名字取的動態此),並將其保存:

Function drawPie ($chart,$hash,$ChartT){ 


    $ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea 
    $Series = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Series 


    $Series.ChartType = $ChartT 
    $Chart.Series.Add($Series) 
    $Chart.ChartAreas.Add($ChartArea) 

    $Chart.Series[$series.name].Points.DataBindXY($hash.keys, $hash.values) 
    $Chart.Series[$series.name][‘PieLabelStyle’] = ‘Disabled’ 
    $Chart.Series[$series.name].ChartArea = $ChartArea.name 

    $Legend = New-Object System.Windows.Forms.DataVisualization.Charting.Legend 
    $Legend.IsEquallySpacedItems = $True 
    $Legend.BorderColor = 'Black' 
    $Chart.Legends.Add($Legend) 
    $chart.Series[$series.name].LegendText = "#VALX (#VALY%)" 
    $chart.Series[$series.name].Legend = $Legend.name 



} 


$mailHash = @{"Email Set"=$emailPer;"No Email"=$noEmailPer} 
#modify this path 
$outFilePic= "d:\temp\stala.jpeg" 
Add-Type -AssemblyName System.Windows.Forms 
Add-Type -AssemblyName System.Windows.Forms.DataVisualization 
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart 
$Chart.Width = 1400 
$Chart.Height = 800 
$Chart.Left = 10 
$Chart.Top = 10 
$Chart.BackColor = [System.Drawing.Color]::White 
$Chart.BorderColor = 'Black' 
$Chart.BorderDashStyle = 'Solid'  
$ChartTypes = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType] 
$data = @{"10"="5";"20"="5";"30"="100";} 
$data2 = @{"100"="1";"200"="49";"300"="50";} 
drawPie $chart $data $ChartTypes::Pie 
drawPie $chart $data2 $ChartTypes::Bar 
$Chart.SaveImage($outFilePic, 'jpeg') 
+0

韓國社交協會這麼多的隊友!對此,我真的非常感激。您好:D – Ender

+0

嗯,當我嘗試將餅圖和條形圖結合在一起時,我遇到了問題。我遇到了一個錯誤「_圖表區域 包含不兼容的圖表類型,例如,條形圖和柱形圖表不能存在於同一個圖表區域中。有沒有可能解決? – Ender

+1

@Ender我已經更新了Pie和Bar chart中的代碼,你可以檢查一下嗎? –