2015-09-27 15:38:11 +02:00
// Time scale tests
describe ( 'Time scale tests' , function ( ) {
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 ) ;
2016-05-01 18:40:14 +02:00
result = Math . abs ( diff ) < ( expected . threshold !== undefined ? expected . threshold : 0.5 ) ;
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
} ) ;
2015-10-17 05:14:07 +02:00
it ( 'Should load moment.js as a dependency' , function ( ) {
expect ( window . moment ) . not . toBe ( undefined ) ;
} ) ;
2015-09-27 15:38:11 +02:00
it ( 'Should register the constructor with the scale service' , function ( ) {
var Constructor = Chart . scaleService . getScaleConstructor ( 'time' ) ;
expect ( Constructor ) . not . toBe ( undefined ) ;
expect ( typeof Constructor ) . toBe ( 'function' ) ;
} ) ;
it ( 'Should have the correct default config' , function ( ) {
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 ,
borderDash : [ ] ,
borderDashOffset : 0.0
2015-09-27 15:38:11 +02:00
} ,
2016-10-16 23:34:59 +02:00
position : 'bottom' ,
2015-09-27 15:38:11 +02:00
scaleLabel : {
labelString : '' ,
2016-04-17 00:38:03 +02:00
display : false
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 ,
padding : 10 ,
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 ,
labelOffset : 0
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' ,
2015-12-01 02:42:48 +01:00
displayFormats : {
2016-10-16 23:34:59 +02:00
millisecond : 'h:mm:ss.SSS a' , // 11:20:01.123 AM
second : 'h:mm:ss a' , // 11:20:01 AM
minute : 'h:mm:ss a' , // 11:20:01 AM
hour : 'MMM D, hA' , // Sept 4, 5PM
day : 'll' , // Sep 4 2015
week : 'll' , // Week 46, or maybe "[W]WW - YYYY" ?
month : 'MMM YYYY' , // Sept 2015
quarter : '[Q]Q - YYYY' , // Q3
year : 'YYYY' // 2015
2016-04-17 00:38:03 +02:00
}
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
} ) ;
2015-10-17 05:14:07 +02:00
it ( 'should build ticks using days' , function ( ) {
2015-09-27 15:38:11 +02:00
var scaleID = 'myScale' ;
var mockData = {
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
2015-09-27 15:38:11 +02:00
} ;
var mockContext = window . createMockContext ( ) ;
var Constructor = Chart . scaleService . getScaleConstructor ( 'time' ) ;
var scale = new Constructor ( {
ctx : mockContext ,
options : Chart . scaleService . getScaleDefaults ( 'time' ) , // use default config for scale
2015-11-13 15:38:35 +01:00
chart : {
data : mockData
} ,
2015-09-27 15:38:11 +02:00
id : scaleID
} ) ;
2016-10-16 23:34:59 +02:00
// scale.buildTicks();
2015-09-27 15:38:11 +02:00
scale . update ( 400 , 50 ) ;
// Counts down because the lines are drawn top to bottom
2016-10-16 23:34:59 +02:00
expect ( scale . ticks ) . toEqual ( [ 'Dec 28, 2014' , 'Jan 4, 2015' , 'Jan 11, 2015' ] ) ;
2015-12-05 22:45:31 +01:00
} ) ;
it ( 'should build ticks using date objects' , function ( ) {
// Helper to build date objects
function newDateFromRef ( days ) {
return moment ( '01/01/2015 12:00' , 'DD/MM/YYYY HH:mm' ) . add ( days , 'd' ) . toDate ( ) ;
}
var scaleID = 'myScale' ;
var mockData = {
labels : [ newDateFromRef ( 0 ) , newDateFromRef ( 1 ) , newDateFromRef ( 2 ) , newDateFromRef ( 4 ) , newDateFromRef ( 6 ) , newDateFromRef ( 7 ) , newDateFromRef ( 9 ) ] , // days
} ;
var mockContext = window . createMockContext ( ) ;
var Constructor = Chart . scaleService . getScaleConstructor ( 'time' ) ;
var scale = new Constructor ( {
ctx : mockContext ,
options : Chart . scaleService . getScaleDefaults ( 'time' ) , // use default config for scale
chart : {
data : mockData
} ,
id : scaleID
} ) ;
scale . update ( 400 , 50 ) ;
// Counts down because the lines are drawn top to bottom
2016-10-16 23:34:59 +02:00
expect ( scale . ticks ) . toEqual ( [ 'Dec 28, 2014' , 'Jan 4, 2015' , 'Jan 11, 2015' ] ) ;
2015-09-27 15:38:11 +02:00
} ) ;
2016-01-23 18:44:55 +01:00
it ( 'should build ticks when the data is xy points' , function ( ) {
// Helper to build date objects
function newDateFromRef ( days ) {
return moment ( '01/01/2015 12:00' , 'DD/MM/YYYY HH:mm' ) . add ( days , 'd' ) . toDate ( ) ;
}
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' ,
yAxisID : 'yScale0' ,
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-01-23 18:44:55 +01:00
} ,
2016-04-28 03:50:31 +02:00
options : {
scales : {
xAxes : [ {
id : 'xScale0' ,
type : 'time' ,
position : 'bottom'
} ] ,
yAxes : [ {
id : 'yScale0' ,
type : 'linear'
} ]
}
}
2016-01-23 18:44:55 +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-10-16 23:34:59 +02:00
expect ( xScale . ticks ) . toEqual ( [ 'Jan 1, 2015' , 'Jan 3, 2015' , 'Jan 5, 2015' , 'Jan 7, 2015' , 'Jan 9, 2015' , 'Jan 11, 2015' ] ) ;
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 : {
datasets : [ {
xAxisID : 'xScale0' ,
yAxisID : 'yScale0' ,
data : [ {
x : 375068900 ,
y : 1
} ]
} ] ,
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 ) {
2016-04-28 03:50:31 +02:00
return moment . unix ( label ) ;
}
}
} ] ,
yAxes : [ {
id : 'yScale0' ,
type : 'linear'
} ]
}
}
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
2016-05-01 18:40:14 +02:00
expect ( xScale . ticks [ 0 ] ) . toEqualOneOf ( [ 'Nov 19, 1981' , 'Nov 20, 1981' , 'Nov 21, 1981' ] ) ; // handle time zone changes
expect ( xScale . ticks [ 1 ] ) . toEqualOneOf ( [ 'Nov 19, 1981' , 'Nov 20, 1981' , 'Nov 21, 1981' ] ) ; // handle time zone changes
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 scaleID = 'myScale' ;
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
} ;
var mockContext = window . createMockContext ( ) ;
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' ;
var Constructor = Chart . scaleService . getScaleConstructor ( 'time' ) ;
var scale = new Constructor ( {
ctx : mockContext ,
options : config , // use default config for scale
2015-11-13 15:38:35 +01:00
chart : {
data : mockData
} ,
2015-09-27 15:38:11 +02:00
id : scaleID
} ) ;
2016-10-16 23:34:59 +02:00
// scale.buildTicks();
2015-09-27 15:38:11 +02:00
scale . update ( 400 , 50 ) ;
expect ( scale . ticks ) . toEqual ( [ 'Jan 1, 8PM' , 'Jan 1, 9PM' , 'Jan 1, 10PM' , 'Jan 1, 11PM' , 'Jan 2, 12AM' , 'Jan 2, 1AM' , 'Jan 2, 2AM' , 'Jan 2, 3AM' , 'Jan 2, 4AM' , 'Jan 2, 5AM' , 'Jan 2, 6AM' , 'Jan 2, 7AM' , 'Jan 2, 8AM' , 'Jan 2, 9AM' , 'Jan 2, 10AM' , 'Jan 2, 11AM' , 'Jan 2, 12PM' , 'Jan 2, 1PM' , 'Jan 2, 2PM' , 'Jan 2, 3PM' , 'Jan 2, 4PM' , 'Jan 2, 5PM' , 'Jan 2, 6PM' , 'Jan 2, 7PM' , 'Jan 2, 8PM' , 'Jan 2, 9PM' ] ) ;
} ) ;
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
it ( 'build ticks honoring the minUnit' , function ( ) {
var scaleID = 'myScale' ;
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
} ;
var mockContext = window . createMockContext ( ) ;
var config = Chart . helpers . clone ( Chart . scaleService . getScaleDefaults ( 'time' ) ) ;
config . time . minUnit = 'day' ;
var Constructor = Chart . scaleService . getScaleConstructor ( 'time' ) ;
var scale = new Constructor ( {
ctx : mockContext ,
options : config , // use default config for scale
chart : {
data : mockData
} ,
id : scaleID
} ) ;
2016-10-16 23:34:59 +02:00
// scale.buildTicks();
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
scale . update ( 400 , 50 ) ;
expect ( scale . ticks ) . toEqual ( [ 'Jan 1, 2015' , 'Jan 2, 2015' , 'Jan 3, 2015' ] ) ;
} ) ;
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 scaleID = 'myScale' ;
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
} ;
var mockContext = window . createMockContext ( ) ;
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 = 'week' ;
config . time . round = 'week' ;
var Constructor = Chart . scaleService . getScaleConstructor ( 'time' ) ;
var scale = new Constructor ( {
ctx : mockContext ,
options : config , // use default config for scale
2015-11-13 15:38:35 +01:00
chart : {
data : mockData
} ,
2015-09-27 15:38:11 +02:00
id : scaleID
} ) ;
2016-10-16 23:34:59 +02:00
// scale.buildTicks();
2015-09-27 15:38:11 +02:00
scale . update ( 400 , 50 ) ;
2016-01-30 22:35:10 +01:00
// last date is feb 15 because we round to start of week
2015-09-27 15:38:11 +02:00
expect ( scale . 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' ] ) ;
} ) ;
2016-01-23 18:44:55 +01:00
it ( 'Should use the min and max options' , function ( ) {
var scaleID = 'myScale' ;
var mockData = {
2016-10-16 23:34:59 +02:00
labels : [ '2015-01-01T20:00:00' , '2015-01-02T20:00:00' , '2015-01-03T20:00:00' ] , // days
2016-01-23 18:44:55 +01:00
} ;
var mockContext = window . createMockContext ( ) ;
var config = Chart . helpers . clone ( Chart . scaleService . getScaleDefaults ( 'time' ) ) ;
2016-10-16 23:34:59 +02:00
config . time . min = '2015-01-01T04:00:00' ;
config . time . max = '2015-01-05T06:00:00' ;
2016-01-23 18:44:55 +01:00
var Constructor = Chart . scaleService . getScaleConstructor ( 'time' ) ;
var scale = new Constructor ( {
ctx : mockContext ,
options : config , // use default config for scale
chart : {
data : mockData
} ,
id : scaleID
} ) ;
scale . update ( 400 , 50 ) ;
2016-10-16 23:34:59 +02:00
expect ( scale . ticks ) . toEqual ( [ 'Jan 1, 2015' , 'Jan 5, 2015' ] ) ;
2016-01-23 18:44:55 +01:00
} ) ;
2016-05-11 10:34:22 +02:00
it ( 'Should use the isoWeekday option' , function ( ) {
var scaleID = 'myScale' ;
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
]
} ;
var mockContext = window . createMockContext ( ) ;
var config = Chart . helpers . clone ( Chart . scaleService . getScaleDefaults ( 'time' ) ) ;
config . time . unit = 'week' ;
// Wednesday
config . time . isoWeekday = 3 ;
var Constructor = Chart . scaleService . getScaleConstructor ( 'time' ) ;
var scale = new Constructor ( {
ctx : mockContext ,
options : config , // use default config for scale
chart : {
data : mockData
} ,
id : scaleID
} ) ;
scale . update ( 400 , 50 ) ;
2016-10-16 23:34:59 +02:00
expect ( scale . ticks ) . toEqual ( [ 'Dec 31, 2014' , 'Jan 7, 2015' ] ) ;
2016-05-11 10:34:22 +02:00
} ) ;
2015-10-17 05:14:07 +02:00
it ( 'should get the correct pixel for a 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' ,
yAxisID : 'yScale0' ,
data : [ ]
} ] ,
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
2015-11-13 15:38:35 +01:00
} ,
2016-04-28 03:50:31 +02:00
options : {
scales : {
xAxes : [ {
id : 'xScale0' ,
type : 'time' ,
position : 'bottom'
} ] ,
yAxes : [ {
id : 'yScale0' ,
type : 'linear' ,
position : 'left'
} ]
}
}
2015-09-27 15:38:11 +02:00
} ) ;
2016-09-06 11:10:44 +02:00
var xScale = chart . scales . xScale0 ;
2015-09-27 15:38:11 +02:00
2016-04-28 03:50:31 +02:00
expect ( xScale . getPixelForValue ( '' , 0 , 0 ) ) . toBeCloseToPixel ( 78 ) ;
2016-05-01 18:40:14 +02:00
expect ( xScale . getPixelForValue ( '' , 6 , 0 ) ) . toBeCloseToPixel ( 452 ) ;
2016-06-22 01:03:30 +02:00
expect ( xScale . getPixelForValue ( '2015-01-01T20:00:00' ) ) . toBeCloseToPixel ( 78 ) ;
2016-04-24 02:08:00 +02:00
2016-04-30 02:03:15 +02:00
expect ( xScale . getValueForPixel ( 78 ) ) . toBeCloseToTime ( {
2016-09-06 11:10:44 +02:00
value : moment ( chart . data . labels [ 0 ] ) ,
2016-05-01 18:40:14 +02:00
unit : 'hour' ,
threshold : 0.75
2015-09-27 15:38:11 +02:00
} ) ;
2016-05-01 18:40:14 +02:00
expect ( xScale . getValueForPixel ( 452 ) ) . toBeCloseToTime ( {
2016-09-06 11:10:44 +02:00
value : moment ( chart . data . labels [ 6 ] ) ,
2016-04-23 19:27:10 +02:00
unit : 'hour'
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' ,
yAxisID : 'yScale0' ,
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'
} ] ,
yAxes : [ {
id : 'yScale0' ,
type : 'linear' ,
position : 'left'
} ]
}
}
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
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 ;
2016-06-09 06:34:15 +02:00
2016-09-06 11:34:33 +02:00
expect ( xScale . getPixelForValue ( '' , 0 , 0 ) ) . toBeCloseToPixel ( 62 ) ;
2016-06-09 06:34:15 +02:00
2016-09-06 11:34:33 +02:00
expect ( xScale . getValueForPixel ( 62 ) ) . toBeCloseToTime ( {
2016-09-06 11:10:44 +02:00
value : moment ( chart . data . labels [ 0 ] ) ,
2016-06-09 06:34:15 +02:00
unit : 'day' ,
threshold : 0.75
} ) ;
} ) ;
2016-07-26 20:45:05 +02:00
2016-10-16 23:34:59 +02:00
it ( 'should not throw an error if the datasetIndex is out of bounds' , function ( ) {
2016-07-26 20:45:05 +02:00
var chart = window . acquireChart ( {
type : 'line' ,
data : {
2016-10-16 23:34:59 +02:00
labels : [ '2016-06-26' ] ,
2016-07-26 20:45:05 +02:00
datasets : [ {
2016-09-06 11:34:33 +02:00
xAxisID : 'xScale0' ,
2016-07-26 20:45:05 +02:00
data : [ 5 ]
} ]
} ,
options : {
scales : {
xAxes : [ {
2016-09-06 11:34:33 +02:00
id : 'xScale0' ,
2016-07-26 20:45:05 +02:00
display : true ,
2016-10-16 23:34:59 +02:00
type : 'time' ,
2016-07-26 20:45:05 +02:00
} ]
}
}
} ) ;
2016-09-06 11:10:44 +02:00
var xScale = chart . scales . xScale0 ;
2016-09-03 18:06:09 +02:00
var getOutOfBoundLabelMoment = function ( ) {
2016-07-26 20:45:05 +02:00
xScale . getLabelMoment ( 12 , 0 ) ;
} ;
2016-09-03 18:06:09 +02:00
expect ( getOutOfBoundLabelMoment ) . not . toThrow ( ) ;
} ) ;
2016-09-06 11:10:44 +02:00
2016-10-16 23:34:59 +02:00
it ( 'should not throw an error if the datasetIndex or index are null' , function ( ) {
2016-09-03 18:06:09 +02:00
var chart = window . acquireChart ( {
type : 'line' ,
data : {
2016-10-16 23:34:59 +02:00
labels : [ '2016-06-26' ] ,
2016-09-03 18:06:09 +02:00
datasets : [ {
2016-09-06 11:34:33 +02:00
xAxisID : 'xScale0' ,
2016-09-03 18:06:09 +02:00
data : [ 5 ]
} ]
} ,
options : {
scales : {
xAxes : [ {
2016-09-06 11:34:33 +02:00
id : 'xScale0' ,
2016-09-03 18:06:09 +02:00
display : true ,
2016-10-16 23:34:59 +02:00
type : 'time' ,
2016-09-03 18:06:09 +02:00
} ]
}
}
} ) ;
2016-09-06 11:10:44 +02:00
var xScale = chart . scales . xScale0 ;
2016-09-03 18:06:09 +02:00
var getNullDatasetIndexLabelMoment = function ( ) {
xScale . getLabelMoment ( null , 1 ) ;
} ;
2016-09-06 11:10:44 +02:00
2016-09-03 18:06:09 +02:00
var getNullIndexLabelMoment = function ( ) {
xScale . getLabelMoment ( 1 , null ) ;
} ;
expect ( getNullDatasetIndexLabelMoment ) . not . toThrow ( ) ;
expect ( getNullIndexLabelMoment ) . not . toThrow ( ) ;
2016-07-26 20:45:05 +02:00
} ) ;
2015-10-17 05:14:07 +02:00
} ) ;