APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Surveys with Google Graphs

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?



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Surveys with Google Graphs



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence



Kerio Samepage


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.

Contact us





I have been complimented many times and they always embarrass me; I always feel that they have not said enough. (Mark Twain)





This post tagged: