d3_sankey_table_group_pp.module 9.31 KB
Newer Older
1
2
3
4
5
6
7
<?php

/**
 * @file
 * Hooks and helper functions for the d3_sankey_table_group_pp module.
 */

8
9
use Drupal\d3_sankey_table_group_pp\TableGroupingPreprocessor;

10
11
12
13
14
15
16
17
/* Hooks. */

/**
 * Implements hook_xautoload().
 */
function d3_sankey_table_group_pp_xautoload($adapter) {
  $adapter->absolute()->addPsr4('Drupal\d3_sankey_table_group_pp', 'src');
}
18
19
20
21
22
23
24
25
26
27
28
29

/**
 * 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'),
30
31
32
33
34
35
36
37
38
    'settings' => array(
      'sankeyType' => D3_SANKEY_SANKEYTYPE_PATH,
      'width' => '720',
      'height' => '500',
      'nodeWidth' => 24,
      'nodePadding' => 8,
      'spread' => TRUE,
      'iterations' => 5,
      'alignLabel' => D3_SANKEY_ALIGNLABEL_AUTO,
39
40
      'node_colors' => '',
      'link_colors' => '',
41
    ),
42
43
44
45
46
  );

  return $formatters;
}

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * 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'],
    );
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

    // Node colors.
    $element['node_colors'] = array(
      '#type' => 'textarea',
      '#title' => t('Override node colors'),
      '#description' => t('Enter a comma- or newline-separated list of colors to use for the nodes in the diagram.'),
      '#default_value' => $settings['node_colors'],
      '#element_validate' => array('d3_sankey_element_validate_optional_color_list'),
    );

    // Link colors.
    $element['link_colors'] = array(
      '#type' => 'textarea',
      '#title' => t('Override link colors'),
      '#description' => t('Enter a comma- or newline-separated list of colors to use for the links in the diagram.'),
      '#default_value' => $settings['link_colors'],
      '#element_validate' => array('d3_sankey_element_validate_optional_color_list'),
    );
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  }

  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;
}

184
185
186
187
188
/**
 * Implements hook_field_formatter_view().
 */
function d3_sankey_table_group_pp_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
189
  $settings = $display['settings'];
190
191
192
193
194

  // 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) {
195
196
197
198
199
200
201
202
203
204
205
206
      // 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();

207
208
209
210
      // If the user chose any node or link colors, add them to the chart.
      _d3_sankey_add_colors_to_chart($chart, 'node_colors', $settings['node_colors']);
      _d3_sankey_add_colors_to_chart($chart, 'link_colors', $settings['link_colors']);

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
      // 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));
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

      // 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;
}
249
250
251
252
253
254
255
256
257
258
259
260

/**
 * Returns some default chart options for Sankey charts generated here.
 *
 * @return array
 *   An array of common Sankey chart settings.
 */
function _d3_sankey_table_group_pp_default_chart_options() {
  return array(
    'type' => 'sankey',
  );
}