我正在尝试将Webform结果显示为Google Charts。我通过在主题的template.php文件上覆盖theme_webform_results_analysis()并使用Chart模块在主题层上进行此操作。Drupal 6.22,Webform 6.x-3.11。
Webform分析页面通常在表中显示数据,因此我试图拆分该表的数组,以将内容传递到Chart API中。
编辑:我想出了如何使用var_dump,发现更好的方法可能是分别进入$ row_data和$ questions数组(而不是使用我在此问题的第一个版本中获得的$ rows数组,两个数组的混搭)。
编辑#2:我想我已经找到了如何获取原始$ questions和$ row_data数组的每一部分的方法(请参见下文-其他foreach中的foreach)。所以现在我需要将这些片段放入适当的数组中(每个问题1个),并找到一种遍历所有这些片段的方法。
这是我在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;
}