Skip to content

Commit e472e14

Browse files
committed
plotly#189 initial round of coercions with test cases
1 parent 166aeb4 commit e472e14

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

src/plots/cartesian/axis_defaults.js

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var Plots = require('../plots');
1717
var layoutAttributes = require('./layout_attributes');
1818
var handleTickValueDefaults = require('./tick_value_defaults');
1919
var handleTickDefaults = require('./tick_defaults');
20+
var handleCategoryModeDefaults = require('./category_mode_defaults');
2021
var setConvert = require('./set_convert');
2122
var orderedCategories = require('./ordered_categories');
2223
var cleanDatum = require('./clean_datum');
@@ -96,6 +97,7 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
9697

9798
handleTickValueDefaults(containerIn, containerOut, coerce, axType);
9899
handleTickDefaults(containerIn, containerOut, coerce, axType, options);
100+
handleCategoryModeDefaults(containerIn, containerOut, coerce);
99101

100102
var lineColor = Lib.coerce2(containerIn, containerOut, layoutAttributes, 'linecolor'),
101103
lineWidth = Lib.coerce2(containerIn, containerOut, layoutAttributes, 'linewidth'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
module.exports = function handleCategoryModeDefaults(containerIn, containerOut, coerce) {
13+
14+
if(containerIn.type === 'category') {
15+
16+
var validCategories = ['trace', 'category ascending', 'category descending', 'array'];
17+
18+
var properCategoryList = Array.isArray(containerIn.categorylist) && containerIn.categorylist.length > 0;
19+
20+
if(validCategories.indexOf(containerIn.categorymode) === -1 && properCategoryList) {
21+
22+
// when unspecified or invalid, use the default, unless categorylist implies 'array'
23+
coerce('categorymode', 'array'); // promote to 'array
24+
25+
} else if(containerIn.categorymode === 'array' && !properCategoryList) {
26+
27+
// when mode is 'array' but no list is given, revert to default
28+
29+
containerIn.categorymode = 'trace'; // revert to default
30+
coerce('categorymode');
31+
32+
} else {
33+
34+
// otherwise use the supplied mode, or the default one if unsupplied or invalid
35+
coerce('categorymode');
36+
37+
}
38+
}
39+
};

test/jasmine/tests/axes_test.js

+62
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,68 @@ describe('Test axes', function() {
321321
});
322322
});
323323

324+
describe('categorymode', function() {
325+
326+
var gd;
327+
328+
beforeEach(function() {
329+
gd = createGraphDiv();
330+
});
331+
332+
afterEach(destroyGraphDiv);
333+
334+
describe('setting, or not setting categorymode if it is not explicitly declared', function() {
335+
336+
it('should set categorymode to default if categorymode and categorylist are not supplied', function() {
337+
PlotlyInternal.plot(gd, [{x: ['c','a','e','b','d'], y: [15,11,12,13,14]}], {xaxis: {type: 'category'}});
338+
expect(gd._fullLayout.xaxis.categorymode).toBe('trace');
339+
});
340+
341+
it('should set categorymode to default even if type is not set to category explicitly', function() {
342+
PlotlyInternal.plot(gd, [{x: ['c','a','e','b','d'], y: [15,11,12,13,14]}]);
343+
expect(gd._fullLayout.xaxis.categorymode).toBe('trace');
344+
});
345+
346+
it('should NOT set categorymode to default if type is not category', function() {
347+
PlotlyInternal.plot(gd, [{x: ['c','a','e','b','d'], y: [15,11,12,13,14]}]);
348+
expect(gd._fullLayout.yaxis.categorymode).toBe(undefined);
349+
});
350+
351+
it('should set categorymode to default if type is overridden to be category', function() {
352+
PlotlyInternal.plot(gd, [{x: [1,2,3,4,5], y: [15,11,12,13,14]}], {yaxis: {type: 'category'}});
353+
expect(gd._fullLayout.xaxis.categorymode).toBe(undefined);
354+
expect(gd._fullLayout.yaxis.categorymode).toBe('trace');
355+
});
356+
357+
});
358+
359+
describe('setting, or not setting categorymode to "array"', function() {
360+
361+
it('should leave categorymode on "array" if it is supplied', function() {
362+
PlotlyInternal.plot(gd, [{x: ['c','a','e','b','d'], y: [15,11,12,13,14]}], {
363+
xaxis: {type: 'category', categorymode: "array", categorylist: ['b','a','d','e','c']}
364+
});
365+
expect(gd._fullLayout.xaxis.categorymode).toBe('array');
366+
});
367+
368+
it('should switch categorymode on "array" if it is not supplied but categorylist is supplied', function() {
369+
PlotlyInternal.plot(gd, [{x: ['c','a','e','b','d'], y: [15,11,12,13,14]}], {
370+
xaxis: {type: 'category', categorylist: ['b','a','d','e','c']}
371+
});
372+
expect(gd._fullLayout.xaxis.categorymode).toBe('array');
373+
});
374+
375+
it('should revert categorymode to "trace" if "array" is supplied but there is no list', function() {
376+
PlotlyInternal.plot(gd, [{x: ['c','a','e','b','d'], y: [15,11,12,13,14]}], {
377+
xaxis: {type: 'category', categorymode: "array"}
378+
});
379+
expect(gd._fullLayout.xaxis.categorymode).toBe('trace');
380+
});
381+
382+
});
383+
384+
});
385+
324386
describe('handleTickDefaults', function() {
325387
var data = [{ x: [1,2,3], y: [3,4,5] }],
326388
gd;

0 commit comments

Comments
 (0)