Jeremy M
Jeremy M

Reputation: 65

Displaying dates in Flot from timestamps

I am trying to get my graph to display dates on the x axis instead of the timestamps. I've searched and read other posts and still can't get it to budge. Here's my code:

for($i = 0; $i < 3; $i++)
{
    $j = $i + 1;
    $query = "SELECT UNIX_TIMESTAMP(date_added)*1000, COUNT(id)
                    FROM likes
                    WHERE date_added BETWEEN
                        DATE_SUB(CURDATE(), INTERVAL $j MONTH)
                        AND DATE_SUB(CURDATE(), INTERVAL $i MONTH)";
    $result = mysql_query($query);
    echo mysql_error();
    $date = mysql_result($result, 0, 0);
    $count = mysql_result($result, 0, 1);
    $pair = array( (string)$date, (int)$count );
    array_push($data_series1, $pair);
}

    $.ajax({
    url: 'graph_likes.php?get_data=month',
    dataType: 'json',
    success: function(returned) {
        $.plot($('#container'), 
        [{
            data: returned,
            points:
            {
                show: true
            },
            lines:
            {
                show: true
            },
            color: '#00d6e2',
            xaxis:
            {
                mode: 'time'
            }
        }]);
    }
});

Upvotes: 2

Views: 4339

Answers (1)

hakre
hakre

Reputation: 197682

If you want flot to display your timestamps nicely formatted, you can try using the timestamp format option (called timeformat):

        ...
        xaxis:
        {
            mode: 'time',
            timeformat: "%y/%m/%d"
        }
        ...

You find the option explained in this document: http://people.iola.dk/olau/flot/API.txt , look for the section called Time series data.

Upvotes: 3

Related Questions