NuCivic / react-nvd3

React component for NVD3 re-usable charting library
MIT License
136 stars 44 forks source link

How do I call functions with multiple parameters ? #67

Open ajaybc opened 7 years ago

ajaybc commented 7 years ago

I need to use the xAxis.ticks function which accepts 2 parameters eg. .ticks(d3.time.weeks, 2). If it was just 1 parameter I could do I what I did with tick format below. <...... xAxis={{tickFormat : function (d) { return d3.time.format('%Y-%m-%d %H:%M')(new Date(d)) }}} ../>

How do I call multiparameter functions using react-nvd3 ?

topicus commented 7 years ago

@ajaybc You can use a closure:

function formatter(offset) {
    return function(d) {
       return d3.time.format('%Y-%m-%d %H:%M')(new Date(d + offset));
    }
}
<...... xAxis={{tickFormat : formatter(10000)}} ../>
ajaybc commented 7 years ago

@topicus Thanks for the reply. But I was asking how to use .ticks(d3.time.weeks, 2) because that function expects 2 parameters. Sorry if the question seemed confusing

topicus commented 7 years ago

@ajaybc I think it doesn't work as d3. Tick function it's not inherited from d3 but used inside nvd3 as you can see in the link https://github.com/novus/nvd3/blob/master/src/models/axis.js#L52

You might play with tickValues and tickFormat to achieve the same result.