xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Adding Padding</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('rect').click(function() {
console.log('clicked a rect');
$(this).attr("fill","#fc9000");
});
$('#hideme').click(function() {
$('#thechart').toggle('fast');
});
});
</script>
<style type="text/css">
body {
background-color: #ddddff;
font-family: Arial;
}
svg {
background-color: white;
}
rect.clickable:hover {
fill: yellow;
}
g.axis path, g.axis line {
stroke: green;
fill: none;
shape-rendering: crispEdges;
}
g.axis text {
font-family: Arial;
font-size: 8px;
opacity: 0.5;
text-anchor: left;
}
g.tick {
fill: black;
opacity: 0.5;
text-anchor: left;
}
g.axis.y line, g.axis.y path {
stroke: orange;
opacity: 0;
}
</style>
</head>
<body>
<h1>Corety RTU-3 Mixed Air Temps</h1>
<p>Date range 2014 January</p>
<p><small><a id="hideme">hideChart</a></small></p>
<div id="thechart" style="border: 1px solid red; width: 90%;">
<script type="text/javascript">
var w = 800;
var h = 800;
var padding = [20, 220, 80, 80 ]; //Top, right, bottom, left
var widthScale = d3.scale.pow().exponent(0.5)
.range([ 0, w - padding[1] - padding[3] ]);
var heightScale = d3.scale.ordinal()
.rangeRoundBands([ padding[0], h - padding[2] ], 0.1);
var xAxis = d3.svg.axis()
.scale(widthScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(heightScale)
.orient("right");
var svg = d3.select("body #thechart")
.append("svg")
.attr("id","svg")
.attr("width", w)
.attr("height", h);
d3.select("svg")
.append("h1")
.attr("width",500)
.attr("height",200)
.attr("fill","yellow")
.text("h1 text inside of svg"); // I didn't expect this to work but thought I should try.
d3.csv("Corey.RTU-3-short-2.csv", function(data) {
data.sort(function(a, b) {
return d3.descending(a['64651.Corey Building.RTU-3.Mixed AIr Temp'], b['64651.Corey Building.RTU-3.Mixed AIr Temp']);
});
//data.push(['Corey.RTU-3','2015-01-02 11:00:00','100','74.27','58.72','410.5','0','19.9','0.25','0','0','0','Th','1','10.68']);
widthScale.domain([ 0, d3.max(data, function(d) {
return +d['64651.Corey Building.RTU-3.Mixed AIr Temp'];
}) ]);
heightScale.domain(data.map(function(d,i) {
return d.DateTime + ' ' + d['64651.Corey Building.RTU-3.Mixed AIr Temp'];
} ));
var rects = svg.selectAll("rect")
.data(data)
.enter()
.append("rect");
rects.attr("x", padding[3])
.attr("y", function(d, i) {
return heightScale(d.DateTime + ' ' + d['64651.Corey Building.RTU-3.Mixed AIr Temp']);
})
.attr("width", function(d) {
return widthScale(d['64651.Corey Building.RTU-3.Mixed AIr Temp']);
})
.attr("height", heightScale.rangeBand())
.attr("class","clickable")
.append("title")
.text(function(d) {
return d.DateTime + "'s life satisfaction score is " + d['64651.Corey Building.RTU-3.Mixed AIr Temp'];
});
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (w - padding[1] + 5) + ",0)")
.call(yAxis);
d3.selectAll('rect').on('click', function() {
console.log('clicked a rect');
d3.select(this).style('fill', '#fc9000');
});
});
</script>
</div>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js
https://code.jquery.com/jquery-2.1.3.js