Hi,

I need to display the results of a poll as a pie chart.

I'm trying with fusioncharts module, using the fusionchart type view, but I couldn't do it.

Has someone already done this?

Is there a way to accomplish this without writing code (using other modules)? If writing code is the only solution, could you please show me some help?

Thanks a lot!

Comments

epicflux’s picture

When I've had to create graphic charts I've used the Google Charts module. This does require doing some coding.

Looking at the poll_view_results you can see how to get your poll results numbers. You'd want to write a similar function to that, but return the results as a chart.

Here's an example of working with code to create a chart: (not adapted to working with poll results)

    foreach ($answer as $key => $value) {
        $chart['#labels'][] = $value['label'].': '.$value['count']; 
        $chart['#data'][] = (($value['count']) ? $value['count'] : '0');
        $chart['#data_colors'][] = chart_unique_color($field.$key);
      }
      $chart['#type'] = CHART_TYPE_PIE;
      $chart['#size'] = chart_size(500, 200);
      $chart['#encoding'] = CHART_ENCODING_TEXT;
      $chart['#chart_id'] = $field;
      $output .= chart_render($chart).'<hr />';

Note: Its been a while since I coded for the charts module so setting up the chart array may have changed.

It can take a bit of reading to understand all the options in the Google Charts api.

taragol’s picture

Thanks a lot! I could do it following your code. I changed the function poll_view_results as you said.

Here is the new function, if anyone else wants to do it:

function poll_view_results(&$node, $teaser, $page, $block) {
//find the total of votes
$sum = 0;
foreach($node->choice as $choice) {
$sum += $choice['chvotes'];
}

foreach($node->choice as $choice) {
$chart['#labels'][] = $choice['chtext'].': '.($sum ? round(($choice['chvotes'] * 100 / $sum),2) : '0').'%';
$chart['#data'][] = (($choice['chvotes']) ? $choice['chvotes'] : '0');
//$chart['#data_colors'][] = chart_unique_color(($choice['chtext']*10).$choice['chvotes']);
}

$chart['#type'] = CHART_TYPE_PIE;
$chart['#size'] = chart_size(500, 350);
$chart['#chart_id'] = 1;
$chart['#encoding'] = CHART_ENCODING_TEXT;
$chart['#data_colors'] = array('FF0000', 'FF7700','FFFF00','00FF00', '0000FF', 'FF00FF');
$chart['#chart_fill'] = array('chart_global_bg','s','FFFFFF');

$output .= chart_render($chart).'


';

return $output;
}

epicflux’s picture

Great to see you've been able to get it working. Please follow aaron1234nz's advice and do your custom output in a template file. You'll want to code a function similar to theme_poll_results using your chart code as the output. You should never edit the core files.

aaron1234nz’s picture

Another option would be overriding the theme functions in the poll module. In this case I've used the fusioncharts module to produce the charts.
Place the contents of this file inside your themes template.php file replacing references to 'mytheme' with the name of your theme.

function mytheme_poll_results($title, $results, $votes, $links, $block, $nid, $vote) {
    $output .= '<div class="poll">';
    $output .= '<div class="title">'. $title .'</div>';
    $output .= fusioncharts_render(FUSIONCHART_ARRAY, 'mytheme_pollresults', 'poll_'. $nid, array('block' => $block));
    $output .= '<div class="total">'. t('Total votes: %votes', array('%votes' => $votes)) .'</div>';
    $output .= '</div>';
    $output .= '<div class="links">'. theme('links', $links) .'</div>';
  return $output;
}


function mytheme_poll_bar($title, $percentage, $votes, $block, $return = NULL) {
  static $data; //we will collect all data from multiple calls to this function
  if (isset($return)) {
    return $data;
  }
  else {
    $data[] = array($title, $percentage);
  }
}

function mytheme_pollresults_fusionchart_callback($args = NULL) {
  $data = mytheme_poll_bar('', '', '', '', 1);
  $type = 'Column 3D';
  $settings = array();
  $attributes = array();
  if ($args['block'] == 1) {
    $width = 200;
    $height = 200;
    $attributes['color'] = array('FF0000', '00ff00', '0000ff', 'ffff00', 'ff00ff', '00ffff'); //no # in front of color
    $settings['showvalues'] = 0;
    $settings['shownames'] = 1;
	}
  else {
    $width = 400;
    $height = 300;    
  }
  return array($data, $type, $settings, $attributes, $width, $height);
}

mohan.ankur’s picture

Using Changing Module suggestion ...

Fatal error: Call to undefined function chart_render() in C:\xampp\htdocs\modules\poll\poll.module on line 669

I get this error when I try to change the code in Poll module.
I am using Drupal 6x, what is it that i need to include to make sure these functions can be referenced?

Using template.php file suggestion:

Can you please explain how do I reference mytheme with the theme I am using. Should I simply put anything which looks like the theme name?

Many Thanks for your help in advance.

epicflux’s picture

The chart_render function comes from the chart module, you'll need to install that module to use that function.

To learn about overriding theme functions please start here.

IbnDrupal’s picture

babusaheb.vikas’s picture

 $chart = array(
      '#chart_id' => 'test_chart',
      '#title' => chart_title(t('Answers'), 'cc0000', 15),
      '#type' => CHART_TYPE_PIE,
      '#size' => chart_size(400, 200),
    );


// Here is my query , i am taking data from database table and creating a pie chart on the basis of data taken 


$result = db_query("select field_p_like_value, count(field_p_like_value) as times_entered from field_revision_field_p_like group by field_p_like_value");

//while( $resquery_res = db_fetch_object($resquery) ) {



	foreach ($result as $resquery_res) {	
    
  $chart['#data'][$resquery_res->field_p_like_value] = $resquery_res->times_entered;


  $chart['#labels'][] = t($resquery_res->field_p_like_value);


}
return theme('chart', array('chart' => $chart)); 


Vikas kumar

Citlalli’s picture

it isnt working, :(