-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtablepress_chartist.php
More file actions
390 lines (359 loc) · 12.5 KB
/
tablepress_chartist.php
File metadata and controls
390 lines (359 loc) · 12.5 KB
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
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
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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/*
Plugin Name: TablePress Extension: Chartist
Plugin URI: https://github.com/silsha/tablepress_chartist
Description: Extension for TablePress to create a responsive chart based on the data in a TablePress table.
Version: 0.10.1
Author: Silsha Fux
Author URI: https://silsha.me
License: GPL
*/
// Prohibit direct script loading.
defined('ABSPATH') || die('No direct script access allowed!');
/*
* Initialize the TablePress Chartist Extension.
*/
add_action('tablepress_run', ['TablePress_Chartist', 'init']);
/**
* Class that contains the TablePress Chartist Extension functionality.
*
* @author Per Soderlind, Tobias Bäthge
*
* @since 0.1
*/
class TablePress_Chartist
{
/**
* Version number of the Extension.
*
* @since 0.1
*
* @var string
*/
protected static $version = '0.10.1';
/**
* Available Shortcode attributes, without the `chartist_` prefix.
*
* @since 0.2
*
* @var array
*/
protected static $shortcode_attributes = [
'low' => null,
'high' => null,
'width' => '',
'height' => '',
'chart' => 'line',
'showline' => true,
'showarea' => false,
'showpoint' => true,
'linesmooth' => true,
'aspect_ratio' => '3:4',
'horizontal' => false,
'stack' => false,
'animation' => false,
'label_offset' => false,
'chart_padding' => false,
'donut_width' => false,
];
/**
* Mapping of some Shortcode parameters to their ChartistJS equivalent.
*
* @since 0.2
*
* @var array
*/
protected static $attribute_to_js_mapping = [
'low' => 'low',
'high' => 'high',
'showline' => 'showLine',
'showarea' => 'showArea',
'showpoint' => 'showPoint',
'linesmooth' => 'lineSmooth',
'horizontal' => 'horizontalBars',
'stack' => 'stackBars',
'label_offset' => 'labelOffset',
'chart_padding' => 'chartPadding',
'donut_width' => 'donutWidth',
];
/**
* Available aspect ratios for the chart.
*
* @since 0.2
*
* @var array
*/
protected static $aspect_ratios = [
'1' => 'ct-square',
'15:16' => 'ct-minor-second',
'8:9' => 'ct-major-second',
'5:6' => 'ct-minor-third',
'4:5' => 'ct-major-third',
'3:4' => 'ct-perfect-fourth',
'2:3' => 'ct-perfect-fifth',
'5:8' => 'ct-minor-sixth',
'1:1.618' => 'ct-golden-section',
'3:5' => 'ct-major-sixth',
'9:16' => 'ct-minor-seventh',
'8:15' => 'ct-major-seventh',
'1:2' => 'ct-octave',
'2:5' => 'ct-major-tenth',
'3:8' => 'ct-major-eleventh',
'1:3' => 'ct-major-twelfth',
'1:4' => 'ct-double-octave',
];
/**
* Register necessary plugin filter hooks and the [table-chart] Shortcode.
*
* @since 0.1
*/
public static function init()
{
add_action('wp_enqueue_scripts', [__CLASS__, 'enqueue_scripts_styles']);
add_filter('tablepress_shortcode_table_default_shortcode_atts', [__CLASS__, 'register_shortcode_attributes']);
add_filter('tablepress_table_output', [__CLASS__, 'generate_chart'], 10, 3);
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
// TablePress doesn't support the REST API
} else {
add_shortcode('table-chart', [__CLASS__, 'handle_table_chart_shortcode']);
}
}
/**
* Handle Shortcode [table-chart id=<ID> /] in `the_content()`.
*
* @since 0.6
*
* @param array $shortcode_atts List of attributes that where included in the Shortcode.
*
* @return string Generated HTML code for the chart with the ID <ID>.
*/
public static function handle_table_chart_shortcode($shortcode_atts)
{
// Generate the attribute query array for the template tag function.
$table_query = [
'chartist' => true,
];
// Pass all parameters to the template tag parameters.
foreach ((array) $shortcode_atts as $attribute => $value) {
// Prepend 'chartist_' to all Shortcode attributes that the Extension understands.
if (isset(self::$shortcode_attributes[$attribute])) {
$attribute = 'chartist_'.$attribute;
}
$table_query[$attribute] = $value;
}
if ( function_exists( 'tablepress_get_table' )) {
return tablepress_get_table($table_query);
}
return false;
}
/**
* Load Chartist JavaScript and CSS files.
*
* @since 0.1
*/
public static function enqueue_scripts_styles()
{
global $post;
if (is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'table-chart')) {
$dir = plugin_dir_url(__FILE__);
wp_enqueue_script('chartist-js', $dir.'libdist/chartist.min.js', ['jquery'], self::$version, true);
wp_enqueue_style('chartist-css', $dir.'libdist/chartist.min.css', [], self::$version);
if (file_exists(WP_CONTENT_DIR.'/tablepress-chartist-custom.css')) {
wp_enqueue_style('chartist-custom-css', content_url('tablepress-chartist-custom.css'), ['chartist-css'], self::$version);
}
}
}
/**
* Add the Extension's parameters as valid [table /] Shortcode attributes.
*
* @since 0.1
*
* @param array $default_atts Default attributes for the TablePress [table /] Shortcode.
*
* @return array Extended attributes for the Shortcode.
*/
public static function register_shortcode_attributes($default_atts)
{
$default_atts['chartist'] = false;
foreach (self::$shortcode_attributes as $attribute => $value) {
$default_atts['chartist_'.$attribute] = $value;
}
return $default_atts;
}
/**
* Generate the HTML and JavaScript code for a Chartist chart, based on the data of the given table.
*
* @since 0.1
*
* @param string $output The generated HTML for the table.
* @param array $table The current table.
* @param array $render_options The render options for the table.
*
* @return string The generated HTML and JavaScript code for the chart.
*/
public static function generate_chart($output, $table, $render_options)
{
if (!$render_options['chartist']) {
return $output;
}
$json_chart_options = [];
// Determine/sanitize the chart type and add JS calculation functions.
switch (strtolower($render_options['chartist_chart'])) {
case 'bar':
$chart = 'Bar';
break;
case 'pie':
$chart = 'Pie';
$json_chart_options[] = 'labelInterpolationFnc: function( value ) { return value; }';
break;
case 'donut':
$chart = 'Pie';
$json_chart_options[] = 'labelInterpolationFnc: function( value ) { return value; }';
$json_chart_options[] = 'donut: true';
break;
case 'percent':
$chart = 'Pie';
$json_chart_options[] = "labelInterpolationFnc: function( value ) { return Math.round( value / data.series.reduce( sum ) * 100 ) + '%'; }";
break;
case 'piepercent':
$chart = 'Pie';
$json_chart_options[] = "labelInterpolationFnc: function( value, index ) { return value + ' (' + Math.round(data.series[index] / data.series.reduce( sum ) * 100) + '%)';}";
break;
case 'donutpercent':
$chart = 'Pie';
$json_chart_options[] = "labelInterpolationFnc: function( value, index ) { return value + ' (' + Math.round(data.series[index] / data.series.reduce( sum ) * 100) + '%)';}";
$json_chart_options[] = 'donut: true';
break;
case 'line':
default:
$chart = 'Line';
break;
}
// animation frame
$animation_script = <<<'JS'
chart.on('draw', function(data) {
%s
});
JS;
// Setup animation for chart
switch (strtolower($render_options['chartist_animation'])) {
case 'buildup':
$animation = <<<'JS'
if(data.type === 'line' || data.type === 'area') {
data.element.animate({
d: {
begin: 1500 * data.index,
dur: 1500,
from: data.path.clone().scale(1, 0).translate(0, data.chartRect.height()).stringify(),
to: data.path.clone().stringify(),
easing: Chartist.Svg.Easing.easeOutQuint
}
});
}
if (data.type === 'bar') {
data.element.attr({
style: 'stroke-width: 0px'
});
var strokeWidth = 10;
for (var s = 0; s < data.series.length; ++s) {
if (data.seriesIndex === s) {
data.element.animate({
y2: {
begin: s * 1500,
dur: 1500,
from: data.y1,
to: data.y2,
easing: Chartist.Svg.Easing.easeOutSine
},
'stroke-width': {
begin: s * 1500,
dur: 1,
from: 0,
to: strokeWidth,
fill: 'freeze'
}
}, false);
}
}
}
JS;
$animation_script = sprintf($animation_script, $animation);
break;
default:
$animation_script = '';
}
// Convert all numeric table cell values to numeric variables, so that they show up as numbers in the JSON encoded string, as ChartistJS requires that.
foreach ($table['data'] as $row_idx => $row) {
foreach ($row as $col_idx => $cell) {
$table['data'][$row_idx][$col_idx] = self::_maybe_string_to_number($cell);
}
}
// Get labels from the first table row.
if ($render_options['table_head']) {
$json_labels = array_shift($table['data']);
}
// Use only the first row in Pie charts.
if ('Pie' === $chart) {
$table['data'] = array_shift($table['data']);
}
// Create JSON object for the chart data.
$json_chart_data = [
'series' => $table['data'],
];
if ($render_options['table_head'] && 'percent' !== $render_options['chartist_chart']) {
$json_chart_data['labels'] = $json_labels;
}
$json_chart_data = json_encode((object) $json_chart_data);
// Add other chart options.
foreach (self::$attribute_to_js_mapping as $option_key => $option_js) {
$option_key = 'chartist_'.$option_key;
if (isset($render_options[$option_key])) {
$value = self::_maybe_string_to_number($render_options[$option_key]);
$json_chart_options[] = $option_js.': '.json_encode($value);
}
}
$json_chart_options = '{ '.implode(', ', $json_chart_options).' }';
// Sanitize the aspect ratio.
$aspect_ratio = 'ct-perfect-fourth';
if (isset(self::$aspect_ratios[$render_options['chartist_aspect_ratio']])) {
$aspect_ratio = self::$aspect_ratios[$render_options['chartist_aspect_ratio']];
}
$chartist_script = <<<JS
<script type="text/javascript">
jQuery(document).ready(function(){
var data = {$json_chart_data},
options = {$json_chart_options},
sum = function( a, b ) { return a + b; };
var chart = new Chartist.{$chart}( '#chartist-{$render_options['html_id']}', data, options );
{$animation_script}
});
</script>
JS;
$chartist_divtag = sprintf(
"<div id=\"%s\" class=\"ct-chart %s\"></div>\n",
"chartist-{$render_options['html_id']}",
$aspect_ratio
);
return $chartist_divtag.$chartist_script;
}
/**
* Convert a string to int or float, if it's a numeric string.
*
* @since 0.6
*
* @param string $string String that shall be converted to a number.
*
* @return mixed Possibly converted string.
*/
protected static function _maybe_string_to_number($string)
{
if (!is_numeric($string)) {
return $string;
}
if ($string == (int) $string) { // Don't do explicit === check here!
return (int) $string;
} else {
return (float) $string;
}
}
} // class TablePress_Chartist