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:


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):

use CGI qw(:standard);
$CGI::POST_MAX=1024 ;  # max 1K posts
$CGI::DISABLE_UPLOADS = 1;  # no uploads
# locking necessary 
open(LOCKF, ">/data/survey_lock");
flock LOCKF,2;
# this is the form data
$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
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;
print I "$votes\n$values\n$bars\n";
close I;
close LOCKF;
flock LOCKF,8;
# now ready to display
$bars=~s/ /\%20/g;
# make votes into percentages
for ($x=0;$x < 4;$x++) {
  my $t=sprintf("%2.2f,",$values[$x]*100/$votes);
print  header, <<EOF;

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

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

print <<EOF;
<img src="http://chart.apis.google.com/chart?cht=bvs&chbh=40,20&
alt="Chart of browser usage" />

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?

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

The object-oriented model makes it easy to build up programs by accretion. What this often means, in practice, is that it provides a structured way to write spaghetti code. (Paul Graham)

This post tagged: