Displaying Neo4j with d3.js - d3.js

I following this tutorial (http://blog.ashokalella.com/neo4j/d3/2015/04/27/neo4j-and-d3/).
How can I add name on the graph?
Like this (http://bl.ocks.org/mbostock/1153292).
Please help me.
Thank you.

Here is a detailed how-to on data visualization:
http://neo4j.com/developer/guide-data-visualization/
In d3 you would then add another function to return the label/caption of the node accessing the node itself like shown in the blocks:
var text = svg.append("g").selectAll("text")
.data(force.nodes())
.enter().append("text")
.attr("x", 8)
.attr("y", ".31em")
.text(function(d) { return d.name; });

Related

internal link for using lightbox (fancyBox) in a d3 script

I'm quite new to web programming and its more or less like searching for a funcion and trying until stuff works. So you have an idea of my skill ;)
I'm trying to build a d3 visualization (after Mike Bostocks dendrogramm: http://bl.ocks.org/mbostock/4063570).
Within the tree I want to use internal links to a hidden div (with id='test') that uses a lightbox script (fancyBox: http://fancyapps.com/fancybox/).
So far, my d3 script produces following code for the link (copied from firebug):
<a class="fancybox" x="10" href="#test"><text class="nodeText" dy=".35em" text-anchor="start" style="fill-opacity: 1;" x="10"test</text>
When I'm using this code anywhere else (even in the d3-div) it works fine opening the lightbox div. But when I click on it within the d3 canvas it doesn't work.
The code for setting up the nodes and their properties (to create the code above) is as following:
nodeEnter.append("svg:a")
.attr("x", function(d) {
return d.children || d._children ? -10 : 10;
})
.attr("xlink:href", function(d){return d.url;}) // <-- reading the new "url" property
.attr('class', 'fancybox')
.append("text")
.attr("dy", ".35em")
.attr('class', 'nodeText')
.attr("text-anchor", function(d) {
return d.children || d._children ? "end" : "start";
})
.text(function(d) {
return d.name;
})
.style("fill-opacity", 0);
Furthermore, when I set an external url this works also fine. But when url is '#test' it doesn't.
I cannot find any articles for such an issue (maybe I have to spcial desires? O.o). Is there another way to setup internal links or did I missed any sign? I'm wondering why this works outside d3 but not within.
Thanks for help!
I solved this problem, building my own lightbox after Jim Nielsons tutorial (https://webdesign.tutsplus.com/articles/super-simple-lightbox-with-css-and-jquery--webdesign-3528). Watch out for the comments on recent versions of jquery.
The solution after having a working lightbox as desired is, to include the code into the d3 script somewhere (I put it to the end). Et voila!
It's not good to have blackboxes in your own code :)
Done.
nodeEnter.append("image")
.attr("xlink:href", function(d) { return d.icon; })
.on("click", function(d){
var link=document.createElement("a");
link.id="lightLink";
link.href= d.url;
link.rel="lightbox";
this.appendChild(link);
document.getElementById("lightLink").click();
})
.attr("x", "-12px")
.attr("y", "-12px")
.attr("width", "24px")
.attr("height", "24px");

Add a list or vertical tspans in a tree using d3

I'm trying to learn d3 library and include a list to a tree. Something like:
JSFiddle: link
I don't think I understand the concept of adding data to a node well. Did go through this link: How selection work in d3?
You can do this with a subselection:
// keep reference to appended text elements
var someText = nodeEnter.append("text")
.attr("class", "properties")
.attr("text-anchor", "middle")
.attr("y", 52)
.style("fill-opacity", 1);
// sub-select them with properties array
someText.selectAll('tspan')
.data(function(d) {
return d.properties || []; // catch situation where child has no properties
})
.enter()
.append('tspan')
.attr("x", 0)
.attr('dy', "0.9em")
.text(function(d) {
return d;
});
Updated fiddle.

Adding a label to a path with D3

I am building a graph with D3 and represent the links between nodes as paths. I want to add a label for each path by using the following code:
path_labels = path_labels.data(links);
path_labels.enter().append("text")
.attr("class", "linklabel")
.style("font-size", "12px")
.attr("text-anchor", "start")
.append("textPath")
.attr("xlink:href", function (d) {
return "#linkId_0";
})
.text(function (d) {
return "my text";
});
When I look at the result, text is appended, but not the textPath inside the text element.
Can someone help?
The full code can be found at http://jsfiddle.net/3u0oage7/
I found the answer to the problem. The code I posted works as supposed. The problem was that in my code I had this:
svg.selectAll('text').
text(function (d) {
return d.label;
});
This code was changing all the text elements. I changed the selector so it selects only the text elements corresponding to a circle.

Creating scatter points on a multi line chart

So I'm using code based off of this..
http://bl.ocks.org/mbostock/3884955
Essentially, what I'm trying to do is at every data point I want to add a circle. Any help would be much appreciated seeing that I have no idea where to start.
This is my code so far: It worked when I was using single lines.
var circlegroup = focus.append("g")
circlegroup.attr("clip-path", "url(#clip)")
circlegroup.selectAll('.dot')
.data(data)
.enter().append("circle")
.attr('class', 'dot')
.attr("cx",function(d){ return x(d.date);})
.attr("cy", function(d){ return y(d.price);})
.attr("r", function(d){ return 4;})
.on('mouseover', function(d){ d3.select(this).attr('r', 8)})
.on('mouseout', function(d){ d3.select(this).attr('r', 4)});
You need nested selections for this. Assuming that data is a two-dimensional array, you would do something like this.
var groups = svg.selectAll("g").data(data).enter().append("g");
groups.data(function(d) { return d; })
.enter()
.append("circle")
// set attributes

Display text in D3

I am just starting to learn D3 and currently working through understanding this example.
I am trying to get text to display next to each node in the graph. In the JSON data, there is a key named name, which contains the name of a character from Les Misérables. I am trying to display this text next to each node using this code:
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", "black")
.text(function(d) { return d.name;}).style("font-family", "Arial").style("font-size", 12)
.call(force.drag);
I've basically just altered the .text to try and display text. Can anyone explain how I can get text to display? Thanks in advance.
The SVG <circle> element will not display any text that you place inside it using the .text function. A way to display the text would be to make the nodes of the graph SVG <g> elements. These can then contain a <circle> and a <text> child elements.
This could look like this :
\\create graph nodes using groups
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g").call(force.drag);
node.append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function (d) { return color(d.group); });
node.append("text")
.text(function (d) { return d.name; });
You would also need to change the force so that it transforms the group <g> on a tick.
force.on("tick", function () {
...
node.attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; });
});
You can see it in action for a small graph at http://jsfiddle.net/vrWDc/19/. This method may not be very efficient when applied to a large graph due to the amount of text that needs to be rendered.

Resources