TimeDirection graph

A work college was wanting some trivial code to draw a time based direction plot, and insisted that I do it.

So here is my drawing class:

using System;  
using System.Collections.Generic;  
using System.Drawing;  
using System.Windows.Forms;  

namespace LineCurve  
{  
  public class TimeDirection  
  {  
    public void Draw(Graphics g, Panel pnl, List<KeyValuePair<int,float>> data, int maxtime)  
    {  
      using (Brush brush = new SolidBrush(Color.Blue))  
      {  
        float halfx = pnl.Width / 2.0f;  
        float halfy = pnl.Height / 2.0f;  
        float scale = Math.Min(halfx, halfy) / (float)maxtime;  

        foreach (KeyValuePair<int, float> kvp in data)  
        {  
          int time = kvp.Key;  
          double dir = kvp.Value / 180.0 * Math.PI;  

          // subtrack y as 0 is top, and +y is bottom.  
          float fx = (float)(halfx + (Math.Sin(dir) * scale * time));  
          float fy = (float)(halfy - (Math.Cos(dir) * scale * time));  

          g.FillRectangle(brush,fx, fy, 1.0f, 1.0f);  
        }  
      } 
    }  
  }  
}  

And given the input:

List<KeyValuePair<int,float>> data = new List<KeyValuePair<int,float>>();  
for (int i = 0; i < 40; i++)  
{  
  data.Add(new KeyValuePair<int, float>(i, i * 9.0f));  
}  

Looks like this:

time/direction based graph

This code is released under the MIT license, except it may not be used for web-based weather applications.

Share

Comments:

Chris Crowe 2008-09-08 21:34:03

Well Simeon

I must say thank you for your code sample, but i have modified it so much that I think your crazy MIT License but not including Weather Sites is invalid now.

Anyway see what I have come up with at:

http://weather.crowe.co.nz/Charts.aspx