score:6
You could use the closure to keep a reference to the parent data like this:
bar.each(function(dbar) { // dbar refers to the data bound to the bar
d3.select(this).selectAll("rect")
.on("click", function(drect) { // drect refers to the data bound to the rect
console.log(dbar.key); // dbar.key will be either 'likes' or 'dislikes'
});
});
update:
See below for various ways to access different levels in your DOM structure. Mix and match! See the live version of this and try to click on the .rect divs: http://bl.ocks.org/4235050
var data = [
{
key: 'likes',
values: [{ key: 'blue-frog', value: 1 }, { key: 'goodbye', value: 2 }]
},
{
key: 'dislikes',
values: [{ key: 'blue-frog', value: 3 }, { key: 'goodbye', value: 4 }]
}];
var chartdivs = d3.select("body").selectAll("div.chart")
.data([data]) // if you want to make multiple charts: .data([data1, data2, data3])
.enter().append("div")
.attr("class", "chart")
.style("width", "500px")
.style("height", "400px");
chartdivs.call(chart); // chartdivs is a d3.selection of one or more chart divs. The function chart is responsible for creating the contents in those divs
function chart(selection) { // selection is one or more chart divs
selection.each(function(d,i) { // for each chartdiv do the following
var chartdiv = d3.select(this);
var bar = chartdiv.selectAll(".bar")
.data(d)
.enter().append("div")
.attr("class", "bar")
.style("width", "100px")
.style("height", "100px")
.style("background-color", "red");
var rect = bar.selectAll(".rect")
.data(function(d) { return d.values; })
.enter().append("div")
.attr("class", "rect")
.text(function(d) { return d.key; })
.style("background-color", "steelblue");
bar.each(function(dbar) {
var bardiv = d3.select(this);
bardiv.selectAll(".rect")
.on("click", function(drect) {
d3.select(this).call(onclickfunc, bardiv);
});
});
function onclickfunc(rect, bar) { // has access to chart, bar, and rect
chartdiv.style("background-color", bar.datum().key === 'likes' ? "green" : "grey");
console.log(rect.datum().key); // will print either 'blue-frog' or 'goodbye'
}
});
}
score:0
rect.on("click", this.onChartClick.bind(this));
won't work because you're not passing a function, you're passing the return value of the function (by appending (this)
).
If you want to pass this
and data (d
), try this:
// assuming you have this somewhere earlier
var onChartClick = function () {}
// change your parent click to
rect.on("click", function(d) {
return onChartClick.call(this, d);
});
score:0
Another way could be f.e passing an object/json or in my case a class instance which creates that rectangle to the event to access it inside
var rect = svgContainer.append("rect");
rect.datum(this);
rect.on('click', function(d){
alert(d);
alert(d.id); // etc.
});
Source: stackoverflow.com
Related Query
- d3.js: Passing data from parent node to child node onclick event
- d3.js: Passing data from parent to child nodes
- How to expand child from JSON, load data after click parent - D3.Js - force directed layout
- How to generate parent child relation data from networkx to work with d3.js?
- Assigning __data__ from parent node to child with javascript & d3js
- Propagate Data from Parent to Child Selection (D3)
- How to get parent node from child in json using d3js as Force Layout?
- How to get data of parent node in d3.js
- Passing data from Django to D3
- d3js: make new parent data descend into child nodes
- nvd3.js : unable to bind onClick event with the data points in the svg
- Passing data from Django view to D3
- How can I connect two parent node into one child node and how to make a tooltip for each node in the tree pragmatically? in D3 js (SVG)
- Passing JSON data from server to client with Flask
- How can we create an onclick event for a d3 data map?
- Hide unrelated parent nodes but child node in D3.js
- How to display rectangular div with data on mouse over event of any node in d3 js tree?
- Select all the paths and parent nodes of a child node in tree layout
- D3 - Prevent parent <g> element from firing click event
- Passing data for nvd3 chart from Flask as an argument
- Need to shift link of child node to parent once parent collapsed in d3 graph
- Fetching data from json on node click
- Is there a way to make the nodes draggable/to another parent or child node to rearrange the nodes in d3.js
- Node generation from json data for force layout
- bring child node close to the parent node in d3
- d3.js: how to use data from the parent node? (to show the grouped bar name at the individual bar tooltip)
- Reference data of d3 element to parent node data
- Get all the data from a clicked node in C3JS
- Selecting d3 node from within child context (e.g. a timer)
- access data of child node when clicked on the link in d3 tree layout
More Query from same tag
- Javascript not working when Alert is removed
- How to use intro.js on d3.js elements
- Performance of nvd3 for a lot of data
- D3 creating a selector from this inside each
- D3 data() does not cause anything to change
- X3DOM ImageTexture in a bl.ocks.org and animate it using d3.js
- Can't make paths draw growing slowly with D3
- 'nvd3' is not a known element: 1. If 'nvd3' is an Angular component, then verify that it is part of this module
- add country id from json to dorling map
- hovering on one class not working for classes defined before it
- d3 zoom: pan axis on scroll, pan axis on drag, zoom axis on pinch
- How to customize the color scale in a D3 line chart?
- How to change labels on the control button in NVD3?
- Having trouble adding text lables to pie chart using d3.js
- D3: Passing extra arguments to attr functions inside a selection.join()
- D3, Typescript - Argument of type 'this' is not assignable to parameter of type 'BaseType'
- How to add laced color band in d3 js?
- How can I set the transform origin to the middle of this rectangle in D3?
- Adding legends to d3.js line charts
- Calculating an average from a javascript array
- d3 overlay object mousedown event handling
- Offset Line stroke-weight d3.js
- How to draw two line on the same xScale but different time frame
- d3 force directed graph, links not being drawn
- Stop bars from centering in chart
- Search functionality for D3 bundle layout
- How to change color of nodes in force directed graph
- d3.js histogram not working with array of total frequency for each bins
- Bar Chart outside bounds and mouseover event issue
- How to access the DOM element that correlates to a D3 SVG object?