Skip to content

Commit

Permalink
Merge pull request #4670 from plotly/rangebreaks-template
Browse files Browse the repository at this point in the history
Set Cartesian axis attributes via template
  • Loading branch information
archmoj committed Mar 25, 2020
2 parents efb54b2 + f824902 commit 6034f6b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
25 changes: 14 additions & 11 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,14 +125,16 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
}
}

if(containerOut.type === 'date') {
var rangebreaks = containerIn.rangebreaks;
if(Array.isArray(rangebreaks) && rangebreaks.length) {
handleArrayContainerDefaults(containerIn, containerOut, {
name: 'rangebreaks',
inclusionAttr: 'enabled',
handleItemDefaults: rangebreaksDefaults
});
if(axType === 'date') {
handleArrayContainerDefaults(containerIn, containerOut, {
name: 'rangebreaks',
inclusionAttr: 'enabled',
handleItemDefaults: rangebreaksDefaults
});

if(!containerOut.rangebreaks.length) {
delete containerOut.rangebreaks;
} else {
setConvert(containerOut, layoutOut);

if(layoutOut._has('scattergl') || layoutOut._has('splom')) {
Expand Down
16 changes: 9 additions & 7 deletions src/plots/cartesian/tick_label_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ function handleOtherDefaults(containerIn, containerOut, coerce, axType, options)

if(axType !== 'category') {
var tickFormat = coerce('tickformat');
var tickformatStops = containerIn.tickformatstops;
if(Array.isArray(tickformatStops) && tickformatStops.length) {
handleArrayContainerDefaults(containerIn, containerOut, {
name: 'tickformatstops',
inclusionAttr: 'enabled',
handleItemDefaults: tickformatstopDefaults
});

handleArrayContainerDefaults(containerIn, containerOut, {
name: 'tickformatstops',
inclusionAttr: 'enabled',
handleItemDefaults: tickformatstopDefaults
});
if(!containerOut.tickformatstops.length) {
delete containerOut.tickformatstops;
}

if(!tickFormat && axType !== 'date') {
coerce('showexponent', showAttrDflt);
coerce('exponentformat');
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);
});
});

0 comments on commit 6034f6b

Please sign in to comment.