Skip to content

Commit cb78d19

Browse files
committed
implement radialaxis.title
1 parent 20b17e5 commit cb78d19

File tree

10 files changed

+154
-14
lines changed

10 files changed

+154
-14
lines changed

src/components/titles/index.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ Titles.draw = function(gd, titleClass, options) {
6565
var group = options.containerGroup;
6666

6767
var fullLayout = gd._fullLayout;
68-
var font = cont.titlefont.family;
69-
var fontSize = cont.titlefont.size;
70-
var fontColor = cont.titlefont.color;
68+
var titlefont = cont.titlefont || {};
69+
var font = titlefont.family;
70+
var fontSize = titlefont.size;
71+
var fontColor = titlefont.color;
7172

7273
var opacity = 1;
7374
var isplaceholder = false;
74-
var txt = cont.title.trim();
75+
var txt = (cont.title || '').trim();
7576

7677
// only make this title editable if we positively identify its property
7778
// as one that has editing enabled.
@@ -120,10 +121,21 @@ Titles.draw = function(gd, titleClass, options) {
120121
}
121122

122123
function drawTitle(titleEl) {
123-
titleEl.attr('transform', transform ?
124-
'rotate(' + [transform.rotate, attributes.x, attributes.y] +
125-
') translate(0, ' + transform.offset + ')' :
126-
null);
124+
var transformVal;
125+
126+
if(transform) {
127+
transformVal = '';
128+
if(transform.rotate) {
129+
transformVal += 'rotate(' + [transform.rotate, attributes.x, attributes.y] + ')';
130+
}
131+
if(transform.offset) {
132+
transformVal += 'translate(0, ' + transform.offset + ')';
133+
}
134+
} else {
135+
transformVal = null;
136+
}
137+
138+
titleEl.attr('transform', transformVal);
127139

128140
titleEl.style({
129141
'font-family': font,

src/plots/polar/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ function clean(newFullData, newFullLayout, oldFullData, oldFullLayout) {
6060

6161
if(!newFullLayout[id] && !!oldSubplot) {
6262
oldSubplot.framework.remove();
63+
oldSubplot.layers['radial-axis-title'].remove();
6364

6465
for(var k in oldSubplot.clipPaths) {
6566
oldSubplot.clipPaths[k].remove();

src/plots/polar/layout_attributes.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,9 @@ var radialAxisAttrs = {
119119
].join(' ')
120120
},
121121

122-
// not sure about these
123-
// maybe just for radialaxis ??
124-
title: axesAttrs.title,
125-
titlefont: axesAttrs.titlefont,
122+
title: extendFlat({}, axesAttrs.title, {editType: 'plot', dflt: ''}),
123+
titlefont: overrideAll(axesAttrs.titlefont, 'plot', 'from-root'),
124+
// might need a 'titleside' and even 'titledirection' down the road
126125

127126
hoverformat: axesAttrs.hoverformat,
128127

src/plots/polar/layout_defaults.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ function handleDefaults(contIn, contOut, coerce, opts) {
104104
coerceAxis('side');
105105
coerceAxis('position', sector[0]);
106106

107+
coerceAxis('title');
108+
Lib.coerceFont(coerceAxis, 'titlefont', {
109+
family: opts.font.family,
110+
size: Math.round(opts.font.size * 1.2),
111+
color: dfltFontColor
112+
});
107113
}
108114
break;
109115

src/plots/polar/polar.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ var Axes = require('../cartesian/axes');
2121
var dragElement = require('../../components/dragelement');
2222
var dragBox = require('../cartesian/dragbox');
2323
var Fx = require('../../components/fx');
24+
var Titles = require('../../components/titles');
2425
var prepSelect = require('../cartesian/select');
2526
var setCursor = require('../../lib/setcursor');
2627

2728
var MID_SHIFT = require('../../constants/alignment').MID_SHIFT;
2829

30+
var _ = Lib._;
2931
var deg2rad = Lib.deg2rad;
3032
var rad2deg = Lib.rad2deg;
3133
var wrap360 = Lib.wrap360;
@@ -203,6 +205,7 @@ proto.updateLayout = function(fullLayout, polarLayout) {
203205
var cy = _this.cy = yOffset2 + radius * sectorBBox[3];
204206

205207
_this.updateRadialAxis(fullLayout, polarLayout);
208+
_this.updateRadialAxisTitle(fullLayout, polarLayout);
206209
_this.updateAngularAxis(fullLayout, polarLayout);
207210

208211
var radialRange = _this.radialAxis.range;
@@ -331,6 +334,42 @@ proto.updateRadialAxis = function(fullLayout, polarLayout) {
331334
.call(Color.stroke, radialLayout.linecolor);
332335
};
333336

337+
proto.updateRadialAxisTitle = function(fullLayout, polarLayout, _angle) {
338+
var _this = this;
339+
var gd = _this.gd;
340+
var radius = _this.radius;
341+
var cx = _this.cx;
342+
var cy = _this.cy;
343+
var radialLayout = polarLayout.radialaxis;
344+
var titleClass = _this.id + 'title';
345+
346+
var angle = _angle !== undefined ? _angle : radialLayout.position;
347+
var angleRad = deg2rad(angle);
348+
var cosa = Math.cos(angleRad);
349+
var sina = Math.sin(angleRad);
350+
351+
var pad = 0;
352+
if(radialLayout.title) {
353+
var h = Drawing.bBox(_this.layers['radial-axis'].node()).height;
354+
var ts = radialLayout.titlefont.size;
355+
pad = radialLayout.side === 'left' ?
356+
-h - ts * 0.4 :
357+
h + ts * 0.8;
358+
}
359+
360+
_this.layers['radial-axis-title'] = Titles.draw(gd, titleClass, {
361+
propContainer: radialLayout,
362+
propName: _this.id + '.radialaxis.title',
363+
placeholder: _(gd, 'Click to enter radial axis title'),
364+
attributes: {
365+
x: cx + (radius / 2) * cosa + pad * sina,
366+
y: cy - (radius / 2) * sina + pad * cosa,
367+
'text-anchor': 'middle'
368+
},
369+
transform: {rotate: -angle}
370+
});
371+
};
372+
334373
proto.updateAngularAxis = function(fullLayout, polarLayout) {
335374
var _this = this;
336375
var gd = _this.gd;

test/image/baselines/polar_blank.png

1.42 KB
Loading
-19.3 KB
Loading

test/image/mocks/polar_blank.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"mode": "markers+text",
1919
"r": [0],
2020
"theta": [0],
21-
"text": "<b>N.B.</b><br>auotrange<br>for (0,0)<br>gives [-1,1]",
21+
"text": "<b>N.B.</b><br>radial<br>auotrange<br>for (0,0)<br>gives [0,1]",
2222
"textposition": "left",
2323
"subplot": "polar4"
2424
}],
@@ -27,6 +27,10 @@
2727
"domain": {
2828
"x": [0, 0.46],
2929
"y": [0.56, 1]
30+
},
31+
"radialaxis": {
32+
"title": "blank",
33+
"position": 180
3034
}
3135
},
3236
"polar2": {
@@ -52,6 +56,10 @@
5256
"x": [0.54, 1],
5357
"y": [0.56, 1]
5458
},
59+
"radialaxis": {
60+
"title": "blank",
61+
"side": "left"
62+
},
5563
"angularaxis": {"visible": false}
5664
},
5765
"polar4": {

test/image/mocks/polar_subplots.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,30 @@
3434
"polar": {
3535
"domain": {
3636
"x": [0, 0.44]
37-
37+
},
38+
"radialaxis": {
39+
"showgrid": false,
40+
"title": "1st subplot",
41+
"titlefont": {
42+
"size": 20,
43+
"color": "blue"
44+
},
45+
"position": -45
3846
}
3947
},
4048
"polar2": {
4149
"domain": {
4250
"x": [0.56, 1]
51+
},
52+
"radialaxis": {
53+
"range": [0, 4.7],
54+
"showgrid": false,
55+
"title": "2nd subplot",
56+
"titlefont": {
57+
"size": 10,
58+
"color": "orange"
59+
},
60+
"position": 45
4361
}
4462
}
4563
}

test/jasmine/tests/polar_test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,61 @@ describe('Test relayout on polar subplots:', function() {
342342
.catch(fail)
343343
.then(done);
344344
});
345+
346+
it('should be able to restyle radial axis title', function(done) {
347+
var gd = createGraphDiv();
348+
var lastBBox;
349+
350+
function assertTitle(content, didBBoxChanged) {
351+
var radialAxisTitle = d3.select('g.g-polartitle');
352+
var txt = radialAxisTitle.select('text');
353+
var bb = radialAxisTitle.node().getBBox();
354+
var newBBox = [bb.x, bb.y, bb.width, bb.height];
355+
356+
if(content === '') {
357+
expect(txt.size()).toBe(0, 'cleared <text>');
358+
} else {
359+
expect(txt.text()).toBe(content, 'radial axis title');
360+
}
361+
362+
expect(newBBox).negateIf(didBBoxChanged).toEqual(lastBBox, 'did bbox change');
363+
lastBBox = newBBox;
364+
}
365+
366+
Plotly.plot(gd, [{
367+
type: 'scatterpolar',
368+
r: [1, 2, 3],
369+
theta: [10, 20, 30]
370+
}], {
371+
polar: {
372+
radialaxis: {title: 'yo'}
373+
}
374+
})
375+
.then(function() {
376+
assertTitle('yo', true);
377+
return Plotly.relayout(gd, 'polar.radialaxis.title', '');
378+
})
379+
.then(function() {
380+
assertTitle('', true);
381+
return Plotly.relayout(gd, 'polar.radialaxis.title', 'yo2');
382+
})
383+
.then(function() {
384+
assertTitle('yo2', true);
385+
return Plotly.relayout(gd, 'polar.radialaxis.ticklen', 20);
386+
})
387+
.then(function() {
388+
assertTitle('yo2', true);
389+
return Plotly.relayout(gd, 'polar.radialaxis.titlefont.color', 'red');
390+
})
391+
.then(function() {
392+
assertTitle('yo2', false);
393+
return Plotly.relayout(gd, 'title', 'dummy');
394+
})
395+
.then(function() {
396+
assertTitle('yo2', false);
397+
})
398+
.catch(fail)
399+
.then(done);
400+
});
401+
345402
});

0 commit comments

Comments
 (0)