d3js: Concatenating "d.y" + year - d3.js

(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.

Related

IMPORTXML To Return a Value from dividendhistory.org

I want to return the value of "Yield:" from a series of stocks from a URL dividenhistory.org, for example HDIF into a Google sheet using IMPORTXML, where F7 represents the user supplied ticker.
=IMPORTXML("https://dividendhistory.org/payout/TSX/"&F7, "/html/body/div/div[2]/div[2]/p[4]")
The problem with the above is the yield value is not always located in the same paragraph, depending on the ticker. It also returns with the word "Yield:" as part of the value.
I believe I should be using the XPATH parameter which should find and return the yield value only, but I am lost. I am open to all suggestions!
I tried with a few of the tickers there, and this should work. For example:
=IMPORTXML("https://dividendhistory.org/payout/ctas/", "//p[contains(.,'Yield')]/text()")
Output:
Yield: 1.05%
Obviously, you can change 'ctas' for any user input.
Try this and see if it works on all tickers.
EDIT:
To get only the number 1.05, you need to split the result and output the 2nd part:
=index(split(IMPORTXML("https://dividendhistory.org/payout/ctas/", "//p[contains(.,'Yield')]/text()"), ": "),2)
Output:
0.0105

get a string and convert it to a variable and assign to a variable in cypress

Hi I want to get this value, convert it to a number and assign as a variable. Then I can do calculations. Can someone help on this?
Tried this not working.
const marks = cy.get('.ant-typography \> span').invoke('text').then(parseInt).should('be.gt', 10)
cy.log('The value is '+ marks)
.ant-typography > span // this is the locator
You need to use .then() to access the variable
cy.get('.ant-typography > span')
.invoke('text')
.then(parseInt)
.should('be.gt', 10)
.then(marks => {
cy.log('The value is '+ marks)
})
Can I return this "marks" value so I can use it outside the .then() method?
See the docs Gleb references, once you have an asynchronous command (even just a .get()) you are pretty much stuck with using a .then() to access values derived from them.
Even an alias does not help - you need a .then() to get it's value.
Please read about Cypress commands https://docs.cypress.io/guides/core-concepts/introduction-to-Cypress#Commands-Are-Asynchronous
For your situation specifically, watch https://www.youtube.com/watch?v=-aptS3yvqcc
You can use an alias to save the value in the variable like this:
cy.get('.ant-typography > span').invoke('text').as('marks')
cy.get('#marks').then((marks) => {
cy.log('The value is ' + marks) //logs marks
cy.wrap(+marks).should('be.gt', 10)
})

timeformat in d3.js not working

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"));

Time scale won't convert dates to numbers

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); });

mustache/hogan i18n and taking word-order into account

Found a couple of questions (and answers) on this: How is internationalization configured for Hogan.js?
,etc.
but non in particular that take word order into account. I need the ability to:
step 1. given a key -> lookup a sentence in a particular language.
step 2. this sentence may contain {{var}} , which need to be
substituted by json-values.
step 2. alone is general mustache-templating.
step 1. alone could be done with several techniques, but I prefer techniques that don't involve any specialized code outside of the Mustache/Hogan engine (in combination with a i18n-resource bundle of course) . Hogan seems to support this with something like: (from url above)
var template = "{{#i18n}}Name{{/i18n}}: {{username}}",
context = {
username: "Jean Luc",
i18n: function (i18nKey) {return translatedStrings[i18nKey];}
};
However to combine 1. and 2. in this example I would want translatedStrings[i18nKey] to return a string which potentially contains {{<some expansion>}} as well.
Someone knows of an elegant way to do this?
Rationale:
Often languages differ a lot in word order, etc. which makes for complex templates without this ability.
The latest version of Hogan.js will handle Mustache tags inside the result returned from a lambda. One minor change to the code in your question however, is that the result of the lambda should be a function in order to modify the string:
var translatedStrings = { name: "Nom {{rank}}" };
var template = "{{#i18n}}name{{/i18n}}: {{username}}",
context = {
username: "Jean Luc",
rank: 'Captain',
i18n: function() {
return function (i18nKey) {return translatedStrings[i18nKey];};
}
};
document.write(Hogan.compile(template).render(context));​ // Nom Captain: Jean Luc
I created a jsfiddle that demonstrates this with the latest version.

Resources