2015-09-27 15:38:11 +02:00
// Time scale tests
describe ( 'Time scale tests' , function ( ) {
2020-02-29 00:56:22 +01:00
describe ( 'auto' , jasmine . fixture . specs ( 'scale.time' ) ) ;
2019-06-26 00:46:24 +02:00
function createScale ( data , options , dimensions ) {
2019-10-31 23:45:13 +01:00
var width = ( dimensions && dimensions . width ) || 400 ;
var height = ( dimensions && dimensions . height ) || 50 ;
2017-04-02 14:49:00 +02:00
2019-11-05 00:07:01 +01:00
options = options || { } ;
options . type = 'time' ;
options . id = 'xScale0' ;
var chart = window . acquireChart ( {
type : 'line' ,
data : data ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : options
2019-11-05 00:07:01 +01:00
}
}
} , { canvas : { width : width , height : height } } ) ;
2019-11-22 00:46:49 +01:00
return chart . scales . x ;
2017-04-02 14:49:00 +02:00
}
2019-10-31 23:45:13 +01:00
function getLabels ( scale ) {
return scale . ticks . map ( t => t . label ) ;
2017-06-15 13:20:16 +02:00
}
2016-03-06 05:16:25 +01:00
beforeEach ( function ( ) {
2016-04-30 02:03:15 +02:00
// Need a time matcher for getValueFromPixel
2016-03-06 05:16:25 +01:00
jasmine . addMatchers ( {
2016-04-23 19:27:10 +02:00
toBeCloseToTime : function ( ) {
return {
2019-10-27 21:36:57 +01:00
compare : function ( time , expected ) {
2016-04-23 19:27:10 +02:00
var result = false ;
2019-10-27 21:36:57 +01:00
var actual = moment ( time ) ;
2016-04-23 19:27:10 +02:00
var diff = actual . diff ( expected . value , expected . unit , true ) ;
2017-04-02 14:49:00 +02:00
result = Math . abs ( diff ) < ( expected . threshold !== undefined ? expected . threshold : 0.01 ) ;
2016-04-23 19:27:10 +02:00
return {
pass : result
} ;
}
2016-10-16 23:34:59 +02:00
} ;
2016-03-06 05:16:25 +01:00
}
} ) ;
2016-04-28 03:50:31 +02:00
} ) ;
2017-07-22 14:22:44 +02:00
it ( 'should load moment.js as a dependency' , function ( ) {
2015-10-17 05:14:07 +02:00
expect ( window . moment ) . not . toBe ( undefined ) ;
} ) ;
2020-07-06 23:38:04 +02:00
it ( 'should register the constructor with the registry' , function ( ) {
var Constructor = Chart . registry . getScale ( 'time' ) ;
2015-09-27 15:38:11 +02:00
expect ( Constructor ) . not . toBe ( undefined ) ;
expect ( typeof Constructor ) . toBe ( 'function' ) ;
} ) ;
2017-07-22 14:22:44 +02:00
it ( 'should have the correct default config' , function ( ) {
2020-07-06 23:38:04 +02:00
var defaultConfig = Chart . defaults . scales . time ;
2015-09-27 15:38:11 +02:00
expect ( defaultConfig ) . toEqual ( {
2017-08-02 07:28:27 +02:00
bounds : 'data' ,
2019-02-21 08:11:32 +01:00
adapters : { } ,
2020-07-06 23:38:04 +02:00
time : {
parser : false , // false == a pattern string from or a custom callback that converts its argument to a timestamp
unit : false , // false == automatic or override with week, month, year, etc.
round : false , // none, or override with week, month, year, etc.
isoWeekday : false , // override week start day
minUnit : 'millisecond' ,
displayFormats : { }
} ,
2015-09-27 15:38:11 +02:00
ticks : {
2017-07-19 21:04:15 +02:00
source : 'auto' ,
2017-09-09 00:25:51 +02:00
major : {
enabled : false
2020-07-06 23:38:04 +02:00
}
2015-09-27 15:38:11 +02:00
}
} ) ;
} ) ;
2019-05-10 15:02:30 +02:00
it ( 'should correctly determine the unit' , function ( ) {
var date = moment ( 'Jan 01 1990' , 'MMM DD YYYY' ) ;
var data = [ ] ;
for ( var i = 0 ; i < 60 ; i ++ ) {
data . push ( { x : date . valueOf ( ) , y : Math . random ( ) } ) ;
date = date . clone ( ) . add ( 1 , 'month' ) ;
}
var chart = window . acquireChart ( {
type : 'line' ,
data : {
datasets : [ {
2019-11-22 00:46:49 +01:00
xAxisID : 'x' ,
2019-05-10 15:02:30 +02:00
data : data
} ] ,
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2019-05-10 15:02:30 +02:00
type : 'time' ,
ticks : {
source : 'data' ,
autoSkip : true
}
2019-11-22 00:46:49 +01:00
} ,
2019-05-10 15:02:30 +02:00
}
}
} ) ;
2019-11-22 00:46:49 +01:00
var scale = chart . scales . x ;
2019-05-10 15:02:30 +02:00
expect ( scale . _unit ) . toEqual ( 'month' ) ;
} ) ;
2017-04-02 14:49:00 +02:00
describe ( 'when specifying limits' , function ( ) {
2016-01-23 18:44:55 +01:00
var mockData = {
2017-04-02 14:49:00 +02:00
labels : [ '2015-01-01T20:00:00' , '2015-01-02T20:00:00' , '2015-01-03T20:00:00' ] ,
2016-01-23 18:44:55 +01:00
} ;
2017-04-02 14:49:00 +02:00
var config ;
beforeEach ( function ( ) {
2020-07-06 23:38:04 +02:00
config = Chart . helpers . clone ( Chart . defaults . scales . time ) ;
2017-10-20 09:03:38 +02:00
config . ticks . source = 'labels' ;
config . time . unit = 'day' ;
2016-01-23 18:44:55 +01:00
} ) ;
2019-04-02 09:43:26 +02:00
it ( 'should use the min option when less than first label for building ticks' , function ( ) {
2019-11-15 18:11:13 +01:00
config . min = '2014-12-29T04:00:00' ;
2019-04-02 09:43:26 +02:00
2019-10-31 23:45:13 +01:00
var labels = getLabels ( createScale ( mockData , config ) ) ;
expect ( labels [ 0 ] ) . toEqual ( 'Jan 1' ) ;
2019-04-02 09:43:26 +02:00
} ) ;
it ( 'should use the min option when greater than first label for building ticks' , function ( ) {
2019-11-15 18:11:13 +01:00
config . min = '2015-01-02T04:00:00' ;
2019-04-02 09:43:26 +02:00
2019-10-31 23:45:13 +01:00
var labels = getLabels ( createScale ( mockData , config ) ) ;
expect ( labels [ 0 ] ) . toEqual ( 'Jan 2' ) ;
2017-10-20 09:03:38 +02:00
} ) ;
it ( 'should use the max option when greater than last label for building ticks' , function ( ) {
2019-11-15 18:11:13 +01:00
config . max = '2015-01-05T06:00:00' ;
2017-04-02 14:49:00 +02:00
2019-10-31 23:45:13 +01:00
var labels = getLabels ( createScale ( mockData , config ) ) ;
expect ( labels [ labels . length - 1 ] ) . toEqual ( 'Jan 3' ) ;
2017-10-20 09:03:38 +02:00
} ) ;
2017-06-15 13:20:16 +02:00
2017-10-20 09:03:38 +02:00
it ( 'should use the max option when less than last label for building ticks' , function ( ) {
2019-11-15 18:11:13 +01:00
config . max = '2015-01-02T23:00:00' ;
2017-10-20 09:03:38 +02:00
2019-10-31 23:45:13 +01:00
var labels = getLabels ( createScale ( mockData , config ) ) ;
expect ( labels [ labels . length - 1 ] ) . toEqual ( 'Jan 2' ) ;
2017-04-02 14:49:00 +02:00
} ) ;
2016-01-23 18:44:55 +01:00
} ) ;
2017-07-22 14:22:44 +02:00
it ( 'should use the isoWeekday option' , function ( ) {
2016-05-11 10:34:22 +02:00
var mockData = {
labels : [
2016-10-16 23:34:59 +02:00
'2015-01-01T20:00:00' , // Thursday
'2015-01-02T20:00:00' , // Friday
'2015-01-03T20:00:00' // Saturday
2016-05-11 10:34:22 +02:00
]
} ;
2017-07-25 10:12:53 +02:00
var config = Chart . helpers . mergeIf ( {
2017-08-02 07:28:27 +02:00
bounds : 'ticks' ,
2017-07-25 10:12:53 +02:00
time : {
unit : 'week' ,
isoWeekday : 3 // Wednesday
}
2020-07-06 23:38:04 +02:00
} , Chart . defaults . scales . time ) ;
2017-07-25 10:12:53 +02:00
2017-04-02 14:49:00 +02:00
var scale = createScale ( mockData , config ) ;
2019-10-31 23:45:13 +01:00
var ticks = getLabels ( scale ) ;
2017-06-15 13:20:16 +02:00
expect ( ticks ) . toEqual ( [ 'Dec 31, 2014' , 'Jan 7, 2015' ] ) ;
2016-05-11 10:34:22 +02:00
} ) ;
2017-04-02 14:49:00 +02:00
describe ( 'when rendering several days' , function ( ) {
2017-07-25 10:12:53 +02:00
beforeEach ( function ( ) {
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
datasets : [ {
2019-11-22 00:46:49 +01:00
xAxisID : 'x' ,
2017-07-25 10:12:53 +02:00
data : [ ]
2016-04-28 03:50:31 +02:00
} ] ,
2017-07-25 10:12:53 +02:00
labels : [
'2015-01-01T20:00:00' ,
'2015-01-02T21:00:00' ,
'2015-01-03T22:00:00' ,
'2015-01-05T23:00:00' ,
'2015-01-07T03:00' ,
'2015-01-08T10:00' ,
'2015-01-10T12:00'
]
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2017-07-25 10:12:53 +02:00
type : 'time' ,
2019-11-22 00:46:49 +01:00
position : 'bottom'
} ,
2017-07-25 10:12:53 +02:00
}
2016-04-28 03:50:31 +02:00
}
2017-07-25 10:12:53 +02:00
} ) ;
2015-09-27 15:38:11 +02:00
2019-11-22 00:46:49 +01:00
this . scale = this . chart . scales . x ;
2017-07-25 10:12:53 +02:00
} ) ;
2015-09-27 15:38:11 +02:00
2017-05-30 00:40:10 +02:00
it ( 'should be bounded by the nearest week beginnings' , function ( ) {
2017-07-25 10:12:53 +02:00
var chart = this . chart ;
var scale = this . scale ;
expect ( scale . getValueForPixel ( scale . left ) ) . toBeGreaterThan ( moment ( chart . data . labels [ 0 ] ) . startOf ( 'week' ) ) ;
expect ( scale . getValueForPixel ( scale . right ) ) . toBeLessThan ( moment ( chart . data . labels [ chart . data . labels . length - 1 ] ) . add ( 1 , 'week' ) . endOf ( 'week' ) ) ;
2017-04-02 14:49:00 +02:00
} ) ;
it ( 'should convert between screen coordinates and times' , function ( ) {
2017-07-25 10:12:53 +02:00
var chart = this . chart ;
var scale = this . scale ;
var timeRange = moment ( scale . max ) . valueOf ( ) - moment ( scale . min ) . valueOf ( ) ;
var msPerPix = timeRange / scale . width ;
var firstPointOffsetMs = moment ( chart . config . data . labels [ 0 ] ) . valueOf ( ) - scale . min ;
var firstPointPixel = scale . left + firstPointOffsetMs / msPerPix ;
var lastPointOffsetMs = moment ( chart . config . data . labels [ chart . config . data . labels . length - 1 ] ) . valueOf ( ) - scale . min ;
var lastPointPixel = scale . left + lastPointOffsetMs / msPerPix ;
2019-11-19 13:09:50 +01:00
expect ( scale . getPixelForValue ( moment ( '2015-01-01T20:00:00' ) . valueOf ( ) ) ) . toBeCloseToPixel ( firstPointPixel ) ;
expect ( scale . getPixelForValue ( moment ( chart . data . labels [ 0 ] ) . valueOf ( ) ) ) . toBeCloseToPixel ( firstPointPixel ) ;
2017-07-25 10:12:53 +02:00
expect ( scale . getValueForPixel ( firstPointPixel ) ) . toBeCloseToTime ( {
2017-04-02 14:49:00 +02:00
value : moment ( chart . data . labels [ 0 ] ) ,
unit : 'hour' ,
} ) ;
2019-11-19 13:09:50 +01:00
expect ( scale . getPixelForValue ( moment ( '2015-01-10T12:00' ) . valueOf ( ) ) ) . toBeCloseToPixel ( lastPointPixel ) ;
2017-07-25 10:12:53 +02:00
expect ( scale . getValueForPixel ( lastPointPixel ) ) . toBeCloseToTime ( {
2017-04-02 14:49:00 +02:00
value : moment ( chart . data . labels [ 6 ] ) ,
unit : 'hour'
} ) ;
} ) ;
} ) ;
2016-04-24 02:08:00 +02:00
2017-04-02 14:49:00 +02:00
describe ( 'when rendering several years' , function ( ) {
2017-07-25 10:12:53 +02:00
beforeEach ( function ( ) {
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
labels : [ '2005-07-04' , '2017-01-20' ] ,
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2017-07-25 10:12:53 +02:00
type : 'time' ,
2017-08-02 07:28:27 +02:00
bounds : 'ticks' ,
position : 'bottom'
2019-11-22 00:46:49 +01:00
} ,
2017-07-25 10:12:53 +02:00
}
2017-04-02 14:49:00 +02:00
}
2019-06-26 00:46:24 +02:00
} , { canvas : { width : 800 , height : 200 } } ) ;
2017-07-19 21:04:15 +02:00
2019-11-22 00:46:49 +01:00
this . scale = this . chart . scales . x ;
2017-07-25 10:12:53 +02:00
} ) ;
2017-04-02 14:49:00 +02:00
2017-07-22 14:22:44 +02:00
it ( 'should be bounded by nearest step\'s year start and end' , function ( ) {
2017-07-25 10:12:53 +02:00
var scale = this . scale ;
2017-07-29 21:26:36 +02:00
var ticks = scale . getTicks ( ) ;
var step = ticks [ 1 ] . value - ticks [ 0 ] . value ;
2017-07-25 10:12:53 +02:00
var stepsAmount = Math . floor ( ( scale . max - scale . min ) / step ) ;
expect ( scale . getValueForPixel ( scale . left ) ) . toBeCloseToTime ( {
value : moment ( scale . min ) . startOf ( 'year' ) ,
2017-04-02 14:49:00 +02:00
unit : 'hour' ,
} ) ;
2017-07-25 10:12:53 +02:00
expect ( scale . getValueForPixel ( scale . right ) ) . toBeCloseToTime ( {
value : moment ( scale . min + step * stepsAmount ) . endOf ( 'year' ) ,
2017-04-02 14:49:00 +02:00
unit : 'hour' ,
} ) ;
2015-09-27 15:38:11 +02:00
} ) ;
2017-04-02 14:49:00 +02:00
it ( 'should build the correct ticks' , function ( ) {
2019-10-31 23:45:13 +01:00
expect ( getLabels ( this . scale ) ) . toEqual ( [ '2005' , '2006' , '2007' , '2008' , '2009' , '2010' , '2011' , '2012' , '2013' , '2014' , '2015' , '2016' , '2017' , '2018' ] ) ;
2017-04-02 14:49:00 +02:00
} ) ;
it ( 'should have ticks with accurate labels' , function ( ) {
2017-07-25 10:12:53 +02:00
var scale = this . scale ;
2017-07-29 21:26:36 +02:00
var ticks = scale . getTicks ( ) ;
2019-10-19 22:13:04 +02:00
// pixelsPerTick is an aproximation which assumes same number of milliseconds per year (not true)
// we use a threshold of 1 day so that we still match these values
var pixelsPerTick = scale . width / ( ticks . length - 1 ) ;
2017-04-02 14:49:00 +02:00
for ( var i = 0 ; i < ticks . length - 1 ; i ++ ) {
2019-10-19 22:13:04 +02:00
var offset = pixelsPerTick * i ;
2017-07-25 10:12:53 +02:00
expect ( scale . getValueForPixel ( scale . left + offset ) ) . toBeCloseToTime ( {
2017-07-29 21:26:36 +02:00
value : moment ( ticks [ i ] . label + '-01-01' ) ,
2017-04-02 14:49:00 +02:00
unit : 'day' ,
2019-10-19 22:13:04 +02:00
threshold : 1 ,
2017-04-02 14:49:00 +02:00
} ) ;
}
2016-04-30 02:03:15 +02:00
} ) ;
2016-01-23 18:44:55 +01:00
} ) ;
it ( 'should get the correct label for a data value' , function ( ) {
2016-09-06 11:10:44 +02:00
var chart = window . acquireChart ( {
2016-04-28 03:50:31 +02:00
type : 'line' ,
data : {
datasets : [ {
2019-11-22 00:46:49 +01:00
xAxisID : 'x' ,
2016-10-31 01:34:06 +01:00
data : [ null , 10 , 3 ]
2016-04-28 03:50:31 +02:00
} ] ,
2016-10-16 23:34:59 +02:00
labels : [ '2015-01-01T20:00:00' , '2015-01-02T21:00:00' , '2015-01-03T22:00:00' , '2015-01-05T23:00:00' , '2015-01-07T03:00' , '2015-01-08T10:00' , '2015-01-10T12:00' ] , // days
2016-01-23 18:44:55 +01:00
} ,
2016-04-28 03:50:31 +02:00
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2016-04-28 03:50:31 +02:00
type : 'time' ,
2019-11-05 00:07:01 +01:00
position : 'bottom' ,
ticks : {
source : 'labels' ,
autoSkip : false
}
2019-11-22 00:46:49 +01:00
}
2016-04-28 03:50:31 +02:00
}
}
2016-01-23 18:44:55 +01:00
} ) ;
2019-11-22 00:46:49 +01:00
var xScale = chart . scales . x ;
2019-11-05 00:07:01 +01:00
var controller = chart . getDatasetMeta ( 0 ) . controller ;
2020-02-22 14:35:16 +01:00
expect ( xScale . getLabelForValue ( controller . getParsed ( 0 ) [ xScale . id ] ) ) . toBeTruthy ( ) ;
expect ( xScale . getLabelForValue ( controller . getParsed ( 0 ) [ xScale . id ] ) ) . toBe ( 'Jan 1, 2015, 8:00:00 pm' ) ;
2019-11-05 00:07:01 +01:00
expect ( xScale . getLabelForValue ( xScale . getValueForPixel ( xScale . getPixelForTick ( 6 ) ) ) ) . toBe ( 'Jan 10, 2015, 12:00:00 pm' ) ;
2015-09-27 15:38:11 +02:00
} ) ;
2016-09-06 11:34:33 +02:00
2019-04-02 09:45:34 +02:00
describe ( 'when ticks.callback is specified' , function ( ) {
beforeEach ( function ( ) {
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
datasets : [ {
2019-11-22 00:46:49 +01:00
xAxisID : 'x' ,
2019-04-02 09:45:34 +02:00
data : [ 0 , 0 ]
} ] ,
labels : [ '2015-01-01T20:00:00' , '2015-01-01T20:01:00' ]
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2019-04-02 09:45:34 +02:00
type : 'time' ,
2019-06-20 00:08:07 +02:00
time : {
displayFormats : {
second : 'h:mm:ss'
}
} ,
2019-04-02 09:45:34 +02:00
ticks : {
callback : function ( value ) {
return '<' + value + '>' ;
}
}
2019-11-22 00:46:49 +01:00
}
2019-04-02 09:45:34 +02:00
}
}
} ) ;
2019-11-22 00:46:49 +01:00
this . scale = this . chart . scales . x ;
2019-04-02 09:45:34 +02:00
} ) ;
it ( 'should get the correct labels for ticks' , function ( ) {
2019-10-31 23:45:13 +01:00
var labels = getLabels ( this . scale ) ;
2019-04-02 09:45:34 +02:00
2019-11-14 13:15:44 +01:00
expect ( labels . length ) . toEqual ( 21 ) ;
2019-10-31 23:45:13 +01:00
expect ( labels [ 0 ] ) . toEqual ( '<8:00:00>' ) ;
expect ( labels [ labels . length - 1 ] ) . toEqual ( '<8:01:00>' ) ;
2019-04-02 09:45:34 +02:00
} ) ;
it ( 'should update ticks.callback correctly' , function ( ) {
var chart = this . chart ;
2019-11-22 00:46:49 +01:00
chart . options . scales . x . ticks . callback = function ( value ) {
2019-04-02 09:45:34 +02:00
return '{' + value + '}' ;
} ;
chart . update ( ) ;
2019-10-31 23:45:13 +01:00
var labels = getLabels ( this . scale ) ;
2019-11-14 13:15:44 +01:00
expect ( labels . length ) . toEqual ( 21 ) ;
2019-10-31 23:45:13 +01:00
expect ( labels [ 0 ] ) . toEqual ( '{8:00:00}' ) ;
expect ( labels [ labels . length - 1 ] ) . toEqual ( '{8:01:00}' ) ;
2019-04-02 09:45:34 +02:00
} ) ;
} ) ;
2018-01-13 16:39:17 +01:00
it ( 'should get the correct label when time is specified as a string' , function ( ) {
var chart = window . acquireChart ( {
type : 'line' ,
data : {
datasets : [ {
2019-11-22 00:46:49 +01:00
xAxisID : 'x' ,
2020-06-18 23:36:53 +02:00
data : [ { x : '2015-01-01T20:00:00' , y : 10 } , { x : '2015-01-02T21:00:00' , y : 3 } ]
2018-01-13 16:39:17 +01:00
} ] ,
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2018-01-13 16:39:17 +01:00
type : 'time' ,
position : 'bottom'
2019-11-22 00:46:49 +01:00
} ,
2018-01-13 16:39:17 +01:00
}
}
} ) ;
2019-11-22 00:46:49 +01:00
var xScale = chart . scales . x ;
2019-11-05 00:07:01 +01:00
var controller = chart . getDatasetMeta ( 0 ) . controller ;
2020-02-22 14:35:16 +01:00
var value = controller . getParsed ( 0 ) [ xScale . id ] ;
2019-11-05 00:07:01 +01:00
expect ( xScale . getLabelForValue ( value ) ) . toBeTruthy ( ) ;
expect ( xScale . getLabelForValue ( value ) ) . toBe ( 'Jan 1, 2015, 8:00:00 pm' ) ;
2018-01-13 16:39:17 +01:00
} ) ;
2020-04-14 01:34:18 +02:00
it ( 'should round to isoWeekday' , function ( ) {
var chart = window . acquireChart ( {
type : 'line' ,
data : {
datasets : [ {
data : [ { x : '2020-04-12T20:00:00' , y : 1 } , { x : '2020-04-13T20:00:00' , y : 2 } ]
} ]
} ,
options : {
scales : {
x : {
type : 'time' ,
ticks : {
source : 'data'
} ,
time : {
unit : 'week' ,
round : 'week' ,
isoWeekday : 1 ,
displayFormats : {
week : 'WW'
}
}
} ,
}
}
} ) ;
expect ( getLabels ( chart . scales . x ) ) . toEqual ( [ '15' , '16' ] ) ;
} ) ;
2019-02-18 19:45:38 +01:00
it ( 'should get the correct label for a timestamp' , function ( ) {
2018-01-13 16:39:17 +01:00
var chart = window . acquireChart ( {
type : 'line' ,
data : {
datasets : [ {
2019-11-22 00:46:49 +01:00
xAxisID : 'x' ,
2018-01-13 16:39:17 +01:00
data : [
{ t : + new Date ( '2018-01-08 05:14:23.234' ) , y : 10 } ,
{ t : + new Date ( '2018-01-09 06:17:43.426' ) , y : 3 }
]
} ] ,
} ,
options : {
2020-06-18 23:36:53 +02:00
parsing : { xAxisKey : 't' } ,
2018-01-13 16:39:17 +01:00
scales : {
2019-11-22 00:46:49 +01:00
x : {
2018-01-13 16:39:17 +01:00
type : 'time' ,
position : 'bottom'
2019-11-22 00:46:49 +01:00
} ,
2018-01-13 16:39:17 +01:00
}
}
} ) ;
2019-11-22 00:46:49 +01:00
var xScale = chart . scales . x ;
2019-11-05 00:07:01 +01:00
var controller = chart . getDatasetMeta ( 0 ) . controller ;
2020-02-22 14:35:16 +01:00
var label = xScale . getLabelForValue ( controller . getParsed ( 0 ) [ xScale . id ] ) ;
2019-02-18 19:45:38 +01:00
expect ( label ) . toEqual ( 'Jan 8, 2018, 5:14:23 am' ) ;
2018-01-13 16:39:17 +01:00
} ) ;
2016-06-09 06:34:15 +02:00
it ( 'should get the correct pixel for only one data in the dataset' , function ( ) {
var chart = window . acquireChart ( {
type : 'line' ,
data : {
2016-10-16 23:34:59 +02:00
labels : [ '2016-05-27' ] ,
2016-06-09 06:34:15 +02:00
datasets : [ {
2019-11-22 00:46:49 +01:00
xAxisID : 'x' ,
2016-06-09 06:34:15 +02:00
data : [ 5 ]
} ]
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2016-06-09 06:34:15 +02:00
display : true ,
2016-10-16 23:34:59 +02:00
type : 'time'
2019-11-22 00:46:49 +01:00
}
2016-06-09 06:34:15 +02:00
}
}
} ) ;
2019-11-22 00:46:49 +01:00
var xScale = chart . scales . x ;
2019-11-19 13:09:50 +01:00
var pixel = xScale . getPixelForValue ( moment ( '2016-05-27' ) . valueOf ( ) ) ;
2016-06-09 06:34:15 +02:00
2019-11-05 00:07:01 +01:00
expect ( xScale . getValueForPixel ( pixel ) ) . toEqual ( moment ( chart . data . labels [ 0 ] ) . valueOf ( ) ) ;
2016-06-09 06:34:15 +02:00
} ) ;
2017-04-04 02:48:55 +02:00
it ( 'does not create a negative width chart when hidden' , function ( ) {
var chart = window . acquireChart ( {
type : 'line' ,
data : {
datasets : [ {
data : [ ]
} ]
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2017-04-04 02:48:55 +02:00
type : 'time' ,
2019-05-06 22:46:22 +02:00
ticks : {
2017-04-04 02:48:55 +02:00
min : moment ( ) . subtract ( 1 , 'months' ) ,
max : moment ( ) ,
}
2019-11-22 00:46:49 +01:00
} ,
2017-04-04 02:48:55 +02:00
} ,
responsive : true ,
} ,
} , {
wrapper : {
style : 'display: none' ,
} ,
} ) ;
2019-11-22 00:46:49 +01:00
expect ( chart . scales . y . width ) . toEqual ( 0 ) ;
expect ( chart . scales . y . maxWidth ) . toEqual ( 0 ) ;
2017-04-04 02:48:55 +02:00
expect ( chart . width ) . toEqual ( 0 ) ;
} ) ;
2017-07-19 21:04:15 +02:00
describe ( 'when ticks.source' , function ( ) {
describe ( 'is "labels"' , function ( ) {
beforeEach ( function ( ) {
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
labels : [ '2017' , '2019' , '2020' , '2025' , '2042' ] ,
datasets : [ { data : [ 0 , 1 , 2 , 3 , 4 , 5 ] } ]
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2017-07-19 21:04:15 +02:00
type : 'time' ,
time : {
parser : 'YYYY'
} ,
ticks : {
source : 'labels'
}
2019-11-22 00:46:49 +01:00
}
2017-07-19 21:04:15 +02:00
}
}
} ) ;
} ) ;
it ( 'should generate ticks from "data.labels"' , function ( ) {
var scale = this . chart . scales . x ;
expect ( scale . min ) . toEqual ( + moment ( '2017' , 'YYYY' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( '2042' , 'YYYY' ) ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [
2017-07-19 21:04:15 +02:00
'2017' , '2019' , '2020' , '2025' , '2042' ] ) ;
} ) ;
it ( 'should not add ticks for min and max if they extend the labels range' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
2019-11-22 00:46:49 +01:00
var options = chart . options . scales . x ;
2017-07-19 21:04:15 +02:00
2019-11-15 18:11:13 +01:00
options . min = '2012' ;
options . max = '2051' ;
2017-07-19 21:04:15 +02:00
chart . update ( ) ;
expect ( scale . min ) . toEqual ( + moment ( '2012' , 'YYYY' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( '2051' , 'YYYY' ) ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [
2017-07-19 21:04:15 +02:00
'2017' , '2019' , '2020' , '2025' , '2042' ] ) ;
} ) ;
2017-07-27 06:41:09 +02:00
it ( 'should not duplicate ticks if min and max are the labels limits' , function ( ) {
2017-07-19 21:04:15 +02:00
var chart = this . chart ;
var scale = chart . scales . x ;
2019-11-22 00:46:49 +01:00
var options = chart . options . scales . x ;
2017-07-19 21:04:15 +02:00
2019-11-15 18:11:13 +01:00
options . min = '2017' ;
options . max = '2042' ;
2017-07-19 21:04:15 +02:00
chart . update ( ) ;
2017-07-27 06:41:09 +02:00
expect ( scale . min ) . toEqual ( + moment ( '2017' , 'YYYY' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( '2042' , 'YYYY' ) ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [
2017-07-27 06:41:09 +02:00
'2017' , '2019' , '2020' , '2025' , '2042' ] ) ;
} ) ;
2017-12-15 18:10:30 +01:00
it ( 'should correctly handle empty `data.labels` using "day" if `time.unit` is undefined`' , function ( ) {
2017-07-27 06:41:09 +02:00
var chart = this . chart ;
var scale = chart . scales . x ;
chart . data . labels = [ ] ;
chart . update ( ) ;
expect ( scale . min ) . toEqual ( + moment ( ) . startOf ( 'day' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( ) . endOf ( 'day' ) + 1 ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [ ] ) ;
2017-07-27 06:41:09 +02:00
} ) ;
2017-12-15 18:10:30 +01:00
it ( 'should correctly handle empty `data.labels` using `time.unit`' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
2019-11-22 00:46:49 +01:00
var options = chart . options . scales . x ;
2017-12-15 18:10:30 +01:00
options . time . unit = 'year' ;
chart . data . labels = [ ] ;
chart . update ( ) ;
expect ( scale . min ) . toEqual ( + moment ( ) . startOf ( 'year' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( ) . endOf ( 'year' ) + 1 ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [ ] ) ;
2017-12-15 18:10:30 +01:00
} ) ;
2017-07-27 06:41:09 +02:00
} ) ;
describe ( 'is "data"' , function ( ) {
beforeEach ( function ( ) {
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
labels : [ '2017' , '2019' , '2020' , '2025' , '2042' ] ,
datasets : [
{ data : [ 0 , 1 , 2 , 3 , 4 , 5 ] } ,
{ data : [
2020-06-18 23:36:53 +02:00
{ x : '2018' , y : 6 } ,
{ x : '2020' , y : 7 } ,
{ x : '2043' , y : 8 }
2017-07-27 06:41:09 +02:00
] }
]
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2017-07-27 06:41:09 +02:00
type : 'time' ,
time : {
parser : 'YYYY'
} ,
ticks : {
source : 'data'
}
2019-11-22 00:46:49 +01:00
}
2017-07-27 06:41:09 +02:00
}
}
} ) ;
} ) ;
it ( 'should generate ticks from "datasets.data"' , function ( ) {
var scale = this . chart . scales . x ;
expect ( scale . min ) . toEqual ( + moment ( '2017' , 'YYYY' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( '2043' , 'YYYY' ) ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [
2017-07-27 06:41:09 +02:00
'2017' , '2018' , '2019' , '2020' , '2025' , '2042' , '2043' ] ) ;
} ) ;
it ( 'should not add ticks for min and max if they extend the labels range' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
2019-11-22 00:46:49 +01:00
var options = chart . options . scales . x ;
2017-07-27 06:41:09 +02:00
2019-11-15 18:11:13 +01:00
options . min = '2012' ;
options . max = '2051' ;
2017-07-27 06:41:09 +02:00
chart . update ( ) ;
expect ( scale . min ) . toEqual ( + moment ( '2012' , 'YYYY' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( '2051' , 'YYYY' ) ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [
2017-07-27 06:41:09 +02:00
'2017' , '2018' , '2019' , '2020' , '2025' , '2042' , '2043' ] ) ;
2017-07-19 21:04:15 +02:00
} ) ;
it ( 'should not duplicate ticks if min and max are the labels limits' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
2019-11-22 00:46:49 +01:00
var options = chart . options . scales . x ;
2017-07-19 21:04:15 +02:00
2019-11-15 18:11:13 +01:00
options . min = '2017' ;
options . max = '2043' ;
2017-07-19 21:04:15 +02:00
chart . update ( ) ;
expect ( scale . min ) . toEqual ( + moment ( '2017' , 'YYYY' ) ) ;
2017-07-27 06:41:09 +02:00
expect ( scale . max ) . toEqual ( + moment ( '2043' , 'YYYY' ) ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [
2017-07-27 06:41:09 +02:00
'2017' , '2018' , '2019' , '2020' , '2025' , '2042' , '2043' ] ) ;
2017-07-19 21:04:15 +02:00
} ) ;
2017-12-15 18:10:30 +01:00
it ( 'should correctly handle empty `data.labels` using "day" if `time.unit` is undefined`' , function ( ) {
2017-07-19 21:04:15 +02:00
var chart = this . chart ;
var scale = chart . scales . x ;
chart . data . labels = [ ] ;
chart . update ( ) ;
2017-07-27 06:41:09 +02:00
expect ( scale . min ) . toEqual ( + moment ( '2018' , 'YYYY' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( '2043' , 'YYYY' ) ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [
2017-07-27 06:41:09 +02:00
'2018' , '2020' , '2043' ] ) ;
2017-07-19 21:04:15 +02:00
} ) ;
2017-12-15 18:10:30 +01:00
it ( 'should correctly handle empty `data.labels` and hidden datasets using `time.unit`' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
2019-11-22 00:46:49 +01:00
var options = chart . options . scales . x ;
2017-12-15 18:10:30 +01:00
options . time . unit = 'year' ;
chart . data . labels = [ ] ;
var meta = chart . getDatasetMeta ( 1 ) ;
meta . hidden = true ;
chart . update ( ) ;
expect ( scale . min ) . toEqual ( + moment ( ) . startOf ( 'year' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( ) . endOf ( 'year' ) + 1 ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [ ] ) ;
2017-12-15 18:10:30 +01:00
} ) ;
2017-07-19 21:04:15 +02:00
} ) ;
} ) ;
2020-07-07 13:50:53 +02:00
[ true , false ] . forEach ( function ( normalized ) {
describe ( 'when normalized is ' + normalized + ' and scale type' , function ( ) {
describe ( 'is "timeseries"' , function ( ) {
beforeEach ( function ( ) {
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
labels : [ '2017' , '2019' , '2020' , '2025' , '2042' ] ,
datasets : [ { data : [ 0 , 1 , 2 , 3 , 4 ] } ]
} ,
options : {
normalized ,
scales : {
x : {
type : 'timeseries' ,
time : {
parser : 'YYYY'
} ,
ticks : {
source : 'labels'
}
2017-07-19 21:04:15 +02:00
} ,
2020-07-07 13:50:53 +02:00
y : {
display : false
2017-07-19 21:04:15 +02:00
}
2019-11-22 00:46:49 +01:00
}
2017-07-19 21:04:15 +02:00
}
2020-07-07 13:50:53 +02:00
} ) ;
2017-07-19 21:04:15 +02:00
} ) ;
2020-07-07 13:50:53 +02:00
it ( 'should space data out with the same gap, whatever their time values' , function ( ) {
var scale = this . chart . scales . x ;
var start = scale . left ;
var slice = scale . width / 4 ;
2017-07-19 21:04:15 +02:00
2020-07-07 13:50:53 +02:00
expect ( scale . getPixelForValue ( moment ( '2017' ) . valueOf ( ) , 0 ) ) . toBeCloseToPixel ( start ) ;
expect ( scale . getPixelForValue ( moment ( '2019' ) . valueOf ( ) , 1 ) ) . toBeCloseToPixel ( start + slice ) ;
expect ( scale . getPixelForValue ( moment ( '2020' ) . valueOf ( ) , 2 ) ) . toBeCloseToPixel ( start + slice * 2 ) ;
expect ( scale . getPixelForValue ( moment ( '2025' ) . valueOf ( ) , 3 ) ) . toBeCloseToPixel ( start + slice * 3 ) ;
expect ( scale . getPixelForValue ( moment ( '2042' ) . valueOf ( ) , 4 ) ) . toBeCloseToPixel ( start + slice * 4 ) ;
} ) ;
it ( 'should add a step before if scale.min is before the first data' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . x ;
2017-07-19 21:04:15 +02:00
2020-07-07 13:50:53 +02:00
options . min = '2012' ;
chart . update ( ) ;
2017-07-19 21:04:15 +02:00
2020-07-07 13:50:53 +02:00
var start = scale . left ;
var slice = scale . width / 5 ;
2017-07-19 21:04:15 +02:00
2020-07-07 13:50:53 +02:00
expect ( scale . getPixelForValue ( moment ( '2017' ) . valueOf ( ) , 1 ) ) . toBeCloseToPixel ( start + slice ) ;
expect ( scale . getPixelForValue ( moment ( '2042' ) . valueOf ( ) , 5 ) ) . toBeCloseToPixel ( start + slice * 5 ) ;
} ) ;
it ( 'should add a step after if scale.max is after the last data' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . x ;
2017-07-19 21:04:15 +02:00
2020-07-07 13:50:53 +02:00
options . max = '2050' ;
chart . update ( ) ;
2017-07-19 21:04:15 +02:00
2020-07-07 13:50:53 +02:00
var start = scale . left ;
var slice = scale . width / 5 ;
2017-07-19 21:04:15 +02:00
2020-07-07 13:50:53 +02:00
expect ( scale . getPixelForValue ( moment ( '2017' ) . valueOf ( ) , 0 ) ) . toBeCloseToPixel ( start ) ;
expect ( scale . getPixelForValue ( moment ( '2042' ) . valueOf ( ) , 4 ) ) . toBeCloseToPixel ( start + slice * 4 ) ;
} ) ;
it ( 'should add steps before and after if scale.min/max are outside the data range' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . x ;
2017-07-19 21:04:15 +02:00
2020-07-07 13:50:53 +02:00
options . min = '2012' ;
options . max = '2050' ;
chart . update ( ) ;
2017-07-19 21:04:15 +02:00
2020-07-07 13:50:53 +02:00
var start = scale . left ;
var slice = scale . width / 6 ;
expect ( scale . getPixelForValue ( moment ( '2017' ) . valueOf ( ) , 1 ) ) . toBeCloseToPixel ( start + slice ) ;
expect ( scale . getPixelForValue ( moment ( '2042' ) . valueOf ( ) , 5 ) ) . toBeCloseToPixel ( start + slice * 5 ) ;
} ) ;
2017-07-19 21:04:15 +02:00
} ) ;
2020-07-07 13:50:53 +02:00
describe ( 'is "time"' , function ( ) {
beforeEach ( function ( ) {
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
labels : [ '2017' , '2019' , '2020' , '2025' , '2042' ] ,
datasets : [ { data : [ 0 , 1 , 2 , 3 , 4 , 5 ] } ]
} ,
options : {
scales : {
x : {
type : 'time' ,
time : {
parser : 'YYYY'
} ,
ticks : {
source : 'labels'
}
2017-07-19 21:04:15 +02:00
} ,
2020-07-07 13:50:53 +02:00
y : {
display : false
2017-07-19 21:04:15 +02:00
}
2019-11-22 00:46:49 +01:00
}
2017-07-19 21:04:15 +02:00
}
2020-07-07 13:50:53 +02:00
} ) ;
2017-07-19 21:04:15 +02:00
} ) ;
2020-07-07 13:50:53 +02:00
it ( 'should space data out with a gap relative to their time values' , function ( ) {
var scale = this . chart . scales . x ;
var start = scale . left ;
var slice = scale . width / ( 2042 - 2017 ) ;
2017-07-19 21:04:15 +02:00
2020-07-07 13:50:53 +02:00
expect ( scale . getPixelForValue ( moment ( '2017' ) . valueOf ( ) , 0 ) ) . toBeCloseToPixel ( start ) ;
expect ( scale . getPixelForValue ( moment ( '2019' ) . valueOf ( ) , 1 ) ) . toBeCloseToPixel ( start + slice * ( 2019 - 2017 ) ) ;
expect ( scale . getPixelForValue ( moment ( '2020' ) . valueOf ( ) , 2 ) ) . toBeCloseToPixel ( start + slice * ( 2020 - 2017 ) ) ;
expect ( scale . getPixelForValue ( moment ( '2025' ) . valueOf ( ) , 3 ) ) . toBeCloseToPixel ( start + slice * ( 2025 - 2017 ) ) ;
expect ( scale . getPixelForValue ( moment ( '2042' ) . valueOf ( ) , 4 ) ) . toBeCloseToPixel ( start + slice * ( 2042 - 2017 ) ) ;
} ) ;
it ( 'should take in account scale min and max if outside the ticks range' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . x ;
options . min = '2012' ;
options . max = '2050' ;
chart . update ( ) ;
var start = scale . left ;
var slice = scale . width / ( 2050 - 2012 ) ;
expect ( scale . getPixelForValue ( moment ( '2017' ) . valueOf ( ) , 0 ) ) . toBeCloseToPixel ( start + slice * ( 2017 - 2012 ) ) ;
expect ( scale . getPixelForValue ( moment ( '2019' ) . valueOf ( ) , 1 ) ) . toBeCloseToPixel ( start + slice * ( 2019 - 2012 ) ) ;
expect ( scale . getPixelForValue ( moment ( '2020' ) . valueOf ( ) , 2 ) ) . toBeCloseToPixel ( start + slice * ( 2020 - 2012 ) ) ;
expect ( scale . getPixelForValue ( moment ( '2025' ) . valueOf ( ) , 3 ) ) . toBeCloseToPixel ( start + slice * ( 2025 - 2012 ) ) ;
expect ( scale . getPixelForValue ( moment ( '2042' ) . valueOf ( ) , 4 ) ) . toBeCloseToPixel ( start + slice * ( 2042 - 2012 ) ) ;
} ) ;
2017-07-19 21:04:15 +02:00
} ) ;
} ) ;
} ) ;
2017-07-25 10:12:53 +02:00
2017-08-02 07:28:27 +02:00
describe ( 'when bounds' , function ( ) {
2017-07-25 10:12:53 +02:00
describe ( 'is "data"' , function ( ) {
it ( 'should preserve the data range' , function ( ) {
var chart = window . acquireChart ( {
type : 'line' ,
data : {
labels : [ '02/20 08:00' , '02/21 09:00' , '02/22 10:00' , '02/23 11:00' ] ,
datasets : [ { data : [ 0 , 1 , 2 , 3 , 4 , 5 ] } ]
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2017-07-25 10:12:53 +02:00
type : 'time' ,
2017-08-02 07:28:27 +02:00
bounds : 'data' ,
2017-07-25 10:12:53 +02:00
time : {
parser : 'MM/DD HH:mm' ,
unit : 'day'
}
2019-11-22 00:46:49 +01:00
} ,
y : {
2017-07-25 10:12:53 +02:00
display : false
2019-11-22 00:46:49 +01:00
}
2017-07-25 10:12:53 +02:00
}
}
} ) ;
var scale = chart . scales . x ;
expect ( scale . min ) . toEqual ( + moment ( '02/20 08:00' , 'MM/DD HH:mm' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( '02/23 11:00' , 'MM/DD HH:mm' ) ) ;
2019-11-19 13:09:50 +01:00
expect ( scale . getPixelForValue ( moment ( '02/20 08:00' , 'MM/DD HH:mm' ) . valueOf ( ) ) ) . toBeCloseToPixel ( scale . left ) ;
expect ( scale . getPixelForValue ( moment ( '02/23 11:00' , 'MM/DD HH:mm' ) . valueOf ( ) ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [
2017-07-25 10:12:53 +02:00
'Feb 21' , 'Feb 22' , 'Feb 23' ] ) ;
} ) ;
} ) ;
describe ( 'is "labels"' , function ( ) {
it ( 'should preserve the label range' , function ( ) {
var chart = window . acquireChart ( {
type : 'line' ,
data : {
labels : [ '02/20 08:00' , '02/21 09:00' , '02/22 10:00' , '02/23 11:00' ] ,
datasets : [ { data : [ 0 , 1 , 2 , 3 , 4 , 5 ] } ]
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2017-07-25 10:12:53 +02:00
type : 'time' ,
2017-08-02 07:28:27 +02:00
bounds : 'ticks' ,
2017-07-25 10:12:53 +02:00
time : {
parser : 'MM/DD HH:mm' ,
unit : 'day'
}
2019-11-22 00:46:49 +01:00
} ,
y : {
2017-07-25 10:12:53 +02:00
display : false
2019-11-22 00:46:49 +01:00
}
2017-07-25 10:12:53 +02:00
}
}
} ) ;
var scale = chart . scales . x ;
2017-07-29 21:26:36 +02:00
var ticks = scale . getTicks ( ) ;
2017-07-25 10:12:53 +02:00
2017-07-29 21:26:36 +02:00
expect ( scale . min ) . toEqual ( ticks [ 0 ] . value ) ;
expect ( scale . max ) . toEqual ( ticks [ ticks . length - 1 ] . value ) ;
2019-11-19 13:09:50 +01:00
expect ( scale . getPixelForValue ( moment ( '02/20 08:00' , 'MM/DD HH:mm' ) . valueOf ( ) ) ) . toBeCloseToPixel ( 60 ) ;
expect ( scale . getPixelForValue ( moment ( '02/23 11:00' , 'MM/DD HH:mm' ) . valueOf ( ) ) ) . toBeCloseToPixel ( 426 ) ;
2019-10-31 23:45:13 +01:00
expect ( getLabels ( scale ) ) . toEqual ( [
2017-07-25 10:12:53 +02:00
'Feb 20' , 'Feb 21' , 'Feb 22' , 'Feb 23' , 'Feb 24' ] ) ;
} ) ;
} ) ;
} ) ;
2019-11-15 18:11:13 +01:00
describe ( 'when min and/or max are defined' , function ( ) {
2017-07-27 06:41:09 +02:00
[ 'auto' , 'data' , 'labels' ] . forEach ( function ( source ) {
2017-08-02 07:28:27 +02:00
[ 'data' , 'ticks' ] . forEach ( function ( bounds ) {
describe ( 'and ticks.source is "' + source + '" and bounds "' + bounds + '"' , function ( ) {
2017-07-25 10:12:53 +02:00
beforeEach ( function ( ) {
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
labels : [ '02/20 08:00' , '02/21 09:00' , '02/22 10:00' , '02/23 11:00' ] ,
datasets : [ { data : [ 0 , 1 , 2 , 3 , 4 , 5 ] } ]
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2017-07-25 10:12:53 +02:00
type : 'time' ,
2017-08-02 07:28:27 +02:00
bounds : bounds ,
2017-07-25 10:12:53 +02:00
time : {
parser : 'MM/DD HH:mm' ,
unit : 'day'
} ,
ticks : {
2017-08-02 07:28:27 +02:00
source : source
2017-07-25 10:12:53 +02:00
}
2019-11-22 00:46:49 +01:00
} ,
y : {
2017-07-25 10:12:53 +02:00
display : false
2019-11-22 00:46:49 +01:00
}
2017-07-25 10:12:53 +02:00
}
}
} ) ;
} ) ;
it ( 'should expand scale to the min/max range' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
2019-11-22 00:46:49 +01:00
var options = chart . options . scales . x ;
2017-07-25 10:12:53 +02:00
var min = '02/19 07:00' ;
var max = '02/24 08:00' ;
2019-11-19 13:09:50 +01:00
var minMillis = + moment ( min , 'MM/DD HH:mm' ) ;
var maxMillis = + moment ( max , 'MM/DD HH:mm' ) ;
2017-07-25 10:12:53 +02:00
2019-11-15 18:11:13 +01:00
options . min = min ;
options . max = max ;
2017-07-25 10:12:53 +02:00
chart . update ( ) ;
2019-11-19 13:09:50 +01:00
expect ( scale . min ) . toEqual ( minMillis ) ;
expect ( scale . max ) . toEqual ( maxMillis ) ;
expect ( scale . getPixelForValue ( minMillis ) ) . toBeCloseToPixel ( scale . left ) ;
expect ( scale . getPixelForValue ( maxMillis ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
2017-07-29 21:26:36 +02:00
scale . getTicks ( ) . forEach ( function ( tick ) {
2019-11-19 13:09:50 +01:00
expect ( tick . value >= minMillis ) . toBeTruthy ( ) ;
expect ( tick . value <= maxMillis ) . toBeTruthy ( ) ;
2017-07-27 06:41:09 +02:00
} ) ;
2017-07-25 10:12:53 +02:00
} ) ;
it ( 'should shrink scale to the min/max range' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
2019-11-22 00:46:49 +01:00
var options = chart . options . scales . x ;
2017-07-25 10:12:53 +02:00
var min = '02/21 07:00' ;
var max = '02/22 20:00' ;
2019-11-19 13:09:50 +01:00
var minMillis = + moment ( min , 'MM/DD HH:mm' ) ;
var maxMillis = + moment ( max , 'MM/DD HH:mm' ) ;
2017-07-25 10:12:53 +02:00
2019-11-15 18:11:13 +01:00
options . min = min ;
options . max = max ;
2017-07-25 10:12:53 +02:00
chart . update ( ) ;
2019-11-19 13:09:50 +01:00
expect ( scale . min ) . toEqual ( minMillis ) ;
expect ( scale . max ) . toEqual ( maxMillis ) ;
expect ( scale . getPixelForValue ( minMillis ) ) . toBeCloseToPixel ( scale . left ) ;
expect ( scale . getPixelForValue ( maxMillis ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
2017-07-29 21:26:36 +02:00
scale . getTicks ( ) . forEach ( function ( tick ) {
2019-11-19 13:09:50 +01:00
expect ( tick . value >= minMillis ) . toBeTruthy ( ) ;
expect ( tick . value <= maxMillis ) . toBeTruthy ( ) ;
2017-07-27 06:41:09 +02:00
} ) ;
2017-07-25 10:12:53 +02:00
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
2017-08-14 10:09:33 +02:00
[ 'auto' , 'data' , 'labels' ] . forEach ( function ( source ) {
2020-06-19 16:21:39 +02:00
[ 'timeseries' , 'time' ] . forEach ( function ( type ) {
describe ( 'when ticks.source is "' + source + '" and scale type is "' + type + '"' , function ( ) {
2017-08-14 10:09:33 +02:00
beforeEach ( function ( ) {
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
2020-02-09 00:08:42 +01:00
labels : [ '2017' , '2018' , '2019' , '2020' , '2021' ] ,
2020-01-07 13:26:29 +01:00
datasets : [ { data : [ 0 , 1 , 2 , 3 , 4 ] } ]
2017-08-14 10:09:33 +02:00
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2020-06-19 16:21:39 +02:00
type : type ,
2017-08-14 10:09:33 +02:00
time : {
2020-02-09 00:08:42 +01:00
parser : 'YYYY' ,
unit : 'year'
2017-08-14 10:09:33 +02:00
} ,
ticks : {
source : source
2020-06-19 16:21:39 +02:00
}
2019-11-22 00:46:49 +01:00
}
2017-08-14 10:09:33 +02:00
}
}
} ) ;
} ) ;
it ( 'should not add offset from the edges' , function ( ) {
var scale = this . chart . scales . x ;
2019-11-19 13:09:50 +01:00
expect ( scale . getPixelForValue ( moment ( '2017' ) . valueOf ( ) ) ) . toBeCloseToPixel ( scale . left ) ;
2020-02-09 00:08:42 +01:00
expect ( scale . getPixelForValue ( moment ( '2021' ) . valueOf ( ) ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
2017-08-14 10:09:33 +02:00
} ) ;
it ( 'should add offset from the edges if offset is true' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
2019-11-22 00:46:49 +01:00
var options = chart . options . scales . x ;
2017-08-14 10:09:33 +02:00
options . offset = true ;
chart . update ( ) ;
var numTicks = scale . ticks . length ;
var firstTickInterval = scale . getPixelForTick ( 1 ) - scale . getPixelForTick ( 0 ) ;
var lastTickInterval = scale . getPixelForTick ( numTicks - 1 ) - scale . getPixelForTick ( numTicks - 2 ) ;
2019-11-19 13:09:50 +01:00
expect ( scale . getPixelForValue ( moment ( '2017' ) . valueOf ( ) ) ) . toBeCloseToPixel ( scale . left + firstTickInterval / 2 ) ;
2020-02-09 00:08:42 +01:00
expect ( scale . getPixelForValue ( moment ( '2021' ) . valueOf ( ) ) ) . toBeCloseToPixel ( scale . left + scale . width - lastTickInterval / 2 ) ;
2017-08-14 10:09:33 +02:00
} ) ;
it ( 'should not add offset if min and max extend the labels range' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
2019-11-22 00:46:49 +01:00
var options = chart . options . scales . x ;
2017-08-14 10:09:33 +02:00
2019-11-15 18:11:13 +01:00
options . min = '2012' ;
options . max = '2051' ;
2017-08-14 10:09:33 +02:00
chart . update ( ) ;
2019-11-19 13:09:50 +01:00
expect ( scale . getPixelForValue ( moment ( '2012' ) . valueOf ( ) ) ) . toBeCloseToPixel ( scale . left ) ;
expect ( scale . getPixelForValue ( moment ( '2051' ) . valueOf ( ) ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
2017-08-14 10:09:33 +02:00
} ) ;
2019-11-14 13:15:44 +01:00
} ) ;
} ) ;
} ) ;
2020-07-06 15:34:43 +02:00
it ( 'should handle offset when there are more data points than ticks' , function ( ) {
const chart = window . acquireChart ( {
type : 'bar' ,
data : {
datasets : [ {
data : [ { x : 631180800000 , y : '31.84' } , { x : 631267200000 , y : '30.89' } , { x : 631353600000 , y : '33.00' } , { x : 631440000000 , y : '33.52' } , { x : 631526400000 , y : '32.24' } , { x : 631785600000 , y : '32.74' } , { x : 631872000000 , y : '31.45' } , { x : 631958400000 , y : '32.60' } , { x : 632044800000 , y : '31.77' } , { x : 632131200000 , y : '32.45' } , { x : 632390400000 , y : '31.13' } , { x : 632476800000 , y : '31.82' } , { x : 632563200000 , y : '30.81' } , { x : 632649600000 , y : '30.07' } , { x : 632736000000 , y : '29.31' } , { x : 632995200000 , y : '29.82' } , { x : 633081600000 , y : '30.20' } , { x : 633168000000 , y : '30.78' } , { x : 633254400000 , y : '30.72' } , { x : 633340800000 , y : '31.62' } , { x : 633600000000 , y : '30.64' } , { x : 633686400000 , y : '32.36' } , { x : 633772800000 , y : '34.66' } , { x : 633859200000 , y : '33.96' } , { x : 633945600000 , y : '34.20' } , { x : 634204800000 , y : '32.20' } , { x : 634291200000 , y : '32.44' } , { x : 634377600000 , y : '32.72' } , { x : 634464000000 , y : '32.95' } , { x : 634550400000 , y : '32.95' } , { x : 634809600000 , y : '30.88' } , { x : 634896000000 , y : '29.44' } , { x : 634982400000 , y : '29.36' } , { x : 635068800000 , y : '28.84' } , { x : 635155200000 , y : '30.85' } , { x : 635414400000 , y : '32.00' } , { x : 635500800000 , y : '32.74' } , { x : 635587200000 , y : '33.16' } , { x : 635673600000 , y : '34.73' } , { x : 635760000000 , y : '32.89' } , { x : 636019200000 , y : '32.41' } , { x : 636105600000 , y : '31.15' } , { x : 636192000000 , y : '30.63' } , { x : 636278400000 , y : '29.60' } , { x : 636364800000 , y : '29.31' } , { x : 636624000000 , y : '29.83' } , { x : 636710400000 , y : '27.97' } , { x : 636796800000 , y : '26.18' } , { x : 636883200000 , y : '26.06' } , { x : 636969600000 , y : '26.34' } , { x : 637228800000 , y : '27.75' } , { x : 637315200000 , y : '29.05' } , { x : 637401600000 , y : '28.82' } , { x : 637488000000 , y : '29.43' } , { x : 637574400000 , y : '29.53' } , { x : 637833600000 , y : '28.50' } , { x : 637920000000 , y : '28.87' } , { x : 638006400000 , y : '28.11' } , { x : 638092800000 , y : '27.79' } , { x : 638179200000 , y : '28.18' } , { x : 638438400000 , y : '28.27' } , { x : 638524800000 , y : '28.29' } , { x : 638611200000 , y : '29.63' } , { x : 638697600000 , y : '29.13' } , { x : 638784000000 , y : '26.57' } , { x : 639039600000 , y : '27.19' } , { x : 639126000000 , y : '27.48' } , { x : 639212400000 , y : '27.79' } , { x : 639298800000 , y : '28.48' } , { x : 639385200000 , y : '27.88' } , { x : 639644400000 , y : '25.63' } , { x : 639730800000 , y : '25.02' } , { x : 639817200000 , y : '25.26' } , { x : 639903600000 , y : '25.00' } , { x : 639990000000 , y : '26.23' } , { x : 640249200000 , y : '26.22' } , { x : 640335600000 , y : '26.36' } , { x : 640422000000 , y : '25.45' } , { x : 640508400000 , y : '24.62' } , { x : 640594800000 , y : '26.65' } , { x : 640854000000 , y : '26.28' } , { x : 640940400000 , y : '27.25' } , { x : 641026800000 , y : '25.93' } ] ,
backgroundColor : '#ff6666'
} ]
} ,
options : {
scales : {
x : {
type : 'timeseries' ,
offset : true ,
ticks : {
source : 'data' ,
autoSkip : true ,
maxRotation : 0
}
} ,
y : {
type : 'linear' ,
gridLines : {
drawBorder : false
}
}
}
} ,
legend : false
} ) ;
const scale = chart . scales . x ;
expect ( scale . getPixelForDecimal ( 0 ) ) . toBeCloseToPixel ( 29 ) ;
expect ( scale . getPixelForDecimal ( 1.0 ) ) . toBeCloseToPixel ( 494 ) ;
} ) ;
2019-11-14 13:15:44 +01:00
[ 'data' , 'labels' ] . forEach ( function ( source ) {
2020-06-19 16:21:39 +02:00
[ 'timeseries' , 'time' ] . forEach ( function ( type ) {
describe ( 'when ticks.source is "' + source + '" and scale type is "' + type + '"' , function ( ) {
2019-11-14 13:15:44 +01:00
beforeEach ( function ( ) {
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
labels : [ '2017' , '2019' , '2020' , '2025' , '2042' ] ,
datasets : [ { data : [ 0 , 1 , 2 , 3 , 4 , 5 ] } ]
} ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2019-11-14 13:15:44 +01:00
id : 'x' ,
2020-06-19 16:21:39 +02:00
type : type ,
2019-11-14 13:15:44 +01:00
time : {
parser : 'YYYY'
} ,
ticks : {
source : source
2020-06-19 16:21:39 +02:00
}
2019-11-22 00:46:49 +01:00
}
2019-11-14 13:15:44 +01:00
}
}
} ) ;
} ) ;
2017-08-14 10:09:33 +02:00
2019-05-12 16:52:30 +02:00
it ( 'should add offset if min and max extend the labels range and offset is true' , function ( ) {
2017-08-14 10:09:33 +02:00
var chart = this . chart ;
var scale = chart . scales . x ;
2019-11-22 00:46:49 +01:00
var options = chart . options . scales . x ;
2017-08-14 10:09:33 +02:00
2019-11-15 18:11:13 +01:00
options . min = '2012' ;
options . max = '2051' ;
2017-08-14 10:09:33 +02:00
options . offset = true ;
chart . update ( ) ;
2019-05-12 16:52:30 +02:00
var numTicks = scale . ticks . length ;
var firstTickInterval = scale . getPixelForTick ( 1 ) - scale . getPixelForTick ( 0 ) ;
var lastTickInterval = scale . getPixelForTick ( numTicks - 1 ) - scale . getPixelForTick ( numTicks - 2 ) ;
2019-11-19 13:09:50 +01:00
expect ( scale . getPixelForValue ( moment ( '2012' ) . valueOf ( ) ) ) . toBeCloseToPixel ( scale . left + firstTickInterval / 2 ) ;
expect ( scale . getPixelForValue ( moment ( '2051' ) . valueOf ( ) ) ) . toBeCloseToPixel ( scale . left + scale . width - lastTickInterval / 2 ) ;
2017-08-14 10:09:33 +02:00
} ) ;
} ) ;
} ) ;
} ) ;
2018-12-20 16:08:13 +01:00
2019-01-11 08:03:28 +01:00
describe ( 'Deprecations' , function ( ) {
describe ( 'options.time.displayFormats' , function ( ) {
it ( 'should generate defaults from adapter presets' , function ( ) {
var chart = window . acquireChart ( {
type : 'line' ,
data : { } ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2019-01-11 08:03:28 +01:00
type : 'time'
2019-11-22 00:46:49 +01:00
}
2019-01-11 08:03:28 +01:00
}
}
} ) ;
// NOTE: built-in adapter uses moment
var expected = {
2019-02-18 19:45:38 +01:00
datetime : 'MMM D, YYYY, h:mm:ss a' ,
2019-01-11 08:03:28 +01:00
millisecond : 'h:mm:ss.SSS a' ,
second : 'h:mm:ss a' ,
minute : 'h:mm a' ,
hour : 'hA' ,
day : 'MMM D' ,
week : 'll' ,
month : 'MMM YYYY' ,
quarter : '[Q]Q - YYYY' ,
year : 'YYYY'
} ;
expect ( chart . scales . x . options . time . displayFormats ) . toEqual ( expected ) ;
2019-11-22 00:46:49 +01:00
expect ( chart . options . scales . x . time . displayFormats ) . toEqual ( expected ) ;
2019-01-11 08:03:28 +01:00
} ) ;
it ( 'should merge user formats with adapter presets' , function ( ) {
var chart = window . acquireChart ( {
type : 'line' ,
data : { } ,
options : {
scales : {
2019-11-22 00:46:49 +01:00
x : {
2019-01-11 08:03:28 +01:00
type : 'time' ,
time : {
displayFormats : {
millisecond : 'foo' ,
hour : 'bar' ,
month : 'bla'
}
}
2019-11-22 00:46:49 +01:00
}
2019-01-11 08:03:28 +01:00
}
}
} ) ;
// NOTE: built-in adapter uses moment
var expected = {
2019-02-18 19:45:38 +01:00
datetime : 'MMM D, YYYY, h:mm:ss a' ,
2019-01-11 08:03:28 +01:00
millisecond : 'foo' ,
second : 'h:mm:ss a' ,
minute : 'h:mm a' ,
hour : 'bar' ,
day : 'MMM D' ,
week : 'll' ,
month : 'bla' ,
quarter : '[Q]Q - YYYY' ,
year : 'YYYY'
} ;
expect ( chart . scales . x . options . time . displayFormats ) . toEqual ( expected ) ;
2019-11-22 00:46:49 +01:00
expect ( chart . options . scales . x . time . displayFormats ) . toEqual ( expected ) ;
2019-01-11 08:03:28 +01:00
} ) ;
} ) ;
} ) ;
2015-10-17 05:14:07 +02:00
} ) ;