Is it feasible to use D3 to locate the key associated with a specific value?
For instance, in the provided array below, how can one extract "Red Delicious" when referencing the value d.y:
[
{ year: "2006", redDelicious: "10", mcintosh: "15", oranges: "9", pears: "6" },
{ year: "2007", redDelicious: "12", mcintosh: "18", oranges: "9", pears: "4" },
{ year: "2008", redDelicious: "05", mcintosh: "20", oranges: "8", pears: "2" },
{ year: "2009", redDelicious: "01", mcintosh: "15", oranges: "5", pears: "4" },
{ year: "2010", redDelicious: "02", mcintosh: "10", oranges: "4", pears: "2" },
{ year: "2011", redDelicious: "03", mcintosh: "12", oranges: "6", pears: "3" },
{ year: "2016", redDelicious: "19", mcintosh: "17", oranges: "5", pears: "7" },
]
Can keys with multiple words be referenced in D3/JS? For example, if we want the key to be "Red Delicious" instead of "redDelicious"
var data = [
{ year: "2006", red Delicious: "10", mcintosh: "15", oranges: "9", pears: "6" },
{ year: "2007", red Delicious: "12", mcintosh: "18", oranges: "9", pears: "4" },
{ year: "2008", red Delicious: "05", mcintosh: "20", oranges: "8", pears: "2" },
{ year: "2009", red Delicious: "01", mcintosh: "15", oranges: "5", pears: "4" },
{ year: "2010", red Delicious: "02", mcintosh: "10", oranges: "4", pears: "2" },
{ year: "2011", red Delicious: "03", mcintosh: "12", oranges: "6", pears: "3" },
{ year: "2016", red Delicious: "19", mcintosh: "17", oranges: "5", pears: "7" },
]
UPDATE: When trying to reference "Red Delicious" it returns as undefined. Refer to this jsfiddle for clarification (data displays as "undefined" on mouseover).
var rect = groups.selectAll("rect")
.data(function(d) { return d; })
.enter()
.append("rect")
.attr("x", function(d) { return x(d.x); })
.attr("y", function(d) { return y(d.y0 + d.y); })
.attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); })
.attr("width", x.rangeBand())
.on("mouseover", function() { tooltip.style("display", null) })
.on("mouseout", function() { tooltip.style("display", "none"); })
.on("mousemove", function(d) {
var xPosition = d3.mouse(this)[0] - 15;
var yPosition = d3.mouse(this)[1] - 25;
tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
tooltip.select("text").text("Apple: "+d['red Delicious']);
});
It would be beneficial to identify the key (in this case, "Red Delicious") of the current value being charted using something like
.text(d.key+": "+d.y) // where d.key is the correct way to find d's key