ganeshts has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create a graph with GD::Graph::hbars ; My issue is with the length of the label text in the vertical axis. I would like to break up each label into 2 lines, but there seems to be no option to do that. (I tried inserting a \n in the label string, but that just puts in a strange character in the final graph) In the piece of code below, I want each label in the vertical axis to break after either 'Vision 3D' or 'Core 100'. The picture output by the code below is http://dl.dropbox.com/u/1172/v3d_usb_perf.gif ; Any suggestions towards helping convert the currently ugly graph into a viewable one would be deeply appreciated :)
use strict; use GD::Text ; use GD::Graph::hbars ; use GD::Graph::colour; my @data = ( ["Vision 3D NEC USB 3.0","Vision 3D Fresco Logic USB 3.0","Vision 3D N +EC USB 2.0","Core 100 NEC USB 3.0"], # Read benchmark [93.2, 84.8, 29.8, 80.5], # Write benchmark [77.1, 83.7, 23.2, 73], ); my @names = qw/v3d_usb_perf/; my @dim = (640,480); for my $my_graph (GD::Graph::hbars->new(@dim)) { my $name = shift @names; print STDERR "Processing $name\n"; $my_graph->set( t_margin => 15, b_margin => 15, l_margin => 15, r_margin => 15, x_label => '', y_label => 'Transfer Bandwidth (MB/s)', title => 'Average USB Transfer Rates in ASRock HTPCs (MB/s)', y_max_value => 100, y_tick_number => 5, y_label_skip => 1, y_tick_offset => 1, line_width => 1, bar_spacing => 2, show_values => 1, borderclrs => [ qw(black) ], # bgclr => 'gray', shadow_depth => 1, bargroup_spacing => 50, accent_treshold => 200, transparent => 0, box_axis => 0, ); $my_graph->set( dclrs => [ qw(lblue lgreen) ] ); $my_graph->set_legend(qw(Read Write)); $my_graph->set_title_font( GD::Font->Giant ); $my_graph->set_x_label_font( GD::Font->MediumBold ); $my_graph->set_y_label_font( GD::Font->MediumBold ); $my_graph->set_legend_font( GD::Font->MediumBold ); $my_graph->set_x_axis_font( GD::Font->MediumBold ); $my_graph->set_y_axis_font( GD::Font->MediumBold ); $my_graph->set_values_font( GD::Font->MediumBold ); $my_graph->plot(\@data); save_chart($my_graph, $name); } sub save_chart { my ($chart, $name) = @_; local(*OUT); my $ext = $chart->export_format; open(OUT, ">$name.$ext") or die "Cannot open $name.$ext for write: $!"; binmode OUT; print OUT $chart->gd->$ext(); close OUT; }

Replies are listed 'Best First'.
Re: Formatting Labels in GD Graphs
by Proclus (Beadle) on Oct 01, 2010 at 21:25 UTC
    I used GD::Graph for two projects before, and I had to hack the source to get what I need. And actually it is not that complicated, once you find the methods responsible for drawing text, ticks and labels you can easily add your logic.