|
var svg = d3.select("svg") |
|
|
|
/* |
|
sample row (u(integer) stands for unused field) |
|
num,date,time,bsl,id,u1,u2,u3,u4,....,u12 |
|
"165", "02/02/2016", "08:26:20", "15.76" |
|
|
|
onetouch .csv writer generates a verbose format. |
|
|
|
*/ |
|
|
|
// "02/02/2016/08:26:20" |
|
var format = d3.time.format("%m/%d/%Y/%H:%M:%S"); |
|
|
|
d3.csv("sugars.csv", function(error, data) { |
|
if (error) throw error; |
|
data = csv_preprocessor(data); |
|
|
|
d3.json("compiled.json", function(error, food) { |
|
if (error) throw error; |
|
food = json_preprocessor(food); |
|
|
|
// both data and food are processed, ready to rock. |
|
draw_graph(data, food); |
|
}); |
|
|
|
}); |
|
|
|
function csv_preprocessor(data){ |
|
|
|
// reduce down to : UNIXTIME / BSL |
|
data.forEach(function(d) { |
|
var unix_str = d.date + "/" + d.time; |
|
var date_stamp = format.parse(unix_str); |
|
d.unix = d3.time.hour.offset(date_stamp, 1); // gmt to dutch |
|
d.bsl = +d.bsl; |
|
}); |
|
|
|
return data; |
|
} |
|
|
|
function json_preprocessor(p){ |
|
var new_object_array = []; |
|
for (var key in p) { |
|
if (p.hasOwnProperty(key)) { |
|
var gmt = format.parse(key); |
|
var unix = d3.time.hour.offset(gmt, 0); |
|
new_object_array.push({time: unix, statement: p[key]}); |
|
} |
|
} |
|
return new_object_array; |
|
|
|
} |
|
|
|
function draw_graph(data, food){ |
|
|
|
var margin = {top: 20, right: 80, bottom: 30, left: 50}, |
|
width = 960 - margin.left - margin.right, |
|
height = 500 - margin.top - margin.bottom; |
|
|
|
// time during day |
|
var x = d3.time.scale() |
|
.range([0, width]); |
|
|
|
// day given range of days. (each y notch shall repr a day) |
|
var y = d3.time.scale() |
|
.range([height, 0]); |
|
|
|
var xAxis = d3.svg.axis() |
|
.scale(x) |
|
.ticks(d3.time.linear) |
|
//.ticks(d3.time.hours, ) |
|
//.tickFormat(d3.time.format('%H:%M')) |
|
.tickSize(0) |
|
.tickPadding(8) |
|
.orient("bottom"); |
|
|
|
var yAxis = d3.svg.axis() |
|
.scale(y) |
|
.ticks(d3.time.days) |
|
.orient("left"); |
|
|
|
function adjust_date(date, add){ |
|
return d3.time.day.offset(date, add) |
|
} |
|
var first_day = adjust_date(data[data.length - 1].unix, -1); |
|
var last_day = adjust_date(data[0].unix, 1); |
|
|
|
x.domain([0, 24]); |
|
y.domain([first_day, last_day]); |
|
|
|
|
|
svg |
|
.attr("width", width + margin.left + margin.right) |
|
.attr("height", height + margin.top + margin.bottom); |
|
|
|
var g1 = svg.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
var g2 = svg.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
var g3 = svg.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
g1.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(0, " + height + ")") |
|
.call(xAxis); |
|
|
|
g1.append("g") |
|
.attr("class", "y axis") |
|
.call(yAxis) |
|
|
|
var circle = g1.selectAll('circle').data(data); |
|
var circleEnter = circle.enter().append('circle'); |
|
circleEnter |
|
.attr("cx", function(d, i){return x(d.unix.getHours());}) |
|
.attr("cy", function(d, i){return y(d.unix);}) |
|
.attr("r", function(d, i) {return 3}) |
|
.style({fill: "#fe42ec"}) |
|
|
|
var circle2 = g2.selectAll('circle').data(food); |
|
var circle2Enter = circle2.enter().append('circle'); |
|
circle2Enter |
|
.attr("cx", function(d, i){return x(d.time.getHours());}) |
|
.attr("cy", function(d, i){return y(d.time)}) |
|
.attr("r", 4) |
|
.style({fill: "#56feb3"}) |
|
|
|
var rect1 = g3.selectAll('rect').data(data); |
|
var rectEnter = rect1.enter().append('rect'); |
|
rectEnter |
|
.attr("width", width) |
|
.attr("height", 1) |
|
.style({fill: "#48f5f8"}) |
|
.attr("transform", function(d){ |
|
var kn = y(d3_time.timeDay.round(d.unix)); |
|
return "translate(0, " + kn + ")" |
|
}) |
|
|
|
// console.log(data, food) |
|
|
|
} |