block by zeffii 4e6db23905df8a3b346c

Glucose + food 2

Full Screen

Built with blockbuilder.org

forked from zeffii‘s block: Glucose over time + food hints

forked from zeffii‘s block: Glucose over time + food hints

forked from zeffii‘s block: Glucose over time + food hints

forked from zeffii‘s block: Glucose over time + food hints

forked from zeffii‘s block: Glucose over time + food hints

forked from zeffii‘s block: Glucose over time + food hints

forked from zeffii‘s block: Glucose over time + food hints

forked from zeffii‘s block: Glucose over time + food hints

forked from zeffii‘s block: Glucose over time + food hints

forked from zeffii‘s block: Glucose over time + food hints

forked from zeffii‘s block: Glucose over time + food hints mod

index.html

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
  <link rel="stylesheet" type="text/css" href="style.css" />
  <style>
    body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
    svg { width:100%; height: 100% }
  </style>
</head>

<body>
  <svg></svg>
  <script src="dillitant.js"></script>
</body>

compiled.json

{
    "01/27/2016/18:00:00": " - ( red lentels / mixed veg / pumpkin ) blended soup. - sweet potatoe wedgies half fried, half grilled. (sauce ketchup, yogonaise) - small yoghurt + milk + raak sirup (sugar free)",
    "01/28/2016/19:00:00": " - last night's soup 1 bowl.   - carrot/sweetpotatoe/pumpkin/brocolli/spinnage / cheese / pastry pie. (small servings)",
    "01/28/2016/21:30:00": " - two craqotte  with apricot jam (thinly spread)",
    "01/29/2016/16:00:00": " - dinner leftover pie, small servings.   ",
    "01/29/2016/22:00:00": " - two craqotte  with peanutbutter (thinly spread)  ",
    "01/30/2016/13:00:00": " - mandarin / bowl half porridge + half bran flakes.  ",
    "01/30/2016/19:00:00": " - sperziebonen / carrots / peas / rode kool - thin piece of lean meat (fried, crispy)  ",
    "01/30/2016/22:00:00": " - mandrin / beschuit + ham.  ",
    "01/31/2016/18:00:00": " - big salad (lettuce, yellow pepper, cucumber, salad dressing) - boiled+fried rice / soyasauce / lean minced meat / fried egg  ",
    "01/31/2016/22:00:00": " - mandarin / volk.knackerbrot + cheese / tea  ",
    "02/01/2016/17:00:00": " - broodje humus in the pit AMC , mint tea, yoghurt  ",
    "02/01/2016/22:00:00": " - volk.beshuit met ham + kaas  ",
    "02/02/2016/18:00:00": " - big salad (almost no dressing) yellow pepper / lettuce / cheese",
    "02/03/2016/18:00:00": " - brown rice / chicken / lentil+veg soup (broccoli+carrot)",
    "02/04/2016/18:00:00": " - small lightly fried (brown rice / chicken / egg) - big salad (lettuce, yellow pepper, pine nuts, tomatoe, cheese, small amount of salad dressing)",
    "02/05/2016/18:00:00": " - lightly fried chicken pieces with curry, carrot+french bean+broccoli, pinenuts, apple",
    "02/06/2016/18:20:00": " - Salad cucumber, dressing, yellow paprika, thinly sliced smoked ham (1x) - dinner sweet potatoe, french beans, broccoli - banana",
    "02/07/2016/18:00:00": " - muskaatpompoen + lentil soep + pease + rice - vla + yoghurt - manderin + appel",
    "02/07/2016/22:00:00": " - matze crackers + sliced banana",
    "02/08/2016/18:00:00": " - lean burgers (2x) + ketchup + brocolli + sweetpotatoe (small servings) - vla + yoghurt (small)",
    "02/08/2016/22:00:00": " - matze crackers + cheese",
    "02/09/2016/12:00:00": " - fried sweet potatoe wedgies + ketchup",
    "02/09/2016/18:00:00": " - big salad (yellow peppers, lettuce, cucumber, pine nuts) - 4 mini pancakes (/w lemon, /w small qauntity poeder suiker) "
}

