Tablesorter ordering dates and empty dates incorrectly - date

My tablesorter table is not sorting dates and empty fields correctly.
I would expect empty fields to be treated like zeros, and be grouped at the top/bottom. Instead I get some dates beneath them.
$('table').tablesorter({
widthFixed: true,
// sort on the last name in ascending order
sortList: [
[2, 0]
],
dateFormat: "ddmmyyyy",
emptyTo: "zero",
sortInitialOrder: "desc",
headers: {
3: {
sorter: "shortDate"
},
4: {
sorter: "shortDate"
},
5: {
sorter: "shortDate"
},
6: {
sorter: "shortDate"
}
},
textExtraction: {
3: function (n, t, c) {
return $(n).attr('data-date');
},
4: function (n, t, c) {
return $(n).attr('data-date');
},
5: function (n, t, c) {
return $(n).attr('data-date');
}
},
widgets: ['zebra', 'columns']
});
See the fiddle here. Sort using the 'Date of birth' column.

The "shortDate" parser converts all dates into a time in milliseconds to make date comparison (using a operator or date range with the filter widget) and sorting of dates easier. In order to do that, it uses a javascript function getTime():
The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC.
So, the issue you are noticing is because non-date cells are treated as having a zero-value, and the date that is not sorting properly is the date "1/1/1930". Entering this date into the javascript console, you'll see this result:
new Date('1/1/1930').getTime()
// returns -1262282400000
Remember, it's the time since 1/1/1970, so the time returns a negative number, which of course is less than zero.
The simplest solution would be to just set the emptyTo option to "min", which sets the value of any empty cells to be a maximum negative number (demo):

Related

How to use sheets.spreadsheets.batchUpdate() to apply formatting?

I am attempting to automatically apply new formatting to my spreadsheet, however while it does not appear to cause any errors, it is not working.
My goal is to apply duration formatting to columns G and H (index 6 and 7) of my spreadsheet. I am automatically updating the spreadsheet with string data in the format without brackets:
[D]:[H]:[M]
Where D, H, and M may be single or double digit. I would like it to display in the following format without brackets on the spreadsheet:
[hh]:[mm]:[ss]
Where there is always at least two digits in each column. However, the spreadsheet seems to automatically choose formatting regardless of what formatting I request, and it chooses a decimal number format. How can I use the batchUpdate() function to apply a working duration formatting to just these two columns?
My Code attempt is below. I am using the Node.js Google API Client Library.
sheets.spreadsheets.batchUpdate({
spreadsheetId: '1AakOf_W90JdAtL0R8XeMmacWrnBx8wLKrMCdHdnNmhM',
resource: {
requests: [{
updateCells:{
range: {
startColumnIndex: 6,
endColumnIndex: 8
},
'fields': 'userEnteredFormat/numberFormat(pattern,type),effectiveFormat/numberFormat(pattern,type)',
rows:[{
values:[{
userEnteredFormat:{
numberFormat:{
pattern: '[hh]:[mm]:[ss]',
type: 'TIME'
}
},
effectiveFormat:{
numberFormat:{
pattern: '[hh]:[mm]:[ss]',
type: 'TIME'
}
}
}]
}]
}
}
]
}
}, function(err, response, responseBody){
if(err){
console.log(err);
console.log(err.code);
}
});
It would seem that the issue lies within the indexing of columns on the spreadsheet. When writing this code, I assumed that the columns were indexed starting at zero. However, it would appear that the indexing starts at -1, as columns G and H are actually represented by indices 5 and 6.

How to get Min/Max Dates From Flot Chart

