forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_convert.js
193 lines (159 loc) · 5.87 KB
/
set_convert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Copyright 2012-2020, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var setConvertCartesian = require('../cartesian/set_convert');
var deg2rad = Lib.deg2rad;
var rad2deg = Lib.rad2deg;
/**
* setConvert for polar axes!
*
* @param {object} ax
* axis in question (works for both radial and angular axes)
* @param {object} polarLayout
* full polar layout of the subplot associated with 'ax'
* @param {object} fullLayout
* full layout
*
* Here, reuse some of the Cartesian setConvert logic,
* but we must extend some of it, as both radial and angular axes
* don't have domains and angular axes don't have _true_ ranges.
*
* Moreover, we introduce two new coordinate systems:
* - 'g' for geometric coordinates and
* - 't' for angular ticks
*
* Radial axis coordinate systems:
* - d, c and l: same as for cartesian axes
* - g: like calcdata but translated about `radialaxis.range[0]` & `polar.hole`
*
* Angular axis coordinate systems:
* - d: data, in whatever form it's provided
* - c: calcdata, turned into radians (for linear axes)
* or category indices (category axes)
* - t: tick calcdata, just like 'c' but in degrees for linear axes
* - g: geometric calcdata, radians coordinates that take into account
* axis rotation and direction
*
* Then, 'g'eometric data is ready to be converted to (x,y).
*/
module.exports = function setConvert(ax, polarLayout, fullLayout) {
setConvertCartesian(ax, fullLayout);
switch(ax._id) {
case 'x':
case 'radialaxis':
setConvertRadial(ax, polarLayout);
break;
case 'angularaxis':
setConvertAngular(ax, polarLayout);
break;
}
};
function setConvertRadial(ax, polarLayout) {
var subplot = polarLayout._subplot;
ax.setGeometry = function() {
var rl0 = ax._rl[0];
var rl1 = ax._rl[1];
var b = subplot.innerRadius;
var m = (subplot.radius - b) / (rl1 - rl0);
var b2 = b / m;
var rFilter = rl0 > rl1 ?
function(v) { return v <= 0; } :
function(v) { return v >= 0; };
ax.c2g = function(v) {
var r = ax.c2l(v) - rl0;
return (rFilter(r) ? r : 0) + b2;
};
ax.g2c = function(v) {
return ax.l2c(v + rl0 - b2);
};
ax.g2p = function(v) { return v * m; };
ax.c2p = function(v) { return ax.g2p(ax.c2g(v)); };
};
}
function toRadians(v, unit) {
return unit === 'degrees' ? deg2rad(v) : v;
}
function fromRadians(v, unit) {
return unit === 'degrees' ? rad2deg(v) : v;
}
function setConvertAngular(ax, polarLayout) {
var axType = ax.type;
if(axType === 'linear') {
var _d2c = ax.d2c;
var _c2d = ax.c2d;
ax.d2c = function(v, unit) { return toRadians(_d2c(v), unit); };
ax.c2d = function(v, unit) { return _c2d(fromRadians(v, unit)); };
}
// override makeCalcdata to handle thetaunit and special theta0/dtheta logic
ax.makeCalcdata = function(trace, coord) {
var arrayIn = trace[coord];
var len = trace._length;
var arrayOut, i;
var _d2c = function(v) { return ax.d2c(v, trace.thetaunit); };
if(arrayIn) {
if(Lib.isTypedArray(arrayIn) && axType === 'linear') {
if(len === arrayIn.length) {
return arrayIn;
} else if(arrayIn.subarray) {
return arrayIn.subarray(0, len);
}
}
arrayOut = new Array(len);
for(i = 0; i < len; i++) {
arrayOut[i] = _d2c(arrayIn[i]);
}
} else {
var coord0 = coord + '0';
var dcoord = 'd' + coord;
var v0 = (coord0 in trace) ? _d2c(trace[coord0]) : 0;
var dv = (trace[dcoord]) ? _d2c(trace[dcoord]) : (ax.period || 2 * Math.PI) / len;
arrayOut = new Array(len);
for(i = 0; i < len; i++) {
arrayOut[i] = v0 + i * dv;
}
}
return arrayOut;
};
// N.B. we mock the axis 'range' here
ax.setGeometry = function() {
var sector = polarLayout.sector;
var sectorInRad = sector.map(deg2rad);
var dir = {clockwise: -1, counterclockwise: 1}[ax.direction];
var rot = deg2rad(ax.rotation);
var rad2g = function(v) { return dir * v + rot; };
var g2rad = function(v) { return (v - rot) / dir; };
var rad2c, c2rad;
var rad2t, t2rad;
switch(axType) {
case 'linear':
c2rad = rad2c = Lib.identity;
t2rad = deg2rad;
rad2t = rad2deg;
// Set the angular range in degrees to make auto-tick computation cleaner,
// changing rotation/direction should not affect the angular tick value.
ax.range = Lib.isFullCircle(sectorInRad) ?
[sector[0], sector[0] + 360] :
sectorInRad.map(g2rad).map(rad2deg);
break;
case 'category':
var catLen = ax._categories.length;
var _period = ax.period ? Math.max(ax.period, catLen) : catLen;
// fallback in case all categories have been filtered out
if(_period === 0) _period = 1;
c2rad = t2rad = function(v) { return v * 2 * Math.PI / _period; };
rad2c = rad2t = function(v) { return v * _period / Math.PI / 2; };
ax.range = [0, _period];
break;
}
ax.c2g = function(v) { return rad2g(c2rad(v)); };
ax.g2c = function(v) { return rad2c(g2rad(v)); };
ax.t2g = function(v) { return rad2g(t2rad(v)); };
ax.g2t = function(v) { return rad2t(g2rad(v)); };
};
}