d3.js - Reducing the number of anonymous functions needed in calls to D3's `attr` and the like -


so i've got rudimentary code works fine [btw i'm not using scales yet]:

        svg.selectall("rect")            .data(dataset)            .enter()            .append("rect")            .attr("x", function(d, i) {                 return * (w / dataset.length);            })            .attr("y", function(d) {                 return h - (d.close * 4);            })            .attr("width", w / dataset.length - barpadding)            .attr("height", function(d) {                 return d.close * 4;            })            .attr("fill", function(d) {                 return "rgb(0, 0, " + (d.close * 10) + ")";            })            .append("title").text(function(d) {                 if (d.date == null) {                     return "close: " + d.close;                 } else {                     return "date: " + datefmt(d.date)                          + "\nclose: " + d.close;                 }            }); 

now i'm told can reduce calls attr having first argument object of name value pairs like:

        svg.selectall("rect")            .data(dataset)            .enter()            .append("rect")            .attr({            "x": function(d, i) {                 return * (w / dataset.length);            },            "y": function(d) {                 return h - (d.close * 4);            },            "width": w / dataset.length - barpadding,            "height": function(d) {                 return d.close * 4;            },            "fill": function(d) {                 return "rgb(0, 0, " + (d.close * 10) + ")";            }})            .append("title").text(function(d) {                 if (d.date == null) {                     return "close: " + d.close;                 } else {                     return "date: " + datefmt(d.date)                          + "\nclose: " + d.close;                 }            }); 

it seems me, however, it'd more terse if first argument function (as second can be) returns such specific object of name value pairs. this:

        svg.selectall("rect")            .data(dataset)            .enter()            .append("rect")            .attr(function(d,i) {              return {                 "x": * (w / dataset.length),                 "y": h - (d.close * 4),                 "width": w / dataset.length - barpadding,                 "height": d.close * 4,                 "fill": "rgb(0, 0, " + (d.close * 10) + ")"            })            .append("title").text(function(d) {                 return ((d.date != null)                         ? "date: " + datefmt(d.date) + "\n"                         : "")                     + "close: " + d.close;            }); 

the latter doesn't work, maybe i'm missing better way form fewer anonymous function definitions need specified. if little more descriptive repeated d , i if wanted to. there such alternative mode available i've missed?

try way:

svg.selectall("rect")    .data(dataset)    .enter()    .append("rect")    .each(function (d,i) {        d3.select(this)          .attr({              "x": * (w / dataset.length),              "y": h - (d.close * 4),              "width": w / dataset.length - barpadding,              "height": d.close * 4,              "fill": "rgb(0, 0, " + (d.close * 10) + ")"          });    }); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -