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

How to create a chart with data from database

by terrykhatri531 (Novice)
on Jan 12, 2013 at 01:52 UTC ( [id://1013001]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I need to create some charts using Spreadsheet::WriteExcel with data driven from database using DBI, I tried googling but could not find any help, here is a sample script that fetches data from a sample database and I want to create a bar chart using the name, surfacearea and population columns can someone please help me with the routines to get it done
my $sth = $dbh->prepare("SELECT a.name,b.name as capital, a.continent, +a.region, a.lifeexpectancy,a.surfacearea,a.population FROM country a, + city b WHERE a.capital=b.id ORDER BY 1"); $dbh->do("SET search_path to world") or die; $sth->execute(); my $row=1; while ($a = $sth->fetchrow_hashref()) { $worksheet->write($row,0, $a->{name}, $bold); $worksheet->write($row,1, $a->{capital}); $worksheet->write($row,2, $a->{continent}); $worksheet->write($row,3, $a->{region}); $worksheet->write($row,4, $a->{lifeexpectancy}); $worksheet->write($row,5, $a->{surfacearea}); $worksheet->write($row,6, $a->{population}); $row++; }
As always, your help would be much appreciated.

Thanks

Terry

Replies are listed 'Best First'.
Re: How to create a chart with data from database
by CountZero (Bishop) on Jan 12, 2013 at 07:54 UTC
    OK. We take it one step at a time.

    First Step: After running your script, do you end up with an Excel spreadsheet file that has the data you want to chart in it? If not, did you get any errors or messages from your script?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      Hi

      First of all thank you very much, yes it does work very well and it does write the data into an excel spreadsheet, now I want to use columns Name, Surfacearea and Population into a chart to be created in the 2nd worksheet of the same excel file.

      If you kindly get me going once I will be very grateful

      Regards

      Terry

        Ok, after studying how the examples work, I have figured out how to map data to the chart, here is the code for my case:
        $worksheet = $workbook->add_worksheet("Population Chart"); $worksheet->add_write_handler(qr[\w], \&store_string_widths); my $chart1 = $workbook->add_chart( type => 'bar', embedded => 1 ); # Configure the series. $chart1->add_series( categories => '=Summary!$A$2:$A$row', values => '=Summary!$G$2:$G$row', name => 'World Population', ); # Add another series. $chart1->add_series( categories => '=Summary!$A$2:$A$row', values => '=Summary!$F$2:$F$row', name => 'World Surface Area', ); # Add some labels. $chart1->set_title( name => 'Results of Population analysis' ); $chart1->set_x_axis( name => 'Countries' ); $chart1->set_y_axis( name => 'Population' ); # Insert the chart into the main worksheet. $worksheet->insert_chart( 'A2', $chart1 ); autofit_columns($worksheet);
        Thank you everyone for your help.

        Regards

        Terry
Re: How to create a chart with data from database
by pvaldes (Chaplain) on Jan 12, 2013 at 12:31 UTC

    I wonder why to use excel here.

    The data are yet in the best place, a database.

    You have the tool to retrieve the desired data from database to a perl "object". Easy to print and to manipulate.

    And in cpan you will find many modules to make graphics without the need of excel.

    If your goal is simply to create a lot of graphics you are using a lot of unnecesary extra space spend in (mostly unused) spreadsheets. Is this what you really want?.

    Adapted from the documentation of the module Chart::Base:

    use Chart::Bars; $g = Chart::Bars->new(600,500); # chart size $g->add_dataset ('Berlin', 'Paris', 'Rome', 'London', 'Munich'); # nam +e of the five items $g->add_dataset (10, 40, 60, 70, 12); # surface area $g->add_dataset (18, 20, 30, 30, 40); # population (Thousands) %hash = ('title' => 'Sold Cars in 2001', 'text_space' => 5, 'grey_background' => 'false', 'integer_ticks_only' => 'true', 'x_label' => 'City', 'y_label' => 'Number of Cars', 'legend' => 'bottom', 'legend_labels' => ['surface' , 'population'], 'min_val' => 0, 'max_val' => 70, 'grid_lines' =>'true', 'colors' => {'title' => 'red', 'x_label' => 'blue', 'y_label' => 'blue'} ); $g->set (%hash); $g->png ("bars.png"); # print chart to the file bars.png
Re: How to create a chart with data from database
by roboticus (Chancellor) on Jan 12, 2013 at 03:59 UTC

    terrykhatri531:

    As the AM indicates, many modules come with utility scripts and/or examples. Another valuable source is the test suite for the module, they'll show the ways to drive all the features.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: How to create a chart with data from database
by Anonymous Monk on Jan 12, 2013 at 03:09 UTC

      Hi,

      I have seen the examples but I don't see any routines using data source from the database or maybe its beyond my understanding as I am very new in perl, therefore it will be very helpful if someone can give me a routine based on my script that will get me going.

      Please help me!

      Regards

      Terry

        I have seen the examples but I don't see any routines using data source from the database ...

        Why is this a problem? Doesn't the code you already posted use stuff from database successfully?

        Please help me!

        No thanks, not me :/

Re: How to create a chart with data from database
by Anonymous Monk on Jan 14, 2013 at 14:55 UTC

    Echoing some of the above ...

    If Excel is ultimately what you want to use to format your data, then you can (and should ...) use Excel to retrieve that data, too.

    If that does not work out, then consider the hybrid approach of using Perl to generate an XML file.   (Use any one of several XML-manipulation CPAN packages ...)   Then, open a spreadsheet which specifies that file as an external data-source.   Now, you can use Excel to specify all of the graphing, any secondary calculations, and so forth.   You can change anything about the Excel spreadsheet within that product.   You can also create any number of spreadsheets which draw from the same data-source.   The only thing your program has to be able to do is to produce that data in the format that you have chosen.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1013001]
Approved by patcat88
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-03-19 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found