score:1
Accepted answer
Okay, as suggested by @altocumulus, as public penance I'm posting my own answer :-)
Of course, the documentation is very clear on how to call
a function. Just pass the other params :-)
But here, I'll go one step further and create a blueprint for a reusable function that does accept function parameter and also makes use of the function, as I didn't find an example online. Being new to Javascript, I learnt several new things here including apply
and arguments
.
// Here's my re-usable component
var myown = {};
myown.brush = function(){
// setup variable
var height = 0;
function my(group){
group.each(function(d,i){
// here is the code for creating brush
// note `this` is also available (in addition to d and i), for e.g.
var b = d3.select(this);
console.log("inside my each "+height);
});
}
my.move = function (group, position){
console.log("in move function");
group.each(function(d,i){
// here is the code for moving the brush
// note `this` is also available (in addition to d and i), for e.g.
var b = d3.select(this), that = this;
console.log("inside move each");
console.log(typeof(position)); // note it is function
// now call that function
// using "apply" and "arguments"
let pos = position.apply(that, arguments);
console.log(pos);
})
return my;
}
my.height = function(value){
if(!arguments.length) return value;
height = +value;
return my;
}
return my;
};
// Okay, now use all that nice code
var data = [
{id: 1, mv: [100,500]},
{id: 2, mv: [300,600]},
{id: 3, mv: [800,1000]}
];
var brush = myown.brush().height(90);
var svg = d3.select("svg");
var g = svg.selectAll("g").data(data).enter()
.append("g")
.attr("transform", (d,i)=>"translate(0,"+i*100+")");
g.call(brush).call(brush.move, (d,i)=>d.mv);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width=1500 height=1000></svg>
Source: stackoverflow.com
Related Query
- D3 Design re-usable component to accept parameter during "call"
- Is it expensive to call D3.js transition multiple times during scroll?
- Unable to call method in component from d3.call()
- Javascript library d3 call function
- Styles in component for D3.js do not show in angular 2
- Determine if Shift key is pressed during mousedown event
- How do I post parameter on d3.json?
- usable D3 replacement in IE8 (graphs with D3 and svg)
- D3js: How to design topographic maps?
- D3 : can't call svg.selectAll("text") twice on a same SVG object
- What object type is the instance element parameter pass into the link function of an angular directive?
- Could someone once and for all please explain Cannot call determinedVisibility() - never saw a connection for the pid
- Can I run a component method only in the client using Angular2 Universal?
- IE11 does not accept SVG height and width when using D3
- What is the `e` parameter passed to the d3.js force tick callback function?
- What's the idiomatic way to extend a native d3 component like d3.svg.axis()?
- Updating the data of a pack layout from JSON call and redrawing
- Add a concurrent transition during another transition if a condition is met
- How to create a reusable component in D3 with an API that manipulates each instance of the component?
- How to call stopPropagation in Typescript
- D3.js - Adding a loading notification during AJAX request
- How to throttle function call on mouse event with D3.js
- When running a D3 update/enter/exit, how to ignore already exiting elements during the new exit?
- Responding to tick events during d3 transitions
- How to use anonymous functions to call other functions in D3.js correctly
- Cannot get D3.js to work inside Svelte component (with Rollup)
- The mouse over event is not fired during the drag and drop action in d3.js
- correct way to call api using react
- D3 - Unable to get data parameter on click event
- Why does my angular component ignore my css file?
More Query from same tag
- d3.js tree: How do I remove a node's siblings when I click on it?
- D3js data format
- Draw line chart using dc js
- D3 Force-directed graph using texts instead of nodes
- Not able to make chart in NVD3. Formatting issue on x/y-axis
- How to move along a path but only between two specified path points
- Where to store remote data in Svelte for responsive data vizualisations with Svelte and D3 (best practices)
- D3 - Creating multiple circles elements for every entry in dataset
- Is traversing all elements in the (MVC) Model necessary every time it is updated?
- how to automatically resize d3.js graph to include axis
- Add Limit Lines to D3.js Line Graph
- Y-axis ticks' labels visible partially
- How do i integrate a custom d3map visualisation into Microstrategy?
- Alternative to eval() when dealing with d3 animation queues
- Drag behavior for nested g elements
- How do I reduce two arrays to a single value that is present at the same index?
- how to align text and labels in D3
- How to Size D3 Contours
- SVG use element to clone SVG
- d3's selection.text() is expensive
- Arrows in links hidden inside rectangles
- Broken code upgrading to d3.js v4.0
- d3 v5 force simulation: how to use stop & tick
- invoking an if…then function on a d3 node
- loop d3 transition a finite number of times
- How to place a label in an area outside of an SVG path?
- d3.js - Allow brushing and zooming on same chart
- d3: replace a number value with text on x axis
- Selecting from SVG with node as argument throws DOM Exception 12
- D3 taking an array of two objects and reading it into a D3 shape