Commit 33a770db authored by M Parker's avatar M Parker

Add settings for the tablefield formatter.

parent 5f3fc149
......@@ -27,22 +27,182 @@ function d3_sankey_table_group_pp_field_formatter_info() {
$formatters['tablefield_d3_sankey_table_group_pp'] = array(
'label' => t('Sankey diagram (table grouping preprocessor)'),
'field types' => array('tablefield'),
'settings' => array(
'sankeyType' => D3_SANKEY_SANKEYTYPE_PATH,
'width' => '720',
'height' => '500',
'nodeWidth' => 24,
'nodePadding' => 8,
'spread' => TRUE,
'iterations' => 5,
'alignLabel' => D3_SANKEY_ALIGNLABEL_AUTO,
),
);
return $formatters;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function d3_sankey_table_group_pp_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$element = array();
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
if ($display['type'] === 'tablefield_d3_sankey_table_group_pp') {
// The type of Sankey chart.
$element['sankeyType'] = array(
'#type' => 'select',
'#title' => t('Sankey chart type'),
'#options' => d3_sankey_options_sankeytype(),
'#default_value' => $settings['sankeyType'],
);
// The width of the chart.
$element['width'] = array(
'#type' => 'textfield',
'#title' => t('Chart width'),
'#element_validate' => array('element_validate_integer_positive'),
'#default_value' => $settings['width'],
);
// The height of the chart.
$element['height'] = array(
'#type' => 'textfield',
'#title' => t('Chart height'),
'#element_validate' => array('element_validate_integer_positive'),
'#default_value' => $settings['height'],
);
// The width of nodes in the chart.
$element['nodeWidth'] = array(
'#type' => 'textfield',
'#title' => t('Node width'),
'#element_validate' => array('element_validate_integer_positive'),
'#default_value' => $settings['nodeWidth'],
);
// The padding on nodes in the chart.
$element['nodePadding'] = array(
'#type' => 'textfield',
'#title' => t('Node padding'),
'#element_validate' => array('element_validate_integer_positive'),
'#default_value' => $settings['nodePadding'],
);
// Whether to spread the nodes across the full chart height.
$element['spread'] = array(
'#type' => 'checkbox',
'#title' => t('Spread nodes across full height of chart'),
'#default_value' => $settings['spread'],
);
// The number of iterations to make so the chart looks good.
$element['iterations'] = array(
'#type' => 'textfield',
'#title' => t('Iterations'),
'#element_validate' => array('element_validate_integer_positive'),
'#default_value' => $settings['iterations'],
);
// Label alignments on the nodes.
$element['alignLabel'] = array(
'#type' => 'select',
'#title' => t('Label alignment'),
'#options' => d3_sankey_options_alignlabel(),
'#default_value' => $settings['alignLabel'],
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function d3_sankey_table_group_pp_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$sankey_types = d3_sankey_options_sankeytype();
$align_label_types = array(
D3_SANKEY_ALIGNLABEL_AUTO => t('automatically'),
D3_SANKEY_ALIGNLABEL_START => t('to the start of paths'),
D3_SANKEY_ALIGNLABEL_END => t('to the end of paths'),
);
$summary = '';
if ($display['type'] === 'tablefield_d3_sankey_table_group_pp') {
if ((bool) $settings['spread'] === TRUE) {
$summary .= t('%sankeyType sankey diagram having a width of %width, height of %height, and %iterations iterations. Nodes in the diagram have a width of %nodeWidth, a padding of %nodePadding, and are spread across the full height of the chart. Node labels are aligned %alignLabel.', array(
'%sankeyType' => (string) $sankey_types[$settings['sankeyType']],
'%width' => (string) $settings['width'],
'%height' => (string) $settings['height'],
'%iterations' => (string) $settings['iterations'],
'%nodeWidth' => (string) $settings['nodeWidth'],
'%nodePadding' => (string) $settings['nodePadding'],
'%alignLabel' => (string) $align_label_types[$settings['alignLabel']],
));
}
else {
$summary .= t('%sankeyType sankey diagram having a width of %width, height of %height, and %iterations iterations.; Nodes in the diagram have a width of %nodeWidth, a padding of %nodePadding, and are clustered at the top of the chart. Node labels are aligned %alignLabel.', array(
'%sankeyType' => (string) $sankey_types[$settings['sankeyType']],
'%width' => (string) $settings['width'],
'%height' => (string) $settings['height'],
'%iterations' => (string) $settings['iterations'],
'%nodeWidth' => (string) $settings['nodeWidth'],
'%nodePadding' => (string) $settings['nodePadding'],
'%alignLabel' => (string) $align_label_types[$settings['alignLabel']],
));
}
}
return $summary;
}
/**
* Implements hook_field_formatter_view().
*/
function d3_sankey_table_group_pp_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
// 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 = _d3_sankey_table_group_pp_default_chart_options();
// Apply the formatter settings to the chart.
$chart = array(
'sankeyType' => (string) $settings['sankeyType'],
'width' => (int) $settings['width'],
'height' => (int) $settings['height'],
'nodeWidth' => (int) $settings['nodeWidth'],
'nodePadding' => (int) $settings['nodePadding'],
'spread' => (bool) $settings['spread'],
'iterations' => (int) $settings['iterations'],
'alignLabel' => (string) $settings['alignLabel'],
) + _d3_sankey_table_group_pp_default_chart_options();
// Calculate an HTML ID and add that to the chart. Note this is required
// in order for the appropriate chart settings to be applied to the chart.
$id_parts = array(
// Because node/1 and user/1 could be displayed on the same page at the
// same time.
$entity_type,
// Note that we cannot reliably get the entity ID (it could be at
// $entity->nid or $entity->uid depending on the entity type), so we
// have to rely on drupal_html_id() to resolve this instance.
// Because there could be two separate fields showing sankey charts in
// the same entity.
$field['field_name'],
// Because there could be more than one value for this field.
$delta,
);
$chart['id'] = drupal_html_id(implode('-', $id_parts));
// Initialize a GroupingPreprocessor, feed it the query results, get the
// raw data out of the preprocessor, and add it to the chart.
......@@ -72,14 +232,5 @@ function d3_sankey_table_group_pp_field_formatter_view($entity_type, $entity, $f
function _d3_sankey_table_group_pp_default_chart_options() {
return array(
'type' => 'sankey',
'id' => 'visualization',
'sankeyType' => D3_SANKEY_SANKEYTYPE_PATH,
'width' => '720',
'height' => '500',
'nodeWidth' => 24,
'nodePadding' => 8,
'spread' => TRUE,
'iterations' => 5,
'alignLabel' => D3_SANKEY_ALIGNLABEL_AUTO,
);
}
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