þÿ# Consuming iCalendar Events Using PowerShell # Greg Shackles, 2010 # http://www.gregshackles.com [Reflection.Assembly]::LoadFile("path\\to\\DDay.iCal.dll") # only display a label when we reach a new month function GetLabelFromDate($date) { if ($date.Day -ne 1) { return "" } $date.ToString("MMMM") } # get and sort the events from the calendar $calendar = [DDay.iCal.iCalendar]::LoadFromUri("http://path/to/your/calendar")[0] $sortedEvents = $calendar.Events | Sort-Object @{Expression={$_.DTStart.Value}; Ascending=$true} $values = @() $labels = @() $chartMin = 190 $chartMax = 220; $valueModifier = 100 / ($chartMax - $chartMin) # process the events in the calendar foreach ($event in $sortedEvents) { $currentDate = $event.DTStart.Value $currentValue = ($event.Summary - $chartMin) * $valueModifier if ($lastDate) { # if there's a gap in the dates, fill it in with empty data while ($lastDate.AddDays(1) -lt $currentDate) { $lastDate = $lastDate.AddDays(1) $values += 0 $labels += GetLabelFromDate $lastDate } } $labels += GetLabelFromDate $currentdate $values += $currentValue $lastDate = $currentDate } # figure out the size of the chart and bars $chartWidth = 800 $chartHeight = 350 $barWidth = [Math]::Floor($chartWidth / $values.length) # put together the URL # Reference: http://code.google.com/apis/chart/docs/gallery/bar_charts.html $url = "http://chart.apis.google.com/chart?cht=bvg&chs=$($chartWidth)x$($chartHeight)&chbh=$($barWidth),0,1&chd=t:$([String]::Join(",", $values))&chco=cc0000&chxt=x,y,r&chxl=0:$([String]::Join("|", $labels))&chxr=1,$($chartMin),$($chartMax)" # go download and display the chart $filename = "$env:temp\weight-chart.png" $webClient = new-object System.Net.WebClient $Webclient.DownloadFile($url, $filename) Invoke-Item $filename