forked from mbostock's block: Heatmap (2D Histogram, CSV)
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.label {
font-weight: bold;
}
.tile {
shape-rendering: crispEdges;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 30, right: 90, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
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 + ")");
// d3.csv('data.csv', function(csv) {
// csv.forEach(function(row) {
// console.log(Object.keys(row).slice(1));
// });});
//TODO: make ordinal scale generalized
// console.log(data[0].keys());
var x = d3.scale.ordinal().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var z = d3.scale.linear().range(["white", "steelblue"]);
// // The size of the buckets in the CSV data file.
// // This could be inferred from the data if it weren't sparse.
var xStep = 864e5,
yStep = 100;
d3.csv("data.csv")
//coerce data
.row(function(d) {
// for each row of the data, create an object with these properties...
return {
hour : +d.Hour,
friday: +d.Friday,
saturday: +d.Saturday,
sunday: +d.Sunday,
monday: +d.Monday,
tuesday: +d.Tuesday,
wednesday: +d.Wednesday,
thursday: +d.Thursday,
};
})
.get(function(error, data) {
if (error) throw error;
// console.log(data);
// });
//TODO: real x domain
// // Compute the scale domains.
x.domain(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"])
// x.domain(function(d){
// console.log(d);
// return d;
// });
// // console.log(function(d){})
y.domain([0, d3.max(data, function(d){return d.hour})]);
// console.log(y.domain());
z.domain([0, d3.max(data, function(d) {
// console.log(d3.max(d));
return d3.max(Object.value(d));
})]);
console.log(z.domain());
// // Extend the x- and y-domain to fit the last bucket.
// // For example, the y-bucket 3200 corresponds to values [3200, 3300].
// x.domain([x.domain()[0], +x.domain()[1] + xStep]);
// y.domain([y.domain()[0], y.domain()[1] + yStep]);
// // Display the tiles for each non-zero bucket.
// // See https://bl.ocks.org/3074470 for an alternative implementation.
// svg.selectAll(".tile")
// .data(buckets)
// .enter().append("rect")
// .attr("class", "tile")
// .attr("x", function(d) { return x(d.date); })
// .attr("y", function(d) { return y(d.bucket + yStep); })
// .attr("width", x(xStep) - x(0))
// .attr("height", y(0) - y(yStep))
// .style("fill", function(d) { return z(d.count); });
// // Add a legend for the color values.
// var legend = svg.selectAll(".legend")
// .data(z.ticks(6).slice(1).reverse())
// .enter().append("g")
// .attr("class", "legend")
// .attr("transform", function(d, i) {
// return "translate(" + (width + 20) + "," + (20 + i * 20) + ")"; });
// legend.append("rect")
// .attr("width", 20)
// .attr("height", 20)
// .style("fill", z);
// legend.append("text")
// .attr("x", 26)
// .attr("y", 10)
// .attr("dy", ".35em")
// .text(String);
// svg.append("text")
// .attr("class", "label")
// .attr("x", width + 20)
// .attr("y", 10)
// .attr("dy", ".35em")
// .text("Count");
// // Add an x-axis with label.
// svg.append("g")
// .attr("class", "x axis")
// .attr("transform", "translate(0," + height + ")")
// .call(d3.svg.axis().scale(x).ticks(d3.time.days).tickFormat(formatDate).orient("bottom"))
// .append("text")
// .attr("class", "label")
// .attr("x", width)
// .attr("y", -6)
// .attr("text-anchor", "end")
// .text("Date");
// // Add a y-axis with label.
// svg.append("g")
// .attr("class", "y axis")
// .call(d3.svg.axis().scale(y).orient("left"))
// .append("text")
// .attr("class", "label")
// .attr("y", 6)
// .attr("dy", ".71em")
// .attr("text-anchor", "end")
// .attr("transform", "rotate(-90)")
// .text("Value");
});
</script>
https://d3js.org/d3.v3.min.js