d3_sankey.module 8.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
<?php

/**
 * @file
 * Hooks and helper functions for the d3_sankey module.
 *
 * To get the D3 module to recognize that this module defines a plugin for it,
 * we need a JS library that begins with 'd3.', contains no further dots in the
 * name, and depends on the 'd3-plugins-sankey', 'd3.chart', and
 * 'd3.chart.sankey' libraries we define in d3_sankey_libraries_info().
 *
 * From experience, the 'd3.chart.sankey' module won't work because it contains
 * an extra dot in the name. So we have to make a dummy library.
 */

/* Constants. */

/**
 * A type of Sankey chart that does not select nodes or links on hover.
 *
 * Set the 'sankeyType' setting to this value in order to create a chart of this
 * type. This type will also be selected if a 'sankeyType' is not specified.
 */
define('D3_SANKEY_SANKEYTYPE_DEFAULT', 'Sankey');

/**
 * A type of Sankey chart that only selects the current node or link on hover.
 *
 * Set the 'sankeyType' setting to this value in order to create a chart of this
 * type.
 */
define('D3_SANKEY_SANKEYTYPE_SELECTION', 'Sankey.Selection');

/**
 * A type of Sankey chart that selects the current node/link and its siblings.
 *
 * Set the 'sankeyType' setting to this value in order to create a chart of this
 * type.
 */
define('D3_SANKEY_SANKEYTYPE_PATH', 'Sankey.Path');

/**
 * A setting to align node labels automatically.
 *
 * You probably want this alignment unless you are prepared to write custom CSS
 * to handle labels overflowing one side of the diagram and being clipped.
 *
 * Set the 'alignLabel' setting to this value in order to create a chart of this
 * type. This type will also be selected if a 'alignLabel' is not specified.
 */
define('D3_SANKEY_ALIGNLABEL_AUTO', 'auto');

/**
 * A setting to align node labels to the start of paths connecting from them.
 *
 * In LTR languages, this setting will result in the labels for the right-most
 * column of nodes overflowing on the right-hand side of the chart; default CSS
 * causes these labels to be clipped (i.e.: not visible).
 *
 * Set the 'alignLabel' setting to this value in order to create a chart of this
 * type.
 */
define('D3_SANKEY_ALIGNLABEL_START', 'start');

/**
 * A setting to align node labels to the end of paths connecting to them.
 *
 * In LTR languages, this setting will result in the labels for the left-most
 * column of nodes overflowing on the left-hand side of the chart; default CSS
 * causes these labels to be clipped (i.e.: not visible).
 *
 * Set the 'alignLabel' setting to this value in order to create a chart of this
 * type.
 */
define('D3_SANKEY_ALIGNLABEL_END', 'end');

/* Hooks. */

79
80
81
82
83
84
85
/**
 * Implements hook_xautoload().
 */
function d3_sankey_xautoload($adapter) {
  $adapter->absolute()->addPsr4('Drupal\d3_sankey', 'src');
}

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
184
185
186
187
188
189
190
191
192
/**
 * Implements hook_libraries_info().
 */
function d3_sankey_libraries_info() {
  $libraries = array();

  // The d3-plugins-sankey library: New Relic's fork of the default D3 Sankey
  // plugin. New Relic's fork displays nodes even if they don't have links to
  // them, has Bower support, interpolated numbers, and uses the D3 API more
  // consistently.
  $libraries['d3-plugins-sankey'] = array(
    'name' => 'd3-sankey',
    'vendor url' => 'https://github.com/newrelic-forks/d3-plugins-sankey',
    'download url' => 'https://github.com/newrelic-forks/d3-plugins-sankey/releases',
    'download file url' => 'https://github.com/newrelic-forks/d3-plugins-sankey/archive/v1.1.0.tar.gz',
    'version arguments' => array(
      'file' => 'bower.json',
      'pattern' => '@"version"[\s]*:[\s]*"([0-9a-zA-Z\.-]+)"@',
    ),
    'files' => array(
      'js' => array('sankey.js'),
    ),
    'dependencies' => array('d3'),
  );

  // The d3.chart library.
  $libraries['d3.chart'] = array(
    'name' => 'd3.chart',
    'vendor url' => 'https://github.com/misoproject/d3.chart',
    'download url' => 'https://github.com/misoproject/d3.chart/releases',
    'download file url' => 'https://github.com/misoproject/d3.chart/archive/v0.2.1.tar.gz',
    'version arguments' => array(
      'file' => 'd3.chart.js',
      'pattern' => '@d3\.chart[\s]+-[\s]+v([0-9a-zA-Z\.-]+)@',
    ),
    'files' => array(
      'js' => array('d3.chart.js'),
    ),
    'variants' => array(
      'minified' => array(
        'js' => array('d3.chart.min.js'),
      ),
    ),
    'dependencies' => array('d3'),
  );

  // The d3.chart.sankey library.
  $libraries['d3.chart.sankey'] = array(
    'name' => 'd3.chart.sankey',
    'vendor url' => 'https://github.com/q-m/d3.chart.sankey',
    'download url' => 'https://github.com/q-m/d3.chart.sankey/releases',
    'download file url' => 'https://github.com/q-m/d3.chart.sankey/archive/v0.2.0.tar.gz',
    'version arguments' => array(
      'file' => 'd3.chart.sankey.js',
      'pattern' => '@d3\.chart\.sankey[\s]+-[\s]+v([0-9a-zA-Z\.-]+)@',
    ),
    'files' => array(
      'js' => array('d3.chart.sankey.js'),
    ),
    'variants' => array(
      'minified' => array(
        'js' => array('d3.chart.sankey.min.js'),
      ),
    ),
    'dependencies' => array('d3-plugins-sankey', 'd3.chart'),
    'integration files' => array(
      'd3_charts_sankey' => array(
        'js' => array('d3_charts_sankey.js'),
      ),
    ),
  );

  return $libraries;
}

