Skip to content

Commit 56117fb

Browse files
Merge pull request #425 from contactashish13/issue-373
Add support for ChartJS
2 parents 8d40a47 + fe24b66 commit 56117fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2042
-54
lines changed

classes/Visualizer/Module.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,19 @@ public function _getDataAs( $chart_id, $type ) {
206206
}
207207
}
208208

209-
$filename = isset( $settings['title'] ) && ! empty( $settings['title'] ) ? $settings['title'] : 'visualizer#' . $chart_id;
209+
$title = 'visualizer#' . $chart_id;
210+
if ( ! empty( $settings['title'] ) ) {
211+
$title = $settings['title'];
212+
}
213+
// for ChartJS, title is an array.
214+
if ( is_array( $title ) && isset( $title['text'] ) ) {
215+
$title = $title['text'];
216+
}
217+
if ( empty( $title ) ) {
218+
$title = 'visualizer#' . $chart_id;
219+
}
220+
221+
$filename = $title;
210222

211223
switch ( $type ) {
212224
case 'csv':
@@ -476,21 +488,39 @@ protected function get_user_customization_js() {
476488
* Load the class for the given chart's chart type so that its assets can be loaded.
477489
*/
478490
protected function load_chart_type( $chart_id ) {
479-
$type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
480-
$name = 'Visualizer_Render_Sidebar_Type_' . ucwords( $type );
491+
$name = $this->load_chart_class_name( $chart_id );
481492
$class = null;
482493
if ( class_exists( $name ) || true === apply_filters( 'visualizer_load_chart', false, $name ) ) {
483494
$class = new $name;
484495
}
485496

486497
if ( is_null( $class ) && VISUALIZER_PRO ) {
487498
// lets see if this type exists in pro. New Lite(3.1.0+) & old Pro(1.8.0-).
488-
$class = apply_filters( 'visualizer_pro_chart_type_sidebar', null, array( 'type' => $type, 'settings' => get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) ) );
499+
$type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
500+
$class = apply_filters( 'visualizer_pro_chart_type_sidebar', null, array( 'id' => $chart_id, 'type' => $type, 'settings' => get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true ) ) );
489501
}
490502

491503
return is_null( $class ) ? null : $class->getLibrary();
492504
}
493505

