Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/commonize #17

Closed
wants to merge 20 commits into from
81 changes: 48 additions & 33 deletions js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
alarmSound = 'alarm.mp3',
urgentAlarmSound = 'alarm2.mp3';

var useMetricBg = true; // true means use mmol/L, false means mg/dL

// create svg and g to contain the chart contents
var charts = d3.select('#chartContainer').append('svg')
Expand All @@ -43,6 +44,14 @@
focus.append('g')
.attr('class', 'y axis');

// lixgbg: Convert mg/dL BG value to metric mmol
function scaleBg(bg) {
if (useMetricBg) {
return (Math.round((by / 18) * 10) / 10).toFixed(1);
} else
return bg;
}

var context = charts.append('g');

// create the x axis container
Expand All @@ -61,13 +70,14 @@
.domain(d3.extent(data, function (d) { return d.date; }));

yScale = d3.scale.log()
.domain([30, 420]);
/*.domain([30, 420]);*/
.domain([scaleBg(30), scaleBg(420)]);

xScale2 = d3.time.scale()
.domain(d3.extent(data, function (d) { return d.date; }));

yScale2 = d3.scale.log()
.domain([36, 420]);
.domain([scaleBg(36), scaleBg(420)]);

xAxis = d3.svg.axis()
.scale(xScale)
Expand All @@ -77,9 +87,11 @@
yAxis = d3.svg.axis()
.scale(yScale)
.tickFormat(d3.format('d'))
.tickValues([40, 60, 80, 120, 180, 300, 400])
/*.tickValues([scaleBg(40), scaleBg(60), scaleBg(80), scaleBg(120), scaleBg(180), scaleBg(300), scaleBg(400)])*/
.tickValues([2.0, 3.0, 4.0, 6.0, 10.0, 15.0, 22.0])
.orient('left');


xAxis2 = d3.svg.axis()
.scale(xScale2)
.ticks(4)
Expand All @@ -88,7 +100,8 @@
yAxis2 = d3.svg.axis()
.scale(yScale2)
.tickFormat(d3.format('d'))
.tickValues([40, 60, 80, 120, 180, 300, 400])
/*.tickValues([scaleBg(40), scaleBg(60), scaleBg(80), scaleBg(120), scaleBg(180), scaleBg(300), scaleBg(400)])*/
.tickValues([2.0, 3.0, 4.0, 6.0, 10.0, 15.0, 22.0])
.orient('right');

// setup a brush
Expand Down Expand Up @@ -201,9 +214,9 @@
// transition open-top line to correct location
focus.select('.open-top')
.attr('x1', xScale2(brush.extent()[0]))
.attr('y1', yScale(30))
.attr('y1', yScale(scaleBg(30)))
.attr('x2', xScale2(brush.extent()[1]))
.attr('y2', yScale(30));
.attr('y2', yScale(scaleBg(30)));

// transition open-left line to correct location
focus.select('.open-left')
Expand All @@ -223,9 +236,9 @@
.transition()
.duration(UPDATE_TRANS_MS)
.attr('x1', xScale(new Date(now)))
.attr('y1', yScale(36))
.attr('y1', yScale(scaleBg(36)))
.attr('x2', xScale(new Date(now)))
.attr('y2', yScale(420));
.attr('y2', yScale(scaleBg(420)));

// update x axis
focus.select('.x.axis')
Expand Down Expand Up @@ -311,29 +324,29 @@
focus.append('line')
.attr('class', 'now-line')
.attr('x1', xScale(new Date(now)))
.attr('y1', yScale(36))
.attr('y1', yScale(scaleBg(36)))
.attr('x2', xScale(new Date(now)))
.attr('y2', yScale(420))
.attr('y2', yScale(scaleBg(420)))
.style('stroke-dasharray', ('3, 3'))
.attr('stroke', 'grey');

// add a y-axis line that shows the high bg threshold
focus.append('line')
.attr('class', 'high-line')
.attr('x1', xScale(dataRange[0]))
.attr('y1', yScale(180))
.attr('y1', yScale(scaleBg(180)))
.attr('x2', xScale(dataRange[1]))
.attr('y2', yScale(180))
.attr('y2', yScale(scaleBg(180)))
.style('stroke-dasharray', ('3, 3'))
.attr('stroke', 'grey');

// add a y-axis line that shows the low bg threshold
focus.append('line')
.attr('class', 'low-line')
.attr('x1', xScale(dataRange[0]))
.attr('y1', yScale(80))
.attr('y1', yScale(scaleBg(80)))
.attr('x2', xScale(dataRange[1]))
.attr('y2', yScale(80))
.attr('y2', yScale(scaleBg(80)))
.style('stroke-dasharray', ('3, 3'))
.attr('stroke', 'grey');

Expand All @@ -357,29 +370,29 @@
context.append('line')
.attr('class', 'now-line')
.attr('x1', xScale(new Date(now)))
.attr('y1', yScale2(36))
.attr('y1', yScale2(scaleBg(36)))
.attr('x2', xScale(new Date(now)))
.attr('y2', yScale2(420))
.attr('y2', yScale2(scaleBg(420)))
.style('stroke-dasharray', ('3, 3'))
.attr('stroke', 'grey');

// add a y-axis line that shows the high bg threshold
context.append('line')
.attr('class', 'high-line')
.attr('x1', xScale(dataRange[0]))
.attr('y1', yScale2(180))
.attr('y1', yScale2(scaleBg(180)))
.attr('x2', xScale(dataRange[1]))
.attr('y2', yScale2(180))
.attr('y2', yScale2(scaleBg(180)))
.style('stroke-dasharray', ('3, 3'))
.attr('stroke', 'grey');

// add a y-axis line that shows the low bg threshold
context.append('line')
.attr('class', 'low-line')
.attr('x1', xScale(dataRange[0]))
.attr('y1', yScale2(80))
.attr('y1', yScale2(scaleBg(80)))
.attr('x2', xScale(dataRange[1]))
.attr('y2', yScale2(80))
.attr('y2', yScale2(scaleBg(80)))
.style('stroke-dasharray', ('3, 3'))
.attr('stroke', 'grey');

Expand Down Expand Up @@ -424,27 +437,27 @@
.transition()
.duration(UPDATE_TRANS_MS)
.attr('x1', xScale(currentBrushExtent[0]))
.attr('y1', yScale(180))
.attr('y1', yScale(scaleBg(180)))
.attr('x2', xScale(currentBrushExtent[1]))
.attr('y2', yScale(180));
.attr('y2', yScale(scaleBg(180)));

// transition low line to correct location
focus.select('.low-line')
.transition()
.duration(UPDATE_TRANS_MS)
.attr('x1', xScale(currentBrushExtent[0]))
.attr('y1', yScale(80))
.attr('y1', yScale(scaleBg(80)))
.attr('x2', xScale(currentBrushExtent[1]))
.attr('y2', yScale(80));
.attr('y2', yScale(scaleBg(80)));

// transition open-top line to correct location
focus.select('.open-top')
.transition()
.duration(UPDATE_TRANS_MS)
.attr('x1', xScale2(currentBrushExtent[0]))
.attr('y1', yScale(30))
.attr('y1', yScale(scaleBg(30)))
.attr('x2', xScale2(currentBrushExtent[1]))
.attr('y2', yScale(30));
.attr('y2', yScale(scaleBg(30)));

// transition open-left line to correct location
focus.select('.open-left')
Expand All @@ -469,18 +482,18 @@
.transition()
.duration(UPDATE_TRANS_MS)
.attr('x1', xScale2(dataRange[0]))
.attr('y1', yScale2(180))
.attr('y1', yScale2(scaleBg(180)))
.attr('x2', xScale2(dataRange[1]))
.attr('y2', yScale2(180));
.attr('y2', yScale2(scaleBg(180)));

// transition low line to correct location
context.select('.low-line')
.transition()
.duration(UPDATE_TRANS_MS)
.attr('x1', xScale2(dataRange[0]))
.attr('y1', yScale2(80))
.attr('y1', yScale2(scaleBg(180)))
.attr('x2', xScale2(dataRange[1]))
.attr('y2', yScale2(80));
.attr('y2', yScale2(scaleBg(180)));
}
}

Expand All @@ -491,9 +504,9 @@
.transition()
.duration(UPDATE_TRANS_MS)
.attr('x1', xScale2(new Date(now)))
.attr('y1', yScale2(36))
.attr('y1', yScale2(scaleBg(36)))
.attr('x2', xScale2(new Date(now)))
.attr('y2', yScale2(420));
.attr('y2', yScale2(scaleBg(420)));

// only if a user brush is not active, update brush and focus chart with recent data
// else, just transition brush
Expand Down Expand Up @@ -580,7 +593,9 @@
if (d.length > 1) {
// change the next line so that it uses the prediction if the signal gets lost (max 1/2 hr)
if (d[0].length) {
var current = d[0][d[0].length - 1];
var bg = scaleBg(d[0][d[0].length - 1].y);
$('#currentBG').text(bg);
$('#bgValue').text(bg);
var secsSinceLast = (Date.now() - new Date(current.x).getTime()) / 1000;
var currentBG = current.y;

Expand Down
5 changes: 5 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var fs = require('fs');
var mongoClient = require('mongodb').MongoClient;
var pebble = require('./lib/pebble');
var cgmData = [];
//kate

// var useMetricBg = true; // true means use mmol/L, false means mg/dL

////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -154,6 +158,7 @@ function update() {
if (element) {
var obj = {};
obj.y = element.sgv;

obj.x = element.date;
obj.d = element.dateString;
obj.direction = directionToChar(element.direction);
Expand Down