Google Chart API を呼び出すスクリプト

既に誰かがやっているだろうけど、Google Chart APIPowerShellから呼び出すスクリプトを作ってみた。

Show-Chart.ps1

param([object[]]$data, [string]$chartType="lc", [int]$width=300, [int]$height=300)

if($data -eq $nll -or $args[0] -eq "-?") {
    $commandName = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name)
	Write-Host @"

Name:
    $commandName

Description:
    Creates a chart by using Google Chart API.

Usage:
    $commandName [[-data] <object[]>] [[-chartType] <string>] [[-width] <int>] [[-height] <int>]
    
    -data <object[]>
        The chart data (ex. 10,20,30)
        
    -chartType <string>
        The type of chart.
        lc (Line chart), sp (Scatter plot), bc (Bar chart), vd (Venn diagram), pc (Pie chart)
    
    -width <int>
        The width of chart.
        
    -height <int>
        The height of chart.
        
Require:
    New-Object
    Import-Assembly
    
"@ -foregroundColor Yellow
    exit 1
}
$URL = "http://chart.apis.google.com/chart"

$fileName = [IO.Path]::GetTempFileName()

$webClient = New-Object Net.WebClient
$webClient.Headers.Add(
    "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)"
)
$webClient.DownloadFile(
    ($URL + "?chs=$widthx$height&chd=t:{0}&cht=$chartType" -f [string]::Join(",", $data)),
    $fileName
)

[void](Import-Assembly System.Drawing)
[void](Import-Assembly System.Windows.Forms)

$img = [Drawing.Image]::FromFile($fileName)

$picbox = New-Object Windows.Forms.PictureBox
$picbox.Dock = "Fill"
$picbox.BorderStyle = "None"
$picbox.SizeMode = "StretchImage"
$picbox.Image = $img

$form = New-Object Windows.Forms.Form
$form.Controls.Add($picbox)
$form.Text = ("created by Google Chart API ({0}x{1})" -f $img.Width, $img.Height)
$form.ShowIcon = $false
$form.FormBorderStyle = "SizableToolWindow"
$form.StartPosition = "CenterScreen"
$form.MaximizeBox = $false
$form.add_Shown({
    $form.Activate()
})
$form.Width = $width;
$form.Height = $height;

[void]($form.ShowDialog())

$img.Dispose()
[IO.File]::Delete($fileName)

使い方

PS > Show-Chart 30.0, 20.0, 10.0, 20.0, 30.0

出力

だいぶ機能が豊富なようなので一部の機能にしか対応していないけど、普通に使う分には大丈夫だと思う。