Google Visualization API – Sort by Date

When i tried to plot the data with Google Visualization API, the data point is not in chronological order.
google-visualization-api-data-order
 

There are 2 ways to solve the problem

1. Sort the data before adding the the DataTable.

***Or***

2. Sort by DataTable.sort() after adding to DataTable.

var chartData = new google.visualization.DataTable();
chartData.addColumn('datetime', 'Datetime');
chartData.addColumn('number', 'Total Requests');
// Other data points ...

// Prepare the data row ...

// Sort the data by the first column
chartData.sort(0);

 

It also works if the x-axis is a timestamp in string.

var chartData = new google.visualization.DataTable();
chartData.addColumn('string', 'Datetime');
chartData.addColumn('number', 'Total Requests');
// Other data points ...

// Prepare the data row ...

// Sort the data by the first column
chartData.sort(0);

 

Done =)

Reference:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s