dillitant.js

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;

  var x = d3.time.scale()
      .range([0, width]);
  
  var x2 = d3.time.scale()
      .range([0, width]);
    
  var y = d3.scale.linear()
      .range([height, 0]);

  var y2 = d3.scale.linear()
      .range([height, 0]);
  
  var xAxis = d3.svg.axis()
      .scale(x)
      .ticks(d3.time.days, 1)
      .orient("bottom");

  var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left");

  var x2Axis = d3.svg.axis()
      .scale(x2)
      .orient("top");

  var y2Axis = d3.svg.axis()
      .scale(y2)
      .orient("right");
    
  
  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([first_day, last_day]);
  x2.domain([first_day, last_day]);
  y.domain(d3.extent(data, function(d) { return d.bsl; }));
  y2.domain([0, 24]);
  
  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 + ")");
  
  g1.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0, " + height + ")")
      .call(xAxis);

  g1.append("g")
      .attr("class", "y axis")
      .call(yAxis)
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Glucose");  

  g2.append("g")
      .attr("class", "y2 axis")
      .attr("transform", "translate(" + width + ", 0)")
      .call(y2Axis)
  
    .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", width)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("Food time");  
  
  
  
  var circle = g1.selectAll('circle').data(data);
  var circleEnter = circle.enter().append('circle');
  circleEnter 
     .attr("cx", function(d, i){return x(d.unix);})
     .attr("cy", function(d, i){return y(d.bsl);})
     .attr("r", function(d, i) {return 3})
     .style({fill: "#fe42ec"})
    
  var crect = g2.selectAll('rect').data(food);
  var crectEnter = crect.enter().append('rect');
  crectEnter
    .attr("width", 7)
    .attr("height", 13)
    .attr("x", function(d, i){return x2(d.time);})
    .attr("y", function(d, i){return y2(d.time.getHours());})
    .attr("r", 3)
    .style({fill: "#56feb3"})
  
}

style.css

sugars.csv

num,date,time,bsl,id,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12
"174","02/08/2016","21:37:49","18.54","JBHPR012","2","10","-1","0","6","","601","3","","","1","0"
"173","02/08/2016","06:00:41","17.93","JBHPR012","2","10","-1","0","1","","601","3","","","2","0"
"172","02/07/2016","16:42:06","28.36","JBHPR012","2","10","-1","0","4","","601","3","","","3","0"
"171","02/07/2016","07:14:21","10.27","JBHPR012","2","10","-1","0","1","","601","3","","","4","0"
"170","02/06/2016","04:39:46","11.82","JBHPR012","2","10","-1","0","7","","601","3","","","5","-1"
"169","02/05/2016","06:36:20","14.27","JBHPR012","2","10","-1","0","1","","601","3","","","6","0"
"168","02/04/2016","22:23:07","24.15","JBHPR012","2","10","-1","0","6","","601","3","","","7","0"
"167","02/04/2016","05:33:57","17.71","JBHPR012","2","10","-1","0","7","","601","3","","","8","-1"
"166","02/03/2016","06:21:34","12.93","JBHPR012","2","10","-1","0","1","","601","3","","","9","0"
"165","02/02/2016","08:26:20","15.76","JBHPR012","2","10","-1","0","1","","601","3","","","1","0"
"164","02/01/2016","04:32:57","13.38","JBHPR012","2","10","-1","0","7","","601","3","","","2","-1"
"fix","01/31/2016","12:45:00","33.3","veria----","n","na","na","n","n","","---","-","","","-","--"
"163","01/30/2016","08:38:46","13.71","JBHPR012","2","10","-1","0","1","","601","3","","","3","0"
"fix","01/30/2016","08:34:00","18.0","veria----","n","na","na","n","n","","---","-","","","-","--"
"162","01/29/2016","08:36:30","19.26","JBHPR012","2","10","-1","0","1","","601","3","","","4","0"
"fix","01/28/2016","16:47:00","27.4","veria----","n","na","na","n","n","","---","-","","","-","--"
"fix","01/28/2016","03:20:00","22.6","veria----","n","na","na","n","n","","---","-","","","-","--"
"fix","01/27/2016","07:06:00","13.7","veria----","n","na","na","n","n","","---","-","","","-","--"
"fix","01/26/2016","08:50:00","28.7","veria----","n","na","na","n","n","","---","-","","","-","--"
"fix","01/26/2016","08:40:00","27.8","veria----","n","na","na","n","n","","---","-","","","-","--"
"161","01/25/2016","08:33:55","16.43","JBHPR012","2","10","-1","0","1","","601","3","","","5","0"
"160","01/25/2016","08:29:55","15.99","JBHPR012","2","10","-1","0","1","","601","3","","","6","0"
"fix","01/25/2016","08:24:00","16.5","veria----","n","na","na","n","n","","---","-","","","-","--"
"159","01/24/2016","08:29:57","17.32","JBHPR012","2","10","-1","0","1","","601","3","","","7","0"
"158","01/24/2016","08:29:16","17.04","JBHPR012","2","10","-1","0","1","","601","3","","","8","0"
"157","01/23/2016","08:59:12","10.55","JBHPR012","2","10","-1","0","1","","601","3","","","9","0"
"156","01/22/2016","08:48:21","9.71","JBHPR012","2","10","-1","0","1","","601","3","","","10","0"
"155","01/21/2016","08:40:38","12.6","JBHPR012","2","10","-1","0","1","","601","3","","","11","0"
"154","01/20/2016","09:25:26","16.43","JBHPR012","2","10","-1","0","2","","601","3","","","12","0"
"153","01/18/2016","11:29:08","15.65","JBHPR012","2","10","-1","0","3","","601","3","","","13","0"