Hi, great module! Totally works as needed. Thank you for all your work on this.

I have a situation where I need to create charts/graphs off of various table fields. Looking into utilizing the api from https://drupal.org/project/charts to do an alter on a field and append a chart to the output, and doing this wrapped in a custom module.

I'm wondering if anyone else has had this need, crafted a solution, etc, and if there is enough need to create an integration with this module.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

duncan.moo’s picture

I used tablefield module to achieve precisely what you describe also using the Charts module. I did not create a module, just created a function in the template.php, but creating a module that brings in a tablefield formater would be perfect.

<?php
function THEMENAME_preprocess_field(&$variables) {
	
	if($variables['element']['#field_name'] == 'field_chart_data'){
		
		$chart = to_bar_chart($variables['element']['#items'][0]['tabledata']);
		$variables['items'][0]['#markup'] = drupal_render($chart);
		
	}
}

function to_bar_chart($chart_data){
	
	$chart = array(
		'#plugin'    => 'google', // Google Charts API will be used
		'#type' => 'chart',
		'#chart_type' => 'bar',
		'#colors' => array('#d8d3d7', '#173966'),
		'#legend_position' => 'bottom',
		'#height'   => 300, // in pixels
		'#width'    => 'auto', // in pixels
	);
	
	foreach($chart_data as $row_key => $item_row){
		
		if($row_key == 0){ // this is the first row, get the series labels
			
			foreach($item_row as $item_key => $item){
				
				if($item_key == 0){ // this is the first item (X axis label)
					
					$chart['xaxis'] = array(
												'#type' => 'chart_xaxis',
												'#title' => $item,
											);
					
				} else{
					
					$chart[ $item_key ] = array(
												'#type' => 'chart_data',
												'#title' => $item
											);
					
				}
				
			}
			
		} else { // this is a row of data
			
			foreach($item_row as $item_key => $item){
				
				if($item_key == 0){ // this is the first item (X axis name)
					
					$chart['xaxis']['#labels'][] = $item;
					
				} else{
					
					$chart[ $item_key ]['#data'][] = (float)$item;
					
				}
				
			}
			
		}
		
	}
	
	return $chart;
	
}
?>
mark@keodesign.com.au’s picture

Issue summary: View changes

I've built a basic implementation to display a tableField as a chart. It's currently not configurable (draws a column chart). I'm just going to see if I can tidy it up a bit and I'll submit a patch. At this stage I'm not completely sure about the approach I've taken, but it fits my use case.

mark@keodesign.com.au’s picture

Status: Active » Needs review
FileSize
9.69 KB

This patch adds basic chart rendering support. Requires Drupal Charts (https://www.drupal.org/project/charts) to be installed to work. Have not added to dependencies as I don't think people want to have to install charts to use tablefield. I'm hoping it's possible to refine this to detect charts is installed and notify user that charts display is not supported without it.

mark@keodesign.com.au’s picture

This patch also detects the charts module is installed (and enabled) before offering the charts view.

mark@keodesign.com.au’s picture

This patch adds a label for the x, and the y axis where there are two columns. If the label is displayed for the y axis, the legend is not displayed (requires support in google charts).

mark@keodesign.com.au’s picture

FileSize
10.93 KB

Please disregard patch 5 - I've re-rolled it with the changes in place and it is attached.

kekkis’s picture

To me, this seems like a prime candidate for a separate module. This would solve the dependencies problem as well, not to mention keeping the structure clearer. I haven't done so yet but I did look into it a bit and found that it's not as simple as it sounds, having to do with fields (which I haven't... yet).

So @patrickfgoddard is in the right here IMO, suggesting creating a module around the integration rather than trying to patch it together with the original tablefield.module.

Anyway, I thought I'd let you know @mark that patch #6 downloads as UTF-16LE when I curl or wget it from its URL. I'm not fully aware of guidelines but I would seem to me that the optimal encoding is UTF-8. To that end, I'm attaching the same patch but with UTF-8 encoding.

thtas’s picture

I've re-rolled the patch against the latest dev 2.x of tablefield and I'll work on creating a stand-alone module version.

thtas’s picture

Module created

https://www.drupal.org/project/tablefield_chart

I've also added the ability to select which charts library to use for rendering, but it should fall back to the default one selected by the Charts module.

lolandese’s picture

Status: Needs review » Fixed

Thanks to all for your contribution. I have included a link to Tablefield Chart on the project page of TableField.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.