123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731 |
- describe('Linear Scale', function() {
- var chartInstance;
- beforeEach(function() {
- window.addDefaultMatchers(jasmine);
- });
- afterEach(function() {
- if (chartInstance)
- {
- releaseChart(chartInstance);
- }
- });
- it('Should register the constructor with the scale service', function() {
- var Constructor = Chart.scaleService.getScaleConstructor('linear');
- expect(Constructor).not.toBe(undefined);
- expect(typeof Constructor).toBe('function');
- });
- it('Should have the correct default config', function() {
- var defaultConfig = Chart.scaleService.getScaleDefaults('linear');
- expect(defaultConfig).toEqual({
- display: true,
- gridLines: {
- color: "rgba(0, 0, 0, 0.1)",
- drawBorder: true,
- drawOnChartArea: true,
- drawTicks: true, // draw ticks extending towards the label
- tickMarkLength: 10,
- lineWidth: 1,
- offsetGridLines: false,
- display: true,
- zeroLineColor: "rgba(0,0,0,0.25)",
- zeroLineWidth: 1,
- },
- position: "left",
- scaleLabel: {
- labelString: '',
- display: false,
- },
- ticks: {
- beginAtZero: false,
- minRotation: 0,
- maxRotation: 50,
- mirror: false,
- padding: 10,
- reverse: false,
- display: true,
- callback: defaultConfig.ticks.callback, // make this work nicer, then check below
- autoSkip: true,
- autoSkipPadding: 0,
- labelOffset: 0
- }
- });
- expect(defaultConfig.ticks.callback).toEqual(jasmine.any(Function));
- });
- it('Should correctly determine the max & min data values', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: [10, 5, 0, -5, 78, -100]
- }, {
- yAxisID: 'yScale1',
- data: [-1000, 1000],
- }, {
- yAxisID: 'yScale0',
- data: [150]
- }],
- labels: ['a', 'b', 'c', 'd', 'e', 'f']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear'
- }, {
- id: 'yScale1',
- type: 'linear'
- }]
- }
- }
- });
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-100);
- expect(chartInstance.scales.yScale0.max).toBe(150);
- });
- it('Should correctly determine the max & min of string data values', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: ['10', '5', '0', '-5', '78', '-100']
- }, {
- yAxisID: 'yScale1',
- data: ['-1000', '1000'],
- }, {
- yAxisID: 'yScale0',
- data: ['150']
- }],
- labels: ['a', 'b', 'c', 'd', 'e', 'f']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear'
- }, {
- id: 'yScale1',
- type: 'linear'
- }]
- }
- }
- });
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-100);
- expect(chartInstance.scales.yScale0.max).toBe(150);
- });
- it('Should correctly determine the max & min data values ignoring hidden datasets', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: ['10', '5', '0', '-5', '78', '-100']
- }, {
- yAxisID: 'yScale1',
- data: ['-1000', '1000'],
- }, {
- yAxisID: 'yScale0',
- data: ['150'],
- hidden: true
- }],
- labels: ['a', 'b', 'c', 'd', 'e', 'f']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear'
- }, {
- id: 'yScale1',
- type: 'linear'
- }]
- }
- }
- });
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-100);
- expect(chartInstance.scales.yScale0.max).toBe(80);
- });
- it('Should correctly determine the max & min data values ignoring data that is NaN', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: [null, 90, NaN, undefined, 45, 30]
- }],
- labels: ['a', 'b', 'c', 'd', 'e', 'f']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear'
- }]
- }
- }
- });
- expect(chartInstance.scales.yScale0.min).toBe(30);
- expect(chartInstance.scales.yScale0.max).toBe(90);
- // Scale is now stacked
- chartInstance.scales.yScale0.options.stacked = true;
- chartInstance.update();
- expect(chartInstance.scales.yScale0.min).toBe(0);
- expect(chartInstance.scales.yScale0.max).toBe(90);
- });
- it('Should correctly determine the max & min for scatter data', function() {
- chartInstance = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- xAxisID: 'xScale0',
- yAxisID: 'yScale0',
- data: [{
- x: 10,
- y: 100
- }, {
- x: -10,
- y: 0
- }, {
- x: 0,
- y: 0
- }, {
- x: 99,
- y: 7
- }]
- }],
- },
- options: {
- scales: {
- xAxes: [{
- id: 'xScale0',
- type: 'linear',
- position: 'bottom'
- }],
- yAxes: [{
- id: 'yScale0',
- type: 'linear'
- }]
- }
- }
- });
- chartInstance.update();
- expect(chartInstance.scales.xScale0.min).toBe(-20);
- expect(chartInstance.scales.xScale0.max).toBe(100);
- expect(chartInstance.scales.yScale0.min).toBe(0);
- expect(chartInstance.scales.yScale0.max).toBe(100);
- });
- it('Should correctly get the label for the given index', function() {
- chartInstance = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- xAxisID: 'xScale0',
- yAxisID: 'yScale0',
- data: [{
- x: 10,
- y: 100
- }, {
- x: -10,
- y: 0
- }, {
- x: 0,
- y: 0
- }, {
- x: 99,
- y: 7
- }]
- }],
- },
- options: {
- scales: {
- xAxes: [{
- id: 'xScale0',
- type: 'linear',
- position: 'bottom'
- }],
- yAxes: [{
- id: 'yScale0',
- type: 'linear'
- }]
- }
- }
- });
- chartInstance.update();
- expect(chartInstance.scales.yScale0.getLabelForIndex(3, 0)).toBe(7);
- });
- it('Should correctly determine the min and max data values when stacked mode is turned on', function() {
- chartInstance = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: [10, 5, 0, -5, 78, -100],
- type: 'bar'
- }, {
- yAxisID: 'yScale1',
- data: [-1000, 1000],
- }, {
- yAxisID: 'yScale0',
- data: [150, 0, 0, -100, -10, 9],
- type: 'bar'
- }, {
- yAxisID: 'yScale0',
- data: [10, 10, 10, 10, 10, 10],
- type: 'line'
- }],
- labels: ['a', 'b', 'c', 'd', 'e', 'f']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear',
- stacked: true
- }, {
- id: 'yScale1',
- type: 'linear'
- }]
- }
- }
- });
- chartInstance.update();
- expect(chartInstance.scales.yScale0.min).toBe(-150);
- expect(chartInstance.scales.yScale0.max).toBe(200);
- });
- it('Should correctly determine the min and max data values when stacked mode is turned on and there are hidden datasets', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: [10, 5, 0, -5, 78, -100],
- }, {
- yAxisID: 'yScale1',
- data: [-1000, 1000],
- }, {
- yAxisID: 'yScale0',
- data: [150, 0, 0, -100, -10, 9],
- }, {
- yAxisID: 'yScale0',
- data: [10, 20, 30, 40, 50, 60],
- hidden: true
- }],
- labels: ['a', 'b', 'c', 'd', 'e', 'f']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear',
- stacked: true
- }, {
- id: 'yScale1',
- type: 'linear'
- }]
- }
- }
- });
- chartInstance.update();
- expect(chartInstance.scales.yScale0.min).toBe(-150);
- expect(chartInstance.scales.yScale0.max).toBe(200);
- });
- it('Should correctly determine the min and max data values when stacked mode is turned on there are multiple types of datasets', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- type: 'bar',
- data: [10, 5, 0, -5, 78, -100]
- }, {
- type: 'line',
- data: [10, 10, 10, 10, 10, 10],
- }, {
- type: 'bar',
- data: [150, 0, 0, -100, -10, 9]
- }],
- labels: ['a', 'b', 'c', 'd', 'e', 'f']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear',
- stacked: true
- }]
- }
- }
- });
- chartInstance.scales.yScale0.determineDataLimits();
- expect(chartInstance.scales.yScale0.min).toBe(-105);
- expect(chartInstance.scales.yScale0.max).toBe(160);
- });
- it('Should ensure that the scale has a max and min that are not equal', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [],
- labels: ['a', 'b', 'c', 'd', 'e', 'f']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear'
- }]
- }
- }
- });
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-1);
- expect(chartInstance.scales.yScale0.max).toBe(1);
- });
- it('Should ensure that the scale has a max and min that are not equal when beginAtZero is set', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [],
- labels: ['a', 'b', 'c', 'd', 'e', 'f']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear',
- ticks: {
- beginAtZero: true
- }
- }]
- }
- }
- });
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(0);
- expect(chartInstance.scales.yScale0.max).toBe(1);
- });
- it('Should use the suggestedMin and suggestedMax options', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: [1, 1, 1, 2, 1, 0]
- }],
- labels: ['a', 'b', 'c', 'd', 'e', 'f']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear',
- ticks: {
- suggestedMax: 10,
- suggestedMin: -10
- }
- }]
- }
- }
- });
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-10);
- expect(chartInstance.scales.yScale0.max).toBe(10);
- });
- it('Should use the min and max options', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: [1, 1, 1, 2, 1, 0]
- }],
- labels: ['a', 'b', 'c', 'd', 'e', 'f']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear',
- ticks: {
- max: 1010,
- min: -1010
- }
- }]
- }
- }
- });
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.min).toBe(-1010);
- expect(chartInstance.scales.yScale0.max).toBe(1010);
- expect(chartInstance.scales.yScale0.ticks[0]).toBe('1010');
- expect(chartInstance.scales.yScale0.ticks[chartInstance.scales.yScale0.ticks.length - 1]).toBe('-1010');
- });
- it('should forcibly include 0 in the range if the beginAtZero option is used', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: [20, 30, 40, 50]
- }],
- labels: ['a', 'b', 'c', 'd']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear',
- }]
- }
- }
- });
- expect(chartInstance.scales.yScale0).not.toEqual(undefined); // must construct
- expect(chartInstance.scales.yScale0.ticks).toEqual(['50', '45', '40', '35', '30', '25', '20']);
- chartInstance.scales.yScale0.options.ticks.beginAtZero = true;
- chartInstance.update();
- expect(chartInstance.scales.yScale0.ticks).toEqual(['50', '45', '40', '35', '30', '25', '20', '15', '10', '5', '0']);
- chartInstance.data.datasets[0].data = [-20, -30, -40, -50];
- chartInstance.update();
- expect(chartInstance.scales.yScale0.ticks).toEqual(['0', '-5', '-10', '-15', '-20', '-25', '-30', '-35', '-40', '-45', '-50']);
- chartInstance.scales.yScale0.options.ticks.beginAtZero = false;
- chartInstance.update();
- expect(chartInstance.scales.yScale0.ticks).toEqual(['-20', '-25', '-30', '-35', '-40', '-45', '-50']);
- });
- it('Should generate tick marks in the correct order in reversed mode', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: [10, 5, 0, 25, 78]
- }],
- labels: ['a', 'b', 'c', 'd']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear',
- ticks: {
- reverse: true
- }
- }]
- }
- }
- });
- expect(chartInstance.scales.yScale0.ticks).toEqual(['0', '10', '20', '30', '40', '50', '60', '70', '80']);
- expect(chartInstance.scales.yScale0.start).toBe(80);
- expect(chartInstance.scales.yScale0.end).toBe(0);
- });
- it('should use the correct number of decimal places in the default format function', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: [0.06, 0.005, 0, 0.025, 0.0078]
- }],
- labels: ['a', 'b', 'c', 'd']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear',
- }]
- }
- }
- });
- expect(chartInstance.scales.yScale0.ticks).toEqual(['0.06', '0.05', '0.04', '0.03', '0.02', '0.01', '0']);
- });
- it('Should build labels using the user supplied callback', function() {
- chartInstance = window.acquireChart({
- type: 'bar',
- data: {
- datasets: [{
- yAxisID: 'yScale0',
- data: [10, 5, 0, 25, 78]
- }],
- labels: ['a', 'b', 'c', 'd']
- },
- options: {
- scales: {
- yAxes: [{
- id: 'yScale0',
- type: 'linear',
- ticks: {
- callback: function(value, index) {
- return index.toString();
- }
- }
- }]
- }
- }
- });
- // Just the index
- expect(chartInstance.scales.yScale0.ticks).toEqual(['0', '1', '2', '3', '4', '5', '6', '7', '8']);
- });
- it('Should get the correct pixel value for a point', function() {
- chartInstance = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- xAxisID: 'xScale0',
- yAxisID: 'yScale0',
- data: []
- }],
- },
- options: {
- scales: {
- xAxes: [{
- id: 'xScale0',
- type: 'linear',
- position: 'bottom'
- }],
- yAxes: [{
- id: 'yScale0',
- type: 'linear'
- }]
- }
- }
- });
- var xScale = chartInstance.scales.xScale0;
- expect(xScale.getPixelForValue(1, 0, 0)).toBeCloseToPixel(501); // right - paddingRight
- expect(xScale.getPixelForValue(-1, 0, 0)).toBeCloseToPixel(41); // left + paddingLeft
- expect(xScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(271); // halfway*/
- expect(xScale.getValueForPixel(501)).toBeCloseTo(1, 1e-2);
- expect(xScale.getValueForPixel(41)).toBeCloseTo(-1, 1e-2);
- expect(xScale.getValueForPixel(271)).toBeCloseTo(0, 1e-2);
- var yScale = chartInstance.scales.yScale0;
- expect(yScale.getPixelForValue(1, 0, 0)).toBeCloseToPixel(32); // right - paddingRight
- expect(yScale.getPixelForValue(-1, 0, 0)).toBeCloseToPixel(484); // left + paddingLeft
- expect(yScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(258); // halfway*/
- expect(yScale.getValueForPixel(32)).toBe(1);
- expect(yScale.getValueForPixel(484)).toBe(-1);
- expect(yScale.getValueForPixel(258)).toBe(0);
- });
- it('should fit correctly', function() {
- chartInstance = window.acquireChart({
- type: 'line',
- data: {
- datasets: [{
- xAxisID: 'xScale0',
- yAxisID: 'yScale0',
- data: [{
- x: 10,
- y: 100
- }, {
- x: -10,
- y: 0
- }, {
- x: 0,
- y: 0
- }, {
- x: 99,
- y: 7
- }]
- }],
- },
- options: {
- scales: {
- xAxes: [{
- id: 'xScale0',
- type: 'linear',
- position: 'bottom'
- }],
- yAxes: [{
- id: 'yScale0',
- type: 'linear'
- }]
- }
- }
- });
- var xScale = chartInstance.scales.xScale0;
- expect(xScale.paddingTop).toBeCloseToPixel(0);
- expect(xScale.paddingBottom).toBeCloseToPixel(0);
- expect(xScale.paddingLeft).toBeCloseToPixel(0);
- expect(xScale.paddingRight).toBeCloseToPixel(13.5);
- expect(xScale.width).toBeCloseToPixel(471);
- expect(xScale.height).toBeCloseToPixel(28);
- var yScale = chartInstance.scales.yScale0;
- expect(yScale.paddingTop).toBeCloseToPixel(0);
- expect(yScale.paddingBottom).toBeCloseToPixel(0);
- expect(yScale.paddingLeft).toBeCloseToPixel(0);
- expect(yScale.paddingRight).toBeCloseToPixel(0);
- expect(yScale.width).toBeCloseToPixel(41);
- expect(yScale.height).toBeCloseToPixel(452);
- // Extra size when scale label showing
- xScale.options.scaleLabel.display = true;
- yScale.options.scaleLabel.display = true;
- chartInstance.update();
- expect(xScale.paddingTop).toBeCloseToPixel(0);
- expect(xScale.paddingBottom).toBeCloseToPixel(0);
- expect(xScale.paddingLeft).toBeCloseToPixel(0);
- expect(xScale.paddingRight).toBeCloseToPixel(13.5);
- expect(xScale.width).toBeCloseToPixel(453);
- expect(xScale.height).toBeCloseToPixel(46);
- expect(yScale.paddingTop).toBeCloseToPixel(0);
- expect(yScale.paddingBottom).toBeCloseToPixel(0);
- expect(yScale.paddingLeft).toBeCloseToPixel(0);
- expect(yScale.paddingRight).toBeCloseToPixel(0);
- expect(yScale.width).toBeCloseToPixel(59);
- expect(yScale.height).toBeCloseToPixel(434);
- });
- });
|