0

chart
  .width(w)
  .height(h)

is it possible in some way to use indirection when chaining? I need to something like

var n = 80;
var ind = "width(' + n + ')";

chart
  .[ind]

... obviously not legal code but hoping it describes question.

Mic C
  • 501
  • 2
  • 7
  • 18
  • It's not clear to me what you want to do. You can simply define a function to set the width. – Lars Kotthoff Dec 03 '15 at 06:20
  • I am dynamically building charts from a json template. I am setting values from the data e.g. .centerBar(parms.isCb), but I want to be able to add ".yAxisLabel(parms.yAxLbl)" to the chart. – Mic C Dec 03 '15 at 09:43
  • What you are really asking is how do I call a JavaScript function stored in a string. See [here](http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string) and [here](http://stackoverflow.com/questions/912596/how-to-turn-a-string-into-a-javascript-function-call). I wouldn't do this unless it's *really*, *really* need to. – Mark Dec 03 '15 at 13:34

2 Answers2

1

You can use chart.options for that:

chart.options({width: w, height: h});

https://github.com/dc-js/dc.js/blob/develop/web/docs/api-latest.md#dc.baseMixin+options

Further, if you're reading the options from somewhere,

var opts = {};
while(/* read option opt, val from somewhere */)
    options[opt] = val;
chart.options(opts);
Gordon
  • 19,811
  • 4
  • 36
  • 74
  • looking at this now ... does this mean that we can build complete charts dynamically from just a list of parms e.g. chart='pie", width=300, dmnsn="custs" etc? I was a little surprised to find that dc does not inherit .attr("...", "...") or .call() from d3. – Mic C Dec 03 '15 at 17:47
  • Yeah, dc objects are not d3 objects, they are psuedo-classes which contain d3 objects. So I don't think `.attr` and `.call` are appropriate on the charts themselves (although you can `chart.select(...).attr(...).call(...)`). – Gordon Dec 03 '15 at 18:09
  • As for the dynamic creation, yes, I think so, but I haven't tried it myself. I bet it won't work for everything - how would you do axes for example. IIUC this is how [angular-dc](https://github.com/TomNeyland/angular-dc) works (and users of that library have contributed to `.options`). – Gordon Dec 03 '15 at 18:10
  • excellent stuff! Sounds best to create base charts with axes and then expand from there with template parms. Thanks again. – Mic C Dec 03 '15 at 20:49
0

It is more a JavaScript question than d3.js.

You can set the function name dynamically in JS.

var n = 80;
var fnc = "width";
chart[func](n);

Do I understand your problem well?

Peter Ambruzs
  • 7,763
  • 3
  • 30
  • 36
  • trying to add ".yAxisLabel(parms.yAxLbl)" as above, I tried all kinds of [] in the equation but only got errors. – Mic C Dec 03 '15 at 09:42