diff --git a/src/traces/table/data_preparation_helper.js b/src/traces/table/data_preparation_helper.js index 5988cdd29e0..46253f97b56 100644 --- a/src/traces/table/data_preparation_helper.js +++ b/src/traces/table/data_preparation_helper.js @@ -10,6 +10,7 @@ var c = require('./constants'); var extendFlat = require('../../lib/extend').extendFlat; +var isNumeric = require('fast-isnumeric'); // pure functions, don't alter but passes on `gd` and parts of `trace` without deep copying module.exports = function calc(gd, trace) { @@ -32,9 +33,10 @@ module.exports = function calc(gd, trace) { var uniqueKeys = {}; var columnOrder = trace._fullInput.columnorder; var columnWidths = headerValues.map(function(d, i) { - return Array.isArray(trace.columnwidth) ? + var value = Array.isArray(trace.columnwidth) ? trace.columnwidth[Math.min(i, trace.columnwidth.length - 1)] : - isFinite(trace.columnwidth) && trace.columnwidth !== null ? trace.columnwidth : 1; + trace.columnwidth; + return isNumeric(value) ? Number(value) : 1; }); var totalColumnWidths = columnWidths.reduce(function(p, n) {return p + n;}, 0); diff --git a/test/image/mocks/table_plain_birds.json b/test/image/mocks/table_plain_birds.json index df3355f94d6..45881c35bac 100644 --- a/test/image/mocks/table_plain_birds.json +++ b/test/image/mocks/table_plain_birds.json @@ -14,7 +14,7 @@ "y": [0, 1] }, - "columnwidth": [20, 40, 25, 25, 30, 20, 30], + "columnwidth": ["20", "40", "25", "25", "30", "20", "30"], "columnorder": [0, 1, 2, 3, 4, 5, 6], "header": { diff --git a/test/jasmine/tests/table_test.js b/test/jasmine/tests/table_test.js index 5e936838b1d..ddc2676d4cb 100644 --- a/test/jasmine/tests/table_test.js +++ b/test/jasmine/tests/table_test.js @@ -92,6 +92,20 @@ describe('table initialization tests', function() { expect(fullTrace.cells.values).toEqual([]); }); + it('\'columnwidth\' specification should accept a numerical array', function() { + var fullTrace = _supply({ + columnwidth: [1, 2, 3] + }); + expect(fullTrace.columnwidth).toEqual([1, 2, 3]); + }); + + it('\'columnwidth\' specification should accept a string array (converted downstream)', function() { + var fullTrace = _supply({ + columnwidth: ['1', '2', '3'] + }); + expect(fullTrace.columnwidth).toEqual(['1', '2', '3']); + }); + it('\'header\' should be used with default values where attributes are not provided', function() { var fullTrace = _supply({ header: {