/*jshint sub:true */
/*global Dygraph:false */
-(function() {
"use strict";
+import * as utils from './dygraph-utils';
+
/** @typedef {Array.<{v:number, label:string, label_v:(string|undefined)}>} */
-Dygraph.TickList = undefined; // the ' = undefined' keeps jshint happy.
+var TickList = undefined; // the ' = undefined' keeps jshint happy.
/** @typedef {function(
* number,
* function(string):*,
* Dygraph=,
* Array.<number>=
- * ): Dygraph.TickList}
+ * ): TickList}
*/
-Dygraph.Ticker = undefined; // the ' = undefined' keeps jshint happy.
+var Ticker = undefined; // the ' = undefined' keeps jshint happy.
-/** @type {Dygraph.Ticker} */
-Dygraph.numericLinearTicks = function(a, b, pixels, opts, dygraph, vals) {
+/** @type {Ticker} */
+export var numericLinearTicks = function(a, b, pixels, opts, dygraph, vals) {
var nonLogscaleOpts = function(opt) {
if (opt === 'logscale') return false;
return opts(opt);
};
- return Dygraph.numericTicks(a, b, pixels, nonLogscaleOpts, dygraph, vals);
+ return numericTicks(a, b, pixels, nonLogscaleOpts, dygraph, vals);
};
-/** @type {Dygraph.Ticker} */
-Dygraph.numericTicks = function(a, b, pixels, opts, dygraph, vals) {
+/** @type {Ticker} */
+export var numericTicks = function(a, b, pixels, opts, dygraph, vals) {
var pixels_per_tick = /** @type{number} */(opts('pixelsPerLabel'));
var ticks = [];
var i, j, tickV, nTicks;
// TODO(danvk): factor this log-scale block out into a separate function.
if (opts("logscale")) {
nTicks = Math.floor(pixels / pixels_per_tick);
- var minIdx = Dygraph.binarySearch(a, Dygraph.PREFERRED_LOG_TICK_VALUES, 1);
- var maxIdx = Dygraph.binarySearch(b, Dygraph.PREFERRED_LOG_TICK_VALUES, -1);
+ var minIdx = utils.binarySearch(a, PREFERRED_LOG_TICK_VALUES, 1);
+ var maxIdx = utils.binarySearch(b, PREFERRED_LOG_TICK_VALUES, -1);
if (minIdx == -1) {
minIdx = 0;
}
if (maxIdx == -1) {
- maxIdx = Dygraph.PREFERRED_LOG_TICK_VALUES.length - 1;
+ maxIdx = PREFERRED_LOG_TICK_VALUES.length - 1;
}
// Count the number of tick values would appear, if we can get at least
// nTicks / 4 accept them.
var lastDisplayed = null;
if (maxIdx - minIdx >= nTicks / 4) {
for (var idx = maxIdx; idx >= minIdx; idx--) {
- var tickValue = Dygraph.PREFERRED_LOG_TICK_VALUES[idx];
+ var tickValue = PREFERRED_LOG_TICK_VALUES[idx];
var pixel_coord = Math.log(tickValue / a) / Math.log(b / a) * pixels;
var tick = { v: tickValue };
if (lastDisplayed === null) {
};
-/** @type {Dygraph.Ticker} */
-Dygraph.dateTicker = function(a, b, pixels, opts, dygraph, vals) {
- var chosen = Dygraph.pickDateTickGranularity(a, b, pixels, opts);
+/** @type {Ticker} */
+export var dateTicker = function(a, b, pixels, opts, dygraph, vals) {
+ var chosen = pickDateTickGranularity(a, b, pixels, opts);
if (chosen >= 0) {
- return Dygraph.getDateAxis(a, b, chosen, opts, dygraph);
+ return getDateAxis(a, b, chosen, opts, dygraph);
} else {
// this can happen if self.width_ is zero.
return [];
};
// Time granularity enumeration
-// TODO(danvk): make this an @enum
-Dygraph.SECONDLY = 0;
-Dygraph.TWO_SECONDLY = 1;
-Dygraph.FIVE_SECONDLY = 2;
-Dygraph.TEN_SECONDLY = 3;
-Dygraph.THIRTY_SECONDLY = 4;
-Dygraph.MINUTELY = 5;
-Dygraph.TWO_MINUTELY = 6;
-Dygraph.FIVE_MINUTELY = 7;
-Dygraph.TEN_MINUTELY = 8;
-Dygraph.THIRTY_MINUTELY = 9;
-Dygraph.HOURLY = 10;
-Dygraph.TWO_HOURLY = 11;
-Dygraph.SIX_HOURLY = 12;
-Dygraph.DAILY = 13;
-Dygraph.TWO_DAILY = 14;
-Dygraph.WEEKLY = 15;
-Dygraph.MONTHLY = 16;
-Dygraph.QUARTERLY = 17;
-Dygraph.BIANNUAL = 18;
-Dygraph.ANNUAL = 19;
-Dygraph.DECADAL = 20;
-Dygraph.CENTENNIAL = 21;
-Dygraph.NUM_GRANULARITIES = 22;
+export var Granularity = {
+ MILLISECONDLY: 0,
+ TWO_MILLISECONDLY: 1,
+ FIVE_MILLISECONDLY: 2,
+ TEN_MILLISECONDLY: 3,
+ FIFTY_MILLISECONDLY: 4,
+ HUNDRED_MILLISECONDLY: 5,
+ FIVE_HUNDRED_MILLISECONDLY: 6,
+ SECONDLY: 7,
+ TWO_SECONDLY: 8,
+ FIVE_SECONDLY: 9,
+ TEN_SECONDLY: 10,
+ THIRTY_SECONDLY: 11,
+ MINUTELY: 12,
+ TWO_MINUTELY: 13,
+ FIVE_MINUTELY: 14,
+ TEN_MINUTELY: 15,
+ THIRTY_MINUTELY: 16,
+ HOURLY: 17,
+ TWO_HOURLY: 18,
+ SIX_HOURLY: 19,
+ DAILY: 20,
+ TWO_DAILY: 21,
+ WEEKLY: 22,
+ MONTHLY: 23,
+ QUARTERLY: 24,
+ BIANNUAL: 25,
+ ANNUAL: 26,
+ DECADAL: 27,
+ CENTENNIAL: 28,
+ NUM_GRANULARITIES: 29
+}
// Date components enumeration (in the order of the arguments in Date)
// TODO: make this an @enum
-Dygraph.DATEFIELD_Y = 0;
-Dygraph.DATEFIELD_M = 1;
-Dygraph.DATEFIELD_D = 2;
-Dygraph.DATEFIELD_HH = 3;
-Dygraph.DATEFIELD_MM = 4;
-Dygraph.DATEFIELD_SS = 5;
-Dygraph.DATEFIELD_MS = 6;
-Dygraph.NUM_DATEFIELDS = 7;
+var DateField = {
+ DATEFIELD_Y: 0,
+ DATEFIELD_M: 1,
+ DATEFIELD_D: 2,
+ DATEFIELD_HH: 3,
+ DATEFIELD_MM: 4,
+ DATEFIELD_SS: 5,
+ DATEFIELD_MS: 6,
+ NUM_DATEFIELDS: 7
+};
/**
*
* @type {Array.<{datefield:number, step:number, spacing:number}>}
*/
-Dygraph.TICK_PLACEMENT = [];
-Dygraph.TICK_PLACEMENT[Dygraph.SECONDLY] = {datefield: Dygraph.DATEFIELD_SS, step: 1, spacing: 1000 * 1};
-Dygraph.TICK_PLACEMENT[Dygraph.TWO_SECONDLY] = {datefield: Dygraph.DATEFIELD_SS, step: 2, spacing: 1000 * 2};
-Dygraph.TICK_PLACEMENT[Dygraph.FIVE_SECONDLY] = {datefield: Dygraph.DATEFIELD_SS, step: 5, spacing: 1000 * 5};
-Dygraph.TICK_PLACEMENT[Dygraph.TEN_SECONDLY] = {datefield: Dygraph.DATEFIELD_SS, step: 10, spacing: 1000 * 10};
-Dygraph.TICK_PLACEMENT[Dygraph.THIRTY_SECONDLY] = {datefield: Dygraph.DATEFIELD_SS, step: 30, spacing: 1000 * 30};
-Dygraph.TICK_PLACEMENT[Dygraph.MINUTELY] = {datefield: Dygraph.DATEFIELD_MM, step: 1, spacing: 1000 * 60};
-Dygraph.TICK_PLACEMENT[Dygraph.TWO_MINUTELY] = {datefield: Dygraph.DATEFIELD_MM, step: 2, spacing: 1000 * 60 * 2};
-Dygraph.TICK_PLACEMENT[Dygraph.FIVE_MINUTELY] = {datefield: Dygraph.DATEFIELD_MM, step: 5, spacing: 1000 * 60 * 5};
-Dygraph.TICK_PLACEMENT[Dygraph.TEN_MINUTELY] = {datefield: Dygraph.DATEFIELD_MM, step: 10, spacing: 1000 * 60 * 10};
-Dygraph.TICK_PLACEMENT[Dygraph.THIRTY_MINUTELY] = {datefield: Dygraph.DATEFIELD_MM, step: 30, spacing: 1000 * 60 * 30};
-Dygraph.TICK_PLACEMENT[Dygraph.HOURLY] = {datefield: Dygraph.DATEFIELD_HH, step: 1, spacing: 1000 * 3600};
-Dygraph.TICK_PLACEMENT[Dygraph.TWO_HOURLY] = {datefield: Dygraph.DATEFIELD_HH, step: 2, spacing: 1000 * 3600 * 2};
-Dygraph.TICK_PLACEMENT[Dygraph.SIX_HOURLY] = {datefield: Dygraph.DATEFIELD_HH, step: 6, spacing: 1000 * 3600 * 6};
-Dygraph.TICK_PLACEMENT[Dygraph.DAILY] = {datefield: Dygraph.DATEFIELD_D, step: 1, spacing: 1000 * 86400};
-Dygraph.TICK_PLACEMENT[Dygraph.TWO_DAILY] = {datefield: Dygraph.DATEFIELD_D, step: 2, spacing: 1000 * 86400 * 2};
-Dygraph.TICK_PLACEMENT[Dygraph.WEEKLY] = {datefield: Dygraph.DATEFIELD_D, step: 7, spacing: 1000 * 604800};
-Dygraph.TICK_PLACEMENT[Dygraph.MONTHLY] = {datefield: Dygraph.DATEFIELD_M, step: 1, spacing: 1000 * 7200 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 / 12
-Dygraph.TICK_PLACEMENT[Dygraph.QUARTERLY] = {datefield: Dygraph.DATEFIELD_M, step: 3, spacing: 1000 * 21600 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 / 4
-Dygraph.TICK_PLACEMENT[Dygraph.BIANNUAL] = {datefield: Dygraph.DATEFIELD_M, step: 6, spacing: 1000 * 43200 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 / 2
-Dygraph.TICK_PLACEMENT[Dygraph.ANNUAL] = {datefield: Dygraph.DATEFIELD_Y, step: 1, spacing: 1000 * 86400 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 * 1
-Dygraph.TICK_PLACEMENT[Dygraph.DECADAL] = {datefield: Dygraph.DATEFIELD_Y, step: 10, spacing: 1000 * 864000 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 * 10
-Dygraph.TICK_PLACEMENT[Dygraph.CENTENNIAL] = {datefield: Dygraph.DATEFIELD_Y, step: 100, spacing: 1000 * 8640000 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 * 100
+var TICK_PLACEMENT = [];
+TICK_PLACEMENT[Granularity.MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 1, spacing: 1};
+TICK_PLACEMENT[Granularity.TWO_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 2, spacing: 2};
+TICK_PLACEMENT[Granularity.FIVE_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 5, spacing: 5};
+TICK_PLACEMENT[Granularity.TEN_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 10, spacing: 10};
+TICK_PLACEMENT[Granularity.FIFTY_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 50, spacing: 50};
+TICK_PLACEMENT[Granularity.HUNDRED_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 100, spacing: 100};
+TICK_PLACEMENT[Granularity.FIVE_HUNDRED_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 500, spacing: 500};
+TICK_PLACEMENT[Granularity.SECONDLY] = {datefield: DateField.DATEFIELD_SS, step: 1, spacing: 1000 * 1};
+TICK_PLACEMENT[Granularity.TWO_SECONDLY] = {datefield: DateField.DATEFIELD_SS, step: 2, spacing: 1000 * 2};
+TICK_PLACEMENT[Granularity.FIVE_SECONDLY] = {datefield: DateField.DATEFIELD_SS, step: 5, spacing: 1000 * 5};
+TICK_PLACEMENT[Granularity.TEN_SECONDLY] = {datefield: DateField.DATEFIELD_SS, step: 10, spacing: 1000 * 10};
+TICK_PLACEMENT[Granularity.THIRTY_SECONDLY] = {datefield: DateField.DATEFIELD_SS, step: 30, spacing: 1000 * 30};
+TICK_PLACEMENT[Granularity.MINUTELY] = {datefield: DateField.DATEFIELD_MM, step: 1, spacing: 1000 * 60};
+TICK_PLACEMENT[Granularity.TWO_MINUTELY] = {datefield: DateField.DATEFIELD_MM, step: 2, spacing: 1000 * 60 * 2};
+TICK_PLACEMENT[Granularity.FIVE_MINUTELY] = {datefield: DateField.DATEFIELD_MM, step: 5, spacing: 1000 * 60 * 5};
+TICK_PLACEMENT[Granularity.TEN_MINUTELY] = {datefield: DateField.DATEFIELD_MM, step: 10, spacing: 1000 * 60 * 10};
+TICK_PLACEMENT[Granularity.THIRTY_MINUTELY] = {datefield: DateField.DATEFIELD_MM, step: 30, spacing: 1000 * 60 * 30};
+TICK_PLACEMENT[Granularity.HOURLY] = {datefield: DateField.DATEFIELD_HH, step: 1, spacing: 1000 * 3600};
+TICK_PLACEMENT[Granularity.TWO_HOURLY] = {datefield: DateField.DATEFIELD_HH, step: 2, spacing: 1000 * 3600 * 2};
+TICK_PLACEMENT[Granularity.SIX_HOURLY] = {datefield: DateField.DATEFIELD_HH, step: 6, spacing: 1000 * 3600 * 6};
+TICK_PLACEMENT[Granularity.DAILY] = {datefield: DateField.DATEFIELD_D, step: 1, spacing: 1000 * 86400};
+TICK_PLACEMENT[Granularity.TWO_DAILY] = {datefield: DateField.DATEFIELD_D, step: 2, spacing: 1000 * 86400 * 2};
+TICK_PLACEMENT[Granularity.WEEKLY] = {datefield: DateField.DATEFIELD_D, step: 7, spacing: 1000 * 604800};
+TICK_PLACEMENT[Granularity.MONTHLY] = {datefield: DateField.DATEFIELD_M, step: 1, spacing: 1000 * 7200 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 / 12
+TICK_PLACEMENT[Granularity.QUARTERLY] = {datefield: DateField.DATEFIELD_M, step: 3, spacing: 1000 * 21600 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 / 4
+TICK_PLACEMENT[Granularity.BIANNUAL] = {datefield: DateField.DATEFIELD_M, step: 6, spacing: 1000 * 43200 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 / 2
+TICK_PLACEMENT[Granularity.ANNUAL] = {datefield: DateField.DATEFIELD_Y, step: 1, spacing: 1000 * 86400 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 * 1
+TICK_PLACEMENT[Granularity.DECADAL] = {datefield: DateField.DATEFIELD_Y, step: 10, spacing: 1000 * 864000 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 * 10
+TICK_PLACEMENT[Granularity.CENTENNIAL] = {datefield: DateField.DATEFIELD_Y, step: 100, spacing: 1000 * 8640000 * 365.2524}; // 1e3 * 60 * 60 * 24 * 365.2524 * 100
/**
* This is a list of human-friendly values at which to show tick marks on a log
* scale. It is k * 10^n, where k=1..9 and n=-39..+39, so:
* ..., 1, 2, 3, 4, 5, ..., 9, 10, 20, 30, ..., 90, 100, 200, 300, ...
- * NOTE: this assumes that Dygraph.LOG_SCALE = 10.
+ * NOTE: this assumes that utils.LOG_SCALE = 10.
* @type {Array.<number>}
*/
-Dygraph.PREFERRED_LOG_TICK_VALUES = (function() {
+var PREFERRED_LOG_TICK_VALUES = (function() {
var vals = [];
for (var power = -39; power <= 39; power++) {
var range = Math.pow(10, power);
* @return {number} The appropriate axis granularity for this chart. See the
* enumeration of possible values in dygraph-tickers.js.
*/
-Dygraph.pickDateTickGranularity = function(a, b, pixels, opts) {
+var pickDateTickGranularity = function(a, b, pixels, opts) {
var pixels_per_tick = /** @type{number} */(opts('pixelsPerLabel'));
- for (var i = 0; i < Dygraph.NUM_GRANULARITIES; i++) {
- var num_ticks = Dygraph.numDateTicks(a, b, i);
+ for (var i = 0; i < Granularity.NUM_GRANULARITIES; i++) {
+ var num_ticks = numDateTicks(a, b, i);
if (pixels / num_ticks >= pixels_per_tick) {
return i;
}
* @param {number} granularity (one of the granularities enumerated above)
* @return {number} (Approximate) number of ticks that would result.
*/
-Dygraph.numDateTicks = function(start_time, end_time, granularity) {
- var spacing = Dygraph.TICK_PLACEMENT[granularity].spacing;
+var numDateTicks = function(start_time, end_time, granularity) {
+ var spacing = TICK_PLACEMENT[granularity].spacing;
return Math.round(1.0 * (end_time - start_time) / spacing);
};
* @param {number} granularity (one of the granularities enumerated above)
* @param {function(string):*} opts Function mapping from option name -> value.
* @param {Dygraph=} dg
- * @return {!Dygraph.TickList}
+ * @return {!TickList}
*/
-Dygraph.getDateAxis = function(start_time, end_time, granularity, opts, dg) {
+export var getDateAxis = function(start_time, end_time, granularity, opts, dg) {
var formatter = /** @type{AxisLabelFormatter} */(
opts("axisLabelFormatter"));
var utc = opts("labelsUTC");
- var accessors = utc ? Dygraph.DateAccessorsUTC : Dygraph.DateAccessorsLocal;
+ var accessors = utc ? utils.DateAccessorsUTC : utils.DateAccessorsLocal;
- var datefield = Dygraph.TICK_PLACEMENT[granularity].datefield;
- var step = Dygraph.TICK_PLACEMENT[granularity].step;
- var spacing = Dygraph.TICK_PLACEMENT[granularity].spacing;
+ var datefield = TICK_PLACEMENT[granularity].datefield;
+ var step = TICK_PLACEMENT[granularity].step;
+ var spacing = TICK_PLACEMENT[granularity].spacing;
// Choose a nice tick position before the initial instant.
// Currently, this code deals properly with the existent daily granularities:
// by setting the start_date_offset to 0.
var start_date = new Date(start_time);
var date_array = [];
- date_array[Dygraph.DATEFIELD_Y] = accessors.getFullYear(start_date);
- date_array[Dygraph.DATEFIELD_M] = accessors.getMonth(start_date);
- date_array[Dygraph.DATEFIELD_D] = accessors.getDate(start_date);
- date_array[Dygraph.DATEFIELD_HH] = accessors.getHours(start_date);
- date_array[Dygraph.DATEFIELD_MM] = accessors.getMinutes(start_date);
- date_array[Dygraph.DATEFIELD_SS] = accessors.getSeconds(start_date);
- date_array[Dygraph.DATEFIELD_MS] = accessors.getMilliseconds(start_date);
+ date_array[DateField.DATEFIELD_Y] = accessors.getFullYear(start_date);
+ date_array[DateField.DATEFIELD_M] = accessors.getMonth(start_date);
+ date_array[DateField.DATEFIELD_D] = accessors.getDate(start_date);
+ date_array[DateField.DATEFIELD_HH] = accessors.getHours(start_date);
+ date_array[DateField.DATEFIELD_MM] = accessors.getMinutes(start_date);
+ date_array[DateField.DATEFIELD_SS] = accessors.getSeconds(start_date);
+ date_array[DateField.DATEFIELD_MS] = accessors.getMilliseconds(start_date);
var start_date_offset = date_array[datefield] % step;
- if (granularity == Dygraph.WEEKLY) {
+ if (granularity == Granularity.WEEKLY) {
// This will put the ticks on Sundays.
start_date_offset = accessors.getDay(start_date);
}
date_array[datefield] -= start_date_offset;
- for (var df = datefield + 1; df < Dygraph.NUM_DATEFIELDS; df++) {
+ for (var df = datefield + 1; df < DateField.NUM_DATEFIELDS; df++) {
// The minimum value is 1 for the day of month, and 0 for all other fields.
- date_array[df] = (df === Dygraph.DATEFIELD_D) ? 1 : 0;
+ date_array[df] = (df === DateField.DATEFIELD_D) ? 1 : 0;
}
// Generate the ticks.
var ticks = [];
var tick_date = accessors.makeDate.apply(null, date_array);
var tick_time = tick_date.getTime();
- if (granularity <= Dygraph.HOURLY) {
+ if (granularity <= Granularity.HOURLY) {
if (tick_time < start_time) {
tick_time += spacing;
tick_date = new Date(tick_time);
tick_time = tick_date.getTime();
}
while (tick_time <= end_time) {
- if (granularity >= Dygraph.DAILY ||
+ if (granularity >= Granularity.DAILY ||
accessors.getHours(tick_date) % step === 0) {
ticks.push({ v: tick_time,
label: formatter.call(dg, tick_date, granularity, opts, dg)
}
return ticks;
};
-
-// These are set here so that this file can be included after dygraph.js
-// or independently.
-if (Dygraph &&
- Dygraph.DEFAULT_ATTRS &&
- Dygraph.DEFAULT_ATTRS['axes'] &&
- Dygraph.DEFAULT_ATTRS['axes']['x'] &&
- Dygraph.DEFAULT_ATTRS['axes']['y'] &&
- Dygraph.DEFAULT_ATTRS['axes']['y2']) {
- Dygraph.DEFAULT_ATTRS['axes']['x']['ticker'] = Dygraph.dateTicker;
- Dygraph.DEFAULT_ATTRS['axes']['y']['ticker'] = Dygraph.numericTicks;
- Dygraph.DEFAULT_ATTRS['axes']['y2']['ticker'] = Dygraph.numericTicks;
-}
-
-})();