Integrate data in html for Parallel Coordinates visualization in D3 - d3.js

Following the example on this page: Parallel Coordinates
I'm trying to add the data inside the html page. I'm using the following code:
var cars = [
['AMC Ambassador Brougham',13,8,360,175,3821,11,73],
['AMC Ambassador DPL',15,8,390,190,3850,8.5,70]
];
var dimensions = ['name','economy (mpg)','cylinders','displacement (cc)','power (hp)','weight (lb)','0-60 mph (s)','year'];
But I'm getting a blank screen.
I'm guessing the format of the data is not right.
Could someone advice on how can I format the data inside the html file so that I can display the data correctly.
Thanks

d3.csv will return an array of objects where each row in the file is an object in the array with properties of the CSV header and values of the row values. So to translate that directly to JSON would look like:
var cars = [{
"name": "AMC Ambassador Brougham",
"economy (mpg)": "13",
"cylinders": "8",
"displacement (cc)": "360",
"power (hp)": "175",
"weight (lb)": "3821",
"0-60 mph (s)": "11",
"year": "73"
}, {
"name": "AMC Ambassador DPL",
"economy (mpg)": "15",
"cylinders": "8",
"displacement (cc)": "390",
"power (hp)": "190",
"weight (lb)": "3850",
"0-60 mph (s)": "8.5",
"year": "70"
}];
Running code:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
}
.background path {
fill: none;
stroke: #ddd;
shape-rendering: crispEdges;
}
.foreground path {
fill: none;
stroke: steelblue;
}
.brush .extent {
fill-opacity: .3;
stroke: #fff;
shape-rendering: crispEdges;
}
.axis line,
.axis path {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text {
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
cursor: move;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {
top: 30,
right: 10,
bottom: 10,
left: 10
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal().rangePoints([0, width], 1),
y = {},
dragging = {};
var line = d3.svg.line(),
axis = d3.svg.axis().orient("left"),
background,
foreground;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var cars = [{
"name": "AMC Ambassador Brougham",
"economy (mpg)": "13",
"cylinders": "8",
"displacement (cc)": "360",
"power (hp)": "175",
"weight (lb)": "3821",
"0-60 mph (s)": "11",
"year": "73"
}, {
"name": "AMC Ambassador DPL",
"economy (mpg)": "15",
"cylinders": "8",
"displacement (cc)": "390",
"power (hp)": "190",
"weight (lb)": "3850",
"0-60 mph (s)": "8.5",
"year": "70"
}];
// Extract the list of dimensions and create a scale for each.
x.domain(dimensions = d3.keys(cars[0]).filter(function(d) {
return d != "name" && (y[d] = d3.scale.linear()
.domain(d3.extent(cars, function(p) {
return +p[d];
}))
.range([height, 0]));
}));
// Add grey background lines for context.
background = svg.append("g")
.attr("class", "background")
.selectAll("path")
.data(cars)
.enter().append("path")
.attr("d", path);
// Add blue foreground lines for focus.
foreground = svg.append("g")
.attr("class", "foreground")
.selectAll("path")
.data(cars)
.enter().append("path")
.attr("d", path);
// Add a group element for each dimension.
var g = svg.selectAll(".dimension")
.data(dimensions)
.enter().append("g")
.attr("class", "dimension")
.attr("transform", function(d) {
return "translate(" + x(d) + ")";
})
.call(d3.behavior.drag()
.origin(function(d) {
return {
x: x(d)
};
})
.on("dragstart", function(d) {
dragging[d] = x(d);
background.attr("visibility", "hidden");
})
.on("drag", function(d) {
dragging[d] = Math.min(width, Math.max(0, d3.event.x));
foreground.attr("d", path);
dimensions.sort(function(a, b) {
return position(a) - position(b);
});
x.domain(dimensions);
g.attr("transform", function(d) {
return "translate(" + position(d) + ")";
})
})
.on("dragend", function(d) {
delete dragging[d];
transition(d3.select(this)).attr("transform", "translate(" + x(d) + ")");
transition(foreground).attr("d", path);
background
.attr("d", path)
.transition()
.delay(500)
.duration(0)
.attr("visibility", null);
}));
// Add an axis and title.
g.append("g")
.attr("class", "axis")
.each(function(d) {
d3.select(this).call(axis.scale(y[d]));
})
.append("text")
.style("text-anchor", "middle")
.attr("y", -9)
.text(function(d) {
return d;
});
// Add and store a brush for each axis.
g.append("g")
.attr("class", "brush")
.each(function(d) {
d3.select(this).call(y[d].brush = d3.svg.brush().y(y[d]).on("brushstart", brushstart).on("brush", brush));
})
.selectAll("rect")
.attr("x", -8)
.attr("width", 16);
function position(d) {
var v = dragging[d];
return v == null ? x(d) : v;
}
function transition(g) {
return g.transition().duration(500);
}
// Returns the path for a given data point.
function path(d) {
return line(dimensions.map(function(p) {
return [position(p), y[p](d[p])];
}));
}
function brushstart() {
d3.event.sourceEvent.stopPropagation();
}
// Handles a brush event, toggling the display of foreground lines.
function brush() {
var actives = dimensions.filter(function(p) {
return !y[p].brush.empty();
}),
extents = actives.map(function(p) {
return y[p].brush.extent();
});
foreground.style("display", function(d) {
return actives.every(function(p, i) {
return extents[i][0] <= d[p] && d[p] <= extents[i][1];
}) ? null : "none";
});
}
</script>

Related

d3 SVG size is much bigger than what I am setting it as when i publish it on Sharepoint

I have tried searching for an answer to this problem but have not been able to find anything that defines my problem.
I have created an Interactive Tree Diagram Menu for my Sharepoint site using D3.js and HTML (Code Below)
In Codepen it works fine and I can adjust the width and length of the SVG using the width and length variables:
// Set the dimensions and margins of the diagram
var margin = {top: 20, right: 90, bottom: 30, left: 90},
width = 960 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
But no matter what I seem to enter in as the height, once I open this up in a browser the SVG is at least 500px high and taking up way too much empty space.
when I publish this on my Sharepoint site via an iframe, the white space can be even bigger.
I have tried setting the iframe width and height dimensions to 500px, 300px, 100% 50% etc. but it doesn't solve the problem, all it does is mean that you have to scroll within the iframe.
I think it may have something to do with the transform/translate settings in the d3 code but I just can't figure it out - any help would be greatly appreciated!
var treeData =
{
"name": "Board", "url":"Site/Board/SitePages/Home.aspx",
"children": [
{
"name": "Executive",
"url":"Site/Board/Executive/SitePages/Home.aspx",
"children": [
{ "name": "CEO's Office",
"url":"hSite/Board/Executive/CEO/SitePages/Home.aspx"
},
{ "name": "Legal & Integrity",
"url":"Site/Board/Executive/LI/SitePages/Home.aspx",},
{ "name": "Communications & Stakeholder Relations",
"url":"Site/Board/Executive/CGR/SitePages/Home.aspx",},
{ "name": "People & Culture",
"url":"Site/Board/Executive/PC/SitePages/Home.aspx",
"children": [
{"name": "Health, Safety, Environment & Wellbeing",
"url":"Site/Board/Executive/PC/HSEW/SitePages/Home.aspx"
}]},
{"name": "Finance & Risk",
"url":"Site/Board/Executive/FR/SitePages/Home.aspx",
"children": [
{"name":"Financial Risk Register",
"url":"Site/Board/Executive/FR/FIN/SitePages/Home.aspx"}
]
},
{"name":"Volunteers & Strategy",
"url":"Site/Board/Executive/VS/SitePages/Home.aspx"},
{"name":"Infrastructure Services",
"url":"Site/Board/Executive/IS/SitePages/Home.aspx"},
{"name":"Fire & Emergency Management",
"url":"Site/Board/Executive/FEM/SitePages/Home.aspx",
"children":[
{"name":"Bushfire Portfolio",
"url":"Site/Board/Executive/FEM/BP/SitePages/Home.aspx"},
{"name":"Capability & Growth Portfolio",
"url":"Site/Board/Executive/FEM/CGP/SitePages/Home.aspx"},
{"name":"Regional Services Portfolio",
"url":"Site/Board/Executive/FEM/RSP/SitePages/Home.aspx"},
{"name":"Training Portfolio",
"url":"Site/Board/Executive/FEM/TP/SitePages/Home.aspx"},
{"name":"Urban Portfolio",
"url":"Site/Board/Executive/FEM/UP/SitePages/Home.aspx"},
{"name":"South West Region",
"url":"Site/Board/Executive/FEM/SW/SitePages/Home.aspx",
"children":[
{"name":"District 04",
"url":"Site/Board/Executive/FEM/SW/D04/SitePages/Home.aspx"},
{"name":"District 05",
"url":"Site/Board/Executive/FEM/SW/D05/SitePages/Home.aspx"},
{"name":"District 06",
"url":"Site/Board/Executive/FEM/SW/D06/SitePages/Home.aspx"},
{"name":"District 07",
"url":"Site/Board/Executive/FEM/SW/D07/SitePages/Home.aspx"},
]
},
{"name":"West Region",
"url":"Site/Board/Executive/FEM/W/SitePages/Home.aspx",
"children":[
{"name":"District 15",
"url":"Site/Board/Executive/FEM/W/D15/SitePages/Home.aspx"},
{"name":"District 16",
"url":"Site/Board/Executive/FEM/W/D16/SitePages/Home.aspx"},
{"name":"District 17",
"url":"Site/Board/Executive/FEM/W/D17/SitePages/Home.aspx"},
]
},
{"name":"North West Region",
"url":"Site/Board/Executive/FEM/NW/SitePages/Home.aspx",
"children":[
{"name":"District 02",
"url":"Site/Board/Executive/FEM/NW/D02/SitePages/Home.aspx"},
{"name":"District 14",
"url":"Site/Board/Executive/FEM/NW/D14/SitePages/Home.aspx"},
{"name":"District 18",
"url":"Site/Board/Executive/FEM/NW/D18/SitePages/Home.aspx"},
{"name":"District 20",
"url":"Site/Board/Executive/FEM/NW/D20/SitePages/Home.aspx"},
]
},
{"name":"North East Region",
"url":"Site/Board/Executive/FEM/NE/SitePages/Home.aspx",
"children":[
{"name":"District 12",
"url":"Site/Board/Executive/FEM/NE/D12/SitePages/Home.aspx"},
{"name":"District 13",
"url":"Site/Board/Executive/FEM/NE/D13/SitePages/Home.aspx"},
{"name":"District 22",
"url":"Site/Board/Executive/FEM/NE/D22/SitePages/Home.aspx"},
{"name":"District 23",
"url":"Site/Board/Executive/FEM/NE/D23/SitePages/Home.aspx"},
{"name":"District 24",
"url":"Site/Board/Executive/FEM/NE/D24/SitePages/Home.aspx"},
]
},
{"name":"South East Region",
"url":"Site/Board/Executive/FEM/SE/SitePages/Home.aspx",
"children":[
{"name":"District 08",
"url":"Site/Board/Executive/FEM/SE/D08/SitePages/Home.aspx"},
{"name":"District 09",
"url":"Site/Board/Executive/FEM/SE/D09/SitePages/Home.aspx"},
{"name":"District 10",
"url":"Site/Board/Executive/FEM/SE/D10/SitePages/Home.aspx"},
{"name":"District 11",
"url":"Site/Board/Executive/FEM/SE/D11/SitePages/Home.aspx"},
{"name":"District 27",
"url":"Site/Board/Executive/FEM/SE/D27/SitePages/Home.aspx"},
]
},
]
}
]
}
]
};
// Set the dimensions and margins of the diagram
var margin = {top: 20, right: 90, bottom: 30, left: 90},
width = 960 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
// append the svg object to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("#Menu").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate("
+ margin.left + "," + margin.top + ")");
var i = 0,
duration = 750,
root;
// declares a tree layout and assigns the size
var treemap = d3.tree().size([height, width]);
// Assigns parent, children, height, depth
root = d3.hierarchy(treeData, function(d) { return d.children; });
root.x0 = height / 2;
root.y0 = 0;
// Collapse after the second level
root.children.forEach(collapse);
update(root);
// Collapse the node and all it's children
function collapse(d) {
if(d.children) {
d._children = d.children
d._children.forEach(collapse)
d.children = null
}
}
function update(source) {
// Assigns the x and y position for the nodes
var treeData = treemap(root);
// Compute the new tree layout.
var nodes = treeData.descendants(),
links = treeData.descendants().slice(1);
// Normalize for fixed-depth.
nodes.forEach(function(d){ d.y = d.depth * 180});
// ****************** Nodes section ***************************
// Update the nodes...
var node = svg.selectAll('g.node')
.data(nodes, function(d) {return d.id || (d.id = ++i); });
// Enter any new modes at the parent's previous position.
var nodeEnter = node.enter().append('g')
.attr('class', 'node')
.attr("transform", function(d) {
return "translate(" + source.y0 + "," + source.x0 + ")";
});
// Add Circle for the nodes
nodeEnter.append('circle')
.attr('class', 'node')
.attr('r', 1e-6)
.style("fill", function(d) {
return d._children ? "#bdc3c7" : "#fff";
})
.on('click', click);
// Add labels for the nodes
nodeEnter.append('text')
.attr("dy", ".30em")
.attr("x", function(d) {
return d.children || d._children ? -13 : 13;
})
.attr("text-anchor", function(d) {
return d.children || d._children ? "end" : "start";
})
.text(function(d) { return d.data.name; })
.attr('class','normalText')
.on('click', hyperclick)
.on('mouseover', function(d,i) {
d3.select(this)
.attr('class', 'bigText')
.style('fill','#c0392b')
;})
.on('mouseout', function(d,i) {
d3.select(this)
.attr('class', 'normalText')
.style('fill','black')
;});
function hyperclick(d) {
window.open(d.data.url);
}
// UPDATE
var nodeUpdate = nodeEnter.merge(node);
// Transition to the proper position for the node
nodeUpdate.transition()
.duration(duration)
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
});
// Update the node attributes and style
nodeUpdate.select('circle.node')
.attr('r', 10)
.style("fill", function(d) {
return d._children ? "#bdc3c7" : "#fff";
})
.attr('cursor', 'pointer');
// Remove any exiting nodes
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) {
return "translate(" + source.y + "," + source.x + ")";
})
.remove();
// On exit reduce the node circles size to 0
nodeExit.select('circle')
.attr('r', 1e-6);
// On exit reduce the opacity of text labels
nodeExit.select('text')
.style('fill-opacity', 1e-6);
// ****************** links section ***************************
// Update the links...
var link = svg.selectAll('path.link')
.data(links, function(d) { return d.id; });
// Enter any new links at the parent's previous position.
var linkEnter = link.enter().insert('path', "g")
.attr("class", "link")
.attr('d', function(d){
var o = {x: source.x0, y: source.y0}
return diagonal(o, o)
});
// UPDATE
var linkUpdate = linkEnter.merge(link);
// Transition back to the parent element position
linkUpdate.transition()
.duration(duration)
.attr('d', function(d){ return diagonal(d, d.parent) });
// Remove any exiting links
var linkExit = link.exit().transition()
.duration(duration)
.attr('d', function(d) {
var o = {x: source.x, y: source.y}
return diagonal(o, o)
})
.remove();
// Store the old positions for transition.
nodes.forEach(function(d){
d.x0 = d.x;
d.y0 = d.y;
});
// Creates a curved (diagonal) path from parent to the child nodes
function diagonal(s, d) {
path = `M ${s.y} ${s.x}
C ${(s.y + d.y) / 2} ${s.x},
${(s.y + d.y) / 2} ${d.x},
${d.y} ${d.x}`
return path
}
// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<style>
.node circle {
fill: none;
stroke: #c0392b;
stroke-width: 3px;
}
.bigText {
font-family: Sans-Serif;
font-size: 20px;
color: #e74c3c;
text-decoration: underline;
cursor: pointer;
font-style: bold;
}
.normalText {
font-family: Sans-Serif;
font-size: 13px;
color: #000000;
text-decoration: none;
font-style: normal;
}
.link {
fill: none;
stroke: #95a5a6;
stroke-width: 2px;
stroke-length: 1px;
}
</style>
</head>
<body>
<div id="heading"></div>
<div id="Instructions"></div>
<div id="Menu"></div>
<script>
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/d3/4.9.1/d3.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
******** BONUS Question **********
Anyone who can tell me to set it up so that three levels of the collapsible tree diagram are shown by default instead of 2 will be awarded 10 internet points!
Have you tried setting the svg width and height attributes to 100%,
and then set the width/height of the containing div (not the iframe)?
This seems to provide consistent results for me. For example, I
changed your JS to: var svg =
d3.select("#Menu").append("svg").attr("width", "100%").attr("height",
"100%").append("g").attr("transform", "translate("+ margin.left + ","
+ margin.top + ")"); and added #Menu { width: 960px; height: 300px; } to your styles. – odin243 Dec 21 at 21:25
This worked a treat

