This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipu-chart.php
223 lines (192 loc) · 7.95 KB
/
ipu-chart.php
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
<?php
/*
Plugin Name: IPU-Chart
Plugin URI: https://www.ipublia.com/support/docs/ipu-chart-for-wordpress-user-guide/
Description: D3/SVG based charts out of your csv, tsv or json data. Currently supports bar, pie, donut, line, scatter, bubble and world map charts.
Author: Thomas Müller Flury, ipublia
Version: 1.0.5
Author URI: https://www.ipublia.com/author/thmufl/
Text Domain: ipuc
Domain Path: /lang
*/
// Plugin Folder Path
if (!defined('IPUC_PLUGIN_DIR'))
define( 'IPUC_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . basename(dirname( __FILE__ ) ) . '/');
// Helper to display plugin version in console logs
function plugin_get_version() {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$plugin_data = get_plugin_data( __FILE__ );
$plugin_version = $plugin_data['Name'] . ', Version ' . $plugin_data['Version'];
return $plugin_version;
}
// This is the csv tag
function ipuc_csv_func($atts, $content = null) {
extract(shortcode_atts(array(
'id' => 'd' . rand(0, 999999),
), $atts));
if(has_filter("the_content", "wpautop")) {
$content = str_replace("<br />", "\r", $content);
}
$content = str_replace(array("‘", "“", "”", "′", "″"), "\"", $content);
$content = trim($content);
return ipuc_render_csv($id, do_shortcode($content));
}
// This is the tsv tag
function ipuc_tsv_func($atts, $content = null) {
extract(shortcode_atts(array(
'id' => 'd' . rand(0, 999999),
), $atts));
if(has_filter("the_content", "wpautop")) {
$content = str_replace("<br />", "\r", $content);
}
$content = str_replace(array("‘", "“", "”", "′", "″"), "\"", $content);
$content = trim($content);
return ipuc_render_tsv($id, do_shortcode($content));
}
// This is the json tag
function ipuc_json_func($atts, $content = null) {
extract(shortcode_atts(array(
'id' => 'd' . rand(0, 999999),
), $atts));
if(has_filter("the_content", "wpautop")) {
$content = str_replace("<br />", "\r", $content);
}
$content = str_replace(array("‘", "“", "”", "′", "″"), "\"", $content);
$content = trim($content);
return ipuc_render_json($id, do_shortcode($content));
}
// This is the chart tag. Will be replaced by the chart-def tag.
function ipuc_chart_func($atts) {
extract(shortcode_atts(array(
'id' => 'c' . rand(0, 999999),
'csv' => '',
'tsv' => '',
'json' => '',
'adapter' => 'return data;',
'type' => 'bar',
'category' => 'name',
'value' => 'value',
'format' => '%s,%f',
'color' => 'auto',
'style' => 'width:100%;height:300px;',
'title' => 'Set a title',
'description' => 'Set a description',
'sort' => 'true',
'interpolate' => 'linear',
'animate' => 'none',
'img' => '',
'debug' => 'false'
), $atts));
return ipuc_render_chart($id, $csv, $tsv, $json, $adapter, $type, $category, $value,
$format, $color, $style,
$title, $description, $sort, $interpolate, $animate,
$img, $debug, plugin_get_version());
}
// The ip4 tag
function ip4_func($atts, $content = null) {
extract(shortcode_atts(array(
'log' => 'ip4.log.level.warn'
), $atts));
if(has_filter('the_content', 'wpautop')) {
$content = str_replace("<br />", "", $content);
}
$content = trim($content);
return ip4_render($log, do_shortcode($content));
}
// This is the table tag (deprecated)
function ipuc_table_func($atts) {
extract(shortcode_atts(array(
'id' => 'table' . rand(0, 999999),
'csv' => '',
'title' => 'Set a title',
'debug' => 'false'
), $atts));
return ipuc_render_table($id, $csv, $title, $debug);
}
add_shortcode("csv", "ipuc_csv_func");
add_shortcode("tsv", "ipuc_tsv_func");
add_shortcode("json", "ipuc_json_func");
add_shortcode("chart", "ipuc_chart_func");
add_shortcode("table", "ipuc_table_func");
// Second shortcode set for compability with some themes.
add_shortcode("ipu-csv", "ipuc_csv_func");
add_shortcode("ipu-tsv", "ipuc_tsv_func");
add_shortcode("ipu-json", "ipuc_json_func");
add_shortcode("ipu-chart", "ipuc_chart_func");
add_shortcode("ipu-table", "ipuc_table_func");
// ip4 shortcode since version 0.8
add_shortcode("ip4", "ip4_func");
function ipuc_render_csv($id, $content) {
return "<div id='{$id}' class='csv' style='display:none;white-space:pre;'>{$content}</div>";
}
function ipuc_render_tsv($id, $content) {
return "<div id='{$id}' class='tsv' style='display:none;white-space:pre;'>{$content}</div>";
}
function ipuc_render_json($id, $content) {
return "<div id='{$id}' class='json' style='display:none;white-space:pre;'>{$content}</div>";
}
function ipuc_render_chart($id, $csv, $tsv, $json, $type, $category, $value, $format, $color, $style, $title, $description, $sort, $interpolate, $animate, $img, $debug, $version) {
if($type === 'table') {
return "<table id='{$id}' class='chart table'>
<script type='text/javascript'>
renderChart(\"{$id}\", \"{$csv}\", \"{$tsv}\", \"{$json}\", \"{$type}\", \"{$category}\", \"{$value}\",
\"{$format}\", \"{$color}\", \"{$style}\",
\"{$title}\", \"{$description}\", \"{$sort}\", \"{$interpolate}\", \"{$animate}\",
\"{$img}\", \"{$debug}\", \"{$version}\");
</script>
</table>";
} else {
return "<figure id='{$id}' class='ipuc'>
<script type='text/javascript'>
renderChart(\"{$id}\", \"{$csv}\", \"{$tsv}\", \"{$json}\", \"{$type}\", \"{$category}\", \"{$value}\",
\"{$format}\", \"{$color}\", \"{$style}\",
\"{$title}\", \"{$description}\", \"{$sort}\", \"{$interpolate}\", \"{$animate}\",
\"{$img}\", \"{$debug}\", \"{$version}\");
</script>
</figure>";
}
}
function ip4_render($log, $content) {
$content = str_replace(array("“", "”", "„", "′", "″"), "\"", $content);
$content = str_replace(array("‘", "’"), "'", $content);
$content = str_replace("–", "-", $content);
$content = str_replace("\n", "", $content);
$content = preg_replace('/\s+/', ' ', $content);
return "<script type='text/javascript' encoding='utf-8'>
ip4.draw({$content});
</script>";
}
function ipuc_render_table($id, $csv, $title, $debug) {
return "<table id='{$id}' class='chart-data'>
<script type='text/javascript'>
renderTableDeprecated('{$id}', '{$csv}', '{$title}', '{$debug}');
</script>
</table>";
}
// Add plug-in's scripts to the header of the pages
function ipuc_add_custom_scripts() {
wp_register_script('d3-script', plugins_url( '/lib/d3/d3.min.js', __FILE__ ));
wp_register_script('queue-script', plugins_url( '/lib/queue/queue.min.js', __FILE__ ));
wp_register_script('d3-geo-projection-script', plugins_url( '/lib/geo/d3.geo.projection.v0.min.js', __FILE__ ));
wp_register_script('d3-topojson-script', plugins_url( '/lib/geo/topojson.v0.min.js', __FILE__ ));
wp_register_script('colorbrewer-script', plugins_url( '/lib/colorbrewer/colorbrewer.js', __FILE__ ));
wp_register_script('ip4-script', plugins_url( '/lib/ip4.v1.min.js', __FILE__ ));
wp_register_script('ipuchart-script', plugins_url( '/lib/ipu-chart.js', __FILE__ ));
wp_enqueue_script('d3-script');
wp_enqueue_script('queue-script');
wp_enqueue_script('d3-geo-projection-script');
wp_enqueue_script('d3-topojson-script');
wp_enqueue_script('colorbrewer-script');
wp_enqueue_script('ip4-script', null, array('d3-script', 'queue-script', 'd3-geo-projection-script', 'd3-topojson-script', 'colorbrewer-script'));
wp_enqueue_script('ipuchart-script', null, array('d3-script', 'queue-script', 'd3-geo-projection-script', 'd3-topojson-script', 'colorbrewer-script'));
}
add_action('wp_enqueue_scripts', 'ipuc_add_custom_scripts' );
// Add plug-in's stylesheets to the header of the pages
function ipuc_add_custom_styles() {
wp_register_style('ip4-style', plugins_url( '/css/ip4.v1.css', __FILE__ ), array(), '0.8', 'all' );
wp_register_style('ipuchart-style', plugins_url( '/css/ipu-chart.css', __FILE__ ), array(), '0.7', 'all' );
wp_enqueue_style('ipuchart-style');
wp_enqueue_style('ip4-style');
}
add_action('wp_enqueue_scripts', 'ipuc_add_custom_styles' );
?>