Andrew James Combs Andrew James Combs - 2 months ago 25
C# Question

Displaying Graphs in C#

I'm working on an application that needs to display a changing graph (graph-theory graph, not science class graph) in a decent looking GUI, but I'm not really sure where to start. Any advice?

Answer

Use something like this... First you would add a button to the forms and within your button click:

    public void RunSearch_Click(object sender, EventArgs e)
    {
        DrawIt();
    }

You would call a method, in this case "DrawIt":

    private void DrawIt()
    {
        Graphics g;
        g = this.CreateGraphics();
        g.Clear(Color.White);
        SolidBrush sb = new SolidBrush(Color.Red);
        Pen p = new Pen(sb, 2);
        Font f = new Font(FontFamily.GenericSansSerif, 10);
        Rectangle rect;
        //Add the nodes at the proper coordinates
        for (int i = 1; i < search.nodes.Count; i++)
        {
            rect = new Rectangle((int)(search.nodes[i].x * 7) + 10, (int)(search.nodes[i].y * 7) + 10, -20, -20);
            g.DrawEllipse(p, rect);
            g.DrawString(search.nodes[i].name.ToString(), f, sb, (float)(search.nodes[i].x * 7) + 10, (float)(search.nodes[i].y * 7) + 10);
        }}