2 * @fileoverview Test cases for how axis labels are chosen and formatted.
4 * @author dan@dygraphs.com (Dan Vanderkam)
7 import Dygraph from
'../../src/dygraph';
8 import * as utils from
'../../src/dygraph-utils';
9 import DEFAULT_ATTRS from
'../../src/dygraph-default-attrs';
10 import Util from
'./Util';
11 import {assertDeepCloseTo
} from
'./custom_asserts';
13 describe("axis-labels", function() {
24 var kCloseFloat
= 1.0e-10;
26 it('testMinusOneToOne', function() {
38 var graph
= document
.getElementById("graph");
39 var g
= new Dygraph(graph
, data
, opts
);
41 // TODO(danvk): would ['-1.0','-0.5','0.0','0.5','1.0'] be better?
42 assert
.deepEqual(['-1','-0.5','0','0.5','1'], Util
.getYLabels());
46 g
.updateOptions({file
: data
});
47 assert
.deepEqual(['-1','-0.5','0','0.5','1','1.5','2'], Util
.getYLabels());
51 g
.updateOptions({file
: data
});
52 assert
.deepEqual(['-2','0','2','4','6','8','10'], Util
.getYLabels());
56 g
.updateOptions({file
: data
});
57 assert
.deepEqual(['0','20','40','60','80','100'], Util
.getYLabels());
60 assert
.equal('0: Y: -1', Util
.getLegend());
63 it('testSmallRangeNearZero', function() {
75 opts
.valueRange
= [-0.1, 0.1];
77 var graph
= document
.getElementById("graph");
78 var g
= new Dygraph(graph
, data
, opts
);
79 assertDeepCloseTo([-0.1,-0.05,0,0.05],
80 Util
.makeNumbers(Util
.getYLabels()), kCloseFloat
);
82 opts
.valueRange
= [-0.05, 0.05];
83 g
.updateOptions(opts
);
84 assert
.deepEqual([-0.04,-0.02,0,0.02,0.04],
85 Util
.makeNumbers(Util
.getYLabels()));
87 opts
.valueRange
= [-0.01, 0.01];
88 g
.updateOptions(opts
);
89 assert
.deepEqual([-0.01,-0.005,0,0.005],
90 Util
.makeNumbers(Util
.getYLabels()));
93 assert
.equal('1: Y: 0', Util
.getLegend());
96 it('testSmallRangeAwayFromZero', function() {
107 var graph
= document
.getElementById("graph");
109 opts
.valueRange
= [9.9, 10.1];
110 var g
= new Dygraph(graph
, data
, opts
);
111 assert
.deepEqual(["9.9","9.92","9.94","9.96","9.98","10","10.02","10.04","10.06","10.08"], Util
.getYLabels());
113 opts
.valueRange
= [9.99, 10.01];
114 g
.updateOptions(opts
);
115 // TODO(danvk): this is bad
116 assert
.deepEqual(["9.99","9.99","9.99","10","10","10","10","10","10.01","10.01"], Util
.getYLabels());
118 opts
.valueRange
= [9.999, 10.001];
119 g
.updateOptions(opts
);
120 // TODO(danvk): this is even worse!
121 assert
.deepEqual(["10","10","10","10"], Util
.getYLabels());
124 assert
.equal('1: Y: 0', Util
.getLegend());
127 it('testXAxisTimeLabelFormatter', function() {
133 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]];
134 var graph
= document
.getElementById("graph");
135 var g
= new Dygraph(graph
, data
, opts
);
139 axisLabelFormatter
: function (totalMinutes
) {
140 var hours
= Math
.floor( totalMinutes
/ 60);
141 var minutes
= Math
.floor((totalMinutes
- (hours
* 60)));
142 var seconds
= Math
.round((totalMinutes
* 60) - (hours
* 3600) - (minutes
* 60));
144 if (hours
< 10) hours
= "0" + hours
;
145 if (minutes
< 10) minutes
= "0" + minutes
;
146 if (seconds
< 10) seconds
= "0" + seconds
;
148 return hours
+ ':' + minutes
+ ':' + seconds
;
154 assert
.deepEqual(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], Util
.getXLabels());
156 // The legend does not use the axisLabelFormatter:
158 assert
.equal('5.1: Y1: 1', Util
.getLegend());
161 it('testAxisLabelFormatter', function() {
167 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
168 assert
.equal('number', typeof(x
));
169 assert
.equal('number', typeof(granularity
));
170 assert
.equal('function', typeof(opts
));
171 assert
.equal('[Dygraph graph]', dg
.toString());
176 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
177 assert
.equal('number', typeof(y
));
178 assert
.equal('number', typeof(granularity
));
179 assert
.equal('function', typeof(opts
));
180 assert
.equal('[Dygraph graph]', dg
.toString());
188 for (var i
= 0; i
< 10; i
++) {
189 data
.push([i
, 2 * i
]);
191 var graph
= document
.getElementById("graph");
192 var g
= new Dygraph(graph
, data
, opts
);
194 assert
.deepEqual(['x0','x2','x4','x6','x8'], Util
.getXLabels());
195 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
198 assert
.equal("2: y: 4", Util
.getLegend());
201 it('testDateAxisLabelFormatter', function() {
208 axisLabelFormatter
: function(x
, granularity
, opts
, dg
) {
209 assert
.isTrue(utils
.isDateLike(x
));
210 assert
.equal('number', typeof(granularity
));
211 assert
.equal('function', typeof(opts
));
212 assert
.equal('[Dygraph graph]', dg
.toString());
213 return 'x' + Util
.formatDate(x
);
217 axisLabelFormatter
: function(y
, granularity
, opts
, dg
) {
218 assert
.equal('number', typeof(y
));
219 assert
.equal('number', typeof(granularity
));
220 assert
.equal('function', typeof(opts
));
221 assert
.equal('[Dygraph graph]', dg
.toString());
229 for (var i
= 1; i
< 10; i
++) {
230 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
232 var graph
= document
.getElementById("graph");
233 var g
= new Dygraph(graph
, data
, opts
);
235 assert
.deepEqual(["x2011/01/02","x2011/01/04","x2011/01/06","x2011/01/08"], Util
.getXLabels());
236 assert
.deepEqual(["y5","y10","y15"], Util
.getYLabels());
239 assert
.equal("2011/01/01: y: 2", Util
.getLegend());
242 // This test verifies that when a valueFormatter is set (but not an
243 // axisLabelFormatter), then the valueFormatter is used to format the axis
245 it('testValueFormatter', function() {
251 valueFormatter
: function(x
, opts
, series_name
, dg
, row
, col
) {
252 assert
.equal('number', typeof(x
));
253 assert
.equal('function', typeof(opts
));
254 assert
.equal('string', typeof(series_name
));
255 assert
.equal('[Dygraph graph]', dg
.toString());
256 assert
.equal('number', typeof(row
));
257 assert
.equal('number', typeof(col
));
258 assert
.equal(dg
, this);
263 valueFormatter
: function(y
, opts
, series_name
, dg
, row
, col
) {
264 assert
.equal('number', typeof(y
));
265 assert
.equal('function', typeof(opts
));
266 assert
.equal('string', typeof(series_name
));
267 assert
.equal('[Dygraph graph]', dg
.toString());
268 assert
.equal('number', typeof(row
));
269 assert
.equal('number', typeof(col
));
270 assert
.equal(dg
, this);
278 for (var i
= 0; i
< 10; i
++) {
279 data
.push([i
, 2 * i
]);
281 var graph
= document
.getElementById("graph");
282 var g
= new Dygraph(graph
, data
, opts
);
284 // the valueFormatter options do not affect the ticks.
285 assert
.deepEqual(['0','2','4','6','8'], Util
.getXLabels());
286 assert
.deepEqual(["0","5","10","15"],
289 // they do affect the legend, however.
291 assert
.equal("x2: y: y4", Util
.getLegend());
294 it('testDateValueFormatter', function() {
301 valueFormatter
: function(x
, opts
, series_name
, dg
, row
, col
) {
302 assert
.equal('number', typeof(x
));
303 assert
.equal('function', typeof(opts
));
304 assert
.equal('string', typeof(series_name
));
305 assert
.equal('[Dygraph graph]', dg
.toString());
306 assert
.equal('number', typeof(row
));
307 assert
.equal('number', typeof(col
));
308 assert
.equal(dg
, this);
309 return 'x' + Util
.formatDate(x
);
313 valueFormatter
: function(y
, opts
, series_name
, dg
, row
, col
) {
314 assert
.equal('number', typeof(y
));
315 assert
.equal('function', typeof(opts
));
316 assert
.equal('string', typeof(series_name
));
317 assert
.equal('[Dygraph graph]', dg
.toString());
318 assert
.equal('number', typeof(row
));
319 assert
.equal('number', typeof(col
));
320 assert
.equal(dg
, this);
329 for (var i
= 1; i
< 10; i
++) {
330 data
.push([new Date("2011/01/0" + i
), 2 * i
]);
332 var graph
= document
.getElementById("graph");
333 var g
= new Dygraph(graph
, data
, opts
);
335 // valueFormatters do not affect ticks.
336 assert
.deepEqual(["02 Jan","04 Jan","06 Jan","08 Jan"], Util
.getXLabels());
337 assert
.deepEqual(["5","10","15"], Util
.getYLabels());
339 // the valueFormatter options also affect the legend.
341 assert
.equal('x2011/01/03: y: y6', Util
.getLegend());
344 // This test verifies that when both a valueFormatter and an axisLabelFormatter
345 // are specified, the axisLabelFormatter takes precedence.
346 it('testAxisLabelFormatterPrecedence', function() {
352 valueFormatter
: function(x
) {
353 assert
.equal('[Dygraph graph]', this.toString());
356 axisLabelFormatter
: function(x
, granularity
) {
357 assert
.equal('[Dygraph graph]', this.toString());
362 valueFormatter
: function(y
) {
363 assert
.equal('[Dygraph graph]', this.toString());
366 axisLabelFormatter
: function(y
) {
367 assert
.equal('[Dygraph graph]', this.toString());
375 for (var i
= 0; i
< 10; i
++) {
376 data
.push([i
, 2 * i
]);
378 var graph
= document
.getElementById("graph");
379 var g
= new Dygraph(graph
, data
, opts
);
381 assert
.deepEqual(['x0','x2','x4','x6','x8'], Util
.getXLabels());
382 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
385 assert
.equal("xvf9: y: yvf18", Util
.getLegend());
388 // This is the same as the previous test, except that options are added
390 it('testAxisLabelFormatterIncremental', function() {
397 for (var i
= 0; i
< 10; i
++) {
398 data
.push([i
, 2 * i
]);
400 var graph
= document
.getElementById("graph");
401 var g
= new Dygraph(graph
, data
, opts
);
405 valueFormatter
: function(x
) {
414 valueFormatter
: function(y
) {
423 axisLabelFormatter
: function(x
, granularity
) {
432 axisLabelFormatter
: function(y
) {
439 assert
.deepEqual(["x0","x2","x4","x6","x8"], Util
.getXLabels());
440 assert
.deepEqual(["y0","y5","y10","y15"], Util
.getYLabels());
443 assert
.equal("xvf9: y: yvf18", Util
.getLegend());
446 it('testGlobalFormatters', function() {
451 valueFormatter
: function(x
) {
452 assert
.equal('[Dygraph graph]', this);
455 axisLabelFormatter
: function(x
) {
456 assert
.equal('[Dygraph graph]', this);
461 for (var i
= 0; i
< 10; i
++) {
462 data
.push([i
, 2 * i
]);
464 var graph
= document
.getElementById("graph");
465 var g
= new Dygraph(graph
, data
, opts
);
467 assert
.deepEqual(['alf0','alf2','alf4','alf6','alf8'], Util
.getXLabels());
468 assert
.deepEqual(["alf0","alf5","alf10","alf15"], Util
.getYLabels());
471 assert
.equal("vf9: y: vf18", Util
.getLegend());
474 it('testValueFormatterParameters', function() {
476 // change any functions in list to 'fn' -- functions can't be asserted.
477 var killFunctions
= function(list
) {
479 for (var i
= 0; i
< list
.length
; i
++) {
480 if (typeof(list
[i
]) == 'function') {
488 var taggedRecorder
= function(tag
) {
490 calls
.push([tag
].concat([this], killFunctions(arguments
)));
496 x
: { valueFormatter
: taggedRecorder('x') },
497 y
: { valueFormatter
: taggedRecorder('y') },
498 y2
: { valueFormatter
: taggedRecorder('y2') }
504 labels
: ['x', 'y1', 'y2']
510 var graph
= document
.getElementById('graph');
511 var g
= new Dygraph(graph
, data
, opts
);
513 assert
.deepEqual([], calls
);
516 // num or millis, opts, series, dygraph, row, col
517 [ 'x', g
, 0, 'fn', 'x', g
, 0, 0],
518 [ 'y', g
, 1, 'fn', 'y1', g
, 0, 1],
519 ['y2', g
, 2, 'fn', 'y2', g
, 0, 2]
525 [ 'x', g
, 1, 'fn', 'x', g
, 1, 0],
526 [ 'y', g
, 3, 'fn', 'y1', g
, 1, 1],
527 ['y2', g
, 4, 'fn', 'y2', g
, 1, 2]
531 it('testSeriesOrder', function() {
536 var data
= "x,00,01,10,11\n" +
537 "0,101,201,301,401\n" +
538 "1,102,202,302,402\n" +
539 "2,103,203,303,403\n" +
540 "3,104,204,304,404\n"
543 var graph
= document
.getElementById("graph");
544 var g
= new Dygraph(graph
, data
, opts
);
547 assert
.equal('2: 00: 103 01: 203 10: 303 11: 403', Util
.getLegend());
549 // Sanity checks for indexFromSetName
550 assert
.equal(0, g
.indexFromSetName("x"));
551 assert
.equal(1, g
.indexFromSetName("00"));
552 assert
.equal(null, g
.indexFromSetName("abcde"));
554 // Verify that we get the label list back in the right order
555 assert
.deepEqual(["x", "00", "01", "10", "11"], g
.getLabels());
558 it('testLabelKMB', function() {
565 document
.getElementById("graph"),
568 labels
: [ 'X', 'bar' ],
577 assert
.deepEqual(["0", "500", "1K", "1.5K", "2K"], Util
.getYLabels());
580 it('testLabelKMG2', function() {
587 document
.getElementById("graph"),
590 labels
: [ 'X', 'bar' ],
599 assert
.deepEqual(["0","256","512","768","1k","1.25k","1.5k","1.75k","2k"],
603 // Same as testLabelKMG2 but specifies the option at the
604 // top of the option dictionary.
605 it('testLabelKMG2_top', function() {
612 document
.getElementById("graph"),
615 labels
: [ 'X', 'bar' ],
621 ["0","256","512","768","1k","1.25k","1.5k","1.75k","2k"],
625 it('testSmallLabelKMB', function() {
628 data
.push([1, 1e-6]);
629 data
.push([2, 2e-6]);
632 document
.getElementById("graph"),
635 labels
: [ 'X', 'bar' ],
644 // TODO(danvk): use prefixes here (e.g. m, µ, n)
645 assert
.deepEqual(['0', '5.00e-7', '1.00e-6', '1.50e-6', '2.00e-6'],
649 it('testSmallLabelKMG2', function() {
652 data
.push([1, 1e-6]);
653 data
.push([2, 2e-6]);
656 document
.getElementById("graph"),
659 labels
: [ 'X', 'bar' ],
668 // TODO(danvk): this is strange--the values aren't on powers of two, and are
669 // these units really used for powers of two in <1? See issue #571.
670 assert
.deepEqual(['0', '0.48u', '0.95u', '1.43u', '1.91u'],
675 * Verify that log scale axis range is properly specified.
677 it('testLogScale', function() {
678 var g
= new Dygraph("graph",
679 [[0, 5], [1, 1000]], {
683 var nonEmptyLabels
= Util
.getYLabels().filter(function(x
) { return x
.length
> 0; });
684 assert
.deepEqual(["5","10","20","50","100","200","500","1000"], nonEmptyLabels
);
686 g
.updateOptions({ logscale
: false });
687 assert
.deepEqual(['0','200','400','600','800','1000'], Util
.getYLabels());
691 * Verify that include zero range is properly specified.
693 it('testIncludeZero', function() {
694 var g
= new Dygraph("graph",
695 [[0, 500], [1, 1000]], {
699 assert
.deepEqual(['0','200','400','600','800','1000'], Util
.getYLabels());
701 g
.updateOptions({ includeZero
: false });
702 assert
.deepEqual(['500','600','700','800','900','1000'], Util
.getYLabels());
705 it('testAxisLabelFontSize', function() {
706 var graph
= document
.getElementById("graph");
707 var g
= new Dygraph(graph
, simpleData
, {});
709 // Be sure we're dealing with a 14-point default.
710 assert
.equal(14, DEFAULT_ATTRS
.axisLabelFontSize
);
712 var assertFontSize
= function(selector
, expected
) {
713 Util
.assertStyleOfChildren(selector
, "font-size", expected
);
716 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "14px");
717 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "14px");
719 g
.updateOptions({axisLabelFontSize
: 8});
720 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "8px");
721 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "8px");
724 axisLabelFontSize
: null,
726 x
: { axisLabelFontSize
: 5 },
730 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "5px");
731 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "14px");
735 y
: { axisLabelFontSize
: 20 },
739 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "5px");
740 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "20px");
744 Y2
: { axis
: "y2" } // copy y2 series to y2 axis.
747 y2
: { axisLabelFontSize
: 12 },
751 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "5px");
752 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y1"), "20px");
753 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y2"), "12px");
756 it('testAxisLabelFontSizeNull', function() {
757 var graph
= document
.getElementById("graph");
758 var g
= new Dygraph(graph
, simpleData
,
760 axisLabelFontSize
: null
763 var assertFontSize
= function(selector
, expected
) {
764 Util
.assertStyleOfChildren(selector
, "font-size", expected
);
767 // Be sure we're dealing with a 14-point default.
768 assert
.equal(14, DEFAULT_ATTRS
.axisLabelFontSize
);
770 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-x"), "14px");
771 assertFontSize(document
.querySelectorAll(".dygraph-axis-label-y"), "14px");
774 it('testAxisLabelColor', function() {
775 var graph
= document
.getElementById("graph");
776 var g
= new Dygraph(graph
, simpleData
, {});
778 // Be sure we're dealing with a black default.
779 assert
.equal("black", DEFAULT_ATTRS
.axisLabelColor
);
781 var assertColor
= function(selector
, expected
) {
782 Util
.assertStyleOfChildren(selector
, "color", expected
);
785 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 0)");
786 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
788 g
.updateOptions({ axisLabelColor
: "red"});
789 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(255, 0, 0)");
790 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(255, 0, 0)");
793 axisLabelColor
: null,
795 x
: { axisLabelColor
: "blue" },
799 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 255)");
800 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
804 y
: { axisLabelColor
: "green" },
808 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 255)");
809 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 128, 0)");
813 Y2
: { axis
: "y2" } // copy y2 series to y2 axis.
816 y2
: { axisLabelColor
: "yellow" },
820 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 255)");
821 assertColor(document
.querySelectorAll(".dygraph-axis-label-y1"), "rgb(0, 128, 0)");
822 assertColor(document
.querySelectorAll(".dygraph-axis-label-y2"), "rgb(255, 255, 0)");
825 it('testAxisLabelColorNull', function() {
826 var graph
= document
.getElementById("graph");
827 var g
= new Dygraph(graph
, simpleData
,
832 var assertColor
= function(selector
, expected
) {
833 Util
.assertStyleOfChildren(selector
, "color", expected
);
836 // Be sure we're dealing with a 14-point default.
837 assert
.equal(14, DEFAULT_ATTRS
.axisLabelFontSize
);
839 assertColor(document
.querySelectorAll(".dygraph-axis-label-x"), "rgb(0, 0, 0)");
840 assertColor(document
.querySelectorAll(".dygraph-axis-label-y"), "rgb(0, 0, 0)");
844 * This test shows that the label formatter overrides labelsKMB for all values.
846 it('testLabelFormatterOverridesLabelsKMB', function() {
848 document
.getElementById("graph"),
855 axisLabelFormatter
: function (v
) {
859 assert
.deepEqual(["0:X","500:X","1000:X","1500:X","2000:X"], Util
.getYLabels());
860 assert
.deepEqual(["1:X","2:X","3:X"], Util
.getXLabels());
864 * This test shows that you can override labelsKMB on the axis level.
866 it('testLabelsKMBPerAxis', function() {
868 document
.getElementById("graph"),
876 y2
: { labelsKMB
: true },
877 x
: { labelsKMB
: true }
884 // labelsKMB doesn't apply to the x axis. This value should be different.
885 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
886 assert
.deepEqual(["1000","2000","3000"], Util
.getXLabels());
887 assert
.deepEqual(["0","500","1000","1500","2000"], Util
.getYLabels(1));
888 assert
.deepEqual(["0","500","1K","1.5K","2K"], Util
.getYLabels(2));
892 * This test shows that you can override labelsKMG2 on the axis level.
894 it('testLabelsKMBG2IPerAxis', function() {
896 document
.getElementById("graph"),
904 y2
: { labelsKMG2
: true },
905 x
: { labelsKMG2
: true, pixelsPerLabel
: 60 }
912 // It is weird that labelsKMG2 does something on the x axis but KMB does not.
913 // Plus I can't be sure they're doing the same thing as they're done in different
915 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
916 assert
.deepEqual(["1024","2048","3072"], Util
.getXLabels());
917 assert
.deepEqual(["0","500","1000","1500","2000"], Util
.getYLabels(1));
918 assert
.deepEqual(["0","500","1000","1.46k","1.95k"], Util
.getYLabels(2));
922 * This test shows you can override sigFigs on the axis level.
924 it('testSigFigsPerAxis', function() {
926 document
.getElementById("graph"),
942 // sigFigs doesn't apply to the x axis. This value should be different.
943 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
944 assert
.deepEqual(["1000","2000","3000"], Util
.getXLabels());
945 assert
.deepEqual(["0.0","5.0e+2","1.0e+3","1.5e+3","2.0e+3"], Util
.getYLabels(1));
946 assert
.deepEqual(["0.00000","500.000","1000.00","1500.00","2000.00"], Util
.getYLabels(2));
950 * This test shows you can override digitsAfterDecimal on the axis level.
952 it('testDigitsAfterDecimalPerAxis', function() {
954 document
.getElementById("graph"),
956 "0.006,0.001,0.008\n" +
957 "0.007,0.002,0.007\n" +
958 "0.008,0.003,0.006\n" +
959 "0.009,0.004,0.005\n", {
960 digitsAfterDecimal
: 1,
967 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: 3 }}});
968 assert
.deepEqual(["0.001","0.002","0.002","0.003","0.003","0.004","0.004"], Util
.getYLabels(1));
969 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: 4 }}});
970 assert
.deepEqual(["0.001","0.0015","0.002","0.0025","0.003","0.0035","0.004"], Util
.getYLabels(1));
971 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: 5 }}});
972 assert
.deepEqual(["0.001","0.0015","0.002","0.0025","0.003","0.0035","0.004"], Util
.getYLabels(1));
973 g
.updateOptions({ axes
: { y
: { digitsAfterDecimal
: null }}});
974 assert
.deepEqual(["1e-3","2e-3","2e-3","3e-3","3e-3","4e-3","4e-3"], Util
.getYLabels(1));
976 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: 3 }}});
977 assert
.deepEqual(["0.005","0.006","0.006","0.007","0.007","0.008","0.008"], Util
.getYLabels(2));
978 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: 4 }}});
979 assert
.deepEqual(["0.005","0.0055","0.006","0.0065","0.007","0.0075","0.008"], Util
.getYLabels(2));
980 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: 5 }}});
981 assert
.deepEqual(["0.005","0.0055","0.006","0.0065","0.007","0.0075","0.008"], Util
.getYLabels(2));
982 g
.updateOptions({ axes
: { y2
: { digitsAfterDecimal
: null }}});
983 assert
.deepEqual(["5e-3","6e-3","6e-3","7e-3","7e-3","7e-3","8e-3"], Util
.getYLabels(2));
986 // digitsAfterDecimal is ignored for the x-axis.
987 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
988 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: 3 }}});
989 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
990 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: 4 }}});
991 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
992 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: 5 }}});
993 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
994 g
.updateOptions({ axes
: { x
: { digitsAfterDecimal
: null }}});
995 assert
.deepEqual(["0.006","0.007","0.008"], Util
.getXLabels());
999 * This test shows you can override digitsAfterDecimal on the axis level.
1001 it('testMaxNumberWidthPerAxis', function() {
1002 var g
= new Dygraph(
1003 document
.getElementById("graph"),
1005 "12401,12601,12804\n" +
1006 "12402,12602,12803\n" +
1007 "12403,12603,12802\n" +
1008 "12404,12604,12801\n", {
1015 g
.updateOptions({ axes
: { y
: { maxNumberWidth
: 4 }}});
1016 assert
.deepEqual(["1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4"] , Util
.getYLabels(1));
1017 g
.updateOptions({ axes
: { y
: { maxNumberWidth
: 5 }}});
1018 assert
.deepEqual(["12601","12601.5","12602","12602.5","12603","12603.5","12604"] , Util
.getYLabels(1));
1019 g
.updateOptions({ axes
: { y
: { maxNumberWidth
: null }}});
1020 assert
.deepEqual(["1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4","1.26e+4"] , Util
.getYLabels(1));
1022 g
.updateOptions({ axes
: { y2
: { maxNumberWidth
: 4 }}});
1023 assert
.deepEqual(["1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4"], Util
.getYLabels(2));
1024 g
.updateOptions({ axes
: { y2
: { maxNumberWidth
: 5 }}});
1025 assert
.deepEqual(["12801","12801.5","12802","12802.5","12803","12803.5","12804"], Util
.getYLabels(2));
1026 g
.updateOptions({ axes
: { y2
: { maxNumberWidth
: null }}});
1027 assert
.deepEqual(["1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4","1.28e+4"], Util
.getYLabels(2));
1029 // maxNumberWidth is ignored for the x-axis.
1030 // BUG : https://code.google.com/p/dygraphs/issues/detail
?id
=488
1031 g
.updateOptions({ axes
: { x
: { maxNumberWidth
: 4 }}});
1032 assert
.deepEqual(["12401","12402","12403"], Util
.getXLabels());
1033 g
.updateOptions({ axes
: { x
: { maxNumberWidth
: 5 }}});
1034 assert
.deepEqual(["12401","12402","12403"], Util
.getXLabels());
1035 g
.updateOptions({ axes
: { x
: { maxNumberWidth
: null }}});
1036 assert
.deepEqual(["12401","12402","12403"], Util
.getXLabels());
1040 // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=147
1041 // Checks that axis labels stay sane across a DST change.
1042 it('testLabelsCrossDstChange', function() {
1043 // (From tests/daylight-savings.html)
1044 var g = new Dygraph(
1045 document.getElementById("graph"),
1046 "Date/Time,Purchases\n" +
1047 "2010-11-05 00:00:00,167082\n" +
1048 "2010-11-06 00:00:00,168571\n" +
1049 "2010-11-07 00:00:00,177796\n" +
1050 "2010-11-08 00:00:00,165587\n" +
1051 "2010-11-09 00:00:00,164380\n",
1055 // Dates and "nice" hours: 6AM/PM and noon, not 5AM/11AM/...
1067 var xLabels = Util.getXLabels();
1068 for (var i = 0; i < xLabels.length; i++) {
1069 assert.isTrue(okLabels[xLabels[i]]);
1072 // This range had issues of its own on tests/daylight-savings.html.
1074 dateWindow: [1289109997722.8127, 1289261208937.7659]
1076 xLabels = Util.getXLabels();
1077 for (var i = 0; i < xLabels.length; i++) {
1078 assert.isTrue(okLabels[xLabels[i]]);
1083 // Tests data which crosses a "fall back" at a high enough frequency that you
1084 // can see both 1:00 A.M.s.
1085 it('testLabelsCrossDstChangeHighFreq', function() {
1086 // Generate data which crosses the EST/EDT boundary.
1088 var base_ms = 1383454200000;
1089 for (var x = base_ms; x < base_ms + 1000 * 60 * 80; x += 1000) {
1090 dst_data.push([new Date(x), x]);
1093 var g = new Dygraph(
1094 document.getElementById("graph"),
1096 { width: 1024, labels: ['Date', 'Value'] }
1101 '01:00', '01:05', '01:10', '01:15', '01:20', '01:25',
1102 '01:30', '01:35', '01:40', '01:45', '01:50', '01:55',
1103 '01:00', '01:05' // 1 AM number two!
1104 ], Util.getXLabels());
1106 // Now zoom past the initial 1 AM. This used to cause trouble.
1108 dateWindow: [1383454200000 + 15*60*1000, g.xAxisExtremes()[1]]}
1111 '01:05', '01:10', '01:15', '01:20', '01:25',
1112 '01:30', '01:35', '01:40', '01:45', '01:50', '01:55',
1113 '01:00', '01:05' // 1 AM number two!
1114 ], Util.getXLabels());
1118 // Tests data which crosses a "spring forward" at a low frequency.
1119 // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=433
1120 it('testLabelsCrossSpringForward', function() {
1121 var g = new Dygraph(
1122 document.getElementById("graph"),
1123 "Date/Time,Purchases\n" +
1124 "2011-03-11 00:00:00,167082\n" +
1125 "2011-03-12 00:00:00,168571\n" +
1126 "2011-03-13 00:00:00,177796\n" +
1127 "2011-03-14 00:00:00,165587\n" +
1128 "2011-03-15 00:00:00,164380\n",
1131 dateWindow: [1299989043119.4365, 1300080693627.4866]
1136 // '02:00': true, // not a real time!
1150 var xLabels = Util.getXLabels();
1151 for (var i = 0; i < xLabels.length; i++) {
1152 assert.isTrue(okLabels[xLabels[i]]);
1156 it('testLabelsCrossSpringForwardHighFreq', function() {
1157 var base_ms_spring = 1299999000000;
1158 var dst_data_spring = [];
1159 for (var x = base_ms_spring; x < base_ms_spring + 1000 * 60 * 80; x += 1000) {
1160 dst_data_spring.push([new Date(x), x]);
1163 var g = new Dygraph(
1164 document.getElementById("graph"),
1166 { width: 1024, labels: ['Date', 'Value'] }
1171 '03:00', '03:05', '03:10', '03:15', '03:20', '03:25',
1172 '03:30', '03:35', '03:40', '03:45', '03:50', '03:55',
1174 ], Util.getXLabels());