Skip to content

Commit e9c48e0

Browse files
Merge pull request #741 from contactashish13/issue-733
Google Table chart does not display chart if boolean values are specified
2 parents bc46686 + 348b368 commit e9c48e0

File tree

5 files changed

+98
-60
lines changed

5 files changed

+98
-60
lines changed

classes/Visualizer/Gutenberg/Block.php

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ public function get_visualizer_data( $post ) {
352352
// handle data filter hooks
353353
$data['visualizer-data'] = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( html_entity_decode( get_the_content( $post_id ) ) ), $post_id, $data['visualizer-chart-type'] );
354354

355+
// we are going to format only for tabular charts, because we are not sure of the effect on others.
356+
// this is to solve the case where boolean data shows up as all-ticks on gutenberg.
357+
if ( in_array( $data['visualizer-chart-type'], array( 'tabular' ), true ) ) {
358+
$data['visualizer-data'] = $this->format_chart_data( $data['visualizer-data'], $data['visualizer-series'] );
359+
}
360+
355361
$data['visualizer-data-exploded'] = '';
356362
// handle annotations for google charts
357363
if ( 'GoogleCharts' === $library ) {
@@ -653,6 +659,8 @@ public function update_chart_data( $data ) {
653659

654660
/**
655661
* Format chart data.
662+
*
663+
* Note: No matter how tempted, don't use the similar method from Visualizer_Source. That works on a different structure.
656664
*/
657665
public function format_chart_data( $data, $series ) {
658666
foreach ( $series as $i => $row ) {
@@ -665,36 +673,35 @@ public function format_chart_data( $data, $series ) {
665673
continue;
666674
}
667675

668-
if ( $row['type'] === 'number' ) {
669-
foreach ( $data as $o => $col ) {
670-
$data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
671-
}
672-
}
673-
674-
if ( $row['type'] === 'boolean' ) {
675-
foreach ( $data as $o => $col ) {
676-
$data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : null;
677-
}
678-
}
679-
680-
if ( $row['type'] === 'timeofday' ) {
681-
foreach ( $data as $o => $col ) {
682-
$date = new DateTime( '1984-03-16T' . $col[ $i ] );
683-
if ( $date ) {
684-
$data[ $o ][ $i ] = array(
685-
intval( $date->format( 'H' ) ),
686-
intval( $date->format( 'i' ) ),
687-
intval( $date->format( 's' ) ),
688-
0,
689-
);
676+
switch ( $row['type'] ) {
677+
case 'number':
678+
foreach ( $data as $o => $col ) {
679+
$data[ $o ][ $i ] = ( is_numeric( $col[ $i ] ) ) ? floatval( $col[ $i ] ) : ( is_numeric( str_replace( ',', '', $col[ $i ] ) ) ? floatval( str_replace( ',', '', $col[ $i ] ) ) : null );
690680
}
691-
}
692-
}
693-
694-
if ( $row['type'] === 'string' ) {
695-
foreach ( $data as $o => $col ) {
696-
$data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
697-
}
681+
break;
682+
case 'boolean':
683+
foreach ( $data as $o => $col ) {
684+
$data[ $o ][ $i ] = ! empty( $col[ $i ] ) ? filter_var( $col[ $i ], FILTER_VALIDATE_BOOLEAN ) : false;
685+
}
686+
break;
687+
case 'timeofday':
688+
foreach ( $data as $o => $col ) {
689+
$date = new DateTime( '1984-03-16T' . $col[ $i ] );
690+
if ( $date ) {
691+
$data[ $o ][ $i ] = array(
692+
intval( $date->format( 'H' ) ),
693+
intval( $date->format( 'i' ) ),
694+
intval( $date->format( 's' ) ),
695+
0,
696+
);
697+
}
698+
}
699+
break;
700+
case 'string':
701+
foreach ( $data as $o => $col ) {
702+
$data[ $o ][ $i ] = $this->toUTF8( $col[ $i ] );
703+
}
704+
break;
698705
}
699706
}
700707

classes/Visualizer/Gutenberg/build/block.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

classes/Visualizer/Gutenberg/src/Components/Sidebar/SeriesSettings.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ class SeriesSettings extends Component {
6060

6161
{ Object.keys( settings.series )
6262
.map( ( i, index ) => {
63+
let indexToFormat = parseInt( i );
64+
65+
if ( 'tabular' !== type ) {
66+
indexToFormat = index;
67+
}
68+
6369
i++;
70+
6471
return (
6572
<PanelBody
6673
title={ series[i].label }
@@ -121,9 +128,9 @@ class SeriesSettings extends Component {
121128
<TextControl
122129
label={ __( 'Format' ) }
123130
help={ __( 'Enter custom format pattern to apply to this series value.' ) }
124-
value={ settings.series[index].format }
131+
value={ settings.series[indexToFormat].format }
125132
onChange={ e => {
126-
settings.series[index].format = e;
133+
settings.series[indexToFormat].format = e;
127134
this.props.edit( settings );
128135
} }
129136
/>
@@ -148,9 +155,9 @@ class SeriesSettings extends Component {
148155
label={ __( 'Date Format' ) }
149156
help={ __( 'Enter custom format pattern to apply to this series value.' ) }
150157
placeholder="dd LLLL yyyy"
151-
value={ settings.series[index].format }
158+
value={ settings.series[indexToFormat].format }
152159
onChange={ e => {
153-
settings.series[index].format = e;
160+
settings.series[indexToFormat].format = e;
154161
this.props.edit( settings );
155162
} }
156163
/>

classes/Visualizer/Gutenberg/src/Editor.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -339,20 +339,39 @@ class Editor extends Component {
339339
let map = series;
340340
let fieldName = 'series';
341341

342-
if ( 'pie' === type ) {
343-
map = chartData;
344-
fieldName = 'slices';
345-
346-
// pie charts are finicky about a number being a number
347-
// and editing a number makes it a string
348-
// so let's convert it back into a number.
349-
chartData.map( ( i, index ) => {
350-
switch ( series[1].type ) {
351-
case 'number':
352-
i[1] = parseFloat( i[1]);
353-
break;
354-
}
355-
});
342+
switch ( type ) {
343+
case 'pie':
344+
map = chartData;
345+
fieldName = 'slices';
346+
347+
// pie charts are finicky about a number being a number
348+
// and editing a number makes it a string
349+
// so let's convert it back into a number.
350+
chartData.map( ( i, index ) => {
351+
switch ( series[1].type ) {
352+
case 'number':
353+
i[1] = parseFloat( i[1]);
354+
break;
355+
}
356+
});
357+
break;
358+
case 'tabular':
359+
360+
// table charts are finicky about a boolean being a boolean
361+
// and editing a boolean makes it a string
362+
// so let's convert it back into a boolean.
363+
chartData.map( ( i, index ) => {
364+
series.map( ( seriesObject, seriesIndex ) => {
365+
switch ( seriesObject.type ) {
366+
case 'boolean':
367+
if ( 'string' === typeof i[seriesIndex]) {
368+
i[seriesIndex] = 'true' === i[seriesIndex];
369+
}
370+
break;
371+
}
372+
});
373+
});
374+
break;
356375
}
357376

358377
map.map( ( i, index ) => {
@@ -362,16 +381,18 @@ class Editor extends Component {
362381

363382
const seriesIndex = 'pie' !== type ? index - 1 : index;
364383

365-
if ( settings[fieldName][seriesIndex] === undefined ) {
384+
if ( Array.isArray( settings[fieldName]) && settings[fieldName][seriesIndex] === undefined ) {
366385
settings[fieldName][seriesIndex] = {};
367386
settings[fieldName][seriesIndex].temp = 1;
368387
}
369388
});
370389

371-
settings[fieldName] = settings[fieldName].filter( ( i, index ) => {
372-
const length = -1 >= [ 'pie', 'tabular', 'dataTable' ].indexOf( type ) ? map.length - 1 : map.length;
373-
return index < length;
374-
});
390+
if ( Array.isArray( settings[fieldName]) ) {
391+
settings[fieldName] = settings[fieldName].filter( ( i, index ) => {
392+
const length = -1 >= [ 'pie', 'tabular', 'dataTable' ].indexOf( type ) ? map.length - 1 : map.length;
393+
return index < length;
394+
});
395+
}
375396

376397
chart['visualizer-source'] = source;
377398
chart['visualizer-default-data'] = 0;

js/render-google.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ var __visualizer_chart_images = [];
1414
function renderChart(id) {
1515
var chart = all_charts[id];
1616
var hasAnnotation = false;
17-
if(id !== 'canvas' && typeof chart.series !== 'undefined' && typeof chart.settings.series !== 'undefined'){
18-
hasAnnotation = chart.series.length - chart.settings.series.length > 1;
19-
}
17+
2018
// re-render the chart only if it doesn't have annotations and it is on the front-end
2119
// this is to prevent the chart from showing "All series on a given axis must be of the same data type" during resize.
22-
if(hasAnnotation){
23-
return;
20+
// table/tabular charts do not have annotations.
21+
if(id !== 'canvas' && typeof chart.series !== 'undefined' && typeof chart.settings.series !== 'undefined' && chart.type !== 'tabular'){
22+
hasAnnotation = chart.series.length - chart.settings.series.length > 1;
23+
}
24+
25+
if(! hasAnnotation){
26+
renderSpecificChart(id, all_charts[id]);
2427
}
25-
renderSpecificChart(id, all_charts[id]);
2628
}
2729

2830
function renderSpecificChart(id, chart) {
@@ -259,6 +261,7 @@ var __visualizer_chart_images = [];
259261
if (settings.series) {
260262
switch(chart.type){
261263
case 'table':
264+
case 'tabular':
262265
for(i in settings.series){
263266
i = parseInt(i);
264267
if (!series[i + 1]) {

0 commit comments

Comments
 (0)