Added license

This commit is contained in:
Henry
2016-07-25 12:09:15 -04:00
parent 19fd63b850
commit a4f6f6f50b
3 changed files with 47 additions and 45 deletions

View File

@ -61,6 +61,17 @@ define([
"key": "DEFAULT_TIME_FORMAT", "key": "DEFAULT_TIME_FORMAT",
"value": "utc" "value": "utc"
} }
],
"licenses": [
{
"name": "d3",
"version": "3.0.0",
"description": "Incorporates modified code from d3 Time Scales",
"author": "Mike Bostock",
"copyright": "Copyright 2010-2016 Mike Bostock. " +
"All rights reserved.",
"link": "https://github.com/d3/d3/blob/master/LICENSE"
}
] ]
} }
}); });

View File

@ -41,6 +41,12 @@ define([
DAYS = 24 * HOURS, DAYS = 24 * HOURS,
MONTHS = (365 / 12) * DAYS; MONTHS = (365 / 12) * DAYS;
/**
* @typedef Scale
* @property {number} min the minimum scale value, in ms
* @property {number} max the maximum scale value, in ms
*/
/** /**
* Formatter for UTC timestamps. Interprets numeric values as * Formatter for UTC timestamps. Interprets numeric values as
* milliseconds since the start of 1970. * milliseconds since the start of 1970.
@ -57,42 +63,43 @@ define([
* the threshold required. * the threshold required.
* @private * @private
*/ */
function getScaledFormat (d, threshold) { function getScaledFormat (d) {
//Adapted from D3 formatting rules var m = moment.utc(d);
if (!(d instanceof Date)){ /**
d = new Date(moment.utc(d)); * Uses logic from d3 Time-Scales, v3 of the API. See
} * https://github.com/d3/d3-3.x-api-reference/blob/master/Time-Scales.md
*
* Licensed
*/
return [ return [
[".SSS", function(d) { return d.getMilliseconds() >= threshold; }], [".SSS", function(m) { return m.milliseconds(); }],
[":ss", function(d) { return d.getSeconds() * SECONDS >= threshold; }], [":ss", function(m) { return m.seconds(); }],
["HH:mm", function(d) { return d.getMinutes() * MINUTES >= threshold; }], ["HH:mm", function(m) { return m.minutes(); }],
["HH", function(d) { return d.getHours() * HOURS >= threshold; }], ["HH", function(m) { return m.hours(); }],
["ddd DD", function(d) { ["ddd DD", function(m) {
return d.getDay() * DAYS >= threshold && return m.days() &&
d.getDate() != 1; m.date() != 1;
}], }],
["MMM DD", function(d) { return d.getDate() != 1; }], ["MMM DD", function(m) { return m.date() != 1; }],
["MMMM", function(d) { ["MMMM", function(m) {
return d.getMonth() * MONTHS >= threshold; return m.month();
}], }],
["YYYY", function() { return true; }] ["YYYY", function() { return true; }]
].filter(function (row){ ].filter(function (row){
return row[1](d); return row[1](m);
})[0][0]; })[0][0];
}; };
/** /**
* *
* @param value * @param value
* @param {number} [threshold] Optionally provides context to the * @param {Scale} [scale] Optionally provides context to the
* format request, allowing for scale-appropriate formatting. This value * format request, allowing for scale-appropriate formatting.
* should be the minimum unit to be represented by this format, in ms. For
* example, to display seconds, a threshold of 1 * 1000 should be provided.
* @returns {string} the formatted date * @returns {string} the formatted date
*/ */
UTCTimeFormat.prototype.format = function (value, threshold) { UTCTimeFormat.prototype.format = function (value, scale) {
if (threshold !== undefined){ if (scale !== undefined){
var scaledFormat = getScaledFormat(value, threshold); var scaledFormat = getScaledFormat(value, scale);
if (scaledFormat) { if (scaledFormat) {
return moment.utc(value).format(scaledFormat); return moment.utc(value).format(scaledFormat);
} }

View File

@ -57,39 +57,23 @@ define(
axisElement.call(xAxis); axisElement.call(xAxis);
} }
// Calculates the precision of date for formatting. Relies on
// D3's behavior to tick on round units
function threshold(date){
var ms = date.getTime();
return [
365 * 24 * 60 * 60 * 1000, // years
(365 / 12) * 24 * 60 * 60 * 1000, // months
7 * 24 * 60 * 60 * 1000, // weeks
24 * 60 * 60 * 1000, // days
60 * 60 * 1000, // hours
60 * 1000, // minutes
1000, // seconds
1 // ms
].filter(function (boundary) {
return ms % boundary === 0;
})[0];
}
function changeTimeSystem(timeSystem) { function changeTimeSystem(timeSystem) {
var key = timeSystem.formats()[0]; var key = timeSystem.formats()[0];
if (key !== undefined) { if (key !== undefined) {
var format = formatService.getFormat(key); var format = formatService.getFormat(key);
var b = conductor.bounds();
//Define a custom format function //Define a custom format function
xAxis.tickFormat(function (date) { xAxis.tickFormat(function (date) {
return format.format(date, threshold(date)); return format.format(date, {min: b.start, max: b.end});
}); });
axisElement.call(xAxis); axisElement.call(xAxis);
} }
} }
scope.resize = function () { scope.resize = function () {
setScale(conductor.bounds().start, conductor.bounds().end); var b = conductor.bounds();
setScale(b.start, b.end);
}; };
conductor.on('timeSystem', changeTimeSystem); conductor.on('timeSystem', changeTimeSystem);
@ -98,9 +82,9 @@ define(
conductor.on('bounds', function (bounds) { conductor.on('bounds', function (bounds) {
setScale(bounds.start, bounds.end); setScale(bounds.start, bounds.end);
}); });
//Set initial scale. //Set initial scale.
setScale(conductor.bounds().start, conductor.bounds().end); var bounds = conductor.bounds();
setScale(bounds.start, bounds.end);
if (conductor.timeSystem() !== undefined) { if (conductor.timeSystem() !== undefined) {
changeTimeSystem(conductor.timeSystem()); changeTimeSystem(conductor.timeSystem());