Generating Clickable Graphs
By Randal L. Schwartz
In this column nearly a year ago, I used the GraphViz program (at www.research.att.com/sw/tools/graphviz) to generate a nice graph to illustrate the flow of users through a Web site. I was recently contracted by a client to complete a similar task. However, as an added requirement, each node on the display had to be clickable, to further refine the query in progress. Not only that, but performing the query and running GraphViz eats up a bit of CPU, so I wanted to cache the picture for a given query result so that others could share it.
Hence, I have a query that generates links. I generate a picture with those links using GraphViz, along with a client side image map that lets me click on the nodes represented in that picture to select a particular node. The image gets written into a cache created with the Cache:FileCache module from the CPAN, indexed by a session number, and referenced in the HTML image map. The session number is computed from the links, so a given link list will reuse the imagemap and image computations if a prior computation has been performed recently enough.
Sound tough? Nope. It's a mere hundred lines of code or so, as illustrated in
Listing 1. For this example, I'm not actually performing the query, just simulating it. A textarea form field lets me enter to/from pairs directly. This lets me play around with the rendering logic independent of the query logic, so I can see what some typical images would look and act like.<>