An example of using nested svg elements to have different coordinate systems in the same visualization. In this case, we provide a histogram of random data that fills the width of the graph while keeping the title in a static size for easy reading.
Last active
December 27, 2015 21:39
-
-
Save jonypawks/7393041 to your computer and use it in GitHub Desktop.
D3 and Nested SVG Elements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
background-color: black; | |
} | |
svg { | |
background-color: #222; | |
} | |
.bars .one { | |
fill: #aec7e8; | |
opacity: 0.2; | |
transition: opacity 0.5s; | |
} | |
svg:hover .bars .one { opacity: 1.0; } | |
.bars .one:hover { | |
fill: #78a4d8; | |
} | |
.axis { | |
opacity: 0.2; | |
transition: opacity 0.5s; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #888; | |
shape-rendering: crispEdges; | |
} | |
.axis text { | |
fill: #888; | |
} | |
svg:hover .axis { opacity: 1.0; } | |
.hover-title { | |
pointer-events: none; | |
opacity: 1.0; | |
transition: opacity 0.5s; | |
} | |
svg:hover .hover-title { opacity: 0;} | |
</style> | |
</head> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script type="text/coffeescript" src="script.coffee"></script> | |
<script src="http://coffeescript.org/extras/coffee-script.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
margin = top: 0, right: 0, bottom: 0, left: 0 | |
width = 960 - margin.right - margin.left | |
height = 150 - margin.top - margin.bottom | |
viewHeight = 100 | |
x = d3.scale.linear() | |
y = d3.scale.linear().range [height, 0] | |
svg = d3.select('body').append('svg') | |
.classed('container', true) | |
.attr('width', '100%') | |
.attr('height', height + margin.top + margin.bottom) | |
randInt = (upper) -> Math.floor(Math.random() * upper) | |
data = for int in [0..30] | |
one: randInt 20 | |
two: randInt 15 | |
three: randInt 10 | |
four: randInt 5 | |
barWidth = Math.floor(width / data.length) - 1 | |
x.range([0, 960]).domain [0, data.length] | |
y.range([0, 100]).domain [0, 20] | |
bar = svg.append('svg') | |
.classed('bars', true) | |
.attr('viewBox', '0 0 960 100') | |
.attr('preserveAspectRatio', 'none') | |
.selectAll('g') | |
.data(data) | |
.enter().append('g') | |
.attr('transform', (d, i) -> "translate(#{x(i)},0)") | |
bar.append('rect') | |
.attr('class', 'one') | |
.attr('x', 1) | |
.attr('y', (d) -> y(d.one)) | |
.attr('width', barWidth) | |
.attr('height', (d) -> viewHeight - y(d.one)) | |
hover = svg.append('g') | |
.classed('hover-title', true) | |
.attr('opacity', 1) | |
hover.append('text') | |
.attr('x', 10) | |
.attr('y', height - 25) | |
.text('Page Title') | |
.style('font-size', 64) | |
.style('stroke', 'black') | |
.style('stroke-width', '5px') | |
hover.append('text') | |
.attr('x', 10) | |
.attr('y', height - 25) | |
.text('Page Title') | |
.style('fill', '#fff') | |
.style('font-size', 64) | |
svg.on 'mouseenter', -> | |
d3.selectAll('.hover-title').transition() | |
.duration(500) | |
.attr('opacity', 0) | |
svg.on 'mouseleave', -> | |
d3.selectAll('.hover-title').transition() | |
.duration(500) | |
.attr('opacity', 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment