I am having trouble with using the time scale. I have dates in the format 'YYYYMMDD', I parse these with:
parseDate = d3.time.format("%Y%m%d").parse
I set the domain to static dates using the same above function. I can see the dates in correct format in the console. But when applying the scale function x, it returns 'NaN' WAT?
It's probably something small I'm not seeing, it's driving me mad...
Code can be found here: http://bl.ocks.org/pberden/5668581
I think the problem is in the way you call d3.nest. According to the spec
The key function will be invoked for each element in the input array, and must return a string identifier that is used to assign the element to its group.
You convert the Day in your csv file to a Date but then, as you are building a map from the array using d3.nest(), the invocation of the key function turns this Date to a String by doing an implicit conversion.
To fix this I think you could try to force your line generator to turn a String into Date like so
var line = d3.svg.line()
.x(function(d) { return x(new Date(d.key)); })
.y(function(d) { return y(d.values.Value); });
Related
I've seen that you can use an ".isValid()" function to check that a given string is in a date format:
moment('2007-05-05', 'YYYY-MM-DD', true).isValid()
But is there a way to confirm that the format is correct? For example:
'YYYY-MM-DD' should return true, but
'YYYY-MM-DDsadsadl' should return false since the characters at the end of the string aren't valid DateTime chars.
We're working on a tool that allows a user to input an existing date format, and then a second input to enter the desired format, but we need validation to ensure the string can properly parse and convert, but they aren't entering a specific date.
The application must accept any and all possible date formats.
Use the following function to validate your format.
validFormat = function(inputFormat){
var validation = moment(moment('2017-06-17').format(inputFormat), inputFormat).inspect();
if(validation.indexOf('invalid') < 0)
return true;
else
return false;
}
Do spend some time to understand this. This simply does a reverse verification using inspect(). The date 2017-06-17 can be replaced by any valid date.
This Moment Js Docs will help you identify the valid formats.
Just make a call to this function as
validFormat('YYYY MM DD')
const getIsValid = inputFormat => moment(moment().format(inputFormat), inputFormat).isValid()
Explanation:
moment().format(inputFormat) - Create a date string from the current time from that format
This is then wrapped with moment() to make that string a moment date object, defining the format to parse it with. Finally we call the isValid() property on that moment date object. This ensures we are able to both create and parse a moment with our custom format.
I have a Line object in a document inside RethinkDB
I am looking for an easy/efficient way of calculating the real world distance of the line.
My initial strategy was to pull the line out of the database and calculate it inside Node.js but ideally would like a way to do this 'in the box'
I cannot find a way to iterate over the coordinates of the line inside ReQl and use the distance function to calculate.
Any help is greatly appreciated.
Unfortunately, the only way I could find how to do this, as it seems that the points within the r.line object are inaccessible, is to convert the line into GeoJSON, convert to points, and then return the calculation.
Using an object like this:
{
id: 101,
route: r.line([-122.423246,37.779388], [-121.886420,37.329898])
}
Your query would look like this:
r.table('data').get(101)('route').do(function(doc){
var points = doc.toGeojson()("coordinates").map(function(point){
return r.point(point(0), point(1));
});
return {
"distance": points(0).distance(points(1))
}
});
Or, as you probably have a whole bunch of line distances to calculate:
r.table('data').hasFields("route")('route').map(function(doc){
var points = doc.toGeojson()("coordinates").map(function(point){
return r.point(point(0), point(1));
});
return {
line: points(0).distance(points(1))
}
});
I've also submitted a Github issue to see if we can find an improvement!
I am using the following code to get the hour and minute in d3.js
e.time=e.time.substring(12,19);
var timeformat=d3.time.format("%H:%M").parse;
e.time2=timeformat(e.time);
console.log(e.time2);
But I am getting null in output. My e.time contains following pattern of value
2015-03-29T20:32:24Z
e.time.substring(12,19) returns 20:32:24.
What is the mistake I am doing?
You need to do 2 things here:
Parse the string 20:32:24, to a date. For that use:
var timeformat=d3.time.format("%H:%M:%S").parse;
Format the Date, returning a string in the valid format )in this case hh:mm:
var hoursandminsformat=d3.time.format("%H:%M");
Fiddle here: http://jsfiddle.net/henbox/mwvvuazz/
A better approach, rather than using e.time.substring(12,19);, would be just to parse the full date object using:
var ISO8601format=d3.time.format("%Y-%m-%dT%H:%M:%SZ")
and then
var fomatted_time = hoursandminsformat(ISO8601format.parse("2015-03-29T20:32:24Z"));
(This is following up on this question.) Super-brief/basic Q: is there any way that I can concatenate d.(something) with a numeric value? I'm using a CSV and my variable names follow a pattern (for year 2012 = y2012, year 2002 = y2002, etc).
I'd like to refer to the current year being examined after each keydown. For example:
svg.selectAll("circle")
.transition(1500)
.attr("r", function(d) {
return Math.sqrt(parseInt("d.y"+year) * 0.0004);
})
Console's telling me "d.y"+year is not kosher. I've tried defining it as a variable, testing it out in the console (where it returns "d.y2012"), but it still breaks the function (d).
In javascript, d.something is the same as d["something"].
In your case, you can try d["y"+year] instead of "d.y"+year.
When using the MongoDB shell, how do I use a guid datatype (which I have used as the _id in my collection).
The following format doesn't work:
>db.person.find({"_id","E3E45566-AFE4-A564-7876-AEFF6745FF"});
Thanks.
You can use easily:
.find({ "_id" : CSUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")})
You have to compare the _id value against an instance of BinData (not against a string). Unfortunately the BinData constructor takes a Base64 string instead of a hex string.
Your GUID value is missing two hex digits at the end, so for the purposes of this example I will assume they are "00". The following values are equivalent:
hex: "E3E45566-AFE4-A564-7876-AEFF6745FF00" (ignoring dashes)
base64: "ZlXk4+SvZKV4dq7/Z0X/AA=="
So your query should be:
>db.person.find({_id : new BinData(3, "ZlXk4+SvZKV4dq7/Z0X/AA==")})
I am assuming that the binary subtype was correctly set to 3. If not, what driver was used to create the data?
You could use the following js function in front of your query like so:
function LUUID(uuid) {
var hex = uuid.replace(/[{}-]/g, ""); // removes extra characters
return new UUID(hex); //creates new UUID
}
db.person.find({"_id" : LUUID("E3E45566-AFE4-A564-7876-AEFF6745FF"});
You could save the function in .js file and load it or open it before you make your query and if you copy the value from your results you should rename the function with:
LUUID for Legacy UUID
JUUID for Java encoding
NUUID for .net encoding
CSUUID for c# encoding
PYUUID for python encoding
I know it's an old issue, but without any additional needs you can use this one:
find({_id:UUID('af64ab4f-1098-458a-a0a3-f0f6c93530b7')})
You can fix this issue by using split() and join() workaround:
for instance if I use "E3E45566-AFE4-A564-7876-AEFF6745FF" hex value with - inside UUID() function, it does not return BinData in mongo so please try removing all the - before passing to UUID function.
db.person.find({"_id":UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))});
Or by defining a variable to do it in multiple line:
var uuid = UUID("E3E45566-AFE4-A564-7876-AEFF6745FF".split("-").join(''))
db.person.find({"_id":uuid});
or by creating a simple function:
function BUUID(uuid){
var str = uuid.split("-").join('');
return new UUID(str);
}
db.person.find({"_id": BUUID("E3E45566-AFE4-A564-7876-AEFF6745FF")}).pretty();