Skip to content

Layout grids #2399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 26, 2018
2 changes: 1 addition & 1 deletion src/lib/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var isNumeric = require('fast-isnumeric');
exports.aggNums = function(f, v, a, len) {
var i,
b;
if(!len) len = a.length;
if(!len || len > a.length) len = a.length;
if(!isNumeric(v)) v = false;
if(Array.isArray(a[0])) {
b = new Array(len);
Expand Down
1 change: 0 additions & 1 deletion src/traces/parcoords/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ module.exports = {
values: {
valType: 'data_array',
role: 'info',
dflt: [],
editType: 'calc',
description: [
'Dimension values. `values[n]` represents the value of the `n`th point in the dataset,',
Expand Down
12 changes: 10 additions & 2 deletions src/traces/parcoords/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ var wrap = require('../../lib/gup').wrap;

module.exports = function calc(gd, trace) {
var cs = !!trace.line.colorscale && Lib.isArray(trace.line.color);
var color = cs ? trace.line.color : Array.apply(0, Array(trace.dimensions.reduce(function(p, n) {return Math.max(p, n.values.length);}, 0))).map(function() {return 0.5;});
var color = cs ? trace.line.color : constHalf(trace._commonLength);
var cscale = cs ? trace.line.colorscale : [[0, trace.line.color], [1, trace.line.color]];

if(hasColorscale(trace, 'line')) {
calcColorscale(trace, trace.line.color, 'line', 'c');
calcColorscale(trace, color, 'line', 'c');
}

return wrap({
lineColor: color,
cscale: cscale
});
};

function constHalf(len) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this more readable albeit less functional. 👌

var out = new Array(len);
for(var i = 0; i < len; i++) {
out[i] = 0.5;
}
return out;
}
49 changes: 29 additions & 20 deletions src/traces/parcoords/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ var handleDomainDefaults = require('../../plots/domain').defaults;

function handleLineDefaults(traceIn, traceOut, defaultColor, layout, coerce) {

coerce('line.color', defaultColor);

if(hasColorscale(traceIn, 'line') && Lib.isArray(traceIn.line.color)) {
coerce('line.colorscale');
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: 'line.', cLetter: 'c'});
}
else {
coerce('line.color', defaultColor);
var lineColor = coerce('line.color', defaultColor);

if(hasColorscale(traceIn, 'line') && Lib.isArray(lineColor)) {
if(lineColor.length) {
coerce('line.colorscale');
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: 'line.', cLetter: 'c'});
// TODO: I think it would be better to keep showing lines beyond the last line color
// but I'm not sure what color to give these lines - probably black or white
// depending on the background color?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree here.

Currently, scatter traces with marker.color arrays shorter than the coordinates get black markers - regardless of plot_bgcolor. Adding some logic around the background color would be even better.

This probably deserves a new issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great -> #2405

traceOut._commonLength = Math.min(traceOut._commonLength, lineColor.length);
}
else {
traceOut.line.color = defaultColor;
}
}
}

Expand Down Expand Up @@ -53,7 +59,10 @@ function dimensionsDefaults(traceIn, traceOut) {
}

var values = coerce('values');
var visible = coerce('visible', values.length > 0);
var visible = coerce('visible');
if(!(values && values.length)) {
visible = dimensionOut.visible = false;
}

if(visible) {
coerce('label');
Expand All @@ -63,21 +72,14 @@ function dimensionsDefaults(traceIn, traceOut) {
coerce('range');
coerce('constraintrange');

commonLength = Math.min(commonLength, dimensionOut.values.length);
commonLength = Math.min(commonLength, values.length);
}

dimensionOut._index = i;
dimensionsOut.push(dimensionOut);
}

if(isFinite(commonLength)) {
for(i = 0; i < dimensionsOut.length; i++) {
dimensionOut = dimensionsOut[i];
if(dimensionOut.visible && dimensionOut.values.length > commonLength) {
dimensionOut.values = dimensionOut.values.slice(0, commonLength);
}
}
}
traceOut._commonLength = commonLength;

return dimensionsOut;
}
Expand All @@ -97,11 +99,18 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
traceOut.visible = false;
}

// make default font size 10px,
// since we're not slicing uneven arrays anymore, stash the length in each dimension
// but we can't do this in dimensionsDefaults (yet?) because line.color can also
// truncate
for(var i = 0; i < dimensions.length; i++) {
if(dimensions[i].visible) dimensions[i]._length = traceOut._commonLength;
}

