Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 11th, 2013, 07:52 PM
terrykhatri531 terrykhatri531 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 8 terrykhatri531 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old January 11th, 2013, 10:22 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 203 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 16 h 10 sec
Reputation Power: 41

Reply With Quote
  #3  
Old January 12th, 2013, 07:49 AM
terrykhatri531 terrykhatri531 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 8 terrykhatri531 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old January 12th, 2013, 02:32 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 203 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 16 h 10 sec
Reputation Power: 41

Reply With Quote
  #5  
Old January 12th, 2013, 02:39 PM
terrykhatri531 terrykhatri531 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 8 terrykhatri531 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > How to create a chart with data from database

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap