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,7 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,

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

var axType = containerOut.type;
var axType = containerOut.type || options.axTemplate.type || '-';

if(axType === 'date') {
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleDefaults');
Expand Down Expand Up @@ -105,10 +105,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 +124,10 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
}
}

if(containerOut.type === 'date') {
var rangebreaks = containerIn.rangebreaks;
if(axType === 'date') {
var fromTemplate = (options.axTemplate || {}).rangebreaks;
var rangebreaks = containerIn.rangebreaks || fromTemplate;

if(Array.isArray(rangebreaks) && rangebreaks.length) {
handleArrayContainerDefaults(containerIn, containerOut, {
name: 'rangebreaks',
Expand Down
12 changes: 12 additions & 0 deletions src/plots/cartesian/layout_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,13 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
axHasImage[axName]
));

var axTemplate;
if(Lib.isPlainObject(layoutOut._template)) {
axTemplate = layoutOut._template[axLayoutOut._name];
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
}

var defaultOptions = {
axTemplate: axTemplate || {},
letter: axLetter,
font: layoutOut.font,
outerTicks: outerTicks[axName],
Expand Down Expand Up @@ -295,7 +301,13 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
axLayoutOut = Template.newContainer(layoutOut, axName, axLetter + 'axis');
newAxLayoutOut();

var axTemplate2;
if(Lib.isPlainObject(layoutOut._template)) {
axTemplate2 = layoutOut._template[axLayoutOut._name];
}

var defaultOptions2 = {
axTemplate: axTemplate2 || {},
letter: axLetter,
font: layoutOut.font,
outerTicks: outerTicks[axName],
Expand Down
4 changes: 3 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,9 @@ function handleOtherDefaults(containerIn, containerOut, coerce, axType, options)

if(axType !== 'category') {
var tickFormat = coerce('tickformat');
var tickformatStops = containerIn.tickformatstops;
var fromTemplate = (options.axTemplate || {}).tickformatstops;
var tickformatStops = containerIn.tickformatstops || fromTemplate;

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);
});
});