2015-09-27 15:38:11 +02:00
// Time scale tests
describe ( 'Time scale tests' , function ( ) {
2017-04-02 14:49:00 +02:00
function createScale ( data , options ) {
var scaleID = 'myScale' ;
var mockContext = window . createMockContext ( ) ;
var Constructor = Chart . scaleService . getScaleConstructor ( 'time' ) ;
var scale = new Constructor ( {
ctx : mockContext ,
options : options ,
chart : {
data : data
} ,
id : scaleID
} ) ;
scale . update ( 400 , 50 ) ;
return scale ;
}
2017-07-29 21:26:36 +02:00
function getTicksLabels ( scale ) {
return scale . ticks ;
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 {
compare : function ( actual , expected ) {
var result = false ;
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 ) ;
} ) ;
2017-07-22 14:22:44 +02:00
it ( 'should register the constructor with the scale service' , function ( ) {
2015-09-27 15:38:11 +02:00
var Constructor = Chart . scaleService . getScaleConstructor ( 'time' ) ;
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 ( ) {
2015-09-27 15:38:11 +02:00
var defaultConfig = Chart . scaleService . getScaleDefaults ( 'time' ) ;
expect ( defaultConfig ) . toEqual ( {
display : true ,
gridLines : {
2016-10-16 23:34:59 +02:00
color : 'rgba(0, 0, 0, 0.1)' ,
2016-05-17 21:11:29 +02:00
drawBorder : true ,
2015-09-27 15:38:11 +02:00
drawOnChartArea : true ,
drawTicks : true ,
2016-04-17 03:49:08 +02:00
tickMarkLength : 10 ,
2015-09-27 15:38:11 +02:00
lineWidth : 1 ,
offsetGridLines : false ,
2015-11-25 01:18:15 +01:00
display : true ,
2016-10-16 23:34:59 +02:00
zeroLineColor : 'rgba(0,0,0,0.25)' ,
2016-08-12 18:31:54 +02:00
zeroLineWidth : 1 ,
2017-03-21 11:38:09 +01:00
zeroLineBorderDash : [ ] ,
zeroLineBorderDashOffset : 0.0 ,
2016-08-12 18:31:54 +02:00
borderDash : [ ] ,
borderDashOffset : 0.0
2015-09-27 15:38:11 +02:00
} ,
2016-10-16 23:34:59 +02:00
position : 'bottom' ,
2017-08-14 10:09:33 +02:00
offset : false ,
2017-09-10 19:15:47 +02:00
scaleLabel : Chart . defaults . scale . scaleLabel ,
2017-08-02 07:28:27 +02:00
bounds : 'data' ,
2017-07-30 21:24:29 +02:00
distribution : 'linear' ,
2019-02-21 08:11:32 +01:00
adapters : { } ,
2015-09-27 15:38:11 +02:00
ticks : {
beginAtZero : false ,
2016-05-10 13:10:01 +02:00
minRotation : 0 ,
2016-04-17 00:38:03 +02:00
maxRotation : 50 ,
2015-09-27 15:38:11 +02:00
mirror : false ,
2017-07-19 21:04:15 +02:00
source : 'auto' ,
2016-11-05 01:41:54 +01:00
padding : 0 ,
2015-09-27 15:38:11 +02:00
reverse : false ,
2015-11-25 01:18:15 +01:00
display : true ,
2015-12-21 08:43:48 +01:00
callback : defaultConfig . ticks . callback , // make this nicer, then check explicitly below,
2016-02-15 15:41:35 +01:00
autoSkip : false ,
2016-05-11 11:43:22 +02:00
autoSkipPadding : 0 ,
2017-06-15 13:20:16 +02:00
labelOffset : 0 ,
minor : { } ,
2017-09-09 00:25:51 +02:00
major : {
enabled : false
} ,
2015-09-27 15:38:11 +02:00
} ,
time : {
2016-03-02 16:40:58 +01:00
parser : false ,
2015-09-27 15:38:11 +02:00
format : false ,
unit : false ,
round : false ,
2016-05-11 10:34:22 +02:00
isoWeekday : false ,
2015-09-27 15:38:11 +02:00
displayFormat : false ,
Suport minUnit for time scale
When dealing with time-delineated datasets, often we have data for known
intervals of time. For example, we may have a dataset which represents number
of purchases per day:
```json
{
labels: ['2016-01-01', '2016-01-02', '2016-01-03']
datasets: [
{
data: [12, 87, 42]
}
],
'...': '...'
}
```
In this case, Chart.js will attempt to figure out the best interval to display
the data, and could pick `hours` as the unit. However, in this case, we would
prefer to just use the `days` interval since our data's granularity can not be
represented well with `hours`.
To remedy this, this commit adds the `minUnit` option which allows
users to (optionally) specify what the minimum unit they would like
to use.
2016-09-02 22:39:25 +02:00
minUnit : 'millisecond' ,
2019-01-11 08:03:28 +01:00
displayFormats : { }
2015-09-27 15:38:11 +02:00
}
} ) ;
2015-10-19 01:48:57 +02:00
// Is this actually a function
expect ( defaultConfig . ticks . callback ) . toEqual ( jasmine . any ( Function ) ) ;
2015-09-27 15:38:11 +02:00
} ) ;
2017-04-02 14:49:00 +02:00
describe ( 'when given inputs of different types' , function ( ) {
2015-12-05 22:45:31 +01:00
// Helper to build date objects
function newDateFromRef ( days ) {
return moment ( '01/01/2015 12:00' , 'DD/MM/YYYY HH:mm' ) . add ( days , 'd' ) . toDate ( ) ;
}
2017-04-02 14:49:00 +02:00
it ( 'should accept labels as strings' , function ( ) {
var mockData = {
labels : [ '2015-01-01T12: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
} ;
2015-12-05 22:45:31 +01:00
2017-05-30 00:40:10 +02:00
var scaleOptions = Chart . scaleService . getScaleDefaults ( 'time' ) ;
var scale = createScale ( mockData , scaleOptions ) ;
2017-04-02 14:49:00 +02:00
scale . update ( 1000 , 200 ) ;
2017-07-29 21:26:36 +02:00
var ticks = getTicksLabels ( scale ) ;
2017-06-15 13:20:16 +02:00
2017-08-02 07:28:27 +02:00
// `bounds === 'data'`: first and last ticks removed since outside the data range
2017-07-25 10:12:53 +02:00
expect ( ticks ) . toEqual ( [ 'Jan 2' , 'Jan 3' , 'Jan 4' , 'Jan 5' , 'Jan 6' , 'Jan 7' , 'Jan 8' , 'Jan 9' , 'Jan 10' ] ) ;
2015-12-05 22:45:31 +01:00
} ) ;
2017-04-02 14:49:00 +02:00
it ( 'should accept labels as date objects' , function ( ) {
var mockData = {
labels : [ newDateFromRef ( 0 ) , newDateFromRef ( 1 ) , newDateFromRef ( 2 ) , newDateFromRef ( 4 ) , newDateFromRef ( 6 ) , newDateFromRef ( 7 ) , newDateFromRef ( 9 ) ] , // days
} ;
var scale = createScale ( mockData , Chart . scaleService . getScaleDefaults ( 'time' ) ) ;
scale . update ( 1000 , 200 ) ;
2017-07-29 21:26:36 +02:00
var ticks = getTicksLabels ( scale ) ;
2017-06-15 13:20:16 +02:00
2017-08-02 07:28:27 +02:00
// `bounds === 'data'`: first and last ticks removed since outside the data range
2017-07-25 10:12:53 +02:00
expect ( ticks ) . toEqual ( [ 'Jan 2' , 'Jan 3' , 'Jan 4' , 'Jan 5' , 'Jan 6' , 'Jan 7' , 'Jan 8' , 'Jan 9' , 'Jan 10' ] ) ;
2017-04-02 14:49:00 +02:00
} ) ;
2016-01-23 18:44:55 +01:00
2017-04-02 14:49:00 +02:00
it ( 'should accept data as xy points' , function ( ) {
var chart = window . acquireChart ( {
type : 'line' ,
data : {
datasets : [ {
xAxisID : 'xScale0' ,
data : [ {
x : newDateFromRef ( 0 ) ,
y : 1
} , {
x : newDateFromRef ( 1 ) ,
y : 10
} , {
x : newDateFromRef ( 2 ) ,
y : 0
} , {
x : newDateFromRef ( 4 ) ,
y : 5
} , {
x : newDateFromRef ( 6 ) ,
y : 77
} , {
x : newDateFromRef ( 7 ) ,
y : 9
} , {
x : newDateFromRef ( 9 ) ,
y : 5
} ]
2016-04-28 03:50:31 +02:00
} ] ,
2017-04-02 14:49:00 +02:00
} ,
options : {
scales : {
xAxes : [ {
id : 'xScale0' ,
type : 'time' ,
position : 'bottom'
} ] ,
}
2016-04-28 03:50:31 +02:00
}
2017-04-02 14:49:00 +02:00
} ) ;
2016-01-23 18:44:55 +01:00
2017-04-02 14:49:00 +02:00
var xScale = chart . scales . xScale0 ;
xScale . update ( 800 , 200 ) ;
2017-07-29 21:26:36 +02:00
var ticks = getTicksLabels ( xScale ) ;
2017-06-15 13:20:16 +02:00
2017-08-02 07:28:27 +02:00
// `bounds === 'data'`: first and last ticks removed since outside the data range
2017-07-25 10:12:53 +02:00
expect ( ticks ) . toEqual ( [ 'Jan 2' , 'Jan 3' , 'Jan 4' , 'Jan 5' , 'Jan 6' , 'Jan 7' , 'Jan 8' , 'Jan 9' , 'Jan 10' ] ) ;
2017-04-02 14:49:00 +02:00
} ) ;
2017-07-22 08:33:22 +02:00
it ( 'should accept data as ty points' , function ( ) {
var chart = window . acquireChart ( {
type : 'line' ,
data : {
datasets : [ {
xAxisID : 'tScale0' ,
data : [ {
t : newDateFromRef ( 0 ) ,
y : 1
} , {
t : newDateFromRef ( 1 ) ,
y : 10
} , {
t : newDateFromRef ( 2 ) ,
y : 0
} , {
t : newDateFromRef ( 4 ) ,
y : 5
} , {
t : newDateFromRef ( 6 ) ,
y : 77
} , {
t : newDateFromRef ( 7 ) ,
y : 9
} , {
t : newDateFromRef ( 9 ) ,
y : 5
} ]
} ] ,
} ,
options : {
scales : {
xAxes : [ {
id : 'tScale0' ,
type : 'time' ,
position : 'bottom'
} ] ,
}
}
} ) ;
var tScale = chart . scales . tScale0 ;
tScale . update ( 800 , 200 ) ;
2017-07-29 21:26:36 +02:00
var ticks = getTicksLabels ( tScale ) ;
2017-07-22 08:33:22 +02:00
2017-08-02 07:28:27 +02:00
// `bounds === 'data'`: first and last ticks removed since outside the data range
2017-07-25 10:12:53 +02:00
expect ( ticks ) . toEqual ( [ 'Jan 2' , 'Jan 3' , 'Jan 4' , 'Jan 5' , 'Jan 6' , 'Jan 7' , 'Jan 8' , 'Jan 9' , 'Jan 10' ] ) ;
2017-07-22 08:33:22 +02:00
} ) ;
2016-01-23 18:44:55 +01:00
} ) ;
2016-03-02 16:40:58 +01:00
it ( 'should allow custom time parsers' , function ( ) {
2016-09-06 11:10:44 +02:00
var chart = window . acquireChart ( {
2016-04-28 03:50:31 +02:00
type : 'line' ,
data : {
2017-07-25 10:12:53 +02:00
labels : [ 'foo' , 'bar' ] ,
2016-04-28 03:50:31 +02:00
datasets : [ {
xAxisID : 'xScale0' ,
2017-07-25 10:12:53 +02:00
data : [ 0 , 1 ]
2016-04-28 03:50:31 +02:00
} ] ,
2016-03-02 16:40:58 +01:00
} ,
2016-04-28 03:50:31 +02:00
options : {
scales : {
xAxes : [ {
id : 'xScale0' ,
type : 'time' ,
position : 'bottom' ,
time : {
unit : 'day' ,
round : true ,
2016-10-16 23:34:59 +02:00
parser : function ( label ) {
2017-07-25 10:12:53 +02:00
return label === 'foo' ?
2017-07-27 06:41:09 +02:00
moment ( '2000/01/02' , 'YYYY/MM/DD' ) :
moment ( '2016/05/08' , 'YYYY/MM/DD' ) ;
2016-04-28 03:50:31 +02:00
}
2017-07-25 10:12:53 +02:00
} ,
ticks : {
source : 'labels'
2016-04-28 03:50:31 +02:00
}
} ] ,
}
}
2016-03-02 16:40:58 +01:00
} ) ;
// Counts down because the lines are drawn top to bottom
2016-09-06 11:10:44 +02:00
var xScale = chart . scales . xScale0 ;
2016-04-28 03:50:31 +02:00
// Counts down because the lines are drawn top to bottom
2017-07-29 21:26:36 +02:00
expect ( xScale . ticks [ 0 ] ) . toBe ( 'Jan 2' ) ;
expect ( xScale . ticks [ 1 ] ) . toBe ( 'May 8' ) ;
2016-03-02 16:40:58 +01:00
} ) ;
2015-10-17 05:14:07 +02:00
it ( 'should build ticks using the config unit' , function ( ) {
2015-09-27 15:38:11 +02:00
var mockData = {
2016-10-16 23:34:59 +02:00
labels : [ '2015-01-01T20:00:00' , '2015-01-02T21:00:00' ] , // days
2015-09-27 15:38:11 +02:00
} ;
2016-01-23 18:44:55 +01:00
var config = Chart . helpers . clone ( Chart . scaleService . getScaleDefaults ( 'time' ) ) ;
2015-09-27 15:38:11 +02:00
config . time . unit = 'hour' ;
2017-04-02 14:49:00 +02:00
var scale = createScale ( mockData , config ) ;
scale . update ( 2500 , 200 ) ;
2017-07-29 21:26:36 +02:00
var ticks = getTicksLabels ( scale ) ;
2017-05-30 00:40:10 +02:00
2017-09-09 00:25:51 +02:00
expect ( ticks ) . toEqual ( [ '8PM' , '9PM' , '10PM' , '11PM' , '12AM' , '1AM' , '2AM' , '3AM' , '4AM' , '5AM' , '6AM' , '7AM' , '8AM' , '9AM' , '10AM' , '11AM' , '12PM' , '1PM' , '2PM' , '3PM' , '4PM' , '5PM' , '6PM' , '7PM' , '8PM' , '9PM' ] ) ;
2015-09-27 15:38:11 +02:00
} ) ;
2019-04-26 09:43:36 +02:00
it ( 'should build ticks honoring the minUnit' , function ( ) {
Suport minUnit for time scale
When dealing with time-delineated datasets, often we have data for known
intervals of time. For example, we may have a dataset which represents number
of purchases per day:
```json
{
labels: ['2016-01-01', '2016-01-02', '2016-01-03']
datasets: [
{
data: [12, 87, 42]
}
],
'...': '...'
}
```
In this case, Chart.js will attempt to figure out the best interval to display
the data, and could pick `hours` as the unit. However, in this case, we would
prefer to just use the `days` interval since our data's granularity can not be
represented well with `hours`.
To remedy this, this commit adds the `minUnit` option which allows
users to (optionally) specify what the minimum unit they would like
to use.
2016-09-02 22:39:25 +02:00
var mockData = {
2016-10-16 23:34:59 +02:00
labels : [ '2015-01-01T20:00:00' , '2015-01-02T21:00:00' ] , // days
Suport minUnit for time scale
When dealing with time-delineated datasets, often we have data for known
intervals of time. For example, we may have a dataset which represents number
of purchases per day:
```json
{
labels: ['2016-01-01', '2016-01-02', '2016-01-03']
datasets: [
{
data: [12, 87, 42]
}
],
'...': '...'
}
```
In this case, Chart.js will attempt to figure out the best interval to display
the data, and could pick `hours` as the unit. However, in this case, we would
prefer to just use the `days` interval since our data's granularity can not be
represented well with `hours`.
To remedy this, this commit adds the `minUnit` option which allows
users to (optionally) specify what the minimum unit they would like
to use.
2016-09-02 22:39:25 +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 : {
minUnit : 'day'
}
} , Chart . scaleService . getScaleDefaults ( 'time' ) ) ;
Suport minUnit for time scale
When dealing with time-delineated datasets, often we have data for known
intervals of time. For example, we may have a dataset which represents number
of purchases per day:
```json
{
labels: ['2016-01-01', '2016-01-02', '2016-01-03']
datasets: [
{
data: [12, 87, 42]
}
],
'...': '...'
}
```
In this case, Chart.js will attempt to figure out the best interval to display
the data, and could pick `hours` as the unit. However, in this case, we would
prefer to just use the `days` interval since our data's granularity can not be
represented well with `hours`.
To remedy this, this commit adds the `minUnit` option which allows
users to (optionally) specify what the minimum unit they would like
to use.
2016-09-02 22:39:25 +02:00
2017-04-02 14:49:00 +02:00
var scale = createScale ( mockData , config ) ;
2017-07-29 21:26:36 +02:00
var ticks = getTicksLabels ( scale ) ;
2017-06-15 13:20:16 +02:00
2017-09-09 00:25:51 +02:00
expect ( ticks ) . toEqual ( [ 'Jan 1' , 'Jan 2' , 'Jan 3' ] ) ;
Suport minUnit for time scale
When dealing with time-delineated datasets, often we have data for known
intervals of time. For example, we may have a dataset which represents number
of purchases per day:
```json
{
labels: ['2016-01-01', '2016-01-02', '2016-01-03']
datasets: [
{
data: [12, 87, 42]
}
],
'...': '...'
}
```
In this case, Chart.js will attempt to figure out the best interval to display
the data, and could pick `hours` as the unit. However, in this case, we would
prefer to just use the `days` interval since our data's granularity can not be
represented well with `hours`.
To remedy this, this commit adds the `minUnit` option which allows
users to (optionally) specify what the minimum unit they would like
to use.
2016-09-02 22:39:25 +02:00
} ) ;
2019-04-26 09:43:36 +02:00
it ( 'should build ticks based on the appropriate label capacity' , function ( ) {
var mockData = {
labels : [
'2012-01-01' , '2013-01-01' , '2014-01-01' , '2015-01-01' ,
'2016-01-01' , '2017-01-01' , '2018-01-01' , '2019-01-01'
]
} ;
var config = Chart . helpers . mergeIf ( {
time : {
unit : 'year'
}
} , Chart . scaleService . getScaleDefaults ( 'time' ) ) ;
var scale = createScale ( mockData , config ) ;
var ticks = getTicksLabels ( scale ) ;
expect ( ticks ) . toEqual ( [ '2012' , '2013' , '2014' , '2015' , '2016' , '2017' , '2018' , '2019' ] ) ;
} ) ;
2015-10-17 05:14:07 +02:00
it ( 'should build ticks using the config diff' , function ( ) {
2015-09-27 15:38:11 +02:00
var mockData = {
2016-10-16 23:34:59 +02:00
labels : [ '2015-01-01T20:00:00' , '2015-02-02T21:00:00' , '2015-02-21T01:00:00' ] , // days
2015-09-27 15:38:11 +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' ,
round : 'week'
}
} , Chart . scaleService . getScaleDefaults ( 'time' ) ) ;
2015-09-27 15:38:11 +02:00
2017-04-02 14:49:00 +02:00
var scale = createScale ( mockData , config ) ;
scale . update ( 800 , 200 ) ;
2017-07-29 21:26:36 +02:00
var ticks = getTicksLabels ( scale ) ;
2016-01-30 22:35:10 +01:00
// last date is feb 15 because we round to start of week
2017-09-09 00:25:51 +02:00
expect ( ticks ) . toEqual ( [ 'Dec 28, 2014' , 'Jan 4, 2015' , 'Jan 11, 2015' , 'Jan 18, 2015' , 'Jan 25, 2015' , 'Feb 1, 2015' , 'Feb 8, 2015' , 'Feb 15, 2015' ] ) ;
2015-09-27 15:38:11 +02:00
} ) ;
2017-06-25 14:37:38 +02:00
describe ( 'config step size' , function ( ) {
it ( 'should use the stepSize property' , function ( ) {
var mockData = {
labels : [ '2015-01-01T20:00:00' , '2015-01-01T21:00: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 : 'hour' ,
stepSize : 2
}
} , Chart . scaleService . getScaleDefaults ( 'time' ) ) ;
2017-06-25 14:37:38 +02:00
var scale = createScale ( mockData , config ) ;
scale . update ( 2500 , 200 ) ;
2017-07-29 21:26:36 +02:00
var ticks = getTicksLabels ( scale ) ;
2017-06-25 14:37:38 +02:00
expect ( ticks ) . toEqual ( [ '8PM' , '10PM' ] ) ;
} ) ;
} ) ;
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 ( ) {
config = Chart . helpers . clone ( Chart . scaleService . getScaleDefaults ( '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 ( ) {
config . ticks . min = '2014-12-29T04:00:00' ;
var scale = createScale ( mockData , config ) ;
expect ( scale . ticks [ 0 ] ) . toEqual ( 'Jan 1' ) ;
} ) ;
it ( 'should use the min option when greater than first label for building ticks' , function ( ) {
config . ticks . min = '2015-01-02T04:00:00' ;
var scale = createScale ( mockData , config ) ;
expect ( scale . ticks [ 0 ] ) . toEqual ( 'Jan 2' ) ;
} ) ;
it ( 'should use the max option when greater than last label for building ticks' , function ( ) {
config . ticks . max = '2015-01-05T06:00:00' ;
var scale = createScale ( mockData , config ) ;
expect ( scale . ticks [ scale . ticks . length - 1 ] ) . toEqual ( 'Jan 3' ) ;
} ) ;
it ( 'should use the max option when less than last label for building ticks' , function ( ) {
config . ticks . max = '2015-01-02T23:00:00' ;
var scale = createScale ( mockData , config ) ;
expect ( scale . ticks [ scale . ticks . length - 1 ] ) . toEqual ( 'Jan 2' ) ;
} ) ;
} ) ;
describe ( 'when specifying limits in a deprecated fashion' , function ( ) {
var mockData = {
labels : [ '2015-01-01T20:00:00' , '2015-01-02T20:00:00' , '2015-01-03T20:00:00' ] ,
} ;
var config ;
beforeEach ( function ( ) {
config = Chart . helpers . clone ( Chart . scaleService . getScaleDefaults ( 'time' ) ) ;
config . ticks . source = 'labels' ;
config . time . unit = 'day' ;
} ) ;
2017-10-20 09:03:38 +02:00
it ( 'should use the min option when less than first label for building ticks' , function ( ) {
2017-04-02 14:49:00 +02:00
config . time . min = '2014-12-29T04:00:00' ;
var scale = createScale ( mockData , config ) ;
2017-10-20 09:03:38 +02:00
expect ( scale . ticks [ 0 ] ) . toEqual ( 'Jan 1' ) ;
2017-04-02 14:49:00 +02:00
} ) ;
2017-10-20 09:03:38 +02:00
it ( 'should use the min option when greater than first label for building ticks' , function ( ) {
config . time . min = '2015-01-02T04:00:00' ;
var scale = createScale ( mockData , config ) ;
expect ( scale . ticks [ 0 ] ) . toEqual ( 'Jan 2' ) ;
} ) ;
it ( 'should use the max option when greater than last label for building ticks' , function ( ) {
2017-04-02 14:49:00 +02:00
config . time . max = '2015-01-05T06:00:00' ;
var scale = createScale ( mockData , config ) ;
2017-10-20 09:03:38 +02:00
expect ( scale . ticks [ scale . ticks . length - 1 ] ) . toEqual ( 'Jan 3' ) ;
} ) ;
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 ( ) {
config . time . max = '2015-01-02T23:00:00' ;
var scale = createScale ( mockData , config ) ;
expect ( scale . ticks [ scale . ticks . 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
}
} , Chart . scaleService . getScaleDefaults ( 'time' ) ) ;
2017-04-02 14:49:00 +02:00
var scale = createScale ( mockData , config ) ;
2017-07-29 21:26:36 +02:00
var ticks = getTicksLabels ( 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 : [ {
xAxisID : 'xScale0' ,
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 : {
xAxes : [ {
id : 'xScale0' ,
type : 'time' ,
position : 'bottom'
} ] ,
}
2016-04-28 03:50:31 +02:00
}
2017-07-25 10:12:53 +02:00
} ) ;
2015-09-27 15:38:11 +02:00
2017-07-25 10:12:53 +02:00
this . scale = this . chart . scales . xScale0 ;
} ) ;
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 ;
expect ( scale . getPixelForValue ( '' , 0 , 0 ) ) . toBeCloseToPixel ( firstPointPixel ) ;
expect ( scale . getPixelForValue ( chart . data . labels [ 0 ] ) ) . toBeCloseToPixel ( firstPointPixel ) ;
expect ( scale . getValueForPixel ( firstPointPixel ) ) . toBeCloseToTime ( {
2017-04-02 14:49:00 +02:00
value : moment ( chart . data . labels [ 0 ] ) ,
unit : 'hour' ,
} ) ;
2017-07-25 10:12:53 +02:00
expect ( scale . getPixelForValue ( '' , 6 , 0 ) ) . toBeCloseToPixel ( lastPointPixel ) ;
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 : {
xAxes : [ {
id : 'xScale0' ,
type : 'time' ,
2017-08-02 07:28:27 +02:00
bounds : 'ticks' ,
position : 'bottom'
2017-07-25 10:12:53 +02:00
} ] ,
}
2017-04-02 14:49:00 +02:00
}
2017-07-25 10:12:53 +02:00
} ) ;
2017-07-19 21:04:15 +02:00
2017-07-25 10:12:53 +02:00
this . scale = this . chart . scales . xScale0 ;
this . scale . update ( 800 , 200 ) ;
} ) ;
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 ( ) {
2017-05-30 00:40:10 +02:00
// Where 'correct' is a two year spacing.
2017-07-29 21:26:36 +02:00
expect ( getTicksLabels ( this . scale ) ) . toEqual ( [ '2005' , '2007' , '2009' , '2011' , '2013' , '2015' , '2017' , '2019' ] ) ;
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 ( ) ;
2017-07-25 10:12:53 +02:00
var pixelsPerYear = scale . width / 14 ;
2017-04-02 14:49:00 +02:00
for ( var i = 0 ; i < ticks . length - 1 ; i ++ ) {
var offset = 2 * pixelsPerYear * 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' ,
threshold : 0.5 ,
} ) ;
}
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 : [ {
xAxisID : 'xScale0' ,
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 : {
xAxes : [ {
id : 'xScale0' ,
type : 'time' ,
position : 'bottom'
} ] ,
}
}
2016-01-23 18:44:55 +01:00
} ) ;
2016-09-06 11:10:44 +02:00
var xScale = chart . scales . xScale0 ;
2016-10-31 01:34:06 +01:00
expect ( xScale . getLabelForIndex ( 0 , 0 ) ) . toBeTruthy ( ) ;
2016-04-28 03:50:31 +02:00
expect ( xScale . getLabelForIndex ( 0 , 0 ) ) . toBe ( '2015-01-01T20:00:00' ) ;
expect ( xScale . getLabelForIndex ( 6 , 0 ) ) . toBe ( '2015-01-10T12:00' ) ;
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 : [ {
xAxisID : 'xScale0' ,
data : [ 0 , 0 ]
} ] ,
labels : [ '2015-01-01T20:00:00' , '2015-01-01T20:01:00' ]
} ,
options : {
scales : {
xAxes : [ {
id : 'xScale0' ,
type : 'time' ,
ticks : {
callback : function ( value ) {
return '<' + value + '>' ;
}
}
} ]
}
}
} ) ;
this . scale = this . chart . scales . xScale0 ;
} ) ;
it ( 'should get the correct labels for ticks' , function ( ) {
var scale = this . scale ;
expect ( scale . _ticks . map ( function ( tick ) {
return tick . major ;
} ) ) . toEqual ( [ true , false , false , false , false , false , true ] ) ;
expect ( scale . ticks ) . toEqual ( [ '<8:00:00 pm>' , '<8:00:10 pm>' , '<8:00:20 pm>' , '<8:00:30 pm>' , '<8:00:40 pm>' , '<8:00:50 pm>' , '<8:01:00 pm>' ] ) ;
} ) ;
it ( 'should update ticks.callback correctly' , function ( ) {
var chart = this . chart ;
var scale = this . scale ;
chart . options . scales . xAxes [ 0 ] . ticks . callback = function ( value ) {
return '{' + value + '}' ;
} ;
chart . update ( ) ;
expect ( scale . ticks ) . toEqual ( [ '{8:00:00 pm}' , '{8:00:10 pm}' , '{8:00:20 pm}' , '{8:00:30 pm}' , '{8:00:40 pm}' , '{8:00:50 pm}' , '{8:01:00 pm}' ] ) ;
} ) ;
} ) ;
describe ( 'when ticks.major.callback and ticks.minor.callback are specified' , function ( ) {
beforeEach ( function ( ) {
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
datasets : [ {
xAxisID : 'xScale0' ,
data : [ 0 , 0 ]
} ] ,
labels : [ '2015-01-01T20:00:00' , '2015-01-01T20:01:00' ]
} ,
options : {
scales : {
xAxes : [ {
id : 'xScale0' ,
type : 'time' ,
ticks : {
callback : function ( value ) {
return '<' + value + '>' ;
} ,
major : {
enabled : true ,
callback : function ( value ) {
return '[' + value + ']' ;
}
} ,
minor : {
callback : function ( value ) {
return '(' + value + ')' ;
}
}
}
} ]
}
}
} ) ;
this . scale = this . chart . scales . xScale0 ;
} ) ;
it ( 'should get the correct labels for major and minor ticks' , function ( ) {
var scale = this . scale ;
expect ( scale . _ticks . map ( function ( tick ) {
return tick . major ;
} ) ) . toEqual ( [ true , false , false , false , false , false , true ] ) ;
expect ( scale . ticks ) . toEqual ( [ '[8:00 pm]' , '(8:00:10 pm)' , '(8:00:20 pm)' , '(8:00:30 pm)' , '(8:00:40 pm)' , '(8:00:50 pm)' , '[8:01 pm]' ] ) ;
} ) ;
it ( 'should only use ticks.minor callback if ticks.major.enabled is false' , function ( ) {
var chart = this . chart ;
var scale = this . scale ;
chart . options . scales . xAxes [ 0 ] . ticks . major . enabled = false ;
chart . update ( ) ;
expect ( scale . ticks ) . toEqual ( [ '(8:00:00 pm)' , '(8:00:10 pm)' , '(8:00:20 pm)' , '(8:00:30 pm)' , '(8:00:40 pm)' , '(8:00:50 pm)' , '(8:01:00 pm)' ] ) ;
} ) ;
it ( 'should use ticks.callback if ticks.major.callback is omitted' , function ( ) {
var chart = this . chart ;
var scale = this . scale ;
chart . options . scales . xAxes [ 0 ] . ticks . major . callback = undefined ;
chart . update ( ) ;
expect ( scale . ticks ) . toEqual ( [ '<8:00 pm>' , '(8:00:10 pm)' , '(8:00:20 pm)' , '(8:00:30 pm)' , '(8:00:40 pm)' , '(8:00:50 pm)' , '<8:01 pm>' ] ) ;
} ) ;
it ( 'should use ticks.callback if ticks.minor.callback is omitted' , function ( ) {
var chart = this . chart ;
var scale = this . scale ;
chart . options . scales . xAxes [ 0 ] . ticks . minor . callback = undefined ;
chart . update ( ) ;
expect ( scale . ticks ) . toEqual ( [ '[8:00 pm]' , '<8:00:10 pm>' , '<8:00:20 pm>' , '<8:00:30 pm>' , '<8:00:40 pm>' , '<8:00:50 pm>' , '[8:01 pm]' ] ) ;
} ) ;
} ) ;
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 : [ {
xAxisID : 'xScale0' ,
data : [ { t : '2015-01-01T20:00:00' , y : 10 } , { t : '2015-01-02T21:00:00' , y : 3 } ]
} ] ,
} ,
options : {
scales : {
xAxes : [ {
id : 'xScale0' ,
type : 'time' ,
position : 'bottom'
} ] ,
}
}
} ) ;
var xScale = chart . scales . xScale0 ;
expect ( xScale . getLabelForIndex ( 0 , 0 ) ) . toBeTruthy ( ) ;
expect ( xScale . getLabelForIndex ( 0 , 0 ) ) . toBe ( '2015-01-01T20:00:00' ) ;
} ) ;
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 : [ {
xAxisID : 'xScale0' ,
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 : {
scales : {
xAxes : [ {
id : 'xScale0' ,
type : 'time' ,
position : 'bottom'
} ] ,
}
}
} ) ;
var xScale = chart . scales . xScale0 ;
var label = xScale . getLabelForIndex ( 0 , 0 ) ;
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 : [ {
2016-09-06 11:34:33 +02:00
xAxisID : 'xScale0' ,
2016-06-09 06:34:15 +02:00
data : [ 5 ]
} ]
} ,
options : {
scales : {
xAxes : [ {
2016-09-06 11:34:33 +02:00
id : 'xScale0' ,
2016-06-09 06:34:15 +02:00
display : true ,
2016-10-16 23:34:59 +02:00
type : 'time'
2016-06-09 06:34:15 +02:00
} ]
}
}
} ) ;
2016-09-06 11:10:44 +02:00
var xScale = chart . scales . xScale0 ;
2017-05-30 00:40:10 +02:00
var pixel = xScale . getPixelForValue ( '' , 0 , 0 ) ;
2016-06-09 06:34:15 +02:00
2017-05-30 00:40:10 +02:00
expect ( xScale . getValueForPixel ( pixel ) . valueOf ( ) ) . 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 : {
xAxes : [ {
type : 'time' ,
time : {
min : moment ( ) . subtract ( 1 , 'months' ) ,
max : moment ( ) ,
}
} ] ,
} ,
responsive : true ,
} ,
} , {
wrapper : {
style : 'display: none' ,
} ,
} ) ;
expect ( chart . scales [ 'y-axis-0' ] . width ) . toEqual ( 0 ) ;
expect ( chart . scales [ 'y-axis-0' ] . maxWidth ) . toEqual ( 0 ) ;
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 : {
xAxes : [ {
id : 'x' ,
type : 'time' ,
time : {
parser : 'YYYY'
} ,
ticks : {
source : 'labels'
}
} ]
}
}
} ) ;
} ) ;
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' ) ) ;
2017-07-29 21:26:36 +02:00
expect ( getTicksLabels ( 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 ;
var options = chart . options . scales . xAxes [ 0 ] ;
options . time . min = '2012' ;
options . time . max = '2051' ;
chart . update ( ) ;
expect ( scale . min ) . toEqual ( + moment ( '2012' , 'YYYY' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( '2051' , 'YYYY' ) ) ;
2017-07-29 21:26:36 +02:00
expect ( getTicksLabels ( 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 ;
var options = chart . options . scales . xAxes [ 0 ] ;
2017-07-27 06:41:09 +02:00
options . time . min = '2017' ;
options . time . 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' ) ) ;
2017-07-29 21:26:36 +02:00
expect ( getTicksLabels ( 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 ) ;
2017-07-29 21:26:36 +02:00
expect ( getTicksLabels ( 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 ;
var options = chart . options . scales . xAxes [ 0 ] ;
options . time . unit = 'year' ;
chart . data . labels = [ ] ;
chart . update ( ) ;
expect ( scale . min ) . toEqual ( + moment ( ) . startOf ( 'year' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( ) . endOf ( 'year' ) + 1 ) ;
expect ( getTicksLabels ( scale ) ) . toEqual ( [ ] ) ;
} ) ;
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 : [
{ t : '2018' , y : 6 } ,
{ t : '2020' , y : 7 } ,
{ t : '2043' , y : 8 }
] }
]
} ,
options : {
scales : {
xAxes : [ {
id : 'x' ,
type : 'time' ,
time : {
parser : 'YYYY'
} ,
ticks : {
source : 'data'
}
} ]
}
}
} ) ;
} ) ;
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' ) ) ;
2017-07-29 21:26:36 +02:00
expect ( getTicksLabels ( 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 ;
var options = chart . options . scales . xAxes [ 0 ] ;
options . time . min = '2012' ;
options . time . max = '2051' ;
chart . update ( ) ;
expect ( scale . min ) . toEqual ( + moment ( '2012' , 'YYYY' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( '2051' , 'YYYY' ) ) ;
2017-07-29 21:26:36 +02:00
expect ( getTicksLabels ( 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 ;
var options = chart . options . scales . xAxes [ 0 ] ;
options . time . min = '2017' ;
2017-07-27 06:41:09 +02:00
options . time . 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' ) ) ;
2017-07-29 21:26:36 +02:00
expect ( getTicksLabels ( 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' ) ) ;
2017-07-29 21:26:36 +02:00
expect ( getTicksLabels ( 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 ;
var options = chart . options . scales . xAxes [ 0 ] ;
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 ) ;
expect ( getTicksLabels ( scale ) ) . toEqual ( [ ] ) ;
} ) ;
2017-07-19 21:04:15 +02:00
} ) ;
} ) ;
2017-07-30 21:24:29 +02:00
describe ( 'when distribution' , function ( ) {
2017-07-19 21:04:15 +02:00
describe ( 'is "series"' , 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 : {
xAxes : [ {
id : 'x' ,
type : 'time' ,
time : {
parser : 'YYYY'
} ,
2017-07-30 21:24:29 +02:00
distribution : 'series' ,
2017-07-19 21:04:15 +02:00
ticks : {
source : 'labels'
}
} ] ,
yAxes : [ {
display : false
} ]
}
}
} ) ;
} ) ;
2017-07-30 21:24:29 +02:00
it ( 'should space data out with the same gap, whatever their time values' , function ( ) {
2017-07-19 21:04:15 +02:00
var scale = this . chart . scales . x ;
var start = scale . left ;
var slice = scale . width / 4 ;
2017-07-30 21:24:29 +02:00
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start ) ;
expect ( scale . getPixelForValue ( '2019' ) ) . toBeCloseToPixel ( start + slice ) ;
expect ( scale . getPixelForValue ( '2020' ) ) . toBeCloseToPixel ( start + slice * 2 ) ;
expect ( scale . getPixelForValue ( '2025' ) ) . toBeCloseToPixel ( start + slice * 3 ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( start + slice * 4 ) ;
2017-07-19 21:04:15 +02:00
} ) ;
2017-07-30 21:24:29 +02:00
it ( 'should add a step before if scale.min is before the first data' , function ( ) {
2017-07-19 21:04:15 +02:00
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . xAxes [ 0 ] ;
options . time . min = '2012' ;
chart . update ( ) ;
var start = scale . left ;
var slice = scale . width / 5 ;
2017-07-30 21:24:29 +02:00
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start + slice ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( start + slice * 5 ) ;
2017-07-19 21:04:15 +02:00
} ) ;
2017-07-30 21:24:29 +02:00
it ( 'should add a step after if scale.max is after the last data' , function ( ) {
2017-07-19 21:04:15 +02:00
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . xAxes [ 0 ] ;
options . time . max = '2050' ;
chart . update ( ) ;
var start = scale . left ;
var slice = scale . width / 5 ;
2017-07-30 21:24:29 +02:00
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( start + slice * 4 ) ;
2017-07-19 21:04:15 +02:00
} ) ;
2017-07-30 21:24:29 +02:00
it ( 'should add steps before and after if scale.min/max are outside the data range' , function ( ) {
2017-07-19 21:04:15 +02:00
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . xAxes [ 0 ] ;
options . time . min = '2012' ;
options . time . max = '2050' ;
chart . update ( ) ;
var start = scale . left ;
var slice = scale . width / 6 ;
2017-07-30 21:24:29 +02:00
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start + slice ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( start + slice * 5 ) ;
2017-07-19 21:04:15 +02:00
} ) ;
} ) ;
describe ( 'is "linear"' , 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 : {
xAxes : [ {
id : 'x' ,
type : 'time' ,
time : {
parser : 'YYYY'
} ,
2017-07-30 21:24:29 +02:00
distribution : 'linear' ,
2017-07-19 21:04:15 +02:00
ticks : {
source : 'labels'
}
} ] ,
yAxes : [ {
display : false
} ]
}
}
} ) ;
} ) ;
2017-07-30 21:24:29 +02:00
it ( 'should space data out with a gap relative to their time values' , function ( ) {
2017-07-19 21:04:15 +02:00
var scale = this . chart . scales . x ;
var start = scale . left ;
var slice = scale . width / ( 2042 - 2017 ) ;
2017-07-30 21:24:29 +02:00
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start ) ;
expect ( scale . getPixelForValue ( '2019' ) ) . toBeCloseToPixel ( start + slice * ( 2019 - 2017 ) ) ;
expect ( scale . getPixelForValue ( '2020' ) ) . toBeCloseToPixel ( start + slice * ( 2020 - 2017 ) ) ;
expect ( scale . getPixelForValue ( '2025' ) ) . toBeCloseToPixel ( start + slice * ( 2025 - 2017 ) ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( start + slice * ( 2042 - 2017 ) ) ;
2017-07-19 21:04:15 +02:00
} ) ;
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 . xAxes [ 0 ] ;
options . time . min = '2012' ;
options . time . max = '2050' ;
chart . update ( ) ;
var start = scale . left ;
var slice = scale . width / ( 2050 - 2012 ) ;
2017-07-30 21:24:29 +02:00
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start + slice * ( 2017 - 2012 ) ) ;
expect ( scale . getPixelForValue ( '2019' ) ) . toBeCloseToPixel ( start + slice * ( 2019 - 2012 ) ) ;
expect ( scale . getPixelForValue ( '2020' ) ) . toBeCloseToPixel ( start + slice * ( 2020 - 2012 ) ) ;
expect ( scale . getPixelForValue ( '2025' ) ) . toBeCloseToPixel ( start + slice * ( 2025 - 2012 ) ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . 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 : {
xAxes : [ {
id : 'x' ,
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'
}
} ] ,
yAxes : [ {
display : false
} ]
}
}
} ) ;
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' ) ) ;
expect ( scale . getPixelForValue ( '02/20 08:00' ) ) . toBeCloseToPixel ( scale . left ) ;
expect ( scale . getPixelForValue ( '02/23 11:00' ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
2017-07-29 21:26:36 +02:00
expect ( getTicksLabels ( 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 : {
xAxes : [ {
id : 'x' ,
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'
}
} ] ,
yAxes : [ {
display : false
} ]
}
}
} ) ;
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 ) ;
expect ( scale . getPixelForValue ( '02/20 08:00' ) ) . toBeCloseToPixel ( 60 ) ;
expect ( scale . getPixelForValue ( '02/23 11:00' ) ) . toBeCloseToPixel ( 426 ) ;
expect ( getTicksLabels ( scale ) ) . toEqual ( [
2017-07-25 10:12:53 +02:00
'Feb 20' , 'Feb 21' , 'Feb 22' , 'Feb 23' , 'Feb 24' ] ) ;
} ) ;
} ) ;
} ) ;
describe ( 'when time.min and/or time.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 : {
xAxes : [ {
id : 'x' ,
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
}
} ] ,
yAxes : [ {
display : false
} ]
}
}
} ) ;
} ) ;
it ( 'should expand scale to the min/max range' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . xAxes [ 0 ] ;
var min = '02/19 07:00' ;
var max = '02/24 08:00' ;
options . time . min = min ;
options . time . max = max ;
chart . update ( ) ;
expect ( scale . min ) . toEqual ( + moment ( min , 'MM/DD HH:mm' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( max , 'MM/DD HH:mm' ) ) ;
expect ( scale . getPixelForValue ( min ) ) . toBeCloseToPixel ( scale . left ) ;
expect ( scale . getPixelForValue ( max ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
2017-07-29 21:26:36 +02:00
scale . getTicks ( ) . forEach ( function ( tick ) {
expect ( tick . value >= + moment ( min , 'MM/DD HH:mm' ) ) . toBeTruthy ( ) ;
expect ( tick . value <= + moment ( max , 'MM/DD HH:mm' ) ) . 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 ;
var options = chart . options . scales . xAxes [ 0 ] ;
var min = '02/21 07:00' ;
var max = '02/22 20:00' ;
options . time . min = min ;
options . time . max = max ;
chart . update ( ) ;
expect ( scale . min ) . toEqual ( + moment ( min , 'MM/DD HH:mm' ) ) ;
expect ( scale . max ) . toEqual ( + moment ( max , 'MM/DD HH:mm' ) ) ;
expect ( scale . getPixelForValue ( min ) ) . toBeCloseToPixel ( scale . left ) ;
expect ( scale . getPixelForValue ( max ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
2017-07-29 21:26:36 +02:00
scale . getTicks ( ) . forEach ( function ( tick ) {
expect ( tick . value >= + moment ( min , 'MM/DD HH:mm' ) ) . toBeTruthy ( ) ;
expect ( tick . value <= + moment ( max , 'MM/DD HH:mm' ) ) . 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 ) {
[ 'series' , 'linear' ] . forEach ( function ( distribution ) {
describe ( 'when ticks.source is "' + source + '" and distribution is "' + distribution + '"' , 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 : {
xAxes : [ {
id : 'x' ,
type : 'time' ,
time : {
parser : 'YYYY'
} ,
ticks : {
source : source
} ,
distribution : distribution
} ]
}
}
} ) ;
} ) ;
it ( 'should not add offset from the edges' , function ( ) {
var scale = this . chart . scales . x ;
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( scale . left ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
} ) ;
it ( 'should add offset from the edges if offset is true' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . xAxes [ 0 ] ;
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 ) ;
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( scale . left + firstTickInterval / 2 ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( scale . left + scale . width - lastTickInterval / 2 ) ;
} ) ;
it ( 'should not add offset if min and max extend the labels range' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . xAxes [ 0 ] ;
options . time . min = '2012' ;
options . time . max = '2051' ;
chart . update ( ) ;
expect ( scale . getPixelForValue ( '2012' ) ) . toBeCloseToPixel ( scale . left ) ;
expect ( scale . getPixelForValue ( '2051' ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
} ) ;
it ( 'should not add offset if min and max extend the labels range and offset is true' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . xAxes [ 0 ] ;
options . time . min = '2012' ;
options . time . max = '2051' ;
options . offset = true ;
chart . update ( ) ;
expect ( scale . getPixelForValue ( '2012' ) ) . toBeCloseToPixel ( scale . left ) ;
expect ( scale . getPixelForValue ( '2051' ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
} ) ;
} ) ;
} ) ;
} ) ;
2018-12-20 16:08:13 +01:00
describe ( 'when ticks.reverse' , function ( ) {
describe ( 'is "true"' , function ( ) {
2019-01-02 16:00:55 +01:00
beforeEach ( function ( ) {
2018-12-20 16:08:13 +01:00
this . chart = window . acquireChart ( {
type : 'line' ,
data : {
labels : [ '2017' , '2019' , '2020' , '2025' , '2042' ] ,
datasets : [ { data : [ 0 , 1 , 2 , 3 , 4 , 5 ] } ]
} ,
options : {
scales : {
xAxes : [ {
id : 'x' ,
type : 'time' ,
time : {
parser : 'YYYY' ,
} ,
ticks : {
source : 'labels' ,
reverse : true
}
} ] ,
yAxes : [ {
display : false
} ]
}
}
} ) ;
2019-01-02 16:00:55 +01:00
} ) ;
it ( 'should reverse the labels' , function ( ) {
2018-12-20 16:08:13 +01:00
var scale = this . chart . scales . x ;
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( scale . left + scale . width ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( scale . left ) ;
} ) ;
2019-01-02 16:00:55 +01:00
it ( 'should reverse the bars and add offsets if offset is true' , function ( ) {
var chart = this . chart ;
var scale = chart . scales . x ;
var options = chart . options . scales . xAxes [ 0 ] ;
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 ) ;
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( scale . left + scale . width - lastTickInterval / 2 ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( scale . left + firstTickInterval / 2 ) ;
} ) ;
2018-12-20 16:08:13 +01:00
} ) ;
} ) ;
describe ( 'when ticks.reverse is "true" and distribution' , function ( ) {
describe ( 'is "series"' , 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 : {
xAxes : [ {
id : 'x' ,
type : 'time' ,
time : {
parser : 'YYYY'
} ,
distribution : 'series' ,
ticks : {
source : 'labels' ,
reverse : true
}
} ] ,
yAxes : [ {
display : false
} ]
}
}
} ) ;
} ) ;
it ( 'should reverse the labels and 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 ;
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start + slice * 4 ) ;
expect ( scale . getPixelForValue ( '2019' ) ) . toBeCloseToPixel ( start + slice * 3 ) ;
expect ( scale . getPixelForValue ( '2020' ) ) . toBeCloseToPixel ( start + slice * 2 ) ;
expect ( scale . getPixelForValue ( '2025' ) ) . toBeCloseToPixel ( start + slice ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( start ) ;
} ) ;
it ( 'should reverse the labels and 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 . xAxes [ 0 ] ;
options . time . min = '2012' ;
chart . update ( ) ;
var start = scale . left ;
var slice = scale . width / 5 ;
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start + slice * 4 ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( start ) ;
} ) ;
it ( 'should reverse the labels and 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 . xAxes [ 0 ] ;
options . time . max = '2050' ;
chart . update ( ) ;
var start = scale . left ;
var slice = scale . width / 5 ;
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start + slice * 5 ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( start + slice ) ;
} ) ;
it ( 'should reverse the labels and 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 . xAxes [ 0 ] ;
options . time . min = '2012' ;
options . time . max = '2050' ;
chart . update ( ) ;
var start = scale . left ;
var slice = scale . width / 6 ;
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start + slice * 5 ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( start + slice ) ;
} ) ;
} ) ;
describe ( 'is "linear"' , 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 : {
xAxes : [ {
id : 'x' ,
type : 'time' ,
time : {
parser : 'YYYY'
} ,
distribution : 'linear' ,
ticks : {
source : 'labels' ,
reverse : true
}
} ] ,
yAxes : [ {
display : false
} ]
}
}
} ) ;
} ) ;
it ( 'should reverse the labels and 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 ) ;
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start + slice * ( 2042 - 2017 ) ) ;
expect ( scale . getPixelForValue ( '2019' ) ) . toBeCloseToPixel ( start + slice * ( 2042 - 2019 ) ) ;
expect ( scale . getPixelForValue ( '2020' ) ) . toBeCloseToPixel ( start + slice * ( 2042 - 2020 ) ) ;
expect ( scale . getPixelForValue ( '2025' ) ) . toBeCloseToPixel ( start + slice * ( 2042 - 2025 ) ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( start ) ;
} ) ;
it ( 'should reverse the labels and 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 . xAxes [ 0 ] ;
options . time . min = '2012' ;
options . time . max = '2050' ;
chart . update ( ) ;
var start = scale . left ;
var slice = scale . width / ( 2050 - 2012 ) ;
expect ( scale . getPixelForValue ( '2017' ) ) . toBeCloseToPixel ( start + slice * ( 2050 - 2017 ) ) ;
expect ( scale . getPixelForValue ( '2019' ) ) . toBeCloseToPixel ( start + slice * ( 2050 - 2019 ) ) ;
expect ( scale . getPixelForValue ( '2020' ) ) . toBeCloseToPixel ( start + slice * ( 2050 - 2020 ) ) ;
expect ( scale . getPixelForValue ( '2025' ) ) . toBeCloseToPixel ( start + slice * ( 2050 - 2025 ) ) ;
expect ( scale . getPixelForValue ( '2042' ) ) . toBeCloseToPixel ( start + slice * ( 2050 - 2042 ) ) ;
} ) ;
} ) ;
} ) ;
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 : {
xAxes : [ {
id : 'x' ,
type : 'time'
} ]
}
}
} ) ;
// 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 ) ;
expect ( chart . options . scales . xAxes [ 0 ] . time . displayFormats ) . toEqual ( expected ) ;
} ) ;
it ( 'should merge user formats with adapter presets' , function ( ) {
var chart = window . acquireChart ( {
type : 'line' ,
data : { } ,
options : {
scales : {
xAxes : [ {
id : 'x' ,
type : 'time' ,
time : {
displayFormats : {
millisecond : 'foo' ,
hour : 'bar' ,
month : 'bla'
}
}
} ]
}
}
} ) ;
// 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 ) ;
expect ( chart . options . scales . xAxes [ 0 ] . time . displayFormats ) . toEqual ( expected ) ;
} ) ;
} ) ;
} ) ;
2015-10-17 05:14:07 +02:00
} ) ;