Drawing a graph from an ArrayList
I've recently purchased 'Beginning VC# 2005' but whilst it's very good in most areas it doesn't take me far enough in GDI+.
I need to draw a graph based on objects in an ArrayList which effectively give me the points on my graph.
The completed graph will need to be considerably longer than the screen is wide.
I've managed to get the mechanics working using the following code :-
[code]
protected void PlotGraph(ArrayList eventList)
{
DateTime start, next;
TimeSpan duration;
int length, xpenposn1, ypenposn1;
int xpenposn2 = 50, ypenposn2 = 550;
int nextLabour, labour, height;
Graphics g = this.pictureBox1.CreateGraphics();
using (Pen bluePen = new Pen(Color.Blue, 2))
{
for (int k = 0; k < eventList.Count - 1; k++ )
{
start = ((Event)eventList[k]).EventTime;
labour = ((Event)eventList[k]).TotalLabour;
next = ((Event)eventList[k+1]).EventTime;
nextLabour = ((Event)eventList[k+1]).TotalLabour;
height = 5 * (nextLabour - labour);
duration = next - start;
length = 3 * duration.Hours + 60*duration.Days;
xpenposn1 = xpenposn2;
xpenposn2 = xpenposn1 + length;
ypenposn1 = ypenposn2;
g.DrawLine(bluePen, new Point(xpenposn1, ypenposn1), new Point(xpenposn2, ypenposn2));
xpenposn1 = xpenposn2;
ypenposn1 = ypenposn2;
ypenposn2 = ypenposn2 - height;
g.DrawLine(bluePen, new Point(xpenposn1, ypenposn1), new Point(xpenposn2, ypenposn2));
}
}
g.Dispose();
}
code]
Of course when I scroll across to look at the hidden portion of the graph it disappears. I recognise that I need to OnPaint method but I can't quite see how.
Thanks for any help.
|