// make default font size 10px (default is 12),
// scale linearly with global font size
var fontDflt = {
family: layout.font.family,
size: Math.round(layout.font.size * (10 / 12)),
size: Math.round(layout.font.size / 1.2),
color: layout.font.color
};

Expand Down
20 changes: 15 additions & 5 deletions src/traces/parcoords/parcoords.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ function visible(dimension) {return !('visible' in dimension) || dimension.visib

function dimensionExtent(dimension) {

var lo = dimension.range ? dimension.range[0] : d3.min(dimension.values);
var hi = dimension.range ? dimension.range[1] : d3.max(dimension.values);
var lo = dimension.range ? dimension.range[0] : Lib.aggNums(Math.min, null, dimension.values, dimension._length);
var hi = dimension.range ? dimension.range[1] : Lib.aggNums(Math.max, null, dimension.values, dimension._length);

if(isNaN(lo) || !isFinite(lo)) {
lo = 0;
Expand Down Expand Up @@ -139,7 +139,11 @@ function model(layout, d, i) {
rangeFont = trace.rangefont;

var lines = Lib.extendDeep({}, line, {
color: lineColor.map(domainToUnitScale({values: lineColor, range: [line.cmin, line.cmax]})),
color: lineColor.map(domainToUnitScale({
values: lineColor,
range: [line.cmin, line.cmax],
_length: trace._commonLength
})),
blockLineCount: c.blockLineCount,
canvasOverdrag: c.overdrag * c.canvasPixelRatio
});
Expand Down Expand Up @@ -201,6 +205,12 @@ function viewModel(model) {
var foundKey = uniqueKeys[dimension.label];
uniqueKeys[dimension.label] = (foundKey || 0) + 1;
var key = dimension.label + (foundKey ? '__' + foundKey : '');

var truncatedValues = dimension.values;
if(truncatedValues.length > dimension._length) {
truncatedValues = truncatedValues.slice(0, dimension._length);
}

return {
key: key,
label: dimension.label,
Expand All @@ -213,8 +223,8 @@ function viewModel(model) {
crossfilterDimensionIndex: i,
visibleIndex: dimension._index,
height: height,
values: dimension.values,
paddedUnitValues: dimension.values.map(domainToUnit).map(paddedUnitScale),
values: truncatedValues,
paddedUnitValues: truncatedValues.map(domainToUnit).map(paddedUnitScale),
xScale: xScale,
x: xScale(i),
canvasX: xScale(i) * canvasPixelRatio,
Expand Down
17 changes: 6 additions & 11 deletions test/jasmine/tests/parcoords_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('parcoords initialization tests', function() {
alienProperty: 'Alpha Centauri'
}]
});
expect(fullTrace.dimensions).toEqual([{values: [1], visible: true, tickformat: '3s', _index: 0}]);
expect(fullTrace.dimensions).toEqual([{values: [1], visible: true, tickformat: '3s', _index: 0, _length: 1}]);
});

it('\'dimension.visible\' should be set to false, and other props just passed through if \'values\' is not provided', function() {
Expand All @@ -127,7 +127,7 @@ describe('parcoords initialization tests', function() {
alienProperty: 'Alpha Centauri'
}]
});
expect(fullTrace.dimensions).toEqual([{visible: false, values: [], _index: 0}]);
expect(fullTrace.dimensions).toEqual([{visible: false, _index: 0}]);
});

it('\'dimension.visible\' should be set to false, and other props just passed through if \'values\' is an empty array', function() {
Expand All @@ -147,22 +147,17 @@ describe('parcoords initialization tests', function() {
alienProperty: 'Alpha Centauri'
}]
});
expect(fullTrace.dimensions).toEqual([{visible: false, values: [], _index: 0}]);
expect(fullTrace.dimensions).toEqual([{visible: false, _index: 0}]);
});

it('\'dimension.values\' should get truncated to a common shortest length', function() {
it('\'dimension.values\' should get truncated to a common shortest *nonzero* length', function() {
var fullTrace = _supply({dimensions: [
{values: [321, 534, 542, 674]},
{values: [562, 124, 942]},
{values: [], visible: true},
{values: [1, 2], visible: false} // shouldn't be truncated to as false
{values: [1, 2], visible: false} // shouldn't be truncated to as visible: false
]});
expect(fullTrace.dimensions).toEqual([
{values: [], visible: true, tickformat: '3s', _index: 0},
{values: [], visible: true, tickformat: '3s', _index: 1},
{values: [], visible: true, tickformat: '3s', _index: 2},
{values: [1, 2], visible: false, _index: 3}
]);
expect(fullTrace._commonLength).toBe(3);
});
});

Expand Down