@@ -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 *
0 commit comments