D3 js - is it possible to draw straight lines between tree nodes?

I have created a horizontal tree diagram as shown in below image. I want straight lines between nodes. The curved lines between nodes are default in d3 js. I saw some answers on google for this but did not found any satisfactory result. So is it possible to draw straight lines between nodes in d3 js? If yes then how can I do that?
enter image description here
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<script src="https://d3js.org/d3.v3.min.js"></script>
<style>
.node circle {
fill: #ff9900;
stroke: #ff9900;
stroke-width: 1px;
}
.node text {
font: 16px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
</style>
</head>
<body>
<script language="javascript">
var treeData = [{
"name": "1",
"parent": "null",
"children": [{
"name": "2",
"parent": "Persons",
"children": [{
"name": "3",
"parent": "Country of residence"
}, {
"name": "4",
"parent": "Country of residence"
}, {
"name": "5",
"parent": "Country of residence"
}, {
"name": "6",
"parent": "Country of residence"
}]
}]
}];
// ************** Generate the tree diagram *****************
var margin = {
top: 20,
right: 120,
bottom: 20,
left: 120
},
width = 960 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0;
var tree = d3.layout.tree().size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) {
return [d.y, d.x];
});
var line = d3.svg.line()
.x(function(d) {
return d.lx;
})
.y(function(d) {
return d.ly;
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
update(root);
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) {
d.y = d.depth * 180;
});
// Declare the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) {
return d.id || (d.id = ++i);
});
// Enter the nodes.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
});
nodeEnter.append("circle")
.attr("r", 40)
.style("fill", "#ff9900");
// append icon inside circle
nodeEnter.append("image")
.attr("xlink:href", "http://localhost/d3/user2.jpg")
.attr("x", "-18px")
.attr("y", "-18px")
.attr("width", "35px")
.attr("height", "35px");
nodeEnter.append("text")
.attr("x", function(d) {
return d.children || d._children ? -40 : -50;
})
.attr("y", function(d) {
return d.children || d._children ? 55 : 55;
})
.attr("dy", ".35em")
.attr("text-anchor", function(d) {
return d.children || d._children ? "start" : "start";
})
.text(function(d) {
return d.name;
})
.style("fill-opacity", 1);
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function(d) {
return d.target.id;
});
// Enter the links.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", diagonal);
}
</script>
</body>
</body>
</html>
Define your line :
var line = d3.svg.line()
.x(function(d) {
return d.y; // because tree is horizontal
})
.y(function(d) {
return d.x; // because tree is horizontal
});
Change your links function to this because d3.svg.line() takes array of points as argument
Hope this helps
// Enter the links.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
return line([d.source, d.target]);
});
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<script src="https://d3js.org/d3.v3.min.js"></script>
<style>
.node circle {
fill: #ff9900;
stroke: #ff9900;
stroke-width: 1px;
}
.node text {
font: 16px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
</style>
</head>
<body>
<script language="javascript">
var treeData = [{
"name": "1",
"parent": "null",
"children": [{
"name": "2",
"parent": "Persons",
"children": [{
"name": "3",
"parent": "Country of residence"
}, {
"name": "4",
"parent": "Country of residence"
}, {
"name": "5",
"parent": "Country of residence"
}, {
"name": "6",
"parent": "Country of residence"
}]
}]
}];
// ************** Generate the tree diagram *****************
var margin = {
top: 20,
right: 120,
bottom: 20,
left: 120
},
width = 960 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0;
var tree = d3.layout.tree().size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) {
return [d.y, d.x];
});
var line = d3.svg.line()
.x(function(d) {
return d.y;
})
.y(function(d) {
return d.x;
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
update(root);
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) {
d.y = d.depth * 180;
});
// Declare the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) {
return d.id || (d.id = ++i);
});
// Enter the nodes.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
});
nodeEnter.append("circle")
.attr("r", 40)
.style("fill", "#ff9900");
// append icon inside circle
nodeEnter.append("image")
.attr("xlink:href", "http://localhost/d3/user2.jpg")
.attr("x", "-18px")
.attr("y", "-18px")
.attr("width", "35px")
.attr("height", "35px");
nodeEnter.append("text")
.attr("x", function(d) {
return d.children || d._children ? -40 : -50;
})
.attr("y", function(d) {
return d.children || d._children ? 55 : 55;
})
.attr("dy", ".35em")
.attr("text-anchor", function(d) {
return d.children || d._children ? "start" : "start";
})
.text(function(d) {
return d.name;
})
.style("fill-opacity", 1);
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function(d) {
return d.target.id;
});
// Enter the links.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) { return line([d.source, d.target])});
}
</script>
</body>
</body>
</html>

