Last active
August 29, 2015 14:02
-
-
Save kaezarrex/f4ad8058456687fde6c5 to your computer and use it in GitHub Desktop.
Chart and Table
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"> | |
<title>D3 Chart and Table</title> | |
<link rel="stylesheet/less" type="text/css" href="style.less"> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/less.js/1.7.1/less.min.js" type="text/javascript"></script> | |
</head> | |
<body> | |
<svg class="chart"></svg> | |
<table class="list"> | |
<thead> | |
<tr><th>index</th> | |
<th>value</th></tr> | |
</thead> | |
<tbody></tbody> | |
</table> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://coffeescript.org/extras/coffee-script.js"></script> | |
<script src="main.coffee" type="text/coffeescript"></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
data = [4, 8, 15, 16, 23, 42, 33] | |
margin = {top: 0, right: 0, bottom: 30, left: 16} | |
barHeight = 20 | |
width = 420 - margin.left - margin.right | |
height = barHeight * data.length | |
mouseover = (d, i) -> | |
d3.select('g[data-index="' + i + '"] rect') | |
.style('fill', 'red') | |
d3.select('tr[data-index="' + i + '"]') | |
.style('background-color', 'red') | |
.style('color', 'white') | |
mouseout = (d, i) -> | |
d3.select('g[data-index="' + i + '"] rect') | |
.style('fill', 'steelblue') | |
d3.select('tr[data-index="' + i + '"]') | |
.style('background-color', null) | |
.style('color', 'black') | |
x = d3.scale.linear() | |
.domain([0, d3.max(data)]) | |
.range([0, width]); | |
y = d3.scale.ordinal() | |
.domain(i for i in [0..data.length-1]) | |
.rangeRoundBands([0, height], .1); | |
xAxis = d3.svg.axis() | |
.scale(x) | |
.orient('bottom'); | |
yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left"); | |
chart = d3.select('.chart') | |
.attr('width', width + margin.left + margin.right) | |
.attr('height', height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
chart.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
chart.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
bar = chart.selectAll('.bar') | |
.data(data) | |
.enter().append('g') | |
.attr("class", "bar") | |
.attr('transform', (d, i) -> 'translate(0,' + i * barHeight + ')') | |
.attr('data-index', (d, i) -> i) | |
.on('mouseover', mouseover) | |
.on('mouseout', mouseout) | |
bar.append('rect') | |
.attr('width', x) | |
.attr('height', barHeight - 1) | |
listItem = d3.select('.list tbody').selectAll('.list-item') | |
.data(data) | |
.enter().append('tr') | |
.attr('class', 'list-item') | |
.attr('data-index', (d, i) -> i) | |
.on('mouseover', mouseover) | |
.on('mouseout', mouseout) | |
listItem.append('td') | |
.text((d, i) -> i) | |
listItem.append('td') | |
.text((d) -> d) |
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
body { | |
margin: 20px; | |
} | |
.chart { | |
rect { | |
fill: steelblue; | |
} | |
} | |
.list { | |
font: 15px monospace; | |
border-collapse: collapse; | |
cursor: default; | |
th { | |
border-bottom: 1px solid black; | |
} | |
td { | |
text-align: right; | |
padding: 0 1.8rem; | |
} | |
th, td { | |
padding-top: 0.1rem; | |
padding-bottom: 0.1rem; | |
} | |
td+td, th+th { | |
border-left: 1px solid black; | |
} | |
} | |
.axis { | |
text { | |
font: 10px sans-serif; | |
fill: #000; | |
} | |
path, line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
&.y path { | |
display: none; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment