block by zeffii b887532acfb8b001940d

Glucose over time + food hints mod

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

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"
}

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"

 food hints:
*/





d3.csv("sugars.csv", function(error, data) {
  if (error) throw error;
 
  data = csv_preprocessor(data)
  draw_graph(data);
  
});

function csv_preprocessor(data){

  // "02/02/2016/08:26:20"
  var format = d3.time.format("%m/%d/%Y/%H:%M:%S");
  
  // 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 draw_graph(data){
  
  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 y = 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");

  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]);
  y.domain(d3.extent(data, function(d) { return d.bsl; }));
  
  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 + ")");
  
  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");  
  
  var circle = g1.selectAll('circle').data(data);
  
  var circleEnter = circle.enter().append('circle');
    
  circleEnter.attr("cx", function(d, i){return x(d.unix);})
  circleEnter.attr("cy", function(d, i){return y(d.bsl);})
  circleEnter.attr("r", function(d, i) {return 3})
  circleEnter.style({fill: "#002200"})
  
}

style.css

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.axis text {
  font: 10px sans-serif;
}

sugars.csv

num,date,time,bsl,id,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12
"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"
"163","01/30/2016","08:38:46","13.71","JBHPR012","2","10","-1","0","1","","601","3","","","3","0"
"162","01/29/2016","08:36:30","19.26","JBHPR012","2","10","-1","0","1","","601","3","","","4","0"
"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"
"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"