Graph library for Cocoa [closed]

2019-03-14 00:29发布

问题:

Is there any good library for some graph-app? I want to create nodes, add weighted edges, etc…

EDIT

I need a graph (like on the image below) not a plot.

回答1:

According to the GraphViz documentation something like this should do the layout job:

- (void) testGraph {

    Agraph_t* G;
    GVC_t* gvc;
    gvc = gvContext();

    char* testGraph = "\
        digraph G {\
        subgraph cluster_c0 {a0 -> a1 -> a2 -> a3;}\
        subgraph cluster_c1 {b0 -> b1 -> b2 -> b3;}\
        x -> a0;\
        x -> b0;\
        a1 -> a3;\
        a3 -> a0;\
    }";

    G = agmemread((char*)testGraph);
    [self dumpGraph:G];

    gvLayout (gvc, G, "dot");
    NSLog(@"after layout:");

    [self dumpGraph:G];

    gvFreeLayout(gvc, G);
    gvFreeContext(gvc);

}

- (void) printNode:(Agnode_t*) node {
    NSLog(@"Node: %s {{%f,%f},{%f,%f}}",node->name,
          node->u.coord.x,
          node->u.coord.y,
          ND_width(node),
          ND_height(node));
}

- (void) dumpGraph:(Agraph_t*)graph {
    if (!graph)
        return;
    Agnode_t *n = NULL;
    Dict_t *dict = graph->nodes;
    for (n= dtfirst(dict); n ; n = dtnext(dict, n)) {
        [self printNode:n];
    }
}

Instead of using logNode: you should write some drawing code.

Log output:

GraphTest[32319:303] Node: a0 {{0.000000,0.000000},{0.000000,0.000000}}
GraphTest[32319:303] Node: a1 {{0.000000,0.000000},{0.000000,0.000000}}
GraphTest[32319:303] Node: a2 {{0.000000,0.000000},{0.000000,0.000000}}
GraphTest[32319:303] Node: a3 {{0.000000,0.000000},{0.000000,0.000000}}
GraphTest[32319:303] Node: b0 {{0.000000,0.000000},{0.000000,0.000000}}
GraphTest[32319:303] Node: b1 {{0.000000,0.000000},{0.000000,0.000000}}
GraphTest[32319:303] Node: b2 {{0.000000,0.000000},{0.000000,0.000000}}
GraphTest[32319:303] Node: b3 {{0.000000,0.000000},{0.000000,0.000000}}
GraphTest[32319:303] Node: x {{0.000000,0.000000},{0.000000,0.000000}}
GraphTest[32319:303] after layout:
GraphTest[32319:303] Node: a0 {{83.000000,250.000000},{0.750000,0.500000}}
GraphTest[32319:303] Node: a1 {{63.000000,178.000000},{0.750000,0.500000}}
GraphTest[32319:303] Node: a2 {{43.000000,106.000000},{0.750000,0.500000}}
GraphTest[32319:303] Node: a3 {{83.000000,34.000000},{0.750000,0.500000}}
GraphTest[32319:303] Node: b0 {{161.000000,250.000000},{0.750000,0.500000}}
GraphTest[32319:303] Node: b1 {{161.000000,178.000000},{0.750000,0.500000}}
GraphTest[32319:303] Node: b2 {{161.000000,106.000000},{0.750000,0.500000}}
GraphTest[32319:303] Node: b3 {{161.000000,34.000000},{0.750000,0.500000}}
GraphTest[32319:303] Node: x {{122.000000,322.000000},{0.750000,0.500000}} 


回答2:

It's not super Cocoa-friedly, but Graphviz might either do a lot of what you need, or provide a good basis for your own efforts. (That is, the graphviz library can handle all the graph layout logic, and tell you where to put shapes which you then draw using your favorite graphics API. For example, this is how OmniGraffle's automatic layout feature works.)



回答3:

If you can restrict yourself to Mountain Lion and above, SceneKit seems to be a promising place to start. You'll have to manage the layout geometry yourself (actual positions of the nodes/edges), but you get hit testing and geometric primitives for free. Properties and textures are animatable: you can enlarge a node, darken an edge, change a number or color, all with your favorite Core Animation style calls.