Skip to content

Commit 6034f6b

Browse files
authored
Merge pull request #4670 from plotly/rangebreaks-template
Set Cartesian axis attributes via template
2 parents efb54b2 + f824902 commit 6034f6b

File tree

3 files changed

+71
-18
lines changed

3 files changed

+71
-18
lines changed

src/plots/cartesian/axis_defaults.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
5151

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

54-
var axType = containerOut.type;
54+
var axTemplate = containerOut._template || {};
55+
var axType = containerOut.type || axTemplate.type || '-';
5556

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

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

108-
var isMultiCategory = containerOut.type === 'multicategory';
109+
var isMultiCategory = axType === 'multicategory';
109110

110111
if(!options.noTickson &&
111-
(containerOut.type === 'category' || isMultiCategory) &&
112+
(axType === 'category' || isMultiCategory) &&
112113
(containerOut.ticks || containerOut.showgrid)
113114
) {
114115
var ticksonDflt;
@@ -124,14 +125,16 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
124125
}
125126
}
126127

127-
if(containerOut.type === 'date') {
128-
var rangebreaks = containerIn.rangebreaks;
129-
if(Array.isArray(rangebreaks) && rangebreaks.length) {
130-
handleArrayContainerDefaults(containerIn, containerOut, {
131-
name: 'rangebreaks',
132-
inclusionAttr: 'enabled',
133-
handleItemDefaults: rangebreaksDefaults
134-
});
128+
if(axType === 'date') {
129+
handleArrayContainerDefaults(containerIn, containerOut, {
130+
name: 'rangebreaks',
131+
inclusionAttr: 'enabled',
132+
handleItemDefaults: rangebreaksDefaults
133+
});
134+
135+
if(!containerOut.rangebreaks.length) {
136+
delete containerOut.rangebreaks;
137+
} else {
135138
setConvert(containerOut, layoutOut);
136139

137140
if(layoutOut._has('scattergl') || layoutOut._has('splom')) {

src/plots/cartesian/tick_label_defaults.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ function handleOtherDefaults(containerIn, containerOut, coerce, axType, options)
5959

6060
if(axType !== 'category') {
6161
var tickFormat = coerce('tickformat');
62-
var tickformatStops = containerIn.tickformatstops;
63-
if(Array.isArray(tickformatStops) && tickformatStops.length) {
64-
handleArrayContainerDefaults(containerIn, containerOut, {
65-
name: 'tickformatstops',
66-
inclusionAttr: 'enabled',
67-
handleItemDefaults: tickformatstopDefaults
68-
});
62+
63+
handleArrayContainerDefaults(containerIn, containerOut, {
64+
name: 'tickformatstops',
65+
inclusionAttr: 'enabled',
66+
handleItemDefaults: tickformatstopDefaults
67+
});
68+
if(!containerOut.tickformatstops.length) {
69+
delete containerOut.tickformatstops;
6970
}
71+
7072
if(!tickFormat && axType !== 'date') {
7173
coerce('showexponent', showAttrDflt);
7274
coerce('exponentformat');

test/jasmine/tests/axes_test.js

+48
Original file line numberDiff line numberDiff line change
@@ -5411,3 +5411,51 @@ describe('Test tickformatstops:', function() {
54115411
.then(done);
54125412
});
54135413
});
5414+
5415+
describe('Test template:', function() {
5416+
'use strict';
5417+
5418+
var gd;
5419+
beforeEach(function() {
5420+
gd = createGraphDiv();
5421+
});
5422+
afterEach(destroyGraphDiv);
5423+
5424+
it('apply axis *type*, *rangebreaks* and *tickformatstops* from template', function(done) {
5425+
Plotly.newPlot(gd, {
5426+
data: [{
5427+
x: [1e10, 2e10, 3e10, 4e10, 5e10, 6e10, 7e10],
5428+
y: [1, 2, 3, 4, 5, 6, 7]
5429+
}],
5430+
layout: {
5431+
template: {
5432+
layout: {
5433+
xaxis: {
5434+
type: 'date',
5435+
rangebreaks: [{
5436+
name: 'name1', // N.B. should provide name
5437+
bounds: ['sat', 'mon']
5438+
}],
5439+
tickformatstops: [{
5440+
name: 'name2', // N.B. should provide name
5441+
enabled: true,
5442+
dtickrange: [1000, 60000],
5443+
value: '%H:%M:%S s'
5444+
}]
5445+
}
5446+
}
5447+
}
5448+
}
5449+
})
5450+
.then(function() {
5451+
var xaxis = gd._fullLayout.xaxis;
5452+
expect(xaxis.type).toBe('date');
5453+
expect(xaxis.rangebreaks).not.toBe(undefined, 'rangebreaks');
5454+
expect(xaxis.rangebreaks.length).toBe(1);
5455+
expect(xaxis.tickformatstops).not.toBe(undefined, 'tickformatstops');
5456+
expect(xaxis.tickformatstops.length).toBe(1);
5457+
})
5458+
.catch(failTest)
5459+
.then(done);
5460+
});
5461+
});

0 commit comments

Comments
 (0)