The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
How to create a chart with data from database
Discuss How to create a chart with data from database in the Perl Programming forum on Dev Shed. How to create a chart with data from database Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 11th, 2013, 07:52 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 8
Time spent in forums: 2 h 23 m 47 sec
Reputation Power: 0
|
|
|
How to create a chart with data from database
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
Code:
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
|

January 11th, 2013, 10:22 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
|

January 12th, 2013, 07:49 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 8
Time spent in forums: 2 h 23 m 47 sec
Reputation Power: 0
|
|
|
All I need is how to map the data retrieved from database and written to excel sheet1 to the chart data source, the columns which the data is written to in sheet1 is in cell A, F and G, I need to do it using perl as detailed in my initial msg for this thread.
Thank you for your help.
Rgds.
Terry
|

January 12th, 2013, 02:32 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
|

January 12th, 2013, 02:39 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 8
Time spent in forums: 2 h 23 m 47 sec
Reputation Power: 0
|
|
OK, after studying the examples I have figured out how to map the data to the chart and it works, here is the code for my case:
Code:
$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 for your help.
Rgds.
Terry
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|