d3js - finding issue with converting `json` to date value

I am getting json data from backend. using the back-end data, I am trying to draw a line chart.
But the chart is very ugly, since the number values are not converting properlty. also i am not getting x axis values as date values here. how to convert the date number to properly here?
my try :
var datas = [
{"date":1404075600000,"ActualPercentage" : 63.4, "PlanPercentage" : 62.7},
{"date":1404680400000,"ActualPercentage" : 58.0, "PlanPercentage" : 59.9},
{"date":1405285200000,"ActualPercentage" : 53.3, "PlanPercentage" : 59.1},
{"date":1405890000000,"ActualPercentage" : 55.7, "PlanPercentage" : 58.8},
{"date":1406494800000,"ActualPercentage" : 64.2, "PlanPercentage" : 58.7},
{"date":1407099600000,"ActualPercentage" : 58.8, "PlanPercentage" : 57.0},
{"date":1407704400000,"ActualPercentage" : 57.9, "PlanPercentage" : 56.7},
{"date":1408309200000,"ActualPercentage" : 61.8, "PlanPercentage" : 56.8},
{"date":1408914000000,"ActualPercentage" : 69.3, "PlanPercentage" : 56.7},
{"date":1409518800000,"ActualPercentage" : 71.2, "PlanPercentage" : 60.1}
]
var margin = {top: 20, right: 80, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format( "%Y%m%d" ).parse;
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var color = d3.scale.ordinal()
.domain(["ActualPercentage", "PlanPercentage"])
.range(["#FF0000", "#009933"]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.temperature); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
color.domain(d3.keys(datas[0]).filter(function(key) { return key !== "date"; }));
datas.forEach(function(d) {
d.date = parseDate( String(d.date).slice(0, 8 ) );
});
var cities = color.domain().map(function(name) {
return {
name: name,
values: datas.map(function(d) {
return {date: d.date, temperature: +d[name]};
})
};
});
x.domain(d3.extent(datas, function(d) { return d.date; }));
y.domain([
d3.min(cities, function(c) { return d3.min(c.values, function(v) { return v.temperature; }); }),
d3.max(cities, function(c) { return d3.max(c.values, function(v) { return v.temperature; }); })
]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
// .append("text")
// .attr("transform", "rotate(-90)")
// .attr("y", 6)
// .attr("dy", ".71em")
// .style("text-anchor", "end")
// .text("Temperature (ºF)");
var city = svg.selectAll(".city")
.data(cities)
.enter().append("g")
.attr("class", "city");
var path = city.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(d.name); });
var totalLength = [path[0][0].getTotalLength()];
path
.attr("stroke-dasharray", totalLength + " " + totalLength)
.attr("stroke-dashoffset", totalLength)
.transition()
.duration(2000)
.ease("linear")
.attr("stroke-dashoffset", 0);
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
You can simply convert the timestamp string to date using Date function.
datas.forEach(function(d) {
d.date = new Date(d.date);
});
var datas = [{
"date": 1404075600000,
"ActualPercentage": 63.4,
"PlanPercentage": 62.7
}, {
"date": 1404680400000,
"ActualPercentage": 58.0,
"PlanPercentage": 59.9
}, {
"date": 1405285200000,
"ActualPercentage": 53.3,
"PlanPercentage": 59.1
}, {
"date": 1405890000000,
"ActualPercentage": 55.7,
"PlanPercentage": 58.8
}, {
"date": 1406494800000,
"ActualPercentage": 64.2,
"PlanPercentage": 58.7
}, {
"date": 1407099600000,
"ActualPercentage": 58.8,
"PlanPercentage": 57.0
}, {
"date": 1407704400000,
"ActualPercentage": 57.9,
"PlanPercentage": 56.7
}, {
"date": 1408309200000,
"ActualPercentage": 61.8,
"PlanPercentage": 56.8
}, {
"date": 1408914000000,
"ActualPercentage": 69.3,
"PlanPercentage": 56.7
}, {
"date": 1409518800000,
"ActualPercentage": 71.2,
"PlanPercentage": 60.1
}
]
var margin = {
top: 20,
right: 80,
bottom: 30,
left: 50
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var color = d3.scale.ordinal()
.domain(["ActualPercentage", "PlanPercentage"])
.range(["#FF0000", "#009933"]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) {
return x(d.date);
})
.y(function(d) {
return y(d.temperature);
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
color.domain(d3.keys(datas[0]).filter(function(key) {
return key !== "date";
}));
datas.forEach(function(d) {
d.date = new Date(d.date);
});
var cities = color.domain().map(function(name) {
return {
name: name,
values: datas.map(function(d) {
return {
date: d.date,
temperature: +d[name]
};
})
};
});
x.domain(d3.extent(datas, function(d) {
return d.date;
}));
y.domain([
d3.min(cities, function(c) {
return d3.min(c.values, function(v) {
return v.temperature;
});
}),
d3.max(cities, function(c) {
return d3.max(c.values, function(v) {
return v.temperature;
});
})
]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
// .append("text")
// .attr("transform", "rotate(-90)")
// .attr("y", 6)
// .attr("dy", ".71em")
// .style("text-anchor", "end")
// .text("Temperature (ºF)");
var city = svg.selectAll(".city")
.data(cities)
.enter().append("g")
.attr("class", "city");
var path = city.append("path")
.attr("class", "line")
.attr("d", function(d) {
return line(d.values);
})
.style("stroke", function(d) {
return color(d.name);
});
var totalLength = [path[0][0].getTotalLength()];
path
.attr("stroke-dasharray", totalLength + " " + totalLength)
.attr("stroke-dashoffset", totalLength)
.transition()
.duration(2000)
.ease("linear")
.attr("stroke-dashoffset", 0);
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

How to remove node by value on d3 tree?

I would like to remove nodes that have 0 as "cardinalita".
For now I have hidden nodes but I would like remove empty link.
Here the situation:
https://jsfiddle.net/d65k3zzy/
var treeData = [
{
"name": "First",
"parent": "null",
"cardinalita": "9",
"children": [
{
"name": "Second",
"parent": "First",
"cardinalita": "1",
"children": [
{
"name": "Third",
"parent": "Second",
"cardinalita": "63",
"children": [
{
"name": "fourth",
"parent": "Third",
"cardinalita": "39",
"children": [
{
"name": "last",
"parent": "fourth",
"cardinalita": "70",
"children": [
{
"name": "special",
"parent": "last",
"cardinalita": "11"
}
]
},
{
"name": "special",
"parent": "fourth",
"cardinalita": "0"
}
]
},
{
"name": "null",
"parent": "Third",
"cardinalita": "0",
"children": [
{
"name": "special",
"parent": "null",
"cardinalita": "10"
}
]
}
]
},
{
"name": "Third",
"parent": "Second",
"cardinalita": "528"
}
]
},
{
"name": "Second",
"parent": "First",
"cardinalita": "33",
"children": [
{
"name": "Third",
"parent": "Second",
"cardinalita": "63"
}
]
}
]
}
];
// ************** Generate the tree diagram *****************
var margin = {top: 0, right: 120, bottom: 40, left: 60},
width = 1000 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0,
duration = 750,
root;
var tree = d3.layout.tree()
// distanza fra i nodi figli
.separation(function separation(a, b) { return a.parent == b.parent ? 1.5 : 1; })
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("#diagramma").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
root.x0 = height / 2;
root.y0 = 0;
update(root);
d3.select(self.frameElement).style("height", "500px");
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 160; });
// Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
//.on("click", click);
// contenitore testo
nodeEnter.append("svg:rect")
.attr("width", function(d) { if(d.name != 'null' || (d.cardinalita < 1)){return "70";} })
.attr("height", function (d) {
return 21;
})
.attr("r", 1e-6)
.attr("y", -10)
.attr("x", -50)
.attr("rx", 0)
.attr("ry", 0)
.attr("stroke", function(d) { if(d.name != 'null' && d.cardinalita >= 1){return "#23527c";} })
.attr("stroke-width", "2")
.style("fill", function (d) {
return d._children ? "#ccc" : "#fff";
})
.style("fill-opacity", function(d) { if(d.name == 'null' || (d.cardinalita < 1)){return "0";} })
//contenitore cardinalità
nodeEnter.append("svg:rect")
.attr("width", 25)
.attr("height", function (d) {
return 21;
})
.attr("r", 1e-6)
.attr("y", 11)
.attr("x", -5)
.attr("rx", 0)
.attr("ry", 0)
.attr("stroke", function(d) { if(d.name != 'null' && d.cardinalita >= 1){return "#23527c";} })
.attr("stroke-width", "2")
.style("fill", "#fff")
.style("fill-opacity", function(d) { if(d.name == 'null' || (d.cardinalita < 1)){return "0";} })
// nome del nodo
nodeEnter.append("text")
.attr("x", function (d) {
return d._children ? -8 : 8;
})
.attr("y", -4)
.attr("x", -40)
.attr("dy", "0.7em")
.style("font-size", "14")
.style("font-weight", "bold")
.text(function (d) {
if(d.name != 'null' && d.cardinalita != 0)
return d.name;
});
// cardinalità
nodeEnter.append("text")
.attr("x", function (d) {
return d._children ? -8 : 8;
})
.attr("y", 18)
.attr("x", -2)
.attr("dy", "0.7em")
.style("font-size", "14")
.text(function (d) {
if(d.name != 'null' && d.cardinalita >= 1)
return d.cardinalita;
});
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
nodeUpdate.select("rect")
.attr("r", 10)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeUpdate.select("text")
.style("fill-opacity", 1);
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove();
nodeExit.select("rect")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
});
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
header { padding: 20px; }
section { overflow: hidden; width: 1000px; margin: 0 auto;}
.node text {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
#diagramma { display: inline; float: left; }
.map { display: inline; float: right; }
.p {
font-family: Arial, sans-serif;
text-align: center;
}
.map svg {
height: auto;
width: auto;
min-width: 300px;
max-width: 400px;
margin: 0 auto;
display: block;
}
.map g {
fill: #ccc;
stroke: #fff;
stroke-width: 2;
}
.map g:hover, g.active, .st0:hover, .active .st0 {
fill: #23527c !important;
cursor: help;
cursor: pointer;
}
.info_panel {
background-color: rgba(255,255,255, .8);
padding: 5px;
font-size: 12px;
font-family: Helvetica, Arial, sans-serif;
position: absolute;
border: 1px solid #333;
color: #333;
white-space: nowrap;
}
.info_panel::first-line {
font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<body>
<section>
<div id="diagramma">
</div>
</section>
</body>
I think you really want to remove links that have cardinalita === "0" and have no children:
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", function(d){
if (!d.target.children && d.target.cardinalita === "0")
return "M0,0"; //<-- draw nothing
else
return diagonal(d);
});
Full code:
var treeData = [
{
"name": "First",
"parent": "null",
"cardinalita": "9",
"children": [
{
"name": "Second",
"parent": "First",
"cardinalita": "1",
"children": [
{
"name": "Third",
"parent": "Second",
"cardinalita": "63",
"children": [
{
"name": "fourth",
"parent": "Third",
"cardinalita": "39",
"children": [
{
"name": "last",
"parent": "fourth",
"cardinalita": "70",
"children": [
{
"name": "special",
"parent": "last",
"cardinalita": "11"
}
]
},
{
"name": "special",
"parent": "fourth",
"cardinalita": "0"
}
]
},
{
"name": "null",
"parent": "Third",
"cardinalita": "0",
"children": [
{
"name": "special",
"parent": "null",
"cardinalita": "10"
}
]
}
]
},
{
"name": "Third",
"parent": "Second",
"cardinalita": "528"
}
]
},
{
"name": "Second",
"parent": "First",
"cardinalita": "33",
"children": [
{
"name": "Third",
"parent": "Second",
"cardinalita": "63"
}
]
}
]
}
];
// ************** Generate the tree diagram *****************
var margin = {top: 0, right: 120, bottom: 40, left: 60},
width = 1000 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0,
duration = 750,
root;
var tree = d3.layout.tree()
// distanza fra i nodi figli
.separation(function separation(a, b) { return a.parent == b.parent ? 1.5 : 1; })
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("#diagramma").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
root.x0 = height / 2;
root.y0 = 0;
update(root);
d3.select(self.frameElement).style("height", "500px");
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 160; });
// Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
//.on("click", click);
// contenitore testo
nodeEnter.append("svg:rect")
.attr("width", function(d) { if(d.name != 'null' || (d.cardinalita < 1)){return "70";} })
.attr("height", function (d) {
return 21;
})
.attr("r", 1e-6)
.attr("y", -10)
.attr("x", -50)
.attr("rx", 0)
.attr("ry", 0)
.attr("stroke", function(d) { if(d.name != 'null' && d.cardinalita >= 1){return "#23527c";} })
.attr("stroke-width", "2")
.style("fill", function (d) {
return d._children ? "#ccc" : "#fff";
})
.style("fill-opacity", function(d) { if(d.name == 'null' || (d.cardinalita < 1)){return "0";} })
//contenitore cardinalità
nodeEnter.append("svg:rect")
.attr("width", 25)
.attr("height", function (d) {
return 21;
})
.attr("r", 1e-6)
.attr("y", 11)
.attr("x", -5)
.attr("rx", 0)
.attr("ry", 0)
.attr("stroke", function(d) { if(d.name != 'null' && d.cardinalita >= 1){return "#23527c";} })
.attr("stroke-width", "2")
.style("fill", "#fff")
.style("fill-opacity", function(d) { if(d.name == 'null' || (d.cardinalita < 1)){return "0";} })
// nome del nodo
nodeEnter.append("text")
.attr("x", function (d) {
return d._children ? -8 : 8;
})
.attr("y", -4)
.attr("x", -40)
.attr("dy", "0.7em")
.style("font-size", "14")
.style("font-weight", "bold")
.text(function (d) {
if(d.name != 'null' && d.cardinalita != 0)
return d.name;
});
// cardinalità
nodeEnter.append("text")
.attr("x", function (d) {
return d._children ? -8 : 8;
})
.attr("y", 18)
.attr("x", -2)
.attr("dy", "0.7em")
.style("font-size", "14")
.text(function (d) {
if(d.name != 'null' && d.cardinalita >= 1)
return d.cardinalita;
});
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
nodeUpdate.select("rect")
.attr("r", 10)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeUpdate.select("text")
.style("fill-opacity", 1);
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove();
nodeExit.select("rect")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link");
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", function(d){
if (!d.target.children && d.target.cardinalita === "0")
return "M0,0";
else
return diagonal(d);
});
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
header { padding: 20px; }
section { overflow: hidden; width: 1000px; margin: 0 auto;}
.node text {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
#diagramma { display: inline; float: left; }
.map { display: inline; float: right; }
.p {
font-family: Arial, sans-serif;
text-align: center;
}
.map svg {
height: auto;
width: auto;
min-width: 300px;
max-width: 400px;
margin: 0 auto;
display: block;
}
.map g {
fill: #ccc;
stroke: #fff;
stroke-width: 2;
}
.map g:hover, g.active, .st0:hover, .active .st0 {
fill: #23527c !important;
cursor: help;
cursor: pointer;
}
.info_panel {
background-color: rgba(255,255,255, .8);
padding: 5px;
font-size: 12px;
font-family: Helvetica, Arial, sans-serif;
position: absolute;
border: 1px solid #333;
color: #333;
white-space: nowrap;
}
.info_panel::first-line {
font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<!DOCTYPE html>
<body>
<section>
<div id="diagramma">
</div>
</section>
</body>

D3 focus/context chart: synchronization of brushing and panning

I have a d3 focus/context chart where I would like to be able to pan on the focus portion after brushing the context, and I would like the brushed section of the context area to move in sync with the panning of the focus area. However, when I click on the focus portion after I select a region in the context chart, the focus scale changes and the points no longer show up at the correct coordinates. The following code is available on jsfiddle as well:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<style type="text/css">
circle {
-webkit-transition: fill-opacity 250ms linear;
}
.selecting circle {
fill-opacity: .5;
}
.selecting circle.selected {
stroke: #f00;
}
.brush .extent {
stroke: #B8C6D0;
fill-opacity: .125;
shape-rendering: crispEdges;
}
#context .axis path.domain {
stroke: lightsteelblue;
stroke-width: 5px;
}
#context .tick {
stroke:black;
stroke-width: 1px;
}
#context .x .tick {
stroke:black;
stroke-width: 2px;
}
.axis path, .axis line {
fill: none;
stroke: #ddd;
stroke-width: 1px;
shape-rendering: crispEdges;
}
.axis path {
stroke: #999;
stroke-width: 2px;
}
</style>
<script type="text/javascript">//<![CDATA[
var data = [{
Id: "1",
Year: 1950,
Relevance: 55,
Category: "Cat1",
SpecFreq: 5,
GenFreq: 10
}, {
Id: "2",
Year: 1975,
Relevance: 25,
Category: "Cat1",
SpecFreq: 2,
GenFreq: 31
}, {
Id: "3",
Year: 1990,
Relevance: 75,
Category: "Cat1",
SpecFreq: 8,
GenFreq: 23
}, {
Id: "4",
Year: 1970,
Relevance: 45,
Category: "Cat1",
SpecFreq: 17,
GenFreq: 60
}, {
Id: "5",
Year: 1985,
Relevance: 90,
Category: "Cat1",
SpecFreq: 17,
GenFreq: 25
}];
$(function () {
//dimensions
var margin = {
top: 5.5,
right: 19.5,
bottom: 39.5,
left: 39.5
};
//data domain extents
var extentX = d3.extent(data, function (d) {
return d.Year;
});
var extentY = d3.extent(data, function (d) {
return d.Relevance;
});
var focusAxisOptions = {
x: {
ticks: {
format: d3.format("d"),
size: -1* (500 - margin.top - margin.bottom),
ticks: 10
},
showLabel: true
},
y: {
ticks: {
format: d3.format(""),
size: -1 * (800 - margin.left - margin.right),
ticks: 10
},
showLabel: true
}
};
var contextAxisOptions = {
x: {
ticks: {
format: d3.format("d"),
size: -1 * (100 - margin.top - margin.bottom),
ticks: 10
},
showLabel: true
},
y: {
ticks: {
format: "",
size: 0,
ticks: 0
},
showLabel: false
}
};
var focus = DrawChart(data, margin, 800 - margin.left - margin.right, 500 - margin.top - margin.bottom, extentX, extentY, focusAxisOptions);
var context = DrawChart(data, margin, 800 - margin.left - margin.right, 100 - margin.top - margin.bottom, extentX, extentY, contextAxisOptions);
MakeContextBrushable(context, focus);
MakeFocusZoomable(focus);
});
function DrawChart(data, margin, width, height, extentX, extentY, axisOptions) {
//pad extents to provide some extra "blank" areas around edge of graph
var paddedExtentX = [extentX[0] - 5, extentX[1] +5];
var paddedExtentY = [extentY[0] - 5, extentY[1] +5];
//scales
var x = d3.scale.linear().domain(paddedExtentX).range([0, width]);
var y = d3.scale.linear().domain(paddedExtentY).range([height, 0]);
var radiusMax = .025 * width;
var radius = d3.scale.sqrt().domain([0, 100]).range([0, radiusMax]);
var color = d3.scale.ordinal().domain(["Cat1", "Cat2", "Cat3"]).range(["#b7b8a0", "#898a72", "#878772"]);
//axes
var xAxis = d3.svg.axis().scale(x).orient("bottom").tickFormat(axisOptions.x.ticks.format).tickSize(axisOptions.x.ticks.size).ticks(axisOptions.x.ticks.ticks);
var yAxis = d3.svg.axis().scale(y).orient("left").tickFormat(axisOptions.y.ticks.format).tickSize(axisOptions.y.ticks.size).ticks(axisOptions.y.ticks.ticks);
//create and size svg element
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("float", "left")
.style("clear", "left");
var g = svg
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); ;
// Add the x-axis.
g.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("font-size", "10px")
.attr("dy", "1.5em");
// Add the y-axis.
g.append("g")
.attr("class", "y axis")
.call(yAxis)
.selectAll("text")
.style("font-size", "10px")
.attr("dx", "-1em");
// Add the x-axis label.
if (axisOptions.x.showLabel) {
g.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", width / 2)
.attr("y", height + 35)
.text(" Year");
}
// Add the y-axis label.
if (axisOptions.y.showLabel) {
g.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("x", -1 * height / 2)
.attr("y", -40)
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.text("Relevance");
}
//plot genFreq
var gGenerally = g.append("g").selectAll("circle")
.data(data)
.enter().append("circle")
.style("fill", function (d) {
return color(d.Category);
})
.attr("cx", function (d) {
return x(d.Year);
})
.attr("cy", function (d) {
return y(d.Relevance);
})
.attr("r", function (d) {
return radius(d.GenFreq);
})
.append("title")
.text(function (d) {
return "(" + d.Year + ", " + d.Relevance + ", " + d.GenFreq + ", " + d.SpecFreq + ")";
});
//plot specFreq
var gWithin = g.append("g").selectAll("circle")
.data(data)
.enter().append("circle")
.style("fill", function (d) {
return "#d6d487";
})
.attr("cx", function (d) {
return x(d.Year);
})
.attr("cy", function (d) {
return y(d.Relevance);
})
.attr("r", function (d) {
return radius(d.SpecFreq);
})
.append("title")
.text(function (d) {
return "(" + d.Year + ", " + d.Relevance + ", " + d.GenFreq + ", " + d.SpecFreq + ")";
});
var chart = {
svg: svg,
g: g,
x: x,
y: y,
xAxis: xAxis,
yAxis: yAxis
}
return chart;
}
function MakeContextBrushable(context, focus) {
var brush = d3.svg.brush().x(context.x).y(context.y)
.on("brushstart", brushstart)
.on("brush", brushmove)
.on("brushend", brushend);
context.g.append("g")
.attr("class", "brush")
.call(brush);
function brushstart() {
context.svg.classed("selecting", true);
}
function brushmove() {
var e = d3.event.target.extent();
var circle = context.svg.selectAll("circle");
circle.classed("selected", function (d) {
return e[0][0] <= d["DecisionYear"] && d["DecisionYear"] <= e[1][0]
&& e[0][1] <= d["Relevance"] && d["Relevance"] <= e[1][1];
});
}
function brushend() {
context.svg.classed("selecting", !d3.event.target.empty());
if (!d3.event.target.empty()) {
var e = d3.event.target.extent();
focus.x.domain([e[0][0], e[1][0]]);
focus.y.domain([e[0][1], e[1][1]]);
focus.g.select(".x.axis").call(focus.xAxis)
.selectAll("text")
.style("font-size", "10px")
.attr("dx", "-1em");
focus.g.select(".y.axis").call(focus.yAxis)
.selectAll("text")
.style("font-size", "10px")
.attr("dx", "-1em");
var circle = focus.svg.selectAll("circle").attr("cx", function (d) { return focus.x(d.Year); })
.attr("cy", function (d) { return focus.y(d.Relevance); })
console.log("BrushEnd Domain: [" + focus.x.domain()[0] + ", " + focus.x.domain()[1] + "]");
}
else {
focus.x.domain(context.x.domain());
focus.y.domain(context.y.domain());
focus.g.select(".x.axis").call(focus.xAxis)
.selectAll("text")
.style("font-size", "10px")
.attr("dx", "-1em");
focus.g.select(".y.axis").call(focus.yAxis)
.selectAll("text")
.style("font-size", "10px")
.attr("dx", "-1em");
var circle = focus.g.selectAll("circle").attr("cx", function (d) { return focus.x(d.Year); })
.attr("cy", function (d) { return focus.y(d.Relevance); })
}
}
}
function MakeFocusZoomable(focus) {
focus.svg.call(d3.behavior.zoom().x(focus.x).y(focus.y).on("zoom", Zoom));
function Zoom() {
focus.svg.select(".x.axis").call(focus.xAxis).selectAll("text")
.style("font-size", "10px")
.attr("dy", "1.5em");
focus.svg.select(".y.axis").call(focus.yAxis).selectAll("text")
.style("font-size", "10px")
.attr("dx", "-1em");
focus.svg.selectAll("circle").attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")");
}
}
</script>
</head>
<body>
<div id="chart">
</div>
</body>
</html>
To reset the brushing after panning, you would need to reset and call brush.extent again:
//Find extent of zoomed area, for example the edges of graphed region
var brushExtent = [x.invert(0), x.invert(width)];
context.select(".brush").call(brush.extent(brushExtent));
Here's an example of focus/context brushing panning synchronization on a line graph:
http://jsfiddle.net/MtXvx/8/
Hope that helps!

Resources