score:1
Accepted answer
Use an array variable images
to hold selected image urls. Update the array on click of bars. You can create the image elements as shown below.
var images = [];
var setEvents = bars
.on('click', function(d) {
d3.select("#tool").style("opacity", 1.0);
// d3.select("#tool").html(d.imgsrc);
images.push(d.imgsrc);
d3.select('#tool')
.selectAll("img")
.data(images)
.enter()
.append('img')
.attr("width", 50)
.attr("height", 50)
.attr('src', function(d, i) {
return d
});
});
Working Code Snippet:
var data = [{
"val": 5,
"imgsrc": "http://www.iconsdb.com/icons/preview/gray/square-rounded-xxl.png"
}, {
"val": 40,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/005500/square-rounded-xxl.png"
}, {
"val": 50,
"imgsrc": "http://www.iconsdb.com/icons/preview/black/square-rounded-xxl.png"
}, {
"val": 60,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/550055/square-rounded-xxl.png"
}, {
"val": 80,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/990000/square-rounded-xxl.png"
}];
var width = 500;
var height = 500;
var widthScale = d3.scale.linear()
.domain([0, 100])
.range([0, width]);
var color = d3.scale.linear()
.domain([0, 100])
.range(["red", "blue"]);
var axis = d3.svg.axis()
.scale(widthScale)
.ticks(5);
var container = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(20, 0)");
var bars = container.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("width", function(d) {
return widthScale(d.val)
})
.attr("height", 30)
.attr("fill", function(d) {
return color(d.val)
})
.attr("y", function(d, i) {
return i * 50
});
var images = [];
var setEvents = bars
.on('click', function(d) {
d3.select("#tool").style("opacity", 1.0);
// d3.select("#tool").html(d.imgsrc);
images.push(d.imgsrc);
d3.select('#tool')
.selectAll("img")
.data(images)
.enter()
.append('img')
.attr("width", 50)
.attr("height", 50)
.attr('src', function(d, i) {
return d
});
});
container.append("g")
.attr("transform", "translate(0, 400)")
.call(axis);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="tool"></div>
Edit:
var data = [{
"val": 5,
"imgsrc": "http://www.iconsdb.com/icons/preview/gray/square-rounded-xxl.png"
}, {
"val": 40,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/005500/square-rounded-xxl.png"
}, {
"val": 50,
"imgsrc": "http://www.iconsdb.com/icons/preview/black/square-rounded-xxl.png"
}, {
"val": 60,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/550055/square-rounded-xxl.png"
}, {
"val": 80,
"imgsrc": "http://www.iconsdb.com/icons/preview/color/990000/square-rounded-xxl.png"
}];
var width = 500;
var height = 500;
var widthScale = d3.scale.linear()
.domain([0, 100])
.range([0, width]);
var color = d3.scale.linear()
.domain([0, 100])
.range(["red", "blue"]);
var axis = d3.svg.axis()
.scale(widthScale)
.ticks(5);
var container = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(20, 0)");
var bars = container.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("width", function(d) {
return widthScale(d.val)
})
.attr("height", 30)
.attr("fill", function(d) {
return color(d.val)
})
.attr("y", function(d, i) {
return i * 50
});
var setEvents = bars
.on('click', function(d) {
d3.select("#tool").style("opacity", 1.0);
d3.select('#tool').selectAll("img").remove();
d3.select('#tool')
.append('img')
.attr("width", 50)
.attr("height", 50)
.attr('src',d.imgsrc);
});
container.append("g")
.attr("transform", "translate(0, 400)")
.call(axis);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="tool"></div>
Source: stackoverflow.com
Related Query
- D3,js on mouse event CLICK appending only the first image from data set
- Display only values from the data set into X axis ticks
- d3js with angular directive - each click appending new graphics instead of re-draw or updating the data
- Get click event from SVG bounding box using d3js
- Do I have to re-join the whole data set even if only one property in one item changed?
- D3 transition from stacked bar to bar chart only works the first time
- In a d3 scatterplot using data from a csv file, how do i draw lines connecting related points when the mouse is over a point?
- on click event D3.js only works first time
- d3 mouse over event doesn't fire correctly if set from function(d)
- d3.js - Tooltips only shows the first properties values of the data object
- D3js horizontal bar chart: how to add numbers from data at the end of every bar?
- D3js take data from an array instead of a file
- In d3, how to get the interpolated line data from a SVG line?
- d3.data skipping the first row of data
- Can d3.js draw two scatterplots on the same graph using data from the same source?
- nvd3.js : unable to bind onClick event with the data points in the svg
- D3: How do I set "click" event and "dbclick" event at the same time?
- Reduce the size of a large data set by sampling/interpolation to improve chart performance
- Constructing Force Directed Graphs From Only Link Data
- dc.js add class to data points in multiple charts based on criteria from first chart
- why isn't the checkbox change event triggered when i do it programatically in d3js
- Updating the data of a pack layout from JSON call and redrawing
- NVD3 - How to refresh the data function to product new data on click
- d3.js undefined data value in callback function click event
- How to update elements of an HTML that the elements are created using data from a CSV file?
- Using Javascript D3 library, how can I determine mouse position in data set of an area element on mousemove event?
- dc.js - how to get the average of a column in data set
- The mouse over event is not fired during the drag and drop action in d3.js
- D3 - Unable to get data parameter on click event
- How to get the data from the c3.js
More Query from same tag
- D3.js: Rotate text on-spot
- How to Write Typescript d.ts FIle to Extend @types/d3
- Using d3.behavior.drag, How to keep a drag image visible outside of the svg element
- D3 difference between ordinal and linear scales
- Venn Diagram with d3js
- d3.js: "Cannot read property 'weight' of undefined" when manually defining both nodes and links for force layout
- How do I color labels for my pie chart in D3?
- Mapping data with topojson and D3
- How to create time scale in d3?
- Js object key: plus value syntax
- Select node and highlight its connections by feature ID in D3.js
- D3 stack area problem using typescript in angular
- D3 Multi Series Line chart not working with correct xAxis values
- Adding an image in forcelayout
- How do I build simple D3 charts that display massive amounts of data?
- How to position the legend in a d3 chart
- d3 tooltip/popup for multiple data series?
- How to translate all text in svg container that are already rotated
- Typeerror when pulling json data from Github
- svg how to add a border on a svg symbol?
- A simple scatterplot example in D3.js?
- D3 -- Arcs for chord diagram not being displayed
- How do I use a nested arrays to draw a grid?
- Interaction with updated/entered elements
- add country id from json to dorling map
- Issue about serving html and drawing in node.js
- D3.js appending wrong location
- calling functions from within functions in d3
- Extract specific row of data in d3
- D3.js - Adding a loading notification during AJAX request