How do i transfer view-data to a chart api?
fryswe - April 11, 2009 - 12:04
hey guys!
i want to create a chart similiar the "usage statistic" on drupal.org (eg http://drupal.org/project/usage/computed_field).
i have a numerous data in a view-table. each data pair represents a separate node. now my question: how do i get the different values from the node into the chart?
i'm using google chart api.
thanks in advance,
fry

You do not say if you are
You do not say if you are using the drupal charts api or not, but regardless there are at least a couple ways to approach this.
One is to create the view and theme the final results to use the chart api not particularly efficient but it does not require a module.
A more advanced approach would be to write a module that implements charts as a view style.
ok, the view was created. now
ok, the view was created. now there is a table with three lines and two rows (i've created three nodes). the cck-field-names are "field_abc1" and "field_abc2".
how got I spend out the different values of the fields for theming? eg
<?php$abcx = $node-> field_abc1 [0] [ 'value'];
?>
thanks for your help
Here is one approach that
Here is one approach that uses the Chart API module.
This uses a view with style set to unformatted and row style to fields.
I picked the fields that represented to data and also the node title (the example uses it for labeling the x-axis.
Look under "Theme: information" (click information). For "Style output" the second choice will be of the form views-view-unformatted--view_name.tpl.php, in my case the view is called 'chart' so the file name is views-view-unformatted--chart.tpl.php.
Now in your theme directory add a new file with name above (in my case views-view-unformatted--chart.tpl.php).
Add the following to the file
<?php if (!empty($title)): ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<?php
$chart = array(
'#chart_id' => 'test_chart',
'#title' => chart_title(t('Chart Title'), 'cc0000', 15),
'#type' => CHART_TYPE_LINE,
'#size' => chart_size(400, 200),
'#adjust_resolution' => TRUE,
);
// You need one element here per datapoint (per row) being displayed
$chart['#data']['abc1'] = array();
$chart['#data']['abc2'] = array();
$max = 0;
foreach ( $view->result as $point ) {
// Set up the data
$chart['#data']['abc1'][] = $point->node_data_field_abc1_field_abc1_value;
$chart['#data']['abc2'][] = $point->node_data_field_abc1_field_abc2_value;
// Calculate the maximum value
if ( $max < $point->node_data_field_abc1_field_abc1_value ) {
$max = $point->node_data_field_abc1_field_abc1_value;
}
if ( $max < $point->node_data_field_abc1_field_abc2_value ) {
$max = $point->node_data_field_abc1_field_abc2_value;
}
// Add the x-axis label for this data point (example uses node title)
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label($point->node_title);
}
// Add a legend per data point
$chart['#legends'][] = t('abc1');
$chart['#legends'][] = t('abc2');
// Add a color for each data point
$chart['#data_colors'][] = '00ff00';
$chart['#data_colors'][] = 'ff0000';
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $max);
$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][1][] = chart_mixed_axis_label(t('Count'), 95);
$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][2][] = chart_mixed_axis_label(t('Points'), 50);
echo chart_render($chart);
?>
<pre>
<?php print print_r($view->result,TRUE); ?>
</pre>
The bottom three lines are for debugging, it shows the field names and values.
thanks for the code
thanks for the code snippet!
i dont understand this introductions! Theme: information? Sytle output? i have created the chart.tpl.php.....
an other question: how should i use the code to insert it into my view footer? if i copy the code above, there's a error like this:
warning: Invalid argument supplied for foreach() in /xxx/test/includes/common.inc(1464) : eval()'d code on line 15.(the chart will be displayed, but no values, screenshot: http://xs.to/xs.php?h=xs138&d=09156&f=gewicht966.jpg)You first need a view that
You first need a view that uses a row style of fields.
When editing the view under "Basic settings" there is a line that reads "Theme: information", click on "information" and you will see the list of template files you can override.
under "basic settings", there
under "basic settings", there is no line like "theme: information" or so.. (see screenshots -> http://xs.to/xs.php?h=xs138&d=09150&f=views_scrren775.jpg).. do i need a module? or it's drupal 6.x (views2)?? i'm only using drupal 5.16. view type is "table view"..the file in the theme-folder called chart.tpl.php
i have found it under views
i have found it under views -> theme wizard (drupal 5!!! *g). i've select my view "chart", choose "simple list" on "Select Theme Type" and pushed the button "Select Theme Type". then the theming wizard generates code for the template.php
then i have copyed your code snippet into views-list-chart.tpl.php...i've choosen "list view"..
now the view get out seven empty charts (because i have seven nodes now) an the following error...
* warning: Invalid argument supplied for foreach() in /test/themes/garland/views-list-chart.tpl.php on line 18.* warning: Invalid argument supplied for foreach() in /test/themes/garland/views-list-chart.tpl.php on line 18.
* warning: Invalid argument supplied for foreach() in /test/themes/garland/views-list-chart.tpl.php on line 18.
* warning: Invalid argument supplied for foreach() in /test/themes/garland/views-list-chart.tpl.php on line 18.
* warning: Invalid argument supplied for foreach() in /test/themes/garland/views-list-chart.tpl.php on line 18.
* warning: Invalid argument supplied for foreach() in /test/themes/garland/views-list-chart.tpl.php on line 18.
* warning: Invalid argument supplied for foreach() in /test/themes/garland/views-list-chart.tpl.php on line 18.
is "$view->result" not an array?? the output with "var_dump($view->result);" is "NULL"
I only want ONE chart with all values in it...."abc1" is a date (x-axis) and "abc2" is a weight (in kilokgrams) (y-axis)...only the weight (abc2) should be shown as a line in the chart.. how do I adjust your code?
Sorry, I missed the fact you
Sorry, I missed the fact you are using Drupal 5, the notes are for Drupal 6
could you not adjust it?
you could not adjust it?
how could i call up the
how could i call up the chart.tpl.php? wahhh...