Tuesday, 28 August 2018

MySQL SELECT does not return the values ​​correctly and the graph of the Google row does not display as desired

I am quite new here and asking question for first time, So I don't know how to ask question, But I need help of your's.

I have 2 problems in the above graph.
1. I am getting repeated dates for views from my database and is display in the graph as above marked as 1 and 2.
2. The dates are not aligned properly with dates and is marked as 3 in above image and also months are not shown some times as visible in the image.
Help me please. Thank's in Advance
php & MySQL Code
function AdminViews()
{
    if(!$this->DBLogin())
    {
        $this->HandleError("Database login failed!");
        return false;
    }
    $out_query = "SELECT count(ip_address) AS count, date(visit_date_time) as visit_date_time FROM db_views WHERE user_id = id GROUP BY visit_date_time ORDER BY visit_date_time LIMIT 30 ";
    $result = mysqli_query($this->connection,$out_query);
    while($row = mysqli_fetch_array($result))
    {
        $resultset[] = $row;
    }
    if(!empty($resultset))
        return $resultset;
}

Google Graph Javascript Code
<script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var data = google.visualization.arrayToDataTable([ ['Date', 'Views']
        <?php
        $results = $ClassName->AdminViews();
        if(!empty($results)){
            foreach($results as $row) {
                $vDate = str_replace("-",", ",$row['visit_date_time']);
                echo ",[new Date('".$vDate."'),".$row['count']."]";
            }
        } ?> ]);

        var options = {
            pointSize: 5,
            legend: { position: 'top', alignment: 'end' },
            hAxis: { format: 'MMM dd, yyyy', gridlines: {count: -1, color: '#fff'} },
            vAxis: { minValue: 0 }
        };
        var chart = new google.visualization.LineChart(document.getElementById("barChart"));
        chart.draw(data, options);
    } </script>

At Browser Js comes like this
<script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var data = google.visualization.arrayToDataTable([ ['Date', 'Views']
        ,[new Date('2017, 01, 01'),6],[new Date('2017, 01, 02'),6],[new Date('2017, 01, 03'),6],[new Date('2017, 01, 04'),6],[new Date('2017, 01, 05'),7],[new Date('2017, 01, 06'),5],[new Date('2017, 01, 07'),5],[new Date('2017, 01, 07'),3],[new Date('2017, 01, 08'),3],[new Date('2017, 01, 08'),4],[new Date('2017, 01, 09'),2],[new Date('2017, 01, 10'),2],[new Date('2017, 01, 10'),6],[new Date('2017, 01, 11'),6],[new Date('2017, 01, 12'),6],[new Date('2017, 01, 13'),6],[new Date('2017, 01, 14'),6],[new Date('2017, 01, 15'),6],[new Date('2017, 01, 16'),6],[new Date('2017, 01, 17'),10],[new Date('2017, 01, 18'),30],[new Date('2017, 01, 19'),3],[new Date('2017, 01, 20'),3] ]);

        var options = {
            pointSize: 5,
            legend: { position: 'top', alignment: 'end' },
            hAxis: { format: 'MMM dd, yyyy', gridlines: {count: -1, color: '#fff'} },
            vAxis: { minValue: 0 }
        };
        var chart = new google.visualization.LineChart(document.getElementById("barChart"));
        chart.draw(data, options);
    }
</script>


Your group by also needs to cast the datetime to a Date, otherwise you will see in your data that you have multiple entries for the same day but slightly different timestamps, this will give you the multiple nodes.
Change this:
  ... GROUP BY visit_date_time ORDER BY visit_date_time ...

To this:
 ...  GROUP BY DATE(visit_date_time) ORDER BY visit_date_time ...

That should do the trick.
EDIT
For your second issue, count: -1 --> means automatic alignment of grid lines which produces the undesirable result here.
Change this:
var options = {
        pointSize: 5,
        legend: { position: 'top', alignment: 'end' },
        hAxis: { format: 'MMM dd, yyyy', gridlines: {count: -1, color: '#fff'} },
        vAxis: { minValue: 0 }
    };

To this (as you figured out):
var options = {
            pointSize: 5,
            legend: { position: 'top', alignment: 'end' },
            hAxis: { slantedText: 'true', format: 'MMM dd', gridlines: {count: 10, color: 'none'} },
            vAxis: { minValue: 0 }
        };

Disclaimer: The second answer came from OP himself as I wasn't sure.

0 comments:

Post a Comment