506+
/**
507+
* Returns the class name for the given chart's chart type.
508+
*/
509+
protected function load_chart_class_name( $chart_id ) {
510+
$type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
511+
$lib = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, true );
512+
513+
// backward compatibility.
514+
if ( empty( $lib ) ) {
515+
$lib = 'GoogleCharts';
516+
if ( 'dataTable' === $type ) {
517+
$lib = 'DataTable';
518+
}
519+
}
520+
$name = 'Visualizer_Render_Sidebar_Type_' . $lib . '_' . ucwords( $type );
521+
return $name;
522+
}
523+
494524
/**
495525
* Generates the inline CSS to apply to the chart and adds these classes to the settings.
496526
*

classes/Visualizer/Module/Admin.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public function enqueueMediaScripts() {
218218
// Load all the assets for the different libraries we support.
219219
$deps = array(
220220
Visualizer_Render_Sidebar_Google::enqueue_assets( array( 'media-editor' ) ),
221-
Visualizer_Render_Sidebar_Type_DataTable::enqueue_assets( array( 'media-editor' ) ),
221+
Visualizer_Render_Sidebar_Type_DataTable_DataTable::enqueue_assets( array( 'media-editor' ) ),
222222
);
223223

224224
wp_enqueue_script( 'visualizer-media-model', VISUALIZER_ABSURL . 'js/media/model.js', $deps, Visualizer_Plugin::VERSION, true );
@@ -297,57 +297,81 @@ public static function _getChartTypesLocalized( $enabledOnly = false, $get2Darra
297297
$additional,
298298
array(
299299
'dataTable' => array(
300-
'name' => esc_html__( 'Table (New)', 'visualizer' ),
300+
'name' => esc_html__( 'Table', 'visualizer' ),
301301
'enabled' => true,
302+
'supports' => array( 'DataTable' ),
302303
),
303304
'pie' => array(
304-
'name' => esc_html__( 'Pie', 'visualizer' ),
305+
'name' => esc_html__( 'Pie/Donut', 'visualizer' ),
305306
'enabled' => true,
307+
'supports' => array( 'Google Charts', 'ChartJS' ),
306308
),
307309
'line' => array(
308310
'name' => esc_html__( 'Line', 'visualizer' ),
309311
'enabled' => true,
312+
'supports' => array( 'Google Charts', 'ChartJS' ),
310313
),
311314
'area' => array(
312315
'name' => esc_html__( 'Area', 'visualizer' ),
313316
'enabled' => true,
317+
// in ChartJS, the fill option is used to make Line chart an area: https://www.chartjs.org/docs/latest/charts/area.html
318+
'supports' => array( 'Google Charts' ),
314319
),
315320
'geo' => array(
316321
'name' => esc_html__( 'Geo', 'visualizer' ),
317322
'enabled' => true,
323+
'supports' => array( 'Google Charts' ),
318324
),
319325
'bar' => array(
320326
'name' => esc_html__( 'Bar', 'visualizer' ),
321327
'enabled' => true,
328+
'supports' => array( 'Google Charts', 'ChartJS' ),
322329
),
323330
'column' => array(
324331
'name' => esc_html__( 'Column', 'visualizer' ),
325332
'enabled' => true,
333+
'supports' => array( 'Google Charts', 'ChartJS' ),
326334
),
327335
'scatter' => array(
328336
'name' => esc_html__( 'Scatter', 'visualizer' ),
329337
'enabled' => true,
338+
'supports' => array( 'Google Charts' ),
330339
),
331340
'gauge' => array(
332341
'name' => esc_html__( 'Gauge', 'visualizer' ),
333342
'enabled' => true,
343+
'supports' => array( 'Google Charts' ),
334344
),
335345
'candlestick' => array(
336346
'name' => esc_html__( 'Candlestick', 'visualizer' ),
337347
'enabled' => true,
348+
'supports' => array( 'Google Charts' ),
338349
),
339350
// pro types
340351
'table' => array(
341352
'name' => esc_html__( 'Table (Deprecated)', 'visualizer' ),
342353
'enabled' => false,
354+
'supports' => array( 'Google Charts' ),
343355
),
344356
'timeline' => array(
345357
'name' => esc_html__( 'Timeline', 'visualizer' ),
346358
'enabled' => false,
359+
'supports' => array( 'Google Charts', 'ChartJS' ),
347360
),
348361
'combo' => array(
349362
'name' => esc_html__( 'Combo', 'visualizer' ),
350363
'enabled' => false,
364+
'supports' => array( 'Google Charts' ),
365+
),
366+
'polarArea' => array(
367+
'name' => esc_html__( 'Polar Area', 'visualizer' ),
368+
'enabled' => false,
369+
'supports' => array( 'ChartJS' ),
370+
),
371+
'radar' => array(
372+
'name' => esc_html__( 'Radar/Spider', 'visualizer' ),
373+
'enabled' => false,
374+
'supports' => array( 'ChartJS' ),
351375
),
352376
)
353377
);
@@ -359,6 +383,10 @@ public static function _getChartTypesLocalized( $enabledOnly = false, $get2Darra
359383
// support for old pro
360384
$array = array( 'enabled' => true, 'name' => $array );
361385
}
386+
// backward compatibility for PRO before v1.9.0
387+
if ( ! array_key_exists( 'supports', $array ) ) {
388+
$array['supports'] = array( 'Google Charts' );
389+
}
362390
if ( ! $array['enabled'] ) {
363391
continue;
364392
}
@@ -373,6 +401,10 @@ public static function _getChartTypesLocalized( $enabledOnly = false, $get2Darra
373401
// support for old pro
374402
$array = array( 'enabled' => true, 'name' => $array );
375403
}
404+
// backward compatibility for PRO before v1.9.0
405+
if ( ! array_key_exists( 'supports', $array ) ) {
406+
$array['supports'] = array( 'Google Charts' );
407+
}
376408
$doubleD[ $type ] = $array['name'];
377409
}
378410
$types = $doubleD;
@@ -388,6 +420,9 @@ private static function handleDeprecatedCharts( $types, $enabledOnly, $get2Darra
388420
$deprecated = array();
389421

390422
switch ( $where ) {
423+
case 'types':
424+
return $types;
425+
break;
391426
case 'library':
392427
// if the user has a Google Table chart, show it as deprecated otherwise remove the option from the library.
393428
if ( ! self::hasChartType( 'table' ) ) {
@@ -694,6 +729,7 @@ public function renderLibraryPage() {
694729
'action' => Visualizer_Plugin::ACTION_CREATE_CHART,
695730
'library' => 'yes',
696731
'type' => isset( $_GET['type'] ) ? $_GET['type'] : '',
732+
'chart-library' => isset( $_GET['chart-library'] ) ? $_GET['chart-library'] : '',
697733
),
698734
$ajaxurl
699735
),

classes/Visualizer/Module/Chart.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,14 @@ public function renderChartPages() {
456456
defined( 'WP_TESTS_DOMAIN' ) ? wp_die() : exit();
457457
}
458458

459-
$this->load_chart_type( $chart_id );
459+
$lib = $this->load_chart_type( $chart_id );
460+
461+
// the alpha color picker (RGBA) is not supported by google.
462+
$color_picker_dep = 'wp-color-picker';
463+
if ( in_array( $lib, array( 'chartjs', 'datatables' ), true ) && ! wp_script_is( 'wp-color-picker-alpha', 'registered' ) ) {
464+
wp_register_script( 'wp-color-picker-alpha', VISUALIZER_ABSURL . 'js/lib/wp-color-picker-alpha.min.js', array( 'wp-color-picker' ), Visualizer_Plugin::VERSION );
465+
$color_picker_dep = 'wp-color-picker-alpha';
466+
}
460467

461468
// enqueue and register scripts and styles
462469
wp_register_script( 'visualizer-chosen', VISUALIZER_ABSURL . 'js/lib/chosen.jquery.min.js', array( 'jquery' ), Visualizer_Plugin::VERSION );
@@ -476,7 +483,7 @@ public function renderChartPages() {
476483
'visualizer-preview',
477484
VISUALIZER_ABSURL . 'js/preview.js',
478485
array(
479-
'wp-color-picker',
486+
$color_picker_dep,
480487
'visualizer-render',
481488
),
482489
Visualizer_Plugin::VERSION,
@@ -608,7 +615,7 @@ private function _handleDataAndSettingsPage() {
608615
}
609616
$data = $this->_getChartArray();
610617
$sidebar = '';
611-
$sidebar_class = 'Visualizer_Render_Sidebar_Type_' . ucfirst( $data['type'] );
618+
$sidebar_class = $this->load_chart_class_name( $this->_chart->ID );
612619
if ( class_exists( $sidebar_class, true ) ) {
613620
$sidebar = new $sidebar_class( $data['settings'] );
614621
$sidebar->__series = $data['series'];
@@ -723,9 +730,17 @@ private function _handleTypesPage() {
723730
// process post request
724731
if ( $_SERVER['REQUEST_METHOD'] === 'POST' && wp_verify_nonce( filter_input( INPUT_POST, 'nonce' ) ) ) {
725732
$type = filter_input( INPUT_POST, 'type' );
733+
$library = filter_input( INPUT_POST, 'chart-library' );
726734
if ( in_array( $type, Visualizer_Plugin::getChartTypes(), true ) ) {
735+
if ( empty( $library ) ) {
736+
// library cannot be empty.
737+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, 'Chart library empty while creating the chart! Aborting...', 'error', __FILE__, __LINE__ );
738+
return;
739+
}
740+
727741
// save new chart type
728742
update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_TYPE, $type );
743+
update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_CHART_LIBRARY, $library );
729744
// if the chart has default data, update it with appropriate default data for new type
730745
if ( filter_var( get_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, true ), FILTER_VALIDATE_BOOLEAN ) ) {
731746
$source = new Visualizer_Source_Csv( VISUALIZER_ABSPATH . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR . $type . '.csv' );
@@ -734,6 +749,9 @@ private function _handleTypesPage() {
734749
wp_update_post( $this->_chart->to_array() );
735750
update_post_meta( $this->_chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
736751
}
752+
753+
Visualizer_Module_Utility::set_defaults( $this->_chart );
754+
737755
// redirect to next tab
738756
// changed by Ash/Upwork
739757
wp_redirect( add_query_arg( 'tab', 'settings' ) );
@@ -952,9 +970,15 @@ public function uploadData() {
952970
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, $source->getSeries() );
953971
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SOURCE, $source->getSourceName() );
954972
update_post_meta( $chart->ID, Visualizer_Plugin::CF_DEFAULT_DATA, 0 );
973+
974+
Visualizer_Module_Utility::set_defaults( $chart, null );
975+
976+
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
977+
955978
$render->id = $chart->ID;
956979
$render->data = json_encode( $source->getRawData() );
957980
$render->series = json_encode( $source->getSeries() );
981+
$render->settings = json_encode( $settings );
958982
} else {
959983
$render->message = esc_html__( 'CSV file is broken or invalid. Please, try again.', 'visualizer' );
960984
}

0 commit comments

Comments
 (0)