Skip to content

Commit 098f8bd

Browse files
committed
feat: add option to toggle palette for existing charts
1 parent 6ed4c50 commit 098f8bd

3 files changed

Lines changed: 171 additions & 0 deletions

File tree

classes/Visualizer/Module/Admin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,7 @@ public static function getGlobalSettings(): array {
11161116
$defaults = array(
11171117
'color_primary' => '',
11181118
'color_secondary' => '',
1119+
'apply_existing' => '0',
11191120
);
11201121
$saved = get_option( self::OPTION_GLOBAL_SETTINGS, array() );
11211122
return wp_parse_args( $saved, $defaults );
@@ -1135,6 +1136,7 @@ public function saveGlobalSettings(): void {
11351136

11361137
$color_primary = sanitize_hex_color( wp_unslash( $_POST['visualizer_color_primary'] ?? '' ) );
11371138
$color_secondary = sanitize_hex_color( wp_unslash( $_POST['visualizer_color_secondary'] ?? '' ) );
1139+
$apply_existing = ! empty( $_POST['visualizer_apply_existing'] ) ? '1' : '0';
11381140
$clear = ! empty( $_POST['visualizer_clear_settings'] );
11391141

11401142
if ( $clear ) {
@@ -1143,6 +1145,7 @@ public function saveGlobalSettings(): void {
11431145
$settings = array(
11441146
'color_primary' => $color_primary ? $color_primary : '',
11451147
'color_secondary' => $color_secondary ? $color_secondary : '',
1148+
'apply_existing' => $apply_existing,
11461149
);
11471150
update_option( self::OPTION_GLOBAL_SETTINGS, $settings );
11481151
}

classes/Visualizer/Module/Utility.php

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Visualizer_Module_Utility extends Visualizer_Module {
5454
*/
5555
public function __construct( Visualizer_Plugin $plugin ) {
5656
parent::__construct( $plugin );
57+
$this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, 'apply_global_style_settings', 999, 3 );
5758
}
5859

5960

@@ -179,10 +180,159 @@ public static function get_global_style_defaults(): array {
179180
array(
180181
'color_primary' => '',
181182
'color_secondary' => '',
183+
'apply_existing' => '0',
182184
)
183185
);
184186
}
185187

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+
186336
/**
187337
* Sets some defaults in the chart.
188338
*

templates/global-settings.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ class="visualizer-color-picker"
6666
</td>
6767
</tr>
6868

69+
<tr>
70+
<th scope="row">
71+
<label for="visualizer-apply-existing"><?php esc_html_e( 'Apply To Existing Charts', 'visualizer' ); ?></label>
72+
</th>
73+
<td>
74+
<label>
75+
<input
76+
type="checkbox"
77+
id="visualizer-apply-existing"
78+
name="visualizer_apply_existing"
79+
value="1"
80+
<?php checked( $settings['apply_existing'], '1' ); ?>
81+
/>
82+
<?php esc_html_e( 'Apply global colors to existing charts at render time (this will override chart colors on display).', 'visualizer' ); ?>
83+
</label>
84+
</td>
85+
</tr>
86+
6987
</tbody>
7088
</table>
7189

0 commit comments

Comments
 (0)