Skip to content

Commit 629f7a0

Browse files
authored
Merge pull request #2182 from plotly/text-col-width
`table` `columnwidth` to accept strings
2 parents b47e174 + 919a1d9 commit 629f7a0

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/traces/table/data_preparation_helper.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
var c = require('./constants');
1212
var extendFlat = require('../../lib/extend').extendFlat;
13+
var isNumeric = require('fast-isnumeric');
1314

1415
// pure functions, don't alter but passes on `gd` and parts of `trace` without deep copying
1516
module.exports = function calc(gd, trace) {
@@ -32,9 +33,10 @@ module.exports = function calc(gd, trace) {
3233
var uniqueKeys = {};
3334
var columnOrder = trace._fullInput.columnorder;
3435
var columnWidths = headerValues.map(function(d, i) {
35-
return Array.isArray(trace.columnwidth) ?
36+
var value = Array.isArray(trace.columnwidth) ?
3637
trace.columnwidth[Math.min(i, trace.columnwidth.length - 1)] :
37-
isFinite(trace.columnwidth) && trace.columnwidth !== null ? trace.columnwidth : 1;
38+
trace.columnwidth;
39+
return isNumeric(value) ? Number(value) : 1;
3840
});
3941
var totalColumnWidths = columnWidths.reduce(function(p, n) {return p + n;}, 0);
4042

test/image/mocks/table_plain_birds.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"y": [0, 1]
1515
},
1616

17-
"columnwidth": [20, 40, 25, 25, 30, 20, 30],
17+
"columnwidth": ["20", "40", "25", "25", "30", "20", "30"],
1818
"columnorder": [0, 1, 2, 3, 4, 5, 6],
1919

2020
"header": {

test/jasmine/tests/table_test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ describe('table initialization tests', function() {
9292
expect(fullTrace.cells.values).toEqual([]);
9393
});
9494

95+
it('\'columnwidth\' specification should accept a numerical array', function() {
96+
var fullTrace = _supply({
97+
columnwidth: [1, 2, 3]
98+
});
99+
expect(fullTrace.columnwidth).toEqual([1, 2, 3]);
100+
});
101+
102+
it('\'columnwidth\' specification should accept a string array (converted downstream)', function() {
103+
var fullTrace = _supply({
104+
columnwidth: ['1', '2', '3']
105+
});
106+
expect(fullTrace.columnwidth).toEqual(['1', '2', '3']);
107+
});
108+
95109
it('\'header\' should be used with default values where attributes are not provided', function() {
96110
var fullTrace = _supply({
97111
header: {

0 commit comments

Comments
 (0)