| 1 |
<?php |
| 2 |
// +----------------------------------------------------------------------+ |
| 3 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 4 |
// +----------------------------------------------------------------------+ |
| 5 |
// | This program is free software; you can redistribute it and/or modify | |
| 6 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 7 |
// | published by the Free Software Foundation. | |
| 8 |
// | | |
| 9 |
// | This program is distributed in the hope that it will be useful, | |
| 10 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 |
// | GNU General Public License for more details. | |
| 13 |
// | | |
| 14 |
// | You should have received a copy of the GNU General Public License | |
| 15 |
// | along with this program; if not, write to the Free Software | |
| 16 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 17 |
// | MA 02110-1301 USA | |
| 18 |
// +----------------------------------------------------------------------+ |
| 19 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 20 |
// +----------------------------------------------------------------------+ |
| 21 |
/** |
| 22 |
* Utilities module class. |
| 23 |
* |
| 24 |
* @category Visualizer |
| 25 |
* @package Module |
| 26 |
* |
| 27 |
* @since 3.3.0 |
| 28 |
*/ |
| 29 |
class Visualizer_Module_Utility extends Visualizer_Module { |
| 30 |
|
| 31 |
const NAME = __CLASS__; |
| 32 |
|
| 33 |
/** |
| 34 |
* Some default distinct colors. |
| 35 |
* |
| 36 |
* @since 3.3.0 |
| 37 |
* |
| 38 |
* @access private |
| 39 |
* @var _CHART_COLORS |
| 40 |
*/ |
| 41 |
private static $_CHART_COLORS = array( |
| 42 |
'#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080', |
| 43 |
); |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Constructor. |
| 48 |
* |
| 49 |
* @since 3.3.0 |
| 50 |
* |
| 51 |
* @access public |
| 52 |
* |
| 53 |
* @param Visualizer_Plugin $plugin The instance of the plugin. |
| 54 |
*/ |
| 55 |
public function __construct( Visualizer_Plugin $plugin ) { |
| 56 |
parent::__construct( $plugin ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* Convert hexdec color string to rgb(a) string. |
| 62 |
* |
| 63 |
* Props to https://mekshq.com/how-to-convert-hexadecimal-color-code-to-rgb-or-rgba-using-php/ |
| 64 |
* |
| 65 |
* @since 3.3.0 |
| 66 |
* |
| 67 |
* @access private |
| 68 |
*/ |
| 69 |
private static function hex2rgba( $color, $opacity = false ) { |
| 70 |
$default = 'rgb(0,0,0)'; |
| 71 |
|
| 72 |
// Return default if no color provided |
| 73 |
if ( empty( $color ) ) { |
| 74 |
return $default; |
| 75 |
} |
| 76 |
|
| 77 |
// Sanitize $color if "#" is provided |
| 78 |
if ( strpos( $color, '#' ) === 0 ) { |
| 79 |
$color = substr( $color, 1 ); |
| 80 |
} |
| 81 |
|
| 82 |
// Check if color has 6 or 3 characters and get values |
| 83 |
if ( strlen( $color ) === 6 ) { |
| 84 |
$hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] ); |
| 85 |
} elseif ( strlen( $color ) === 3 ) { |
| 86 |
$hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] ); |
| 87 |
} else { |
| 88 |
return $default; |
| 89 |
} |
| 90 |
|
| 91 |
// Convert hexadec to rgb |
| 92 |
$rgb = array_map( 'hexdec', $hex ); |
| 93 |
|
| 94 |
// Check if opacity is set(rgba or rgb) |
| 95 |
if ( $opacity ) { |
| 96 |
if ( abs( $opacity ) > 1 ) { |
| 97 |
$opacity = 1.0; |
| 98 |
} |
| 99 |
$output = 'rgba(' . implode( ',', $rgb ) . ',' . $opacity . ')'; |
| 100 |
} else { |
| 101 |
$output = 'rgb(' . implode( ',', $rgb ) . ')'; |
| 102 |
} |
| 103 |
|
| 104 |
// Return rgb(a) color string |
| 105 |
return $output; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Gets a random color from the array of chart colors and returns it as well as its transparent equivalent. |
| 110 |
* |
| 111 |
* @since 3.3.0 |
| 112 |
* |
| 113 |
* @access private |
| 114 |
*/ |
| 115 |
private static function get_random_color() { |
| 116 |
$color = self::$_CHART_COLORS[ rand( 0, count( self::$_CHART_COLORS ) - 1 ) ]; |
| 117 |
return array( self::hex2rgba( $color, 0.5 ), $color ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Sets some defaults in the chart. |
| 122 |
* |
| 123 |
* @since 3.3.0 |
| 124 |
* |
| 125 |
* @access public |
| 126 |
*/ |
| 127 |
public static function set_defaults( $chart, $post_status = 'auto-draft' ) { |
| 128 |
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 129 |
$library = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_LIBRARY, true ); |
| 130 |
|
| 131 |
// if post_status is null, operate on the chart irrespective of the post_status |
| 132 |
if ( ( ! is_null( $post_status ) && $chart->post_status !== $post_status ) ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ); |
| 137 |
if ( ! $series || ! is_array( $series ) ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
switch ( $library ) { |
| 142 |
case 'ChartJS': |
| 143 |
self::set_defaults_chartjs( $chart, $post_status ); |
| 144 |
break; |
| 145 |
case 'GoogleCharts': |
| 146 |
self::set_defaults_google( $chart, $post_status ); |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Sets some defaults in the chart for Google charts. |
| 153 |
* |
| 154 |
* @since 3.3.0 |
| 155 |
* |
| 156 |
* @access private |
| 157 |
*/ |
| 158 |
private static function set_defaults_google( $chart, $post_status ) { |
| 159 |
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 160 |
$series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ); |
| 161 |
|
| 162 |
$attributes = array(); |
| 163 |
if ( $post_status === 'auto-draft' ) { |
| 164 |
switch ( $type ) { |
| 165 |
case 'combo': |
| 166 |
// chart type 'bars' and randomly choose the type of each series. |
| 167 |
$types = array( 'area', 'line', 'steppedArea', 'bar' ); |
| 168 |
$attributes['seriesType'] = 'bars'; |
| 169 |
for ( $x = 0; $x < count( $series ); $x++ ) { |
| 170 |
$attributes['series'][ $x ]['type'] = $types[ rand( 0, count( $types ) - 1 ) ]; |
| 171 |
} |
| 172 |
break; |
| 173 |
case 'candlestick': |
| 174 |
// add stroke and fill color so that behavior is consistent. |
| 175 |
$attributes['candlestick']['fallingColor']['stroke'] = '#3366cc'; |
| 176 |
$attributes['candlestick']['fallingColor']['fill'] = '#fff'; |
| 177 |
$attributes['candlestick']['risingColor']['stroke'] = '#3366cc'; |
| 178 |
$attributes['candlestick']['risingColor']['fill'] = '#3366cc'; |
| 179 |
break; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
if ( $attributes ) { |
| 184 |
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); |
| 185 |
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, array_merge( $settings, $attributes ) ); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Sets some defaults in the chart for ChartJS charts. Only during first creation. |
| 191 |
* |
| 192 |
* @since 3.3.0 |
| 193 |
* |
| 194 |
* @access private |
| 195 |
*/ |
| 196 |
private static function set_defaults_chartjs( $chart, $post_status ) { |
| 197 |
if ( $post_status !== 'auto-draft' ) { |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true ); |
| 202 |
$series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true ); |
| 203 |
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); |
| 204 |
|
| 205 |
$attributes = array(); |
| 206 |
$name = 'series'; |
| 207 |
$count = count( $series ); |
| 208 |
$max = $count - 1; |
| 209 |
switch ( $type ) { |
| 210 |
case 'polarArea': |
| 211 |
// fall through. |
| 212 |
case 'pie': |
| 213 |
$data = unserialize( $chart->post_content ); |
| 214 |
$name = 'slices'; |
| 215 |
$max = count( $data ); |
| 216 |
// fall through. |
| 217 |
case 'column': |
| 218 |
// fall through. |
| 219 |
case 'bar': |
| 220 |
for ( $i = 0; $i < $max; $i++ ) { |
| 221 |
$colors = self::get_random_color(); |
| 222 |
$attributes[] = array( 'backgroundColor' => $colors[0], 'hoverBackgroundColor' => $colors[1] ); |
| 223 |
} |
| 224 |
break; |
| 225 |
case 'radar': |
| 226 |
// fall through. |
| 227 |
case 'line': |
| 228 |
// fall through. |
| 229 |
case 'area': |
| 230 |
for ( $i = 0; $i < $max; $i++ ) { |
| 231 |
$colors = self::get_random_color(); |
| 232 |
$attributes[] = array( 'borderColor' => $colors[0] ); |
| 233 |
} |
| 234 |
break; |
| 235 |
} |
| 236 |
|
| 237 |
if ( $post_status === 'auto-draft' ) { |
| 238 |
// the charts are huge in size so let's always get them down to 50%. |
| 239 |
$settings['width'] = $settings['height'] = '50%'; |
| 240 |
} |
| 241 |
|
| 242 |
if ( $attributes ) { |
| 243 |
$settings[ $name ] = $attributes; |
| 244 |
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, $settings ); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|