Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I once did something like this. Below is a sample that creates a simple graph with nodes and edges showing relationships between them:
#!/usr/bin/perl use strict; use warnings; use DBI; use GraphViz; use Data::Dumper; use Getopt::Long; use Log::Log4perl; use Pod::Usage; my $conf = undef; my $log = undef; my $help = undef; my $db = undef; my $dbuser = undef; my $dbpass = undef; $conf = q( log4perl.category.Script = DEBUG, ScreenAppender log4perl.appender.ScreenAppender = Log::Log4perl::Appender:: +Screen log4perl.appender.ScreenAppender.stderr = 1 log4perl.appender.ScreenAppender.layout = PatternLayout log4perl.appender.ScreenAppender.layout.ConversionPattern=[%p] %d %F +:%L - %m%n ); Log::Log4perl::init( \$conf ); $log = Log::Log4perl::->get_logger(q(Script)); $log->debug("start") if $log->is_debug(); # GetOptions GetOptions("help|?" => \$help, "db=s" => \$db, "dbuser=s" => \$dbuser, "dbpass=s" => \$dbpass) or pod2usage(2); pod2usage(1) if $help; pod2usage(1) if !$db; pod2usage(1) if !$dbuser; pod2usage(1) if !$dbpass; my $dbh = DBI->connect ('DBI:mysql:' . $db, $dbuser, $dbpass); my $sth = $dbh->prepare( q{select name, friend from friends} ); $sth->execute() or $log->logdie($dbh->errstr); my %friends = (); while (my $hashref = $sth->fetchrow_hashref()) { my ($name, $friend) = ($hashref->{name}, $hashref->{friend}); $log->debug(qq{$name, $friend}) if $log->is_debug(); push @{$friends{$hashref->{name}}}, $friend; } $sth->finish(); $log->debug(Dumper(\%friends)) if $log->is_debug(); my $g = GraphViz->new(layout => 'fdp', ratio => 'compress'); # 1st iteration : Add nodes foreach my $name (keys %friends) { $g->add_node( "$name", label => $name, shape => 'box', style => 'filled', ) or $log->logdie("add_node failed"); } # 2nd iteration : Add edges foreach my $name (keys %friends) { foreach my $friend ( @{$friends{$name}}) { $log->debug(qq{$name => $friend}) if $log->is_debug(); $g->add_edge("$name" => "$friend", label => q{loves}) or $log->logdie("add_edge failed"); } } # Write to file my $fh = undef; open($fh, "> friends.png") or die "open failed:$!"; print $fh $g->as_png; close $fh or die "close failed:$!"; __END__ =head1 NAME simpsons - Graphviz DBI example =head1 SYNOPSIS ./simpsons.pl --db DB --dbuser DBUSER --dbpass DBPASS =head1 MySQL DDL, DML create table friends (name varchar(32) not null, friend varchar(32) no +t null); alter table friends add unique key (name, friend); insert into friends values ('Homer', 'Marge'), ('Homer', 'Bart'), ('Homer', 'Lisa'),('Marge', 'Homer'),('Bart', 'Santa\'s Little Helper'),('Lisa', 'Snowball II'); =cut
--
Andreas

In reply to Re: how to use Graphviz with sql by andreas1234567
in thread how to use Graphviz with sql by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2022-05-23 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (82 votes). Check out past polls.

    Notices?