score:1
Accepted answer
you should add x and y co-ordinates to your nodes:
var tempx = window.innerwidth/2;
var tempy = window.innerheight/2;
var point = [tempx,tempy],
node = {imgsrc: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/48799_806120304_700633127_n.jpg", x: tempx, y: tempy};
and
var point = d3.mouse(this),
node = {imgsrc: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/211698_100002756979859_374256176_n.jpg", x:point[0], y:point[1]},
n = nodes.push(node);
and then need to add a transform to the force.on("tick"....
function:
vis.selectall(".node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ") scale(0.30)"; });
this scales your images down to 30%, but you can configure this.
for completeness, here is all of the code:
var nodescreated = 1;
var newdistance = 100;
var width = document.documentelement.clientwidth,
height = document.documentelement.clientheight,
fill = d3.scale.category20(),
nodes = [],
links = [];
var vis = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height);
vis.append("rect")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.linkdistance(newdistance)
.nodes(nodes)
.links(links)
.gravity(.01)
.size([width, height]);
force.on("tick", function() {
vis.selectall("line.link")
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
vis.selectall(".node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
vis.selectall(".node")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ") scale(0.30)"; });
});
var tempx = window.innerwidth/2;
var tempy = window.innerheight/2;
var point = [tempx,tempy],
node = {imgsrc: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/48799_806120304_700633127_n.jpg", x: tempx, y:tempy};
n = nodes.push(node);
vis.on("mousedown", function() {
var point = d3.mouse(this),
node = {imgsrc: "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash4/211698_100002756979859_374256176_n.jpg", x:point[0], y:point[1]},
n = nodes.push(node);
nodescreated++;
console.log(nodescreated);
var tempcounter = 0;
newdistance == 10;
force.linkdistance(newdistance);
nodes.foreach(function(target) {
if (/*math.sqrt(x * x + y * y) < 100 ||*/ tempcounter == 0) {
links.push({source: node, target: target});
tempcounter++;
}
});
restart();
});
function restart() {
force.start();
vis.selectall("line.link")
.data(links)
.enter().insert("line", ".node")
.attr("class", "link");
var realnode = vis.selectall(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
realnode.append("image")
.attr("xlink:href", function(d) { return d.imgsrc; })
.attr("x", -8)
.attr("y", -8)
.attr("width", 160)
.attr("height", 160);
}
Source: stackoverflow.com
Related Query
- D3.js Image at corner issue
- SVG D3 image resize issue
- express svg image element rendering issue in safari/firefox
- How to add an image to an svg container using D3.js
- Adding an image within a circle object in d3 javascript?
- Setting an id issue in d3
- Auto width and height for SVG image
- Save D3 chart as image
- d3 append an image with svg extension
- how to use svg file for image source in D3
- Concept Map Network - Node Issue
- D3: Grayscale image display driven by 2D array data
- Does Force-Directed Layout of d3-js support image as node?
- D3 fill shape with image using pattern
- IOS app webview SVG ClipPath Issue
- Add image inside a circle D3
- issue while using d3 brush in a line+bar chart (zoom in line+bar chart)
- Build issue angular 5 has no exported member 'DSVRowAny'
- Pie chart click issue - click not detected
- Best way to preload SVG image tags?
- How can I display a placeholder image in my SVG until the real image is loaded?
- how to make an image round in d3.js
- d3.js download graph as svg image
- d3 / svg layering issue when adding new nodes after a time
- How to fill an image inside my svg circles in d3.js
- svg to image on demand
- save image of d3heatmap in a file
- D3 topology rendering issue in Chrome
- D3 Scaling issue of Graph with appropriate scaling of Scales(Y-axis,X-axis)
- How to not preserve aspect ratio when resizing an image in d3.js
More Query from same tag
- Link lines across small multiple charts d3js
- Mouse hovering over circle data point using D3.js
- Error on horizontal Bar chart using d3.js
- Invert a color scale in d3.js
- nvd3 positive and negative value for same timestamp
- D3 data() vs. datum()
- Appending non-nested elements
- Side-by-side paths in d3
- Open all Children at once under a parent on click in D3 Js Vertical Tree
- How can I wrap the long label text in the bubble chart in d3?
- d3js timeline -- on scrub day generate a clean granular view for date and time
- In Internet Explorer, how do I programmatically change the (displayed) value of a 'select' element?
- Retrieve node name by it's ID. (D3JS)
- How to show the end scale value in the axis?
- Horizontal Panning D3.js
- Adding color to points in Multi-Line D3 Graph; re-render on dropdown change
- Stack and nest not working converting v3 to v4
- D3.js - Working with major and minor gridlines
- D3.js: making a circle which is itself made up of rectangles, in SVG
- Confused by d3.js's event dispatch
- Pie is not a function in Ember D3 Pie Chart
- Mouseover Hiding Stacked Elements From Mouse (HitTesting)
- On Chrome SVG chart arrowhead marker-mid is viewed 3 times instead 1
- How to add SVG object in HTML?
- Adding multiple class name to an element D3
- TypeError: e.values is undefined in nvd3 graph
- D3.js Overlaying Barchart display error
- Novice d3 stacked area graph
- How to code checkbox checked/unchecked behavior in d3.js
- d3 force not applying x scale properly