Commit 7385134a authored by M Parker's avatar M Parker

Add an example to demonstrate the table grouping preprocessor.

parent 8813bbcc
......@@ -36,6 +36,21 @@ function d3_sankey_examples_menu() {
'weight' => -1,
) + $items['d3/examples/sankey'];
// Uses d3_sankey_table_group_pp to create a Sankey chart.
if (module_exists('d3_sankey_table_group_pp')) {
$items['d3/examples/sankey/table_group_preprocess'] = array(
'title' => 'Table grouping preprocessor',
'description' => 'Creates a Sankey chart using a preprocessor that links values in the same row, grouping duplicates.',
// For consistency with d3_examples, this has no access restriction (i.e.:
// an open page callback). Note that in this module's implementation of
// hook_requirements() (in d3_sankey_examples.install), we remind users to
// disable this module on the live site.
'access callback' => TRUE,
'page callback' => '_d3_sankey_examples_table_group_pp',
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
......@@ -174,3 +189,51 @@ function _d3_sankey_examples_sankey() {
return d3_draw($chart);
}
/**
* Menu callback: Uses d3_sankey_table_group_pp to create a Sankey chart.
*/
function _d3_sankey_examples_table_group_pp() {
$chart = array(
'type' => 'sankey',
// See _d3_sankey_examples_sankey() for an explanation of these properties.
'id' => 'visualization',
'sankeyType' => D3_SANKEY_SANKEYTYPE_PATH,
'width' => '700',
'height' => '400',
'nodeWidth' => 24,
'nodePadding' => 8,
'spread' => TRUE,
'iterations' => 1,
'alignLabel' => D3_SANKEY_ALIGNLABEL_AUTO,
);
// Create some sample data.
$unprocessed_data = array(
array('1970s', 'Drama', 'The Godfather'),
array('1970s', 'Sci-fi/Fantasy', 'Star Wars'),
array('1980s', 'Sci-fi/Fantasy', 'The Empire Strikes Back'),
array('1990s', 'Drama', 'The Shawshank Redemption'),
array('1990s', 'Biography', "Schindler's List"),
array('2000s', 'Action', 'The Dark Knight'),
array('2000s', 'Sci-fi/Fantasy', 'LotR: Return of the King'),
);
// Initialize a GroupingPreprocessor.
$preprocessor = new \Drupal\d3_sankey_table_group_pp\TableGroupingPreprocessor();
// Feed the preprocessor the sample data.
foreach ($unprocessed_data as $row) {
$preprocessor->ingestRow($row);
}
// Get an object containing the raw chart data out of the preprocessor.
$raw_data = $preprocessor->getRawData();
// Add the nodes and links to the chart.
$chart['nodes'] = $raw_data->getAssocArrayNodes();
$chart['links'] = $raw_data->getAssocArrayLinks();
return d3_draw($chart);
}
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