[Richard Suchenwirth] 2003-03-25 - In this evening fun project, I played with [Tcldot] which produces code for rendering a directed graph on a [canvas]. To make it match work requirements, I decided to represent graphs in [XML] and control the graph buildup from [tDOM]'s expat parser.
The example shows a graph that might be the result of character segmentation, with a "m" alternatively split into "r" and "n". The best path through this (simple) graph is marked with the ''best="1"'' attribute, and highlighted on the canvas. Here is the very simple code:
----
[WikiDbImage doteddemo.jpg]
======
package require tdom
package require Tcldot
set sample {
}
set c [canvas .c]
pack $c -fill both -expand 1
set g [dotnew digraph rankdir LR]
$g setnodeattribute style filled color white shape box
#------------------- parser callback for element start:
proc start {name atts} {
global g ;# graph
array set a [concat {best 0} $atts]
switch -- $name {
node {
set ::n$a(id) [$g addnode $a(id) label ($a(id))\n$a(label)]
}
edge {
set cmd [list [set ::n$a(from)] addedge [set ::n$a(to)]]
if $a(best) {lappend cmd style bold}
eval $cmd
}
}
}
set parser [expat -elementstartcommand start]
$parser parse $sample
$g layout ;# determine suitable x/y coordinates for canvas items
eval [$g render] ;# put them on the canvas named $c
======
<>Example | Arts and crafts of Tcl-Tk programming | XML | Graph theory