How to do a custom sorting in datatables? - sorting

Is it possible to sort numeric item according to its values in a column which contains both numeric and string items in datatables?
I tried columnDefs:
[{ type: 'natural', targets: [0,1] }]
But it's not working. Any help is appreciated.

Well, perhaps you just need to see a working example? Here is the values from the other question you are referring to, and the usage of a sorting plugin I once made for exactly this, any-number -> https://github.com/davidkonrad/Plugins/blob/master/sorting/any-number.js
var table = $('#example').DataTable({
columnDefs : [
{ type: 'any-number', targets: [0] }
]
})
see how it is working here -> http://jsfiddle.net/o53burrf/
This is how most of the other sorting plugins works as well - if you want to use natural, include the source snippet an replace any-number with natural.

Related

Binning Data With Two Timestamps

I'm posting because I have found no content surrounding this topic.
My goal is essentially to produce a time-binned graph that plots some aggregated value. For Example. Usually this would be a doddle, since there is a single timestamp for each value, making it relatively straight forward to bin.
However, my problem lies in having two timestamps for each value - a start and an end. Similar to a gantt chart, here is an example of my plotted data. I essentially want to bin the values (average) for when the timelines exist within said bin (bin boundaries could be where a new/old task starts/ends). Likeso.
I'm looking for a basic example or an answer to whether this is even supported, in Vega-Lite. My current working example would yield no benefit to this discussion.
I see that you found a Vega solution, but I think in Vega-Lite what you were looking for was something like the following. You put the start field in "x" and the end field in x2, add bin and type to x and all should work.
"encoding": {
"x": {
"field": "start_time",
"bin": { "binned": true },
"type": "temporal",
"title": "Time"
},
"x2": {
"field": "end_time"
}
}
I lost my old account, but I was the person who posted this. Here is my solution to my question. The value I am aggregating here is the sum of times the timelines for each datapoint is contained within each bin.
First you want to use a join aggregate to get the max and min times your data extend to. You could also hardcode this.
{
type: joinaggregate
fields: [
startTime
endTime
]
ops: [
min
max
]
as: [
min
max
]
}
You want to find a step for your bins, you can hard code this later or use a formula and write this into a new field.
You want to create two new fields in your data that is a sequence between the max and min, and the other the same sequence offset by your step.
{
type: formula
expr: sequence(datum.min, datum.max, datum.step)
as: startBin
}
{
type: formula
expr: sequence(datum.min + datum.step, datum.max + datum.step, datum.step)
as: endBin
}
The new fields will be arrays. So if we go ahead and use a flatten transform we will get a row for each data value in each bin.
{
type: flatten
fields: [
startBin
endBin
]
}
You then want to calculate the total time your data spans across each specific bin. In order to do this you will need to round up the start time to the bin start and round down the end time to the bin end. Then taking the difference between the start and end times.
{
type: formula
expr: if(datum.startTime<datum.startBin, datum.startBin, if(datum.startTime>datum.endBin, datum.endBin, datum.startTime))
as: startBinTime
}
{
type: formula
expr: if(datum.endTime<datum.startBin, datum.startBin, if(datum.endTime>datum.endBin, datum.endBin, datum.endTime))
as: endBinTime
}
{
type: formula
expr: datum.endBinTime - datum.startBinTime
as: timeInBin
}
Finally, you just need to aggregate the data by the bins and sum up these times. Then your data is ready to be plotted.
{
type: aggregate
groupby: [
startBin
endBin
]
fields: [
timeInBin
]
ops: [
sum
]
as: [
timeInBin
]
}
Although this solution is long, it is relatively easily to implement in the transform section of your data. From my experience this runs fast and just displays how versatile Vega can be. Freedom to visualisations!

mapbox comparsion filter not working on numbers

i have tried to find anything about my problem online but unfortunately i had no luck finding anything that would help me.
i have a custom javascript map built with mapbox-gl-js.
the map shows real estate objects which can be filtered by country, city etc. using a custom built filter bar. now on this filter bar there are dropdown fields and two input fields. the filters i built for the dropdown fields work just fine. in the code use equal-to-comparsions for this task.
the two input fields are there to filter the maximum price and a minimum square meters - here i use the less or equal than and greater or equal than comparsion filters.
for some reason these two filter's won't work as desired. for example if i filter for objects with max price 1000000 objects with greater price still show.
this is how my filter would look like in JSON:
["all",["<=","preis",1000000]]
this is how the features look like:
feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [...]
},
"properties": {
[...],
"preis": 20000000,
[...]
}
}
i have also tried to reproduce this in a simple test map with simple objects - there the problem also exists.
does anyone have a clue why this is acting up on me or has anyone had or got an familiar issue?
thanks and br, John

Getting all nested data object using facets with Elastic

