Search code examples
mysqlchartsgoogle-visualizationbar-chart

Bars are showing up even when value is zero - Google Charts


I have created a monthly chart using Google charts but the bars are showing up even when the value is zero.

Here is the screenshot of it

Monthly Sale Chart

and here is the code and I know code is vulnerable to SQL Injection. You can ignore it. This is only for testing purpose

Previously order_total_amount column was set to "VARCHAR" datatype, then someone suggested that it should set to the "INT". So I changed it from Varchar to Int. But that didn't solved the problem. Bar is still showing up despite having 0 value

     <script type="text/javascript">
      google.charts.load('current', {'packages':['bar']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Monthly', 'Sales'],
          <?php   
            
    $sale_chart = "SELECT 
    SUM(IF(month = 'Jan', total, 0)) AS 'Jan',
    SUM(IF(month = 'Feb', total, 0)) AS 'Feb',
    SUM(IF(month = 'Mar', total, 0)) AS 'Mar',
    SUM(IF(month = 'Apr', total, 0)) AS 'Apr',
    SUM(IF(month = 'May', total, 0)) AS 'May',
    SUM(IF(month = 'Jun', total, 0)) AS 'Jun',
    SUM(IF(month = 'Jul', total, 0)) AS 'Jul',
    SUM(IF(month = 'Aug', total, 0)) AS 'Aug',
    SUM(IF(month = 'Sep', total, 0)) AS 'Sep',
    SUM(IF(month = 'Oct', total, 0)) AS 'Oct',
    SUM(IF(month = 'Nov', total, 0)) AS 'Nov',
    SUM(IF(month = 'Dec', total, 0)) AS 'Dec'
    FROM
    (SELECT 
        MIN(DATE_FORMAT(order_date, '%b')) AS month,
            SUM(order_total_amount) AS total
    FROM
        invoice_order
    WHERE
        user_id = '$user_id'
    GROUP BY YEAR(order_date) , MONTH(order_date)
    ORDER BY YEAR(order_date) , MONTH(order_date)) AS sale";

$sale_chart_query = mysqli_query($connection,$sale_chart) or die(mysqli_error($connection));

$sale_chart_array = mysqli_fetch_assoc($sale_chart_query);
            
          foreach($sale_chart_array as $x => $val) { ?>
          ['<?php echo $x; ?>','<?php echo $val; ?>'],
           <?php } ?>
        ]);

        var options = {
        
        };

        var chart = new google.charts.Bar(document.getElementById('chart_div'));

        chart.draw(data, google.charts.Bar.convertOptions(options));
      }
     </script>

After replacing zero with null, that is how it showing up the result

screenshot


Solution

  • using a value of null in the chart's data table will prevent a bar from appearing.
    see following working snippet...

    in this case, you're including strings instead of numbers in the data table.
    remove the single quotes from the second array value, here...

    ['<?php echo $x; ?>',<?php echo $val; ?>],
    

    google.charts.load('current', {
      packages: ['bar']
    }).then(function drawChart() {
      var data = google.visualization.arrayToDataTable([
        ['Monthly', 'Sales'],
        ['Jan', 1000],
        ['Feb', null],
        ['Mar', null],
        ['Apr', null],
        ['May', 40000],
        ['Jun', null],
        ['Jul', null],
        ['Aug', null],
        ['Sep', null],
        ['Oct', null],
        ['Nov', null],
        ['Dec', null]
      ]);
    
      var options = {};
    
      var chart = new google.charts.Bar(document.getElementById('chart_div'));
    
      chart.draw(data, google.charts.Bar.convertOptions(options));
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="chart_div"></div>