(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



Surveys with Google Graphs


2007/12/16





I mentioned Google's Chart API last week. An obvious use for that is to show the results of surveys. The survey itself is meaningless, and doesn't prevent you from voting multiple times. It's only meant to show how easy it is to produce the graphs or charts, so feel free to click on that link and vote as much as you want.

I've included a simplified version of the code here with comments.

The code uses a data file that looks like this initially:


0
0,0,0,0,0
Network|Mail|Users|Printing|Other
 

The first line will be the total number of votes, and the rest will be our data for the individual voting items. I'm showing some simplified code here (it just takes out extraneous html):

#!/usr/bin/perl
use CGI qw(:standard);
$CGI::POST_MAX=1024 ;  # max 1K posts
$CGI::DISABLE_UPLOADS = 1;  # no uploads
$FORM_TITLE="Survey";
$SURVEY="newsurvey.pl";
#
# locking necessary 
open(LOCKF, ">/data/survey_lock");
flock LOCKF,2;
#
# this is the form data
$passed=param('group');
#
open(I,"/data/survey/$SURVEY");
$votes=<I>; $values=<I>; $bars=<I>;
close I;
chomp $votes; chomp $bars; chomp $values;
#
@bars=split /\|/,$bars;
$values=~s/ //g;
@values=split /,/,$values;
# ok, ready to check voting
$votes++;
for ($x=0;$x < 5;$x++) {
   $values[$x]++ if ($passed eq $bars[$x]);
}
$values[4]++ if (not $passed );
# 
# and write them back out
$values=join ',',@values;
open(I,">/data/survey/$SURVEY");
print I "$votes\n$values\n$bars\n";
close I;
close LOCKF;
flock LOCKF,8;
#
# now ready to display
$bars=~s/ /\%20/g;
$gvalues="";
#
# make votes into percentages
for ($x=0;$x < 4;$x++) {
  my $t=sprintf("%2.2f,",$values[$x]*100/$votes);
  $gvalues.=$t;
}
$t=sprintf("%2.2f",$values[4]*100/$votes);
$gvalues.=$t;
#
print  header, <<EOF;
<html><head><title>$FORM_TITLE</title>
</head>
<body>

<h2>$FORM_TITLE</h2>
<hr>
<form action="/cgi-bin/$SURVEY" method=POST>
<table>
<tr><td>
What's your responsibility?<br>
EOF
foreach(@bars) { 
print <<EOF;
<br /><input name=group  type=radio value="$_">$_
EOF
}

print <<EOF;
<br /><br /><input type=submit name=posting>
</form>
</td><td>
<table class="open"><tr><th colspan="2">$votes votes</th></tr></p>
EOF
for ($x=0;$x < 5;$x++) {
  print "<tr><td>$bars[$x]</td><td>$values[$x]</td></tr>";
}

print <<EOF;
</table>
</td><td>
<img src="http://chart.apis.google.com/chart?cht=bvs&chbh=40,20&
chd=t:$gvalues&chs=540x100&chl=$bars"
alt="Chart of browser usage" />
</td></tr></table>
</body></html>
EOF
 

Pretty simple, isn't it?


;


Click here to add your comments



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

book graphic Internet Income guide

My Hard Truths about Easy Money on the Internet will show you how to make money on the Internet!

 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Web/HTML
       - Programming
       - Blogging
       - Code
       - MacOSX
       - Linux
       - Unix


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress