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

Surveys with Google Graphs


© December 2007 Anthony Lawrence

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="https://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


Inexpensive and informative Apple related e-books:

Photos: A Take Control Crash Course

Take Control of OS X Server

Photos for Mac: A Take Control Crash Course

Take Control of Numbers

Take Control of IOS 11




More Articles by © Anthony Lawrence




Printer Friendly Version

Have you tried Searching this site?

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


Printer Friendly Version





I was taught that the human brain was the crowning glory of evolution so far, but I think it’s a very poor scheme for survival (Kurt Vonnegut).




Linux posts

Troubleshooting posts


This post tagged:

Blogging

Code

Google

Linux

Programming

Unix

Web/HTML



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode





SCO Unix Sales, Support, & Service

Phone:  707-SCO-UNIX (707-726-8649Toll Free: 833-SCO-UNIX (833-726-8649)
www.SCOsales.com