Skip to content
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

Set Cartesian axis attributes via template #4670

Merged
merged 7 commits into from
Mar 25, 2020
Merged
12 changes: 7 additions & 5 deletions src/plots/cartesian/axis_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,

var visible = coerce('visible', !options.visibleDflt);

var axType = containerOut.type;
var axTemplate = containerOut._template || {};
var axType = containerOut.type || axTemplate.type || '-';

if(axType === 'date') {
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleDefaults');
Expand Down Expand Up @@ -105,10 +106,10 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,

if(options.automargin) coerce('automargin');

var isMultiCategory = containerOut.type === 'multicategory';
var isMultiCategory = axType === 'multicategory';

if(!options.noTickson &&
(containerOut.type === 'category' || isMultiCategory) &&
(axType === 'category' || isMultiCategory) &&
(containerOut.ticks || containerOut.showgrid)
) {
var ticksonDflt;
Expand All @@ -124,8 +125,9 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
}
}

if(containerOut.type === 'date') {
var rangebreaks = containerIn.rangebreaks;
if(axType === 'date') {
var rangebreaks = containerIn.rangebreaks || axTemplate.rangebreaks;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't quite right... [] is truthy, also axTemplate.rangeBreaks items will extend the array if it contains named items.

Can we instead do:

handleArrayContainerDefaults(...);

if (containerOut.rangebreaks.length) {
   // setConvert and gl / splom check
}
else {
   delete containerOut.rangebreaks;
}

Same for tickformatstops

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Done in f824902.


if(Array.isArray(rangebreaks) && rangebreaks.length) {
handleArrayContainerDefaults(containerIn, containerOut, {
name: 'rangebreaks',
Expand Down
5 changes: 4 additions & 1 deletion src/plots/cartesian/tick_label_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ function handleOtherDefaults(containerIn, containerOut, coerce, axType, options)

if(axType !== 'category') {
var tickFormat = coerce('tickformat');
var tickformatStops = containerIn.tickformatstops;

var axTemplate = containerOut._template || {};
var tickformatStops = containerIn.tickformatstops || axTemplate.tickformatstops;

if(Array.isArray(tickformatStops) && tickformatStops.length) {
handleArrayContainerDefaults(containerIn, containerOut, {
name: 'tickformatstops',
Expand Down
48 changes: 48 additions & 0 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5411,3 +5411,51 @@ describe('Test tickformatstops:', function() {
.then(done);
});
});

describe('Test template:', function() {
'use strict';

var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);

it('apply axis *type*, *rangebreaks* and *tickformatstops* from template', function(done) {
Plotly.newPlot(gd, {
data: [{
x: [1e10, 2e10, 3e10, 4e10, 5e10, 6e10, 7e10],
y: [1, 2, 3, 4, 5, 6, 7]
}],
layout: {
template: {
layout: {
xaxis: {
type: 'date',
rangebreaks: [{
name: 'name1', // N.B. should provide name
bounds: ['sat', 'mon']
}],
tickformatstops: [{
name: 'name2', // N.B. should provide name
enabled: true,
dtickrange: [1000, 60000],
value: '%H:%M:%S s'
}]
}
}
}
}
})
.then(function() {
var xaxis = gd._fullLayout.xaxis;
expect(xaxis.type).toBe('date');
expect(xaxis.rangebreaks).not.toBe(undefined, 'rangebreaks');
expect(xaxis.rangebreaks.length).toBe(1);
expect(xaxis.tickformatstops).not.toBe(undefined, 'tickformatstops');
expect(xaxis.tickformatstops.length).toBe(1);
})
.catch(failTest)
.then(done);
});
});