+ assert.deepEqual(["10","10","10","10"], Util.getYLabels());
+
+ g.setSelection(1);
+ assert.equal('1: Y: 0', Util.getLegend());
+});
+
+it('testXAxisTimeLabelFormatter', function() {
+ var opts = {
+ width: 480,
+ height: 320,
+ labels: ['X', 'Y1']
+ };
+ var data = [[5.0,0],[5.1,1],[5.2,2],[5.3,3],[5.4,4],[5.5,5],[5.6,6],[5.7,7],[5.8,8],[5.9,9]];
+ var graph = document.getElementById("graph");
+ var g = new Dygraph(graph, data, opts);
+ g.updateOptions({
+ axes : {
+ x : {
+ axisLabelFormatter : function (totalMinutes) {
+ var hours = Math.floor( totalMinutes / 60);
+ var minutes = Math.floor((totalMinutes - (hours * 60)));
+ var seconds = Math.round((totalMinutes * 60) - (hours * 3600) - (minutes * 60));
+
+ if (hours < 10) hours = "0" + hours;
+ if (minutes < 10) minutes = "0" + minutes;
+ if (seconds < 10) seconds = "0" + seconds;
+
+ return hours + ':' + minutes + ':' + seconds;
+ }
+ }
+ }
+ });
+
+ assert.deepEqual(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], Util.getXLabels());
+
+ // The legend does not use the axisLabelFormatter:
+ g.setSelection(1);
+ assert.equal('5.1: Y1: 1', Util.getLegend());
+});
+
+it('testAxisLabelFormatter', function() {
+ var opts = {
+ width: 480,
+ height: 320,
+ axes : {
+ x : {
+ axisLabelFormatter : function(x, granularity, opts, dg) {
+ assert.equal('number', typeof(x));
+ assert.equal('number', typeof(granularity));
+ assert.equal('function', typeof(opts));
+ assert.equal('[Dygraph graph]', dg.toString());
+ return 'x' + x;
+ }
+ },
+ y : {
+ axisLabelFormatter : function(y, granularity, opts, dg) {
+ assert.equal('number', typeof(y));
+ assert.equal('number', typeof(granularity));
+ assert.equal('function', typeof(opts));
+ assert.equal('[Dygraph graph]', dg.toString());
+ return 'y' + y;
+ }
+ }
+ },
+ labels: ['x', 'y']
+ };
+ var data = [];
+ for (var i = 0; i < 10; i++) {
+ data.push([i, 2 * i]);
+ }
+ var graph = document.getElementById("graph");
+ var g = new Dygraph(graph, data, opts);
+
+ assert.deepEqual(['x0','x2','x4','x6','x8'], Util.getXLabels());
+ assert.deepEqual(["y0","y5","y10","y15"], Util.getYLabels());
+
+ g.setSelection(2);
+ assert.equal("2: y: 4", Util.getLegend());
+});
+
+it('testDateAxisLabelFormatter', function() {
+ var opts = {
+ width: 480,
+ height: 320,
+ axes : {
+ x : {
+ pixelsPerLabel: 60,
+ axisLabelFormatter : function(x, granularity, opts, dg) {
+ assert.isTrue(utils.isDateLike(x));
+ assert.equal('number', typeof(granularity));
+ assert.equal('function', typeof(opts));
+ assert.equal('[Dygraph graph]', dg.toString());
+ return 'x' + Util.formatDate(x);
+ }
+ },
+ y : {
+ axisLabelFormatter : function(y, granularity, opts, dg) {
+ assert.equal('number', typeof(y));
+ assert.equal('number', typeof(granularity));
+ assert.equal('function', typeof(opts));
+ assert.equal('[Dygraph graph]', dg.toString());
+ return 'y' + y;
+ }
+ }
+ },
+ labels: ['x', 'y']
+ };
+ var data = [];
+ for (var i = 1; i < 10; i++) {
+ data.push([new Date("2011/01/0" + i), 2 * i]);
+ }
+ var graph = document.getElementById("graph");
+ var g = new Dygraph(graph, data, opts);
+
+ assert.deepEqual(["x2011/01/02","x2011/01/04","x2011/01/06","x2011/01/08"], Util.getXLabels());
+ assert.deepEqual(["y5","y10","y15"], Util.getYLabels());
+
+ g.setSelection(0);
+ assert.equal("2011/01/01: y: 2", Util.getLegend());
+});
+
+// This test verifies that when a valueFormatter is set (but not an
+// axisLabelFormatter), then the valueFormatter is used to format the axis
+// labels.
+it('testValueFormatter', function() {
+ var opts = {
+ width: 480,
+ height: 320,
+ axes: {
+ x: {
+ valueFormatter: function(x, opts, series_name, dg, row, col) {
+ assert.equal('number', typeof(x));
+ assert.equal('function', typeof(opts));
+ assert.equal('string', typeof(series_name));
+ assert.equal('[Dygraph graph]', dg.toString());
+ assert.equal('number', typeof(row));
+ assert.equal('number', typeof(col));
+ assert.equal(dg, this);
+ return 'x' + x;
+ }
+ },
+ y: {
+ valueFormatter: function(y, opts, series_name, dg, row, col) {
+ assert.equal('number', typeof(y));
+ assert.equal('function', typeof(opts));
+ assert.equal('string', typeof(series_name));
+ assert.equal('[Dygraph graph]', dg.toString());
+ assert.equal('number', typeof(row));
+ assert.equal('number', typeof(col));
+ assert.equal(dg, this);
+ return 'y' + y;
+ }
+ }
+ },
+ labels: ['x', 'y']
+ };
+ var data = [];
+ for (var i = 0; i < 10; i++) {
+ data.push([i, 2 * i]);
+ }
+ var graph = document.getElementById("graph");
+ var g = new Dygraph(graph, data, opts);
+
+ // the valueFormatter options do not affect the ticks.
+ assert.deepEqual(['0','2','4','6','8'], Util.getXLabels());
+ assert.deepEqual(["0","5","10","15"],
+ Util.getYLabels());
+
+ // they do affect the legend, however.
+ g.setSelection(2);
+ assert.equal("x2: y: y4", Util.getLegend());
+});
+
+it('testDateValueFormatter', function() {
+ var opts = {
+ width: 480,
+ height: 320,
+ axes : {
+ x : {
+ pixelsPerLabel: 60,
+ valueFormatter: function(x, opts, series_name, dg, row, col) {
+ assert.equal('number', typeof(x));
+ assert.equal('function', typeof(opts));
+ assert.equal('string', typeof(series_name));
+ assert.equal('[Dygraph graph]', dg.toString());
+ assert.equal('number', typeof(row));
+ assert.equal('number', typeof(col));
+ assert.equal(dg, this);
+ return 'x' + Util.formatDate(x);
+ }
+ },
+ y : {
+ valueFormatter: function(y, opts, series_name, dg, row, col) {
+ assert.equal('number', typeof(y));
+ assert.equal('function', typeof(opts));
+ assert.equal('string', typeof(series_name));
+ assert.equal('[Dygraph graph]', dg.toString());
+ assert.equal('number', typeof(row));
+ assert.equal('number', typeof(col));
+ assert.equal(dg, this);
+ return 'y' + y;
+ }
+ }
+ },
+ labels: ['x', 'y']
+ };
+
+ var data = [];
+ for (var i = 1; i < 10; i++) {
+ data.push([new Date("2011/01/0" + i), 2 * i]);
+ }
+ var graph = document.getElementById("graph");
+ var g = new Dygraph(graph, data, opts);
+
+ // valueFormatters do not affect ticks.
+ assert.deepEqual(["02 Jan","04 Jan","06 Jan","08 Jan"], Util.getXLabels());
+ assert.deepEqual(["5","10","15"], Util.getYLabels());
+
+ // the valueFormatter options also affect the legend.
+ g.setSelection(2);
+ assert.equal('x2011/01/03: y: y6', Util.getLegend());
+});
+
+// This test verifies that when both a valueFormatter and an axisLabelFormatter
+// are specified, the axisLabelFormatter takes precedence.
+it('testAxisLabelFormatterPrecedence', function() {
+ var opts = {
+ width: 480,
+ height: 320,
+ axes : {
+ x : {
+ valueFormatter: function(x) {
+ assert.equal('[Dygraph graph]', this.toString());
+ return 'xvf' + x;
+ },
+ axisLabelFormatter: function(x, granularity) {
+ assert.equal('[Dygraph graph]', this.toString());
+ return 'x' + x;
+ }
+ },
+ y : {
+ valueFormatter: function(y) {
+ assert.equal('[Dygraph graph]', this.toString());
+ return 'yvf' + y;
+ },
+ axisLabelFormatter: function(y) {
+ assert.equal('[Dygraph graph]', this.toString());
+ return 'y' + y;
+ }
+ }
+ },
+ labels: ['x', 'y']
+ };
+ var data = [];
+ for (var i = 0; i < 10; i++) {
+ data.push([i, 2 * i]);
+ }
+ var graph = document.getElementById("graph");
+ var g = new Dygraph(graph, data, opts);
+
+ assert.deepEqual(['x0','x2','x4','x6','x8'], Util.getXLabels());
+ assert.deepEqual(["y0","y5","y10","y15"], Util.getYLabels());
+
+ g.setSelection(9);
+ assert.equal("xvf9: y: yvf18", Util.getLegend());
+});
+
+// This is the same as the previous test, except that options are added
+// one-by-one.
+it('testAxisLabelFormatterIncremental', function() {
+ var opts = {
+ width: 480,
+ height: 320,
+ labels: ['x', 'y']
+ };
+ var data = [];
+ for (var i = 0; i < 10; i++) {
+ data.push([i, 2 * i]);
+ }
+ var graph = document.getElementById("graph");
+ var g = new Dygraph(graph, data, opts);
+ g.updateOptions({
+ axes : {
+ x : {
+ valueFormatter: function(x) {
+ return 'xvf' + x;
+ }
+ }
+ }
+ });
+ g.updateOptions({
+ axes : {
+ y : {
+ valueFormatter: function(y) {
+ return 'yvf' + y;
+ }
+ }
+ }
+ });
+ g.updateOptions({
+ axes : {
+ x : {
+ axisLabelFormatter: function(x, granularity) {
+ return 'x' + x;
+ }
+ }
+ }
+ });
+ g.updateOptions({
+ axes : {
+ y : {
+ axisLabelFormatter: function(y) {
+ return 'y' + y;
+ }
+ }
+ }
+ });
+
+ assert.deepEqual(["x0","x2","x4","x6","x8"], Util.getXLabels());
+ assert.deepEqual(["y0","y5","y10","y15"], Util.getYLabels());
+
+ g.setSelection(9);
+ assert.equal("xvf9: y: yvf18", Util.getLegend());
+});
+
+it('testGlobalFormatters', function() {
+ var opts = {
+ width: 480,
+ height: 320,
+ labels: ['x', 'y'],
+ valueFormatter: function(x) {
+ assert.equal('[Dygraph graph]', this);
+ return 'vf' + x;
+ },
+ axisLabelFormatter: function(x) {
+ assert.equal('[Dygraph graph]', this);
+ return 'alf' + x;
+ }
+ };
+ var data = [];
+ for (var i = 0; i < 10; i++) {
+ data.push([i, 2 * i]);
+ }
+ var graph = document.getElementById("graph");
+ var g = new Dygraph(graph, data, opts);
+
+ assert.deepEqual(['alf0','alf2','alf4','alf6','alf8'], Util.getXLabels());
+ assert.deepEqual(["alf0","alf5","alf10","alf15"], Util.getYLabels());
+
+ g.setSelection(9);
+ assert.equal("vf9: y: vf18", Util.getLegend());
+});
+
+it('testValueFormatterParameters', function() {
+ var calls = [];
+ // change any functions in list to 'fn' -- functions can't be asserted.
+ var killFunctions = function(list) {
+ var out = [];
+ for (var i = 0; i < list.length; i++) {
+ if (typeof(list[i]) == 'function') {
+ out[i] = 'fn';
+ } else {
+ out[i] = list[i];
+ }
+ }
+ return out;
+ };
+ var taggedRecorder = function(tag) {
+ return function() {
+ calls.push([tag].concat([this], killFunctions(arguments)));
+ return '';
+ }
+ };
+ var opts = {
+ axes: {
+ x: { valueFormatter: taggedRecorder('x') },
+ y: { valueFormatter: taggedRecorder('y') },
+ y2: { valueFormatter: taggedRecorder('y2') }
+ },
+ series: {
+ 'y1': { axis: 'y1'},
+ 'y2': { axis: 'y2'}
+ },
+ labels: ['x', 'y1', 'y2']
+ };
+ var data = [
+ [0, 1, 2],
+ [1, 3, 4]
+ ];
+ var graph = document.getElementById('graph');
+ var g = new Dygraph(graph, data, opts);
+
+ assert.deepEqual([], calls);
+ g.setSelection(0);
+ assert.deepEqual([
+ // num or millis, opts, series, dygraph, row, col
+ [ 'x', g, 0, 'fn', 'x', g, 0, 0],
+ [ 'y', g, 1, 'fn', 'y1', g, 0, 1],
+ ['y2', g, 2, 'fn', 'y2', g, 0, 2]
+ ], calls);
+
+ calls = [];
+ g.setSelection(1);
+ assert.deepEqual([
+ [ 'x', g, 1, 'fn', 'x', g, 1, 0],
+ [ 'y', g, 3, 'fn', 'y1', g, 1, 1],
+ ['y2', g, 4, 'fn', 'y2', g, 1, 2]
+ ], calls);
+});
+
+it('testSeriesOrder', function() {
+ var opts = {
+ width: 480,
+ height: 320
+ };
+ var data = "x,00,01,10,11\n" +
+ "0,101,201,301,401\n" +
+ "1,102,202,302,402\n" +
+ "2,103,203,303,403\n" +
+ "3,104,204,304,404\n"
+ ;
+
+ var graph = document.getElementById("graph");
+ var g = new Dygraph(graph, data, opts);
+
+ g.setSelection(2);
+ assert.equal('2: 00: 103 01: 203 10: 303 11: 403', Util.getLegend());
+
+ // Sanity checks for indexFromSetName
+ assert.equal(0, g.indexFromSetName("x"));
+ assert.equal(1, g.indexFromSetName("00"));
+ assert.equal(null, g.indexFromSetName("abcde"));
+
+ // Verify that we get the label list back in the right order
+ assert.deepEqual(["x", "00", "01", "10", "11"], g.getLabels());
+});
+
+it('testLabelKMB', function() {
+ var data = [];
+ data.push([0,0]);
+ data.push([1,2000]);
+ data.push([2,1000]);
+
+ var g = new Dygraph(
+ document.getElementById("graph"),
+ data,
+ {
+ labels: [ 'X', 'bar' ],
+ axes : {
+ y: {
+ labelsKMB: true
+ }
+ }
+ }
+ );
+
+ assert.deepEqual(["0", "500", "1K", "1.5K", "2K"], Util.getYLabels());
+});
+
+it('testLabelKMG2', function() {
+ var data = [];
+ data.push([0,0]);
+ data.push([1,2000]);
+ data.push([2,1000]);
+
+ var g = new Dygraph(
+ document.getElementById("graph"),
+ data,
+ {
+ labels: [ 'X', 'bar' ],
+ axes : {
+ y: {
+ labelsKMG2: true