Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting data on x-axis in google line chart

I'm using google line chart to plot data by timestamp(x-axis).

my problem is that the x-axis is not being sorted automatically by the google charting.

e.g. "15:45:23 678" data point which should be the first in the chart is being shown at #2. If I put it at the first place then it is fine. That means Google chart doesn't care about the data and it plots as it sees them.

The issue is that my data set is huge and sorting it based on x-axis data takes lot of time. is there any flag in the line chart which could sort it in the browser?

below is the sample data:

['x', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6'],

['15:48:13 744', 0, 0, 0, 0, 0, 0], 

['15:45:23 678', 5, 17, 36, 3957, 20, 21], 

['15:48:18 749', 0, 0, 0, 0, 0, 0]
like image 922
Java Spring Coder Avatar asked Mar 15 '13 23:03

Java Spring Coder


People also ask

How do I sort data in a Google chart?

Alphabetize your dataFrom the Google Sheets menu, select Data and click Sort range. A window will prompt you to choose the column you want to sort; you can choose the A–Z option to quickly alphabetize your selection.

How do you arrange a graph in ascending order in Google Sheets?

Click Data and select Sort range from the drop-down menu. The Sorting dialog box appears. Select the desired column you want to sort by. Select ascending or descending.


1 Answers

You can use the following function to sort the underlying DataTable:
data.sort([{column: 0}]);. Column 0 indicates the x-axis.
This will sort the data in asc order, for desc use data.sort([{column: 0, desc: true}]);

You may also be interested in the getSortedRows(sortColumns) function.

More information about these functions can be read about here
https://developers.google.com/chart/interactive/docs/reference#DataTable_sort
https://developers.google.com/chart/interactive/docs/reference#DataTable_getSortedRows

like image 161
Byron Avatar answered Oct 20 '22 01:10

Byron