By taragol on
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
When I've had to create
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)
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.
Thanks a lot! I could do it
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;
}
Great to see you've been
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.
Another option
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.
Great Info, but can't get it to work
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.
The chart_render function
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.
See this thread also
http://drupal.org/node/404840
you can display Pie chart without writing a module
Vikas kumar
Iprove it
it isnt working, :(