Greatings,
I have done some searching around but I have not come across a solid answer. We are using elastic search and trying to use facets to group together nested objects. Here is what the data looks like:
{
id:1,
name:abc,
object:{
tag: 5,
name:'test1',
set: true
id
}
},
{
id:2,
name:def,
object:{
tag: 2,
name:'test2',
set: false
}
}
and I want to use facets to get a count of object nested. I can get one field from the object using something like this:
{"facets":{"tags":{"terms":{"field":"object.name"}}}}'
but that just gets me the name and a count from the parent object its in. I want all the properties within object. I want tag,name and set to come back within the facet.
Is this possible? All signs seem to point to no, or to use something like script, where i can create a composite field of all 3, but that would require post processing, which i was hoping to avoid doing.
Any help would surely be appreciated. Thank you in advance.

How to change Kendo Grid Filter Format

I have a field in my grid that are IDs that range from 1 to 2000. I have it designated as a number in the field definition.
The issue is when I use the filter and type in say "1000" when I return to the filter to put in another number it displays "1,000.00". I don't want the filtered text box to show the comma or decimal point. That format isn't relevant for this field.
How do I correct this?
Thanks in advance!
While #Flores answer pointed me in the right direction, it did not do what was expected. At least I still had commas in my filter using his snippet. I ended up making a small modification to the code to achieve the desired result.
filterable: {
ui: function (element) {
element.kendoNumericTextBox({
format: '#',
decimals: 0,
});
},
},
That will give you only numbers. No commas and no decimals.
You can set format on the filterable on the column like this:
field: "TaskId",
title: "TaskId",
width: 80,
filterable: {
ui: function (element) {
element.kendoNumericTextBox({
format: "n0"
});
}
}
You should consider using custom filter menu. Here is how to create DDL. In your case you will need to create numeric textbox with specific format.
Per Kendo support:
If filterable.mode is set to 'row', columns.filterable.cell.template should be used to customize the input. Please refer to this example http://dojo.telerik.com/UKulA/2.
You can limit the range using min and max like in the sample above.

Getting the highest value of a column in MongoDB

I've been for some help on getting the highest value on a column for a mongo document. I can sort it and get the top/bottom, but I'm pretty sure there is a better way to do it.
I tried the following (and different combinations):
transactions.find("id" => x).max({"sellprice" => 0})
But it keeps throwing errors. What's a good way to do it besides sorting and getting the top/bottom?
Thank you!
max() does not work the way you would expect it to in SQL for Mongo. This is perhaps going to change in future versions but as of now, max,min are to be used with indexed keys primarily internally for sharding.
see http://www.mongodb.org/display/DOCS/min+and+max+Query+Specifiers
Unfortunately for now the only way to get the max value is to sort the collection desc on that value and take the first.
transactions.find("id" => x).sort({"sellprice" => -1}).limit(1).first()
Sorting might be overkill. You can just do a group by
db.messages.group(
{key: { created_at:true },
cond: { active:1 },
reduce: function(obj,prev) { if(prev.cmax<obj.created_at) prev.cmax = obj.created_at; },
initial: { cmax: **any one value** }
});
db.collectionName.aggregate(
{
$group :
{
_id : "",
last :
{
$max : "$sellprice"
}
}
}
)
Example mongodb shell code for computing aggregates.
see mongodb manual entry for group (many applications) :: http://docs.mongodb.org/manual/reference/aggregation/group/#stage._S_group
In the below, replace the $vars with your collection key and target variable.
db.activity.aggregate(
{ $group : {
_id:"$your_collection_key",
min: {$min : "$your_target_variable"},
max: {$max : "$your_target_variable"}
}
}
)
Use aggregate():
db.transactions.aggregate([
{$match: {id: x}},
{$sort: {sellprice:-1}},
{$limit: 1},
{$project: {sellprice: 1}}
]);
It will work as per your requirement.
transactions.find("id" => x).sort({"sellprice" => -1}).limit(1).first()
If the column's indexed then a sort should be OK, assuming Mongo just uses the index to get an ordered collection. Otherwise it's more efficient to iterate over the collection, keeping note of the largest value seen. e.g.
max = nil
coll.find("id" => x).each do |doc|
if max == nil or doc['sellprice'] > max then
max = doc['sellprice']
end
end
(Apologies if my Ruby's a bit ropey, I haven't used it for a long time - but the general approach should be clear from the code.)
Assuming I was using the Ruby driver (I saw a mongodb-ruby tag on the bottom), I'd do something like the following if I wanted to get the maximum _id (assuming my _id is sortable). In my implementation, my _id was an integer.
result = my_collection.find({}, :sort => ['_id', :desc]).limit(1)
To get the minimum _id in the collection, just change :desc to :asc
Following query does the same thing:
db.student.find({}, {'_id':1}).sort({_id:-1}).limit(1)
For me, this produced following result:
{ "_id" : NumberLong(10934) }

Resources