| @@ -35,9 +35,9 @@ | ||
| 35 | 35 | * |
| 36 | 36 | * @since 3.3.0 |
| 37 | 37 | * |
| 38 | 38 | * @access private |
| 39 | - * @var string[] | |
| 39 | + * @var _CHART_COLORS | |
| 40 | 40 | */ |
| 41 | 41 | private static $_CHART_COLORS = array( |
| 42 | 42 | '#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080', |
| 43 | 43 | ); |
| @@ -53,9 +53,8 @@ | ||
| 53 | 53 | * @param Visualizer_Plugin $plugin The instance of the plugin. |
| 54 | 54 | */ |
| 55 | 55 | public function __construct( Visualizer_Plugin $plugin ) { |
| 56 | 56 | parent::__construct( $plugin ); |
| 57 | - $this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, 'apply_global_style_settings', 999, 3 ); | |
| 58 | 57 | } |
| 59 | 58 | |
| 60 | 59 | |
| 61 | 60 | /** |
| @@ -106,235 +105,20 @@ | ||
| 106 | 105 | return $output; |
| 107 | 106 | } |
| 108 | 107 | |
| 109 | 108 | /** |
| 110 | - * Returns a color at a specific index from the palette, with its transparent equivalent. | |
| 109 | + * Gets a random color from the array of chart colors and returns it as well as its transparent equivalent. | |
| 111 | 110 | * |
| 112 | - * @return array{string, string} | |
| 111 | + * @since 3.3.0 | |
| 112 | + * | |
| 113 | 113 | * @access private |
| 114 | 114 | */ |
| 115 | - private static function get_color_at( int $index ): array { | |
| 116 | - $colors = self::get_color_palette(); | |
| 117 | - $color = $colors[ $index % count( $colors ) ]; | |
| 115 | + private static function get_random_color() { | |
| 116 | + $color = self::$_CHART_COLORS[ rand( 0, count( self::$_CHART_COLORS ) - 1 ) ]; | |
| 118 | 117 | return array( self::hex2rgba( $color, 0.5 ), $color ); |
| 119 | 118 | } |
| 120 | 119 | |
| 121 | 120 | /** |
| 122 | - * Mixes a hex color toward white by the given factor (0 = original, 1 = white). | |
| 123 | - * | |
| 124 | - * @access private | |
| 125 | - */ | |
| 126 | - private static function tint_color( string $hex, float $factor ): string { | |
| 127 | - $hex = ltrim( $hex, '#' ); | |
| 128 | - if ( strlen( $hex ) === 3 ) { | |
| 129 | - $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; | |
| 130 | - } | |
| 131 | - $r = (int) round( hexdec( substr( $hex, 0, 2 ) ) + ( 255 - hexdec( substr( $hex, 0, 2 ) ) ) * $factor ); | |
| 132 | - $g = (int) round( hexdec( substr( $hex, 2, 2 ) ) + ( 255 - hexdec( substr( $hex, 2, 2 ) ) ) * $factor ); | |
| 133 | - $b = (int) round( hexdec( substr( $hex, 4, 2 ) ) + ( 255 - hexdec( substr( $hex, 4, 2 ) ) ) * $factor ); | |
| 134 | - return sprintf( '#%02x%02x%02x', $r, $g, $b ); | |
| 135 | - } | |
| 136 | - | |
| 137 | - /** | |
| 138 | - * Returns the color palette to use for charts. | |
| 139 | - * | |
| 140 | - * When global primary/secondary colors are configured, generates a palette of | |
| 141 | - * alternating tints: [primary, secondary, primary@25%, secondary@25%, ...]. | |
| 142 | - * Falls back to the built-in colors otherwise. | |
| 143 | - * | |
| 144 | - * @return string[] | |
| 145 | - * @access private | |
| 146 | - */ | |
| 147 | - private static function get_color_palette(): array { | |
| 148 | - $global = self::get_global_style_defaults(); | |
| 149 | - $primary = $global['color_primary']; | |
| 150 | - $secondary = $global['color_secondary']; | |
| 151 | - | |
| 152 | - if ( empty( $primary ) && empty( $secondary ) ) { | |
| 153 | - return self::$_CHART_COLORS; | |
| 154 | - } | |
| 155 | - | |
| 156 | - $bases = array_filter( array( $primary, $secondary ) ); | |
| 157 | - $factors = array( 0, 0.25, 0.5, 0.75 ); | |
| 158 | - $palette = array(); | |
| 159 | - | |
| 160 | - foreach ( $factors as $factor ) { | |
| 161 | - foreach ( $bases as $base ) { | |
| 162 | - $palette[] = $factor === 0 ? $base : self::tint_color( $base, $factor ); | |
| 163 | - } | |
| 164 | - } | |
| 165 | - | |
| 166 | - return $palette; | |
| 167 | - } | |
| 168 | - | |
| 169 | - /** | |
| 170 | - * Returns the global style defaults stored in the plugin settings. | |
| 171 | - * | |
| 172 | - * @return array<string, string> | |
| 173 | - * @access public | |
| 174 | - * @static | |
| 175 | - */ | |
| 176 | - public static function get_global_style_defaults(): array { | |
| 177 | - $option = get_option( Visualizer_Module_Admin::OPTION_GLOBAL_SETTINGS, array() ); | |
| 178 | - return wp_parse_args( | |
| 179 | - $option, | |
| 180 | - array( | |
| 181 | - 'color_primary' => '', | |
| 182 | - 'color_secondary' => '', | |
| 183 | - 'apply_existing' => '0', | |
| 184 | - ) | |
| 185 | - ); | |
| 186 | - } | |
| 187 | - | |
| 188 | - /** | |
| 189 | - * Applies global styles to existing charts at render time. | |
| 190 | - * | |
| 191 | - * @param array<string, mixed>|mixed $settings Chart settings. | |
| 192 | - * @param int $chart_id Chart ID. | |
| 193 | - * @param string $type Chart type. | |
| 194 | - * @return array<string, mixed> | |
| 195 | - * @access public | |
| 196 | - */ | |
| 197 | - public function apply_global_style_settings( $settings, $chart_id, $type ): array { | |
| 198 | - $settings = is_array( $settings ) ? $settings : array(); | |
| 199 | - $global = self::get_global_style_defaults(); | |
| 200 | - | |
| 201 | - if ( empty( $global['apply_existing'] ) ) { | |
| 202 | - return $settings; | |
| 203 | - } | |
| 204 | - | |
| 205 | - if ( empty( $global['color_primary'] ) && empty( $global['color_secondary'] ) ) { | |
| 206 | - return $settings; | |
| 207 | - } | |
| 208 | - | |
| 209 | - if ( empty( $chart_id ) ) { | |
| 210 | - return $settings; | |
| 211 | - } | |
| 212 | - | |
| 213 | - $library = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, true ); | |
| 214 | - $library = is_string( $library ) ? strtolower( $library ) : ''; | |
| 215 | - if ( empty( $type ) ) { | |
| 216 | - $type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true ); | |
| 217 | - } | |
| 218 | - $type = is_string( $type ) ? strtolower( $type ) : ''; | |
| 219 | - | |
| 220 | - if ( empty( $library ) ) { | |
| 221 | - $library = in_array( $type, array( 'datatable', 'tabular' ), true ) ? 'datatable' : 'googlecharts'; | |
| 222 | - } | |
| 223 | - | |
| 224 | - if ( 'googlecharts' === $library || 'google' === $library ) { | |
| 225 | - if ( 'geo' !== $type ) { | |
| 226 | - $has_explicit = false; | |
| 227 | - if ( ! empty( $settings['colors'] ) ) { | |
| 228 | - $has_explicit = true; | |
| 229 | - } | |
| 230 | - if ( ! $has_explicit && isset( $settings['series'] ) && is_array( $settings['series'] ) ) { | |
| 231 | - foreach ( $settings['series'] as $series_settings ) { | |
| 232 | - if ( ! empty( $series_settings['color'] ) ) { | |
| 233 | - $has_explicit = true; | |
| 234 | - break; | |
| 235 | - } | |
| 236 | - } | |
| 237 | - } | |
| 238 | - if ( ! $has_explicit && isset( $settings['slices'] ) && is_array( $settings['slices'] ) ) { | |
| 239 | - foreach ( $settings['slices'] as $slice_settings ) { | |
| 240 | - if ( ! empty( $slice_settings['color'] ) ) { | |
| 241 | - $has_explicit = true; | |
| 242 | - break; | |
| 243 | - } | |
| 244 | - } | |
| 245 | - } | |
| 246 | - if ( ! $has_explicit ) { | |
| 247 | - $settings['colors'] = self::get_color_palette(); | |
| 248 | - } | |
| 249 | - } | |
| 250 | - return $settings; | |
| 251 | - } | |
| 252 | - | |
| 253 | - if ( 'chartjs' === $library ) { | |
| 254 | - $has_explicit = false; | |
| 255 | - if ( isset( $settings['series'] ) && is_array( $settings['series'] ) ) { | |
| 256 | - foreach ( $settings['series'] as $series_settings ) { | |
| 257 | - if ( ! empty( $series_settings['backgroundColor'] ) || ! empty( $series_settings['borderColor'] ) || ! empty( $series_settings['hoverBackgroundColor'] ) ) { | |
| 258 | - $has_explicit = true; | |
| 259 | - break; | |
| 260 | - } | |
| 261 | - } | |
| 262 | - } | |
| 263 | - if ( ! $has_explicit && isset( $settings['slices'] ) && is_array( $settings['slices'] ) ) { | |
| 264 | - foreach ( $settings['slices'] as $slice_settings ) { | |
| 265 | - if ( ! empty( $slice_settings['backgroundColor'] ) || ! empty( $slice_settings['borderColor'] ) || ! empty( $slice_settings['hoverBackgroundColor'] ) ) { | |
| 266 | - $has_explicit = true; | |
| 267 | - break; | |
| 268 | - } | |
| 269 | - } | |
| 270 | - } | |
| 271 | - if ( $has_explicit ) { | |
| 272 | - return $settings; | |
| 273 | - } | |
| 274 | - $series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true ); | |
| 275 | - if ( is_array( $series ) ) { | |
| 276 | - $settings = self::apply_chartjs_palette( $settings, $type, $series, $chart_id ); | |
| 277 | - } | |
| 278 | - } | |
| 279 | - | |
| 280 | - return $settings; | |
| 281 | - } | |
| 282 | - | |
| 283 | - /** | |
| 284 | - * Applies the global palette to ChartJS settings for a chart. | |
| 285 | - * | |
| 286 | - * @param array<string, mixed> $settings Current chart settings. | |
| 287 | - * @param string $type Chart type. | |
| 288 | - * @param array<int, mixed> $series Series definitions. | |
| 289 | - * @param int $chart_id Chart ID. | |
| 290 | - * @return array<string, mixed> | |
| 291 | - * @access private | |
| 292 | - * @static | |
| 293 | - */ | |
| 294 | - private static function apply_chartjs_palette( array $settings, string $type, array $series, int $chart_id ): array { | |
| 295 | - $attributes = array(); | |
| 296 | - $name = 'series'; | |
| 297 | - $count = count( $series ); | |
| 298 | - $max = $count - 1; | |
| 299 | - | |
| 300 | - switch ( $type ) { | |
| 301 | - case 'polarArea': | |
| 302 | - // fall through. | |
| 303 | - case 'pie': | |
| 304 | - $chart = get_post( $chart_id ); | |
| 305 | - $data = $chart instanceof WP_Post ? maybe_unserialize( $chart->post_content ) : array(); | |
| 306 | - $name = 'slices'; | |
| 307 | - $max = is_array( $data ) ? count( $data ) : $count; | |
| 308 | - // fall through. | |
| 309 | - case 'column': | |
| 310 | - // fall through. | |
| 311 | - case 'bar': | |
| 312 | - for ( $i = 0; $i < $max; $i++ ) { | |
| 313 | - $colors = self::get_color_at( $i ); | |
| 314 | - $attributes[] = array( 'backgroundColor' => $colors[0], 'hoverBackgroundColor' => $colors[1] ); | |
| 315 | - } | |
| 316 | - break; | |
| 317 | - case 'radar': | |
| 318 | - // fall through. | |
| 319 | - case 'line': | |
| 320 | - // fall through. | |
| 321 | - case 'area': | |
| 322 | - for ( $i = 0; $i < $max; $i++ ) { | |
| 323 | - $colors = self::get_color_at( $i ); | |
| 324 | - $attributes[] = array( 'borderColor' => $colors[0] ); | |
| 325 | - } | |
| 326 | - break; | |
| 327 | - } | |
| 328 | - | |
| 329 | - if ( $attributes ) { | |
| 330 | - $settings[ $name ] = $attributes; | |
| 331 | - } | |
| 332 | - | |
| 333 | - return $settings; | |
| 334 | - } | |
| 335 | - | |
| 336 | - /** | |
| 337 | 121 | * Sets some defaults in the chart. |
| 338 | 122 | * |
| 339 | 123 | * @since 3.3.0 |
| 340 | 124 | * |
| @@ -393,16 +177,8 @@ | ||
| 393 | 177 | $attributes['candlestick']['risingColor']['stroke'] = '#3366cc'; |
| 394 | 178 | $attributes['candlestick']['risingColor']['fill'] = '#3366cc'; |
| 395 | 179 | break; |
| 396 | 180 | } |
| 397 | - | |
| 398 | - // Apply global color defaults to new Google Charts (not Geo — it uses colorAxis). | |
| 399 | - if ( 'geo' !== $type ) { | |
| 400 | - $global = self::get_global_style_defaults(); | |
| 401 | - if ( ! empty( $global['color_primary'] ) || ! empty( $global['color_secondary'] ) ) { | |
| 402 | - $attributes['colors'] = self::get_color_palette(); | |
| 403 | - } | |
| 404 | - } | |
| 405 | 181 | } |
| 406 | 182 | |
| 407 | 183 | if ( $attributes ) { |
| 408 | 184 | $settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true ); |
| @@ -441,9 +217,9 @@ | ||
| 441 | 217 | case 'column': |
| 442 | 218 | // fall through. |
| 443 | 219 | case 'bar': |
| 444 | 220 | for ( $i = 0; $i < $max; $i++ ) { |
| 445 | - $colors = self::get_color_at( $i ); | |
| 221 | + $colors = self::get_random_color(); | |
| 446 | 222 | $attributes[] = array( 'backgroundColor' => $colors[0], 'hoverBackgroundColor' => $colors[1] ); |
| 447 | 223 | } |
| 448 | 224 | break; |
| 449 | 225 | case 'radar': |
| @@ -451,9 +227,9 @@ | ||
| 451 | 227 | case 'line': |
| 452 | 228 | // fall through. |
| 453 | 229 | case 'area': |
| 454 | 230 | for ( $i = 0; $i < $max; $i++ ) { |
| 455 | - $colors = self::get_color_at( $i ); | |
| 231 | + $colors = self::get_random_color(); | |
| 456 | 232 | $attributes[] = array( 'borderColor' => $colors[0] ); |
| 457 | 233 | } |
| 458 | 234 | break; |
| 459 | 235 | } |