Commit 987e7ca3 authored by M Parker's avatar M Parker

Add a tablefield formatter that displays a Sankey diagram.

parent 7385134a
......@@ -5,6 +5,8 @@
* Hooks and helper functions for the d3_sankey_table_group_pp module.
*/
use Drupal\d3_sankey_table_group_pp\TableGroupingPreprocessor;
/* Hooks. */
/**
......@@ -13,3 +15,50 @@
function d3_sankey_table_group_pp_xautoload($adapter) {
$adapter->absolute()->addPsr4('Drupal\d3_sankey_table_group_pp', 'src');
}
/**
* Implements hook_field_formatter_info().
*/
function d3_sankey_table_group_pp_field_formatter_info() {
$formatters = array();
// A field formatter for tablefields, which displays a Sankey diagram using
// the table grouping preprocessor.
$formatters['tablefield_d3_sankey_table_group_pp'] = array(
'label' => t('Sankey diagram (table grouping preprocessor)'),
'field types' => array('tablefield'),
);
return $formatters;
}
/**
* Implements hook_field_formatter_view().
*/
function d3_sankey_table_group_pp_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
// A field formatter for tablefields, which displays a Sankey diagram using
// the table grouping preprocessor.
if ($display['type'] === 'tablefield_d3_sankey_table_group_pp') {
foreach ($items as $delta => $item) {
$chart = _uw_career_common_chart_options();
// Initialize a GroupingPreprocessor, feed it the query results, get the
// raw data out of the preprocessor, and add it to the chart.
$preprocessor = new TableGroupingPreprocessor();
foreach ($item['tabledata'] as $row) {
$preprocessor->ingestRow((array) $row);
}
$raw_data = $preprocessor->getRawData();
$chart['nodes'] = $raw_data->getAssocArrayNodes();
$chart['links'] = $raw_data->getAssocArrayLinks();
// Now draw the chart.
$chart = d3_draw($chart);
$element[$delta]['#markup'] = $chart;
}
}
return $element;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment