Debian Codegraph @

bqplot/js/src/Markers.ts

raw
1 /* Copyright 2015 Bloomberg Finance L.P.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 import * as d3 from 'd3';
17 // var d3 =Object.assign({}, require("d3-collection"));
18
19 const pi = Math.PI,
20 radian = pi / 180,
21 sqrt3 = Math.sqrt(3),
22 tan30 = Math.tan(pi / 6);
23
24 const circleSymbol = function (size) {
25 const r = Math.sqrt(size / pi);
26 return (
27 'M0,' +
28 r +
29 'A' +
30 r +
31 ',' +
32 r +
33 ' 0 1,1 0,' +
34 -r +
35 'A' +
36 r +
37 ',' +
38 r +
39 ' 0 1,1 0,' +
40 r +
41 'Z'
42 );
43 };
44
45 const bqSymbolTypes = d3.map({
46 circle: circleSymbol,
47 cross: function (size, e) {
48 const r = Math.sqrt(size / 5) / 2;
49 return (
50 'M' +
51 -3 * r +
52 ',' +
53 -r +
54 'H' +
55 -r +
56 'V' +
57 -3 * r +
58 'H' +
59 r +
60 'V' +
61 -r +
62 'H' +
63 3 * r +
64 'V' +
65 r +
66 'H' +
67 r +
68 'V' +
69 3 * r +
70 'H' +
71 -r +
72 'V' +
73 r +
74 'H' +
75 -3 * r +
76 'Z'
77 );
78 },
79 diamond: function (size, s) {
80 const ry = Math.sqrt(size / (2 * tan30)),
81 rx = ry * tan30;
82 return 'M0,' + -ry + 'L' + rx + ',0' + ' 0,' + ry + ' ' + -rx + ',0' + 'Z';
83 },
84 square: function (size, s) {
85 const r = Math.sqrt(size) / 2;
86 return (
87 'M' +
88 -r +
89 ',' +
90 -r +
91 'L' +
92 r +
93 ',' +
94 -r +
95 ' ' +
96 r +
97 ',' +
98 r +
99 ' ' +
100 -r +
101 ',' +
102 r +
103 'Z'
104 );
105 },
106 'triangle-down': function (size, s) {
107 const rx = Math.sqrt(size / sqrt3),
108 ry = (rx * sqrt3) / 2;
109 return 'M0,' + ry + 'L' + rx + ',' + -ry + ' ' + -rx + ',' + -ry + 'Z';
110 },
111 'triangle-up': function (size, s) {
112 const rx = Math.sqrt(size / sqrt3),
113 ry = (rx * sqrt3) / 2;
114 return 'M0,' + -ry + 'L' + rx + ',' + ry + ' ' + -rx + ',' + ry + 'Z';
115 },
116 arrow: function (size, skew) {
117 const angle = 60 + (5 - 60) * skew,
118 s = Math.tan(angle * radian),
119 ry = Math.sqrt(size / s),
120 rx = (ry * s) / 2;
121 return 'M0,' + -ry + 'L' + rx + ',' + ry + ' ' + -rx + ',' + ry + 'Z';
122 },
123 ellipse: function (size, skew) {
124 const s = Math.pow(10, skew),
125 rx = Math.sqrt(size / (pi * s)),
126 ry = rx * s;
127 return (
128 'M0,' +
129 ry +
130 'A' +
131 rx +
132 ',' +
133 ry +
134 ' 0 1,1 0,' +
135 -ry +
136 'A' +
137 rx +
138 ',' +
139 ry +
140 ' 0 1,1 0,' +
141 ry +
142 'Z'
143 );
144 },
145 rectangle: function (size, skew) {
146 const s = Math.pow(10, skew),
147 rx = Math.sqrt(size / s) / 2,
148 ry = rx * s;
149 return (
150 'M' +
151 -rx +
152 ',' +
153 -ry +
154 'L' +
155 rx +
156 ',' +
157 -ry +
158 ' ' +
159 rx +
160 ',' +
161 ry +
162 ' ' +
163 -rx +
164 ',' +
165 ry +
166 'Z'
167 );
168 },
169 plus: function (size, skew) {
170 // Use the same radius as square so that this mark is the same size as
171 // the square.
172 const r = Math.sqrt(size) / 2;
173 return (
174 'M0,' +
175 -r +
176 'L' +
177 '0' +
178 ',' +
179 r +
180 'M' +
181 -r +
182 ',' +
183 '0' +
184 'L' +
185 r +
186 ',' +
187 '0'
188 );
189 },
190 crosshair: function (size, skew) {
191 // Use a radius so that this mark is the same size as
192 // the square in the sense that it fits just inside a square
193 // of the same size
194 const r = Math.sqrt(size) / 2;
195 return (
196 'M' +
197 -r +
198 ',' +
199 -r +
200 'L' +
201 r +
202 ',' +
203 r +
204 'M' +
205 -r +
206 ',' +
207 r +
208 'L' +
209 r +
210 ',' +
211 -r
212 );
213 },
214 point: function (size) {
215 const pointFactor = 10;
216 const r = Math.sqrt(size / pi / pointFactor);
217 return (
218 'M0,' +
219 r +
220 'A' +
221 r +
222 ',' +
223 r +
224 ' 0 1,1 0,' +
225 -r +
226 'A' +
227 r +
228 ',' +
229 r +
230 ' 0 1,1 0,' +
231 r +
232 'Z'
233 );
234 },
235 });
236
237 function symbolSize() {
238 return 64;
239 }
240
241 function symbolType() {
242 return 'circle';
243 }
244
245 function symbolSkew() {
246 return 0.5;
247 }
248
249 function constant(x) {
250 return function () {
251 return x;
252 };
253 }
254
255 function functor(x) {
256 return typeof x === 'function' ? x : constant(x);
257 }
258
259 const bqSymbol = function () {
260 let type = symbolType;
261 let size = symbolSize;
262 let skew = symbolSkew;
263
264 function symbol(d, i) {
265 return (bqSymbolTypes.get(type.call(this, d, i)) || circleSymbol)(
266 size.call(this, d, i),
267 skew.call(this, d, i)
268 );
269 }
270
271 symbol.type = function (x) {
272 if (!arguments.length) {
273 return type;
274 }
275 type = functor(x);
276 return symbol;
277 };
278
279 // size of symbol in square pixels
280 symbol.size = function (x) {
281 if (!arguments.length) {
282 return size;
283 }
284 size = functor(x);
285 return symbol;
286 };
287
288 // skew of symbol, in [0, 1]
289 symbol.skew = function (x) {
290 if (!arguments.length) {
291 return skew;
292 }
293 skew = functor(x);
294 return symbol;
295 };
296
297 return symbol;
298 };
299
300 export const types = bqSymbolTypes.keys();
301 export { bqSymbol as symbol };