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:


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

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:








Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode