Skip to content

Commit

Permalink
Fix the timezone-offset issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
trhodeos committed Mar 29, 2014
1 parent 5362e5f commit 8b543d2
Showing 1 changed file with 42 additions and 64 deletions.
106 changes: 42 additions & 64 deletions js/plot/util/fill.js
@@ -1,74 +1,70 @@
/*
/*
* == BSD2 LICENSE ==
* Copyright (c) 2014, Tidepool Project
*
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the associated License, which is identical to the BSD 2-Clause
* License as published by the Open Source Initiative at opensource.org.
*
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the License for more details.
*
*
* You should have received a copy of the License along with this program; if
* not, you can obtain one from Tidepool Project at tidepool.org.
* == BSD2 LICENSE ==
*/

var _ = require('../../lib/')._;

var log = require('../../lib/').bows('Fill');

module.exports = function(pool, opts) {

var first = new Date(opts.endpoints[0]),
last = new Date(opts.endpoints[1]),
nearest, fills = [];
var fills = [],
defaults = {
classes: {
0: 'darkest',
3: 'dark',
6: 'lighter',
9: 'light',
12: 'lightest',
15: 'lighter',
18: 'dark',
21: 'darkest'
},
duration: 3,
gutter: 0
};

first.setMinutes(first.getMinutes() + first.getTimezoneOffset());
last.setMinutes(last.getMinutes() + last.getTimezoneOffset());
_.defaults(opts || {}, defaults);

var defaults = {
classes: {
0: 'darkest',
3: 'dark',
6: 'lighter',
9: 'light',
12: 'lightest',
15: 'lighter',
18: 'dark',
21: 'darkest'
},
duration: 3,
gutter: 0
};
function pushFillFor(start, end) {
fills.push({
width: opts.xScale(end) - opts.xScale(start),
x: opts.xScale(start),
fill: opts.classes[start.getUTCHours()]
});
}

function durationSegmentedDomain() {
var first = new Date(opts.endpoints[0]);

// start on a divisible-by-opts.duration date.
first.setUTCHours(first.getUTCHours() - first.getUTCHours() % opts.duration);

return d3.time.hour.utc.range(first, opts.endpoints[1], opts.duration);
}

_.defaults(opts || {}, defaults);

function fill(selection) {
if (!opts.xScale) {
opts.xScale = pool.xScale().copy();
}
fill.findNearest(opts.endpoints[1]);
var otherNear = new Date(nearest);
otherNear.setMinutes(otherNear.getMinutes() - otherNear.getTimezoneOffset());
fills.push({
width: opts.xScale(last) - opts.xScale(nearest),
x: opts.xScale(otherNear),
fill: opts.classes[nearest.getHours()]
});
var current = new Date(nearest);
while (current > first) {
var next = new Date(current);
next.setHours(current.getHours() - opts.duration);
var otherNext = new Date(next);
otherNext.setMinutes(otherNext.getMinutes() - otherNext.getTimezoneOffset());
fills.push({
width: opts.xScale(current) - opts.xScale(next),
x: opts.xScale(otherNext),
fill: opts.classes[next.getHours()]
});
current = next;
var i, range;

range = durationSegmentedDomain();
for (i = 0; i < range.length - 1; i++) {
pushFillFor(range[i], range[i + 1]);
}

if (opts.dataGutter) {
Expand Down Expand Up @@ -128,23 +124,5 @@ module.exports = function(pool, opts) {
});
}

fill.findNearest = function(d) {
var date = new Date(d);
date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
var hourBreaks = [];
var i = 0;
while (i <= 24) {
hourBreaks.push(i);
i += opts.duration;
}
for(var j = 0; j < hourBreaks.length; j++) {
var br = hourBreaks[j];
var nextBr = hourBreaks[j + 1];
if ((date.getHours() >= br) && (date.getHours() < nextBr)) {
nearest = new Date(date.getFullYear(), date.getMonth(), date.getDate(), br, 0, 0);
}
}
};

return fill;
};
};

0 comments on commit 8b543d2

Please sign in to comment.