Display Drupal poll results as a pie chart

Using the Drupal Advanced Poll module in conjunction with the Drupal Charts module, I managed to setup an Advanced Poll which returns it's results as a Pie Chart rather than the default bar chart. This uses the Google Charts API to actually generate the Pie Chart.

Steps to reproduce:

  1. Install the Advanced Poll module & Charts module
  2. You need to add the below code to the poll results page template found in the Advanced Poll module, ie. /sites/all/modules/advpoll/templates/advpoll-results.tpl.php
    To ensure your changes don't get overwritten when doing module updates, make a copy of the results template and put it in your theme folder, eg. /sites/all/themes/myTheme/templates/advpoll-results.tpl.php
  3. Add the below code to the copy of your advpoll-results.tpl.php file.
<?php
$chart = array(
	'#chart_id' => 'pie_chart',
	'#title' => t('Results'),
	// the type of chart you want to display
	'#type' => CHART_TYPE_PIE,
	// the size of the chart
	'#size' => chart_size(818, 360),
);

// get the results of your poll
$result = db_query('SELECT advpoll_choice_choice, advpoll_choice_choice_id FROM field_data_advpoll_choice WHERE entity_id = ' . $nid );
$i = 0;
	if ($result) {
		while ($row = $result->fetchAssoc()) {
			// get poll choices
			$pollChoices = $row['advpoll_choice_choice'];
			// get ID associated with the choice
			$voteChoiceID = $row['advpoll_choice_choice_id'];
			// count votes per a choice
			$countVotes = db_query("SELECT tag, count(tag) AS total FROM votingapi_vote WHERE entity_id = " . $nid . " AND tag = '" . $voteChoiceID ."'");
			if ($countVotes) {
				while ($row = $countVotes->fetchAssoc()) {
					// get the count of votes per a choice 
					$voteTotal = $row['total'];
				}
			}	
			
			// iterate through the pie chart results
			if ($voteTotal > 0) {
				$i++;
				// get segment percentage
				$percentage = round($voteTotal / $total * 100);

				// generate the pie chart with the labels
				$chart['#data'][$pollChoices] = $voteTotal;	
				$chart['#labels'][] = t($percentage . '% (' . $voteTotal . ' votes)');
				$chart['#legends'][] = t($pollChoices);
				
				// set chart colours for up to 12 choices/segments)
				if ($i == 1) {$chart['#data_colors'][] = '8dd3c7';}
				if ($i == 2) {$chart['#data_colors'][] = 'ffffb3';}
				if ($i == 3) {$chart['#data_colors'][] = 'bebada';}
				if ($i == 4) {$chart['#data_colors'][] = 'fb8072';}
				if ($i == 5) {$chart['#data_colors'][] = '80b1d3';}
				if ($i == 6) {$chart['#data_colors'][] = 'fdb462';}
				if ($i == 7) {$chart['#data_colors'][] = 'b3de69';}
				if ($i == 8) {$chart['#data_colors'][] = 'fccde5';}
				if ($i == 9) {$chart['#data_colors'][] = 'd9d9d9';}
				if ($i == 10) {$chart['#data_colors'][] = 'bc80bd';}
				if ($i == 11) {$chart['#data_colors'][] = 'ccebc5';}
				if ($i == 12) {$chart['#data_colors'][] = 'ffed6f';}
				
			}
			
		}
	}
	echo theme('chart', array('chart' => $chart));
	
?>
Drupal version: 
Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +9 years.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.