11

I'm trying to display webform results as Google Charts. I'm doing this on the theme layer by overriding theme_webform_results_analysis() on my theme's template.php file and using the Chart module. Drupal 6.22, Webform 6.x-3.11.

The webform analysis page normally shows the data in a table, so I'm trying to hack apart the arrays for that table to pass stuff into the Chart API.

EDIT: I figured out how to use var_dump and found that the better approach might be to get up in the $row_data and $questions arrays separately (instead of using the $rows array that I had in the first version of this question, which was a mashup of both arrays).

EDIT #2: I think I've discovered how to grab each piece of the original $questions and $row_data arrays (see below - the foreach in the other foreach). So now I need to get those pieces into proper arrays (1 per question) and find a way to iterate through all of those.

Here's what I've got in template.php:

/**
 * Output the content of the Analysis page.
 * @see webform_results_analysis()
 */
function mytheme_webform_results_analysis($node, $data, $sids = array(), $analysis_component = NULL) {

  foreach ($data as $cid => $row_data) {

    if (is_array($row_data)) {

      // get the questions, put them in an array
      $questions = array();
      $questions[] = array('data' => check_plain($node->webform['components'][$cid]['name']));

      // this will print everything out in the right order - it really needs to
      // make an array for each question that looks like $test_chart below
      foreach ($questions as $question) {
        print $question['data'] . '<br />'; // questions 
        foreach ($row_data as $key => $value) {
          print $value[0] . '<br />'; // labels
          print $value[1] . '<br />'; // results 
        }
      }

      // Set up the chart
      $chart = array(
        '#chart_id' => 'webform_analysis',
        '#type' => CHART_TYPE_PIE_3D,
        '#size' => chart_size(658, 250)
      );

      // not real data here, this just shows the format I'm shooting for
      $test_chart = array(
        'option 1' => '12',
        'option 2' => '45',
        'option 3' => '122'
      ); 

      // separate the above array into labels and values, add a percentage to the label
      foreach ($test_chart as $key => $value) {
        $chart['#data'][] = $test_chart[$key];
        $chart['#labels'][] = strip_tags($key) . ' (' . round($test_chart[$key], 2) .  '%)';
      }
      // pick some colors
      $chart['#data_colors'][] = 'b0c73d';
      $chart['#data_colors'][] = '667323';
      $chart['#data_colors'][] = '221f1f';

      $output = chart_render($chart);    
    }
  }

  if (count($row_data) == 0) {
    $output = t('There are no submissions for this form.');
  }

  // return the data that goes into chart function, just for testing
  // return $chart_data; 

  // someday, this might return a set of webform charts. right now it returns the fake test chart
  // return $output;
}
2
  • 1
    did you solve your own question. If so post your own answer
    – iStryker
    Commented Jul 19, 2011 at 1:00
  • Nope, I haven't figured this out yet. It's on hold for now until I finish reading "Beginning PHP and MySQL5: from novice to professional" (link), and then I'm going to give it another shot. I struck out at getting help online, so I figured I just need to get a better handle on fundamentals before attempting something this tricky. Commented Aug 1, 2011 at 17:09

3 Answers 3

2

There is another Drupal charts module that provides support for webforms: http://drupal.org/project/fusioncharts

Fusionchart has a little sub-module called fusioncharts_webform

1
  • Thanks for your response! I looked at FusionCharts, but it's not a good fit for my project since it uses Flash and we'd need the paid version - we try to stick to open-source solutions. But it looks nice, I bet that solves this problem for lots of people. Commented Aug 1, 2011 at 17:14
1

More then 3 years later now, I know, but things have evolved a bit since then ...

By only considering the title of this question, one might want to address this via the Charts module these days (though the original question started from Chart, which is another module). And this in combination with either Webform Chart or Webform Charts.

Depending on what your actual needs are today, it appears that with these 2 Webform related modules, quite a lot can be achieved by site building activities (probably no more need for Drupal developers skills). Refer to bullet 5 in Modules facilitating charting via the Charts module for some more details about these Webform Chart(s) modules (and link to an interesting discussion).

Use the Comparison of charting modules for potential other alternatives these days (and be ready to really get confused about the dozens of charting modules available ...).

Be aware: I'm a new (co-) maintainer of Chart and Charts, and the author of the Comparison of charting modules. I'm trying to continue Quicksketch's (amazing) contributions to Charts, who is also the maintainer of Webform, and Webform ChartS (with an 's').

PS: Sorry for all those confusing *chart(s) namespaces, I did not invent them ...

1

Charts now integrates with webform views (views cumulative field is also quite usable too).

See also charts ecosystem for even more possibilites.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.