Does Flot offer us a function that will return the minimum and maximum dates of the data that is used in the chart?
To be clear, I'm not looking for a function that returns the min/max dates of the x-axis, but rather the min/max dates that exists within all the dataset that the chart is using. (i.e. I'm aware of the plot.getAxes().xaxis.max/min functions that return the dates that are shown on the xaxis).
I'm guessing that Flot does "know" the min/max dates within a dataset, since it is able to dynamically create the xaxis date range properly. I'm hoping that it makes those dates available to us.
Here are all properties from plot.getAxes().yaxis:
yaxis: Object
box: Object
c2p: (c) { return m + c / s; }
-> datamax: 116.2
-> datamin: 0
direction: "y"
innermost: true
labelHeight: 11
labelWidth: 18
max: 118
min: 0
n: 1
options: Object
p2c: (p) { return (p - m) * s; }
position: "left"
reserveSpace: true
scale: 6.3559322033898304
show: true
tickDecimals: 0
tickFormatter: (v, axis) {
tickGenerator: (axis) {
tickLength: "full"
tickSize: 10
ticks: Array[13]
used: true
What you want are the datamax and datamin properties.

mongodb query opposite of value in range

I have a mongoDB collection with data like this
{
start: "5.3",
end: "8.10",
owner: "guy1"
}
{
start: "9.0",
end: "14.5",
owner: "guy2"
}
Suppose I wish to know who got element 6.2.
We can see that guy1 has got element 6.2 because he own element 5.3 to element 8.10.
This is opposite to
db.collection.find( { field: { $gt: value1, $lt: value2 } } );
The fields specify the start and end. The supplied data is in between the range.
Looking for the largest value of start which is smaller than the required value.
How to query this?
Negating the logic, this should work:
db.collection.find( { start : { $lte : 6.2 }, end : { $gt : 6.2 } })
The problem is you need to ensure that you have no overlapping intervals, which can be tricky, but that is a problem at the application level. Also, please make sure you're not storing doubles as strings, otherwise the $gt/$lt queries will not work as expected, i.e make sure your data looks like this instead:
{
start: 9.0,
end: 14.5,
owner: "guy2"
}
As pointed out by Michael, you should also make sure your interval logic works out, because there are different methods interval inclusion can be defined. Math notation is ) vs. ], but the discussion gets more complicated with floating point numbers where there are limits on the value of a smallest epsilon which depends on the value of the number itself...
If I understood you correctly, you can use $not operator (put your original query inside $not) or you knowing that
not (x > a and x < b) is equal to x < a or x > b
you can rewrite it with $or query

MongoDB aggregation over a range

I have documents of the following format:
[
{
date:"2014-07-07",
value: 20
},
{
date:"2014-07-08",
value: 29
},
{
date:"2014-07-09",
value: 24
},
{
date:"2014-07-10",
value: 21
}
]
I want to run an aggregation query that gives me results in date ranges. for example
[
{ sum: 49 },
{ sum:45 },
]
So these are daily values, I need to know the sum of value field for last 7 days. and 7 days before these. for example sum from May 1 to May 6 and then sum from May 7 to May 14.
Can I use aggregation with multiple groups and range to get this result in a single mongodb query?
You can use aggregation to group by anything that can be computed from the source documents, as long as you know exactly what you want to do.
Based on your document content and sample output, I'm guessing that you are summing by two day intervals. Here is how you would write aggregation to output this on your sample data:
var range1={$and:[{"$gte":["$date","2014-07-07"]},{$lte:["$date","2014-07-08"]}]}
var range2={$and:[{"$gte":["$date","2014-07-09"]},{$lte:["$date","2014-07-10"]}]}
db.range.aggregate(
{$project:{
dateRange:{$cond:{if:range1, then:"dateRange1",else:{$cond:{if:range2, then:"dateRange2", else:"NotInRange"}}}},
value:1}
},
{$group:{_id:"$dateRange", sum:{$sum:"$value"}}}
)
{ "_id" : "dateRange2", "sum" : 45 }
{ "_id" : "dateRange1", "sum" : 49 }
Substitute your dates for strings in range1 and range2 and optionally you can filter before you start to only operate on documents which are already in the full ranges you are aggregating over.

Kendo Grid Decimal Column Sorting

I want sort the kendo grid column. In the below screen shot i need sort the WBS column either ascending or descending. In that column have that values like 1.1, 1.1.1, 1.2.1, 1.2.1.1, 1.2.1.2, 1.3, 2, 2.1, 2.1.1, 2.2 etc.,
Basically you need to define an ad-hoc compare function for WBS column. This is possible using columns.sortable.compare.
Now, the question is inventing a function that is able to compare two values as you need.
It comes to my mind doing the following:
Split the original value separated by "." in an array of values. Ex: "10.1.2" becomes [ "10", "1", "2" ].
Compare one by one the values of the arrays keeping in mind that one might be shorter than the other which means that is smaller (i.e. 10.1 is smaller 10.1.1).
This would be something like:
columns : [
...
{
field: "wbs",
width: 150,
title: "WBS",
sortable: {
compare: function (a, b) {
var wbs1 = a.wbs.split(".");
var wbs2 = b.wbs.split(".");
var idx = 0;
while (wbs1.length > idx && wbs2.length > idx) {
if (+wbs1[idx] > +wbs2[idx]) return true;
if (+wbs1[idx] < +wbs2[idx]) return false;
idx++;
}
return (wbs1.length > wbs2.length);
}
}
}
...
]
NOTE: As I said before and as result of the split, what I get in the arrays are strings. So it is pretty important that we use a little trick for comparing the values as number that is prepend a +. So when I do if (+wbs1[idx] > +wbs2[idx]) I'm actually comparing the values on wbs1[idx] and wbs2[idx] as numbers.
You can see it here: http://jsfiddle.net/OnaBai/H4U7D/2/