/**
 * Implements hook_libraries_info_file_paths().
 */
function d3_sankey_libraries_info_file_paths() {
  $paths = array();

  // See the file doc comment for context. Since we are defining a dummy JS
  // library that exists solely for the purpose of making this module work with
  // the D3 Drupal module, and also because D3 expects us to include it in this
  // module, we need to tell the Libraries API module where to find the library
  // file.
  $paths[] = _d3_sankey_js_library_path_prefix() . 'd3.sankey';

  return $paths;
}

/**
 * Implements hook_libraries_info_alter().
 */
function d3_sankey_libraries_info_alter(&$libraries) {
  // See the file doc comment for context. By a convention set by the D3 Drupal
  // module and also used by the d3_charts module (a separate project that also
  // defines a plugin for the D3 Drupal module), we are putting the
  // .libraries.info file that defines a library to the Libraries API inside the
  // folder that contains the library. So we also have to tell the Libraries API
  // where to look for the .libraries.info file.
  $libraries['d3.sankey']['library path'] = _d3_sankey_js_library_path_prefix()
    . 'd3.sankey';
}

/* Helper functions. */

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
 * Helper function to return an array of Sankey chart type options.
 *
 * The output from this function is intended to be used as the '#options'
 * property for Form API 'checkboxes', 'radios', 'select', and 'tableselect'
 * controls.
 *
 * @return array
 *   An associative array, where the key is a string representing the type of
 *   Sankey chart, and the value is a string containing a translated label for
 *   that type of Sankey chart.
 */
function d3_sankey_options_sankeytype() {
  return array(
    D3_SANKEY_SANKEYTYPE_DEFAULT => t('Default'),
    D3_SANKEY_SANKEYTYPE_SELECTION => t('Current item selection'),
    D3_SANKEY_SANKEYTYPE_PATH => t('Current item and path selection'),
  );
}

/**
 * Helper function to return an array of Sankey label alignment options.
 *
 * The output from this function is intended to be used as the '#options'
 * property for Form API 'checkboxes', 'radios', 'select', and 'tableselect'
 * controls.
 *
 * @return array
 *   An associative array, where the key is a string representing the alignment
 *   for node labels in a Sankey chart, and the value is a string containing a
 *   translated label for that type of label alignment.
 */
function d3_sankey_options_alignlabel() {
  return array(
    D3_SANKEY_ALIGNLABEL_AUTO => t('Automatically align labels'),
    D3_SANKEY_ALIGNLABEL_START => t('Align labels to start of paths'),
    D3_SANKEY_ALIGNLABEL_END => t('Align labels to end of paths'),
  );
}

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
 * Helper function to return the path to the libraries contained in this module.
 *
 * @param bool $reset
 *   Reset the static cache of the path.
 *
 * @return string
 *   A string representing the path to the libraries contained in this module.
 */
function _d3_sankey_js_library_path_prefix($reset = FALSE) {
  $path = &drupal_static(__FUNCTION__);
  if (!isset($path) || $reset) {
    $path = drupal_get_path('module', 'd3_sankey') . '/libraries/';
  }

  return (string) $path;
}