Search code examples
memgraphdb

Is there a way to show count results on a graph in Memgraph Lab using GSS?


Is there any way to show count results on a graph in Memgraph Lab. For example, if I have a hashtag and tweets on a certain platform (Desktop, IOS, Android) under that hashtag, can I show a count of those on a graph and maybe display the circle in different sizes depending on the count?

I know I can probably do this programmatically, but I was wondering if I can do it directly in Memgraph Lab with the Graph Style Script?


Solution

  • Currently, Lab can’t read data table view with primitives (not edges, nodes, or paths) and present it as a graph. But, you can still make your tabular data results returned as something that Lab can render. The lab is looking for something that looks like a node, edge, or path object.

    Here is how you can do it:

    Change the structure of the query. You are probably running something like this:

    MATCH (n:Tweet { hashtag: "#memgraph" })
    RETURN n.hashtag as hashtag, n.platform as platform, count(n) as cnt
    

    So what you want is to return an object (map) that must have the following keys: id, type = "node", labels and properties (imitate a node):

    MATCH (n:Tweet { hashtag: "#memgraph" })
    WITH n.hashtag as hashtag, n.platform as platform, count(n) as cnt
    return {
      id: counter('node', 1),
      type: "node",
      labels: ['Platform'],
      properties: { platform: platform, count: cnt }
    };
    

    For the above query, Lab will show 3 nodes. If you wish to create an edge connecting those three nodes with a single Tag node (e.g. #memgraph), you can do that too. A query is a little bit more complex, but here it is:

    MATCH (n:Tweet { hashtag: "#memgraph" })
    WITH n.hashtag as hashtag, n.platform as platform, count(n) as cnt
    WITH
      { id: 0, type: "node", labels: ['Tag'], properties: { hashtag: hashtag }} as hashtag_node,
      { id: counter('node', 1), type: "node", labels: ['Platform'], properties: { platform: platform, count: cnt }} as node
    RETURN [
      hashtag_node,
      { id: counter('edge', 0), type: "relationship", start: hashtag_node.id, end: node.id, label: 'CONTAINS' },
      node
    ]
    

    In the above query, a hashtag node is created that is connected with a simulation of an edge with the platform nodes. As you can see, the imitation of the edge must have id, type = "relationship", start , end and label.

    And for styling the graph: Switch over to the “Graph Style Editor” in the tab and you can style your graph. Change the size, label, even images depending on the node properties.