Okay
  Public Ticket #2075354
Null values on line chart
Closed

Comments

  •  2
    Chris Berry started the conversation

    Just got the plug in. Trying to create a line chart showing data from 2018 vs. data from 2019. Of course there are several months of 2019 with no values. I left those fields blank but the plugin renders each of those fields as a zero, which shows a line plummeting to the bottom of the chart. Not what I want to show. How can I just get the line for this year to stop when there are no values for the rest of the year? What I want is something like the second attachment. I can't give you a URL to my site because it's still under construction. Thanks.

  •  2,572
    Aleksandar replied

    Hi Chris.

    Thank you for your purchase.

    You have to make some changes in code in file :

    /wp-content/plugins/wpdatatables/source/class.wpdatachart.php around line 1584

    case 'int':
        $return_data_row[] = (float)$row[$columnKey];
        break;
    case 'float':
        $return_data_row[] = (float)$row[$columnKey];
        break;

    and replace with this

    case 'int':
        if (is_null($row[$columnKey])){
            $return_data_row[]=null;
        }else{
           $return_data_row[] = (float)$row[$columnKey];
        }
        break;
    case 'float':
        if (is_null($row[$columnKey])){
             $return_data_row[]=null;
        }else{
             $return_data_row[] = (float)$row[$columnKey];
        }
        break;                            

    With these changes you will get charts like on the first screenshot.

    If you want to make charts like on the second screenshot, you have to use wpdatachart callbacks and insert that on page where your chart is.

    <script type="text/javascript">
    jQuery(window).load(function(){
        if( typeof wpDataChartsCallbacks == 'undefined' ){ wpDataChartsCallbacks = {};
     }
            wpDataChartsCallbacks[39] = function(obj){
                    obj.options.plotOptions.series= {connectNulls: true};
                }
    });
    </script>

    One more thing you have to change in this callback number 39 in (wpDataChartsCallbacks[39]) to id of the chart that you use on page.

    This will be overwritten on next update so you have to come back here and do it again.

    1539290246.png
    3942732156.png

    Best regards.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • Matt replied

    Hey Aleksander,

    This fix used to work for me, but on the latest update replacing the code in class.wpdatachart.php isn't working.

    Looking at "// The data itself" section int & float cases.

    Is there something more or different I need to do in the latest version?

    Many thanks,

    Matt.

  •  2,572
    Aleksandar replied

    Hey Matt

    We simplified this a bit, but it still requires some custom work. With our next update, that custom work will be integrated with the plugin, so you won't have to redo it.

    You'll need to modify some files since we somehow forgot to pass the chart ID in there. Please go to wp-content/plugins/wpdatatables/source/class.wpdatachart.php and on line 1634 you will find this:

     $row[$columnKey] = apply_filters('wpdatatables_filter_int_cell_data_in_charts', $row[$columnKey], $columnKey, $this->_wpdatatable->getWpId());

    Replace it with this:

    $row[$columnKey] = apply_filters('wpdatatables_filter_int_cell_data_in_charts', $row[$columnKey], $columnKey, $this->getId(), $this->_wpdatatable->getWpId());

    In the same file, on line 1642 you will find this:

    $row[$columnKey] = apply_filters('wpdatatables_filter_float_cell_data_in_charts', $row[$columnKey], $columnKey, $this->_wpdatatable->getWpId());

    And you need to replace it with this:

    $row[$columnKey] = apply_filters('wpdatatables_filter_float_cell_data_in_charts', $row[$columnKey], $columnKey, $this->getId(), $this->_wpdatatable->getWpId());

    Again, in the same file, on line 1725 you will find this:

    $cellData = apply_filters('wpdatatables_filter_int_cell_data_in_charts', $this->_wpdatatable->getCell($columnKey, $rowIndex), $columnKey, $this->_wpdatatable->getWpId());

    Replace it with this:

    $cellData = apply_filters('wpdatatables_filter_int_cell_data_in_charts', $this->_wpdatatable->getCell($columnKey, $rowIndex), $columnKey, $this->getId(), $this->_wpdatatable->getWpId());

    Then go to line 1733 of the same file, and find this:

    $floatNumber = apply_filters('wpdatatables_filter_float_cell_data_in_charts', $this->_wpdatatable->getCell($columnKey, $rowIndex),$columnKey , $this->_wpdatatable->getWpId());

    Replace it with this:

    $floatNumber = apply_filters('wpdatatables_filter_float_cell_data_in_charts', $this->_wpdatatable->getCell($columnKey, $rowIndex),$columnKey , $this->getId(), $this->_wpdatatable->getWpId());

    This adds the chart ID in the parameters. You'll only have to do this one time, as it will be included in our next update. 

    Then, go to the functions.php file of your theme or child theme, and add this:

    function filterNullValues ($value, $origHeader ,$chartID, $tableID){
         // You can add conditions for specific org header, chart id or table id
        // Here we only check if value is null and provide same value to the chart object
        if (is_null($value)){
            $value = null;
        }
        return $value;
    }
    add_filter( 'wpdatatables_filter_int_cell_data_in_charts', 'filterNullValues', 10, 4 );
    add_filter( 'wpdatatables_filter_float_cell_data_in_charts', 'filterNullValues', 10, 4 );

    This works for integer and float columns that have NULL values.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • Matt replied

    Aleksandar, thank you so much!

    I'm thrilled to hear it'll be built into next update, and I'm going to try your custom code now.

    Cheers!

    Matt.

  • Matt replied

    Hey Aleksander,

    Successful on all the changes to class.wpdatachart.php, but when I add:

    function filterNullValues ($value, $origHeader ,$chartID, $tableID){
         // You can add conditions for specific org header, chart id or table id
        // Here we only check if value is null and provide same value to the chart object
        if (is_null($value)){
            $value = null;
        }
        return $value;
    }
    add_filter( 'wpdatatables_filter_int_cell_data_in_charts', 'filterNullValues', 10, 4 );
    add_filter( 'wpdatatables_filter_float_cell_data_in_charts', 'filterNullValues', 10, 4 );

    to my child theme's functions.php none of my charts plot at all :(

  •  2,572
    Aleksandar replied

    Hey Matt, sorry about that.

    There was an issue for Integer and Float columns that had values that were treated as strings, not as floats (which is a requirement for Highcharts API).

    In file wp-content/plugins/wpdatatables/source/class.wpdatachart.php on line 2187 you will find this:

    case 'int':
                                    if (has_filter('wpdatatables_filter_int_cell_data_in_charts')) {
                                        $row[$columnKey] = apply_filters('wpdatatables_filter_int_cell_data_in_charts', $row[$columnKey], $columnKey, $this->_wpdatatable->getWpId());
                                        $return_data_row[] = $row[$columnKey];
                                    } else {
                                        $return_data_row[] = (float)$row[$columnKey];
                                    }
                                    break;
                                case 'float':
                                    if (has_filter('wpdatatables_filter_float_cell_data_in_charts')) {
                                        $row[$columnKey] = apply_filters('wpdatatables_filter_float_cell_data_in_charts', $row[$columnKey], $columnKey, $this->_wpdatatable->getWpId());
                                        if ($decimalPlaces != -1){
                                            $return_data_row[] = number_format(
                                                ($row[$columnKey]),
                                                $decimalPlaces,
                                                '.',
                                                $thousandsSeparator ? '' : '.');
                                        }else {
                                            $return_data_row[] = $row[$columnKey];
                                        }
                                    } else {
                                        if ($decimalPlaces != -1){
                                            $return_data_row[] = (float)number_format(
                                                (float)($row[$columnKey]),
                                                $decimalPlaces,
                                                '.',
                                                $thousandsSeparator ? '' : '.');
                                        }else {
                                            $return_data_row[] = (float)$row[$columnKey];
                                        }
                                    }
                                    break;

    Replace it with this:

    case 'int':
                                    if (has_filter('wpdatatables_filter_int_cell_data_in_charts')) {
                                        $row[$columnKey] = apply_filters('wpdatatables_filter_int_cell_data_in_charts', $row[$columnKey], $columnKey, $this->getId(), $this->_wpdatatable->getWpId());
                                        if (!is_null($row[$columnKey])) {
                                            $return_data_row[] = (float)$row[$columnKey];
                                        } else {
                                            $return_data_row[] = null;
                                        }
                                    } else {
                                        $return_data_row[] = (float)$row[$columnKey];
                                    }
                                    break;
                                case 'float':
                                    if (has_filter('wpdatatables_filter_float_cell_data_in_charts')) {
                                        $row[$columnKey] = apply_filters('wpdatatables_filter_float_cell_data_in_charts', $row[$columnKey], $columnKey, $this->getId(), $this->_wpdatatable->getWpId());
                                       if (!is_null($row[$columnKey])) {
                                            if ($decimalPlaces != -1) {
                                                $return_data_row[] = (float)number_format(
                                                    (float)($row[$columnKey]),
                                                    $decimalPlaces,
                                                    '.',
                                                    $thousandsSeparator ? '' : '.');
                                            } else {
                                                $return_data_row[] = (float)$row[$columnKey];
                                            }
                                        } else {
                                            $return_data_row[] = null;
                                        }
                                    } else {
                                        if ($decimalPlaces != -1){
                                            $return_data_row[] = (float)number_format(
                                                (float)($row[$columnKey]),
                                                $decimalPlaces,
                                                '.',
                                                $thousandsSeparator ? '' : '.');
                                        }else {
                                            $return_data_row[] = (float)$row[$columnKey];
                                        }
                                    }
                                    break;

    Then, in the same file, on line 2278 you will find this:

    case 'int':
                                     if (has_filter('wpdatatables_filter_int_cell_data_in_charts')) {
                                         $cellData = apply_filters('wpdatatables_filter_int_cell_data_in_charts', $this->_wpdatatable->getCell($columnKey, $rowIndex), $columnKey, $this->_wpdatatable->getWpId());
                                         $return_data_row[] = $cellData;
                                     }else {
                                         $return_data_row[] = (float)$this->_wpdatatable->getCell($columnKey, $rowIndex);
                                     }
                                    break;
                                case 'float':
                                    if (has_filter('wpdatatables_filter_float_cell_data_in_charts')) {
                                        $floatNumber = apply_filters('wpdatatables_filter_float_cell_data_in_charts', $this->_wpdatatable->getCell($columnKey, $rowIndex),$columnKey , $this->_wpdatatable->getWpId());
                                        if ($decimalPlaces != -1){
                                            $return_data_row[] = number_format ($floatNumber, $decimalPlaces);
                                        }else {
                                            $return_data_row[] = $floatNumber;
                                        }
                                    }else {
                                        $floatNumber= (float)$this->_wpdatatable->getCell($columnKey, $rowIndex);;
                                        if ($decimalPlaces != -1){
                                            $return_data_row[] = (float)number_format ($floatNumber, $decimalPlaces);
                                        } else {
                                            $return_data_row[] = $floatNumber;
                                        }
                                    }
                                    break;

    Replace it with this:

    case 'int':
                                     if (has_filter('wpdatatables_filter_int_cell_data_in_charts')) {
                                         $cellData = apply_filters('wpdatatables_filter_int_cell_data_in_charts', $this->_wpdatatable->getCell($columnKey, $rowIndex), $columnKey, $this->getId(), $this->_wpdatatable->getWpId());
                                          if (!is_null($cellData)) {
                                              $return_data_row[] = (float)$cellData;
                                          } else {
                                              $return_data_row[] = null;
                                          }
                                     }else {
                                         $return_data_row[] = (float)$this->_wpdatatable->getCell($columnKey, $rowIndex);
                                     }
                                    break;
                                case 'float':
                                    if (has_filter('wpdatatables_filter_float_cell_data_in_charts')) {
                                        $floatNumber = apply_filters('wpdatatables_filter_float_cell_data_in_charts', $this->_wpdatatable->getCell($columnKey, $rowIndex),$columnKey , $this->getId(), $this->_wpdatatable->getWpId());
                                          if (!is_null($floatNumber)) {
                                              if ($decimalPlaces != -1) {
                                                  $return_data_row[] = (float)number_format($floatNumber, $decimalPlaces);
                                              } else {
                                                  $return_data_row[] = $floatNumber;
                                              }
                                          } else {
                                              $return_data_row[] = null;
                                          }
                                    } else {
                                        $floatNumber= (float)$this->_wpdatatable->getCell($columnKey, $rowIndex);;
                                        if ($decimalPlaces != -1){
                                            $return_data_row[] = (float)number_format ($floatNumber, $decimalPlaces);
                                        } else {
                                            $return_data_row[] = $floatNumber;
                                        }
                                    }
                                    break;

    Along with the previous changes, it will work fine. Please give it a go, and let me know.

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • Matt replied

    Success!  Thank you very much Aleksandar!

  •  2,572
    Aleksandar replied

    You're welcome, Matt.

    Glad I could be of assistance!

    Kind Regards, 

    Aleksandar Vuković
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables