forked from vega/vega
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vega.js
6006 lines (5175 loc) · 150 KB
/
vega.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
vg = (function(d3){ // take d3 instance as sole import
var vg = {};
// semantic versioning
vg.version = '1.3.0';
// type checking functions
var toString = Object.prototype.toString;
vg.isObject = function(obj) {
return obj === Object(obj);
};
vg.isFunction = function(obj) {
return toString.call(obj) == '[object Function]';
};
vg.isString = function(obj) {
return toString.call(obj) == '[object String]';
};
vg.isArray = Array.isArray || function(obj) {
return toString.call(obj) == '[object Array]';
};
vg.isNumber = function(obj) {
return toString.call(obj) == '[object Number]';
};
vg.isBoolean = function(obj) {
return toString.call(obj) == '[object Boolean]';
};
vg.number = function(s) { return +s; };
vg.boolean = function(s) { return !!s; };
// utility functions
vg.identity = function(x) { return x; };
vg.extend = function(obj) {
for (var x, name, i=1, len=arguments.length; i<len; ++i) {
x = arguments[i];
for (name in x) { obj[name] = x[name]; }
}
return obj;
};
vg.duplicate = function(obj) {
return JSON.parse(JSON.stringify(obj));
};
vg.field = function(f) {
return f.split("\\.")
.map(function(d) { return d.split("."); })
.reduce(function(a, b) {
if (a.length) { a[a.length-1] += "." + b.shift(); }
a.push.apply(a, b);
return a;
}, []);
};
vg.accessor = function(f) {
var s;
return (vg.isFunction(f) || f==null)
? f : vg.isString(f) && (s=vg.field(f)).length > 1
? function(x) { return s.reduce(function(x,f) { return x[f]; }, x); }
: function(x) { return x[f]; };
};
vg.comparator = function(sort) {
var sign = [];
if (sort === undefined) sort = [];
sort = vg.array(sort).map(function(f) {
var s = 1;
if (f[0] === "-") { s = -1; f = f.slice(1); }
else if (f[0] === "+") { s = +1; f = f.slice(1); }
sign.push(s);
return vg.accessor(f);
});
return function(a,b) {
var i, n, f, x, y;
for (i=0, n=sort.length; i<n; ++i) {
f = sort[i]; x = f(a); y = f(b);
if (x < y) return -1 * sign[i];
if (x > y) return sign[i];
}
return 0;
};
};
vg.cmp = function(a, b) { return a<b ? -1 : a>b ? 1 : 0; };
vg.numcmp = function(a, b) { return a - b; };
vg.array = function(x) {
return x != null ? (vg.isArray(x) ? x : [x]) : [];
};
vg.values = function(x) {
return (vg.isObject(x) && !vg.isArray(x) && x.values) ? x.values : x;
};
vg.str = function(x) {
return vg.isArray(x) ? "[" + str.map(x) + "]"
: vg.isObject(x) ? JSON.stringify(x)
: vg.isString(x) ? ("'"+x+"'") : x;
};
vg.keys = function(x) {
var keys = [];
for (var key in x) keys.push(key);
return keys;
};
vg.unique = function(data, f, results) {
if (!vg.isArray(data) || data.length==0) return [];
f = f || vg.identity;
results = results || [];
for (var v, i=0, n=data.length; i<n; ++i) {
v = f(data[i]);
if (results.indexOf(v) < 0) results.push(v);
}
return results;
};
vg.minIndex = function(data, f) {
if (!vg.isArray(data) || data.length==0) return -1;
f = f || vg.identity;
var idx = 0, min = f(data[0]), v = min;
for (var i=1, n=data.length; i<n; ++i) {
v = f(data[i]);
if (v < min) { min = v; idx = i; }
}
return idx;
};
vg.maxIndex = function(data, f) {
if (!vg.isArray(data) || data.length==0) return -1;
f = f || vg.identity;
var idx = 0, max = f(data[0]), v = max;
for (var i=1, n=data.length; i<n; ++i) {
v = f(data[i]);
if (v > max) { max = v; idx = i; }
}
return idx;
};
vg.truncate = function(s, length, pos, word, ellipsis) {
var len = s.length;
if (len <= length) return s;
ellipsis = ellipsis || "...";
var l = Math.max(0, length - ellipsis.length);
switch (pos) {
case "left":
return ellipsis + (word ? vg_truncateOnWord(s,l,1) : s.slice(len-l));
case "middle":
case "center":
var l1 = Math.ceil(l/2), l2 = Math.floor(l/2);
return (word ? vg_truncateOnWord(s,l1) : s.slice(0,l1)) + ellipsis
+ (word ? vg_truncateOnWord(s,l2,1) : s.slice(len-l2));
default:
return (word ? vg_truncateOnWord(s,l) : s.slice(0,l)) + ellipsis;
}
}
function vg_truncateOnWord(s, len, rev) {
var cnt = 0, tok = s.split(vg_truncate_word_re);
if (rev) {
s = (tok = tok.reverse())
.filter(function(w) { cnt += w.length; return cnt <= len; })
.reverse();
} else {
s = tok.filter(function(w) { cnt += w.length; return cnt <= len; });
}
return s.length ? s.join("").trim() : tok[0].slice(0, len);
}
var vg_truncate_word_re = /([\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u2028\u2029\u3000\uFEFF])/;
// Logging
function vg_write(msg) {
vg.config.isNode
? process.stderr.write(msg + "\n")
: console.log(msg);
}
vg.log = function(msg) {
vg_write("[Vega Log] " + msg);
};
vg.error = function(msg) {
msg = "[Vega Err] " + msg;
vg_write(msg);
if (typeof alert !== "undefined") alert(msg);
};vg.config = {};
// are we running in node.js?
// via timetler.com/2012/10/13/environment-detection-in-javascript/
vg.config.isNode = typeof exports !== 'undefined' && this.exports !== exports;
// base url for loading external data files
// used only for server-side operation
vg.config.baseURL = "";
// version and namepsaces for exported svg
vg.config.svgNamespace =
'version="1.1" xmlns="http://www.w3.org/2000/svg" ' +
'xmlns:xlink="http://www.w3.org/1999/xlink"';
// default axis properties
vg.config.axis = {
orient: "bottom",
ticks: 10,
padding: 3,
axisColor: "#000",
gridColor: "#d8d8d8",
tickColor: "#000",
tickLabelColor: "#000",
axisWidth: 1,
tickWidth: 1,
tickSize: 6,
tickLabelFontSize: 11,
tickLabelFont: "sans-serif",
titleColor: "#000",
titleFont: "sans-serif",
titleFontSize: 11,
titleFontWeight: "bold",
titleOffset: 35
};
// default legend properties
vg.config.legend = {
orient: "right",
offset: 10,
padding: 3,
gradientStrokeColor: "#888",
gradientStrokeWidth: 1,
gradientHeight: 16,
gradientWidth: 100,
labelColor: "#000",
labelFontSize: 10,
labelFont: "sans-serif",
labelAlign: "left",
labelBaseline: "middle",
labelOffset: 8,
symbolShape: "circle",
symbolSize: 50,
symbolColor: "#888",
symbolStrokeWidth: 1,
titleColor: "#000",
titleFont: "sans-serif",
titleFontSize: 11,
titleFontWeight: "bold"
};
// default color values
vg.config.color = {
rgb: [128, 128, 128],
lab: [50, 0, 0],
hcl: [0, 0, 50],
hsl: [0, 0, 0.5]
};
// default scale ranges
vg.config.range = {
category10: [
"#1f77b4",
"#ff7f0e",
"#2ca02c",
"#d62728",
"#9467bd",
"#8c564b",
"#e377c2",
"#7f7f7f",
"#bcbd22",
"#17becf"
],
category20: [
"#1f77b4",
"#aec7e8",
"#ff7f0e",
"#ffbb78",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5",
"#8c564b",
"#c49c94",
"#e377c2",
"#f7b6d2",
"#7f7f7f",
"#c7c7c7",
"#bcbd22",
"#dbdb8d",
"#17becf",
"#9edae5"
],
shapes: [
"circle",
"cross",
"diamond",
"square",
"triangle-down",
"triangle-up"
]
};vg.Bounds = (function() {
var bounds = function(b) {
this.clear();
if (b) this.union(b);
};
var prototype = bounds.prototype;
prototype.clear = function() {
this.x1 = +Number.MAX_VALUE;
this.y1 = +Number.MAX_VALUE;
this.x2 = -Number.MAX_VALUE;
this.y2 = -Number.MAX_VALUE;
return this;
};
prototype.set = function(x1, y1, x2, y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
return this;
};
prototype.add = function(x, y) {
if (x < this.x1) this.x1 = x;
if (y < this.y1) this.y1 = y;
if (x > this.x2) this.x2 = x;
if (y > this.y2) this.y2 = y;
return this;
};
prototype.expand = function(d) {
this.x1 -= d;
this.y1 -= d;
this.x2 += d;
this.y2 += d;
return this;
};
prototype.round = function() {
this.x1 = Math.floor(this.x1);
this.y1 = Math.floor(this.y1);
this.x2 = Math.ceil(this.x2);
this.y2 = Math.ceil(this.y2);
return this;
};
prototype.translate = function(dx, dy) {
this.x1 += dx;
this.x2 += dx;
this.y1 += dy;
this.y2 += dy;
return this;
};
prototype.rotate = function(angle, x, y) {
var cos = Math.cos(angle),
sin = Math.sin(angle),
cx = x - x*cos + y*sin,
cy = y - x*sin - y*cos,
x1 = this.x1, x2 = this.x2,
y1 = this.y1, y2 = this.y2;
return this.clear()
.add(cos*x1 - sin*y1 + cx, sin*x1 + cos*y1 + cy)
.add(cos*x1 - sin*y2 + cx, sin*x1 + cos*y2 + cy)
.add(cos*x2 - sin*y1 + cx, sin*x2 + cos*y1 + cy)
.add(cos*x2 - sin*y2 + cx, sin*x2 + cos*y2 + cy);
}
prototype.union = function(b) {
if (b.x1 < this.x1) this.x1 = b.x1;
if (b.y1 < this.y1) this.y1 = b.y1;
if (b.x2 > this.x2) this.x2 = b.x2;
if (b.y2 > this.y2) this.y2 = b.y2;
return this;
};
prototype.encloses = function(b) {
return b && (
this.x1 <= b.x1 &&
this.x2 >= b.x2 &&
this.y1 <= b.y1 &&
this.y2 >= b.y2
);
};
prototype.intersects = function(b) {
return b && !(
this.x2 < b.x1 ||
this.x1 > b.x2 ||
this.y2 < b.y1 ||
this.y1 > b.y2
);
};
prototype.contains = function(x, y) {
return !(
x < this.x1 ||
x > this.x2 ||
y < this.y1 ||
y > this.y2
);
};
prototype.width = function() {
return this.x2 - this.x1;
};
prototype.height = function() {
return this.y2 - this.y1;
};
return bounds;
})();vg.Gradient = (function() {
function gradient(type) {
this.id = "grad_" + (vg_gradient_id++);
this.type = type || "linear";
this.stops = [];
this.x1 = 0;
this.x2 = 1;
this.y1 = 0;
this.y2 = 0;
};
var prototype = gradient.prototype;
prototype.stop = function(offset, color) {
this.stops.push({
offset: offset,
color: color
});
return this;
};
return gradient;
})();
var vg_gradient_id = 0;vg.canvas = {};vg.canvas.path = (function() {
// Path parsing and rendering code taken from fabric.js -- Thanks!
var cmdLength = { m:2, l:2, h:1, v:1, c:6, s:4, q:4, t:2, a:7 },
re = [/([MLHVCSQTAZmlhvcsqtaz])/g, /###/, /(\d)-/g, /\s|,|###/];
function parse(path) {
var result = [],
currentPath,
chunks,
parsed;
// First, break path into command sequence
path = path.slice().replace(re[0], '###$1').split(re[1]).slice(1);
// Next, parse each command in turn
for (var i=0, j, chunksParsed, len=path.length; i<len; i++) {
currentPath = path[i];
chunks = currentPath.slice(1).trim().replace(re[2],'$1###-').split(re[3]);
chunksParsed = [currentPath.charAt(0)];
for (var j = 0, jlen = chunks.length; j < jlen; j++) {
parsed = parseFloat(chunks[j]);
if (!isNaN(parsed)) {
chunksParsed.push(parsed);
}
}
var command = chunksParsed[0].toLowerCase(),
commandLength = cmdLength[command];
if (chunksParsed.length - 1 > commandLength) {
for (var k = 1, klen = chunksParsed.length; k < klen; k += commandLength) {
result.push([ chunksParsed[0] ].concat(chunksParsed.slice(k, k + commandLength)));
}
}
else {
result.push(chunksParsed);
}
}
return result;
}
function drawArc(g, x, y, coords, bounds, l, t) {
var rx = coords[0];
var ry = coords[1];
var rot = coords[2];
var large = coords[3];
var sweep = coords[4];
var ex = coords[5];
var ey = coords[6];
var segs = arcToSegments(ex, ey, rx, ry, large, sweep, rot, x, y);
for (var i=0; i<segs.length; i++) {
var bez = segmentToBezier.apply(null, segs[i]);
g.bezierCurveTo.apply(g, bez);
bounds.add(bez[0]-l, bez[1]-t);
bounds.add(bez[2]-l, bez[3]-t);
bounds.add(bez[4]-l, bez[5]-t);
}
}
var arcToSegmentsCache = { },
segmentToBezierCache = { },
join = Array.prototype.join,
argsStr;
// Copied from Inkscape svgtopdf, thanks!
function arcToSegments(x, y, rx, ry, large, sweep, rotateX, ox, oy) {
argsStr = join.call(arguments);
if (arcToSegmentsCache[argsStr]) {
return arcToSegmentsCache[argsStr];
}
var th = rotateX * (Math.PI/180);
var sin_th = Math.sin(th);
var cos_th = Math.cos(th);
rx = Math.abs(rx);
ry = Math.abs(ry);
var px = cos_th * (ox - x) * 0.5 + sin_th * (oy - y) * 0.5;
var py = cos_th * (oy - y) * 0.5 - sin_th * (ox - x) * 0.5;
var pl = (px*px) / (rx*rx) + (py*py) / (ry*ry);
if (pl > 1) {
pl = Math.sqrt(pl);
rx *= pl;
ry *= pl;
}
var a00 = cos_th / rx;
var a01 = sin_th / rx;
var a10 = (-sin_th) / ry;
var a11 = (cos_th) / ry;
var x0 = a00 * ox + a01 * oy;
var y0 = a10 * ox + a11 * oy;
var x1 = a00 * x + a01 * y;
var y1 = a10 * x + a11 * y;
var d = (x1-x0) * (x1-x0) + (y1-y0) * (y1-y0);
var sfactor_sq = 1 / d - 0.25;
if (sfactor_sq < 0) sfactor_sq = 0;
var sfactor = Math.sqrt(sfactor_sq);
if (sweep == large) sfactor = -sfactor;
var xc = 0.5 * (x0 + x1) - sfactor * (y1-y0);
var yc = 0.5 * (y0 + y1) + sfactor * (x1-x0);
var th0 = Math.atan2(y0-yc, x0-xc);
var th1 = Math.atan2(y1-yc, x1-xc);
var th_arc = th1-th0;
if (th_arc < 0 && sweep == 1){
th_arc += 2*Math.PI;
} else if (th_arc > 0 && sweep == 0) {
th_arc -= 2 * Math.PI;
}
var segments = Math.ceil(Math.abs(th_arc / (Math.PI * 0.5 + 0.001)));
var result = [];
for (var i=0; i<segments; i++) {
var th2 = th0 + i * th_arc / segments;
var th3 = th0 + (i+1) * th_arc / segments;
result[i] = [xc, yc, th2, th3, rx, ry, sin_th, cos_th];
}
return (arcToSegmentsCache[argsStr] = result);
}
function segmentToBezier(cx, cy, th0, th1, rx, ry, sin_th, cos_th) {
argsStr = join.call(arguments);
if (segmentToBezierCache[argsStr]) {
return segmentToBezierCache[argsStr];
}
var a00 = cos_th * rx;
var a01 = -sin_th * ry;
var a10 = sin_th * rx;
var a11 = cos_th * ry;
var cos_th0 = Math.cos(th0);
var sin_th0 = Math.sin(th0);
var cos_th1 = Math.cos(th1);
var sin_th1 = Math.sin(th1);
var th_half = 0.5 * (th1 - th0);
var sin_th_h2 = Math.sin(th_half * 0.5);
var t = (8/3) * sin_th_h2 * sin_th_h2 / Math.sin(th_half);
var x1 = cx + cos_th0 - t * sin_th0;
var y1 = cy + sin_th0 + t * cos_th0;
var x3 = cx + cos_th1;
var y3 = cy + sin_th1;
var x2 = x3 + t * sin_th1;
var y2 = y3 - t * cos_th1;
return (segmentToBezierCache[argsStr] = [
a00 * x1 + a01 * y1, a10 * x1 + a11 * y1,
a00 * x2 + a01 * y2, a10 * x2 + a11 * y2,
a00 * x3 + a01 * y3, a10 * x3 + a11 * y3
]);
}
function render(g, path, l, t) {
var current, // current instruction
previous = null,
x = 0, // current x
y = 0, // current y
controlX = 0, // current control point x
controlY = 0, // current control point y
tempX,
tempY,
tempControlX,
tempControlY,
bounds = new vg.Bounds();
if (l == undefined) l = 0;
if (t == undefined) t = 0;
g.beginPath();
for (var i=0, len=path.length; i<len; ++i) {
current = path[i];
switch (current[0]) { // first letter
case 'l': // lineto, relative
x += current[1];
y += current[2];
g.lineTo(x + l, y + t);
bounds.add(x, y);
break;
case 'L': // lineto, absolute
x = current[1];
y = current[2];
g.lineTo(x + l, y + t);
bounds.add(x, y);
break;
case 'h': // horizontal lineto, relative
x += current[1];
g.lineTo(x + l, y + t);
bounds.add(x, y);
break;
case 'H': // horizontal lineto, absolute
x = current[1];
g.lineTo(x + l, y + t);
bounds.add(x, y);
break;
case 'v': // vertical lineto, relative
y += current[1];
g.lineTo(x + l, y + t);
bounds.add(x, y);
break;
case 'V': // verical lineto, absolute
y = current[1];
g.lineTo(x + l, y + t);
bounds.add(x, y);
break;
case 'm': // moveTo, relative
x += current[1];
y += current[2];
g.moveTo(x + l, y + t);
bounds.add(x, y);
break;
case 'M': // moveTo, absolute
x = current[1];
y = current[2];
g.moveTo(x + l, y + t);
bounds.add(x, y);
break;
case 'c': // bezierCurveTo, relative
tempX = x + current[5];
tempY = y + current[6];
controlX = x + current[3];
controlY = y + current[4];
g.bezierCurveTo(
x + current[1] + l, // x1
y + current[2] + t, // y1
controlX + l, // x2
controlY + t, // y2
tempX + l,
tempY + t
);
bounds.add(x + current[1], y + current[2]);
bounds.add(controlX, controlY);
bounds.add(tempX, tempY);
x = tempX;
y = tempY;
break;
case 'C': // bezierCurveTo, absolute
x = current[5];
y = current[6];
controlX = current[3];
controlY = current[4];
g.bezierCurveTo(
current[1] + l,
current[2] + t,
controlX + l,
controlY + t,
x + l,
y + t
);
bounds.add(current[1], current[2]);
bounds.add(controlX, controlY);
bounds.add(x, y);
break;
case 's': // shorthand cubic bezierCurveTo, relative
// transform to absolute x,y
tempX = x + current[3];
tempY = y + current[4];
// calculate reflection of previous control points
controlX = 2 * x - controlX;
controlY = 2 * y - controlY;
g.bezierCurveTo(
controlX + l,
controlY + t,
x + current[1] + l,
y + current[2] + t,
tempX + l,
tempY + t
);
bounds.add(controlX, controlY);
bounds.add(x + current[1], y + current[2]);
bounds.add(tempX, tempY);
// set control point to 2nd one of this command
// "... the first control point is assumed to be the reflection of the second control point on the previous command relative to the current point."
controlX = x + current[1];
controlY = y + current[2];
x = tempX;
y = tempY;
break;
case 'S': // shorthand cubic bezierCurveTo, absolute
tempX = current[3];
tempY = current[4];
// calculate reflection of previous control points
controlX = 2*x - controlX;
controlY = 2*y - controlY;
g.bezierCurveTo(
controlX + l,
controlY + t,
current[1] + l,
current[2] + t,
tempX + l,
tempY + t
);
x = tempX;
y = tempY;
bounds.add(current[1], current[2]);
bounds.add(controlX, controlY);
bounds.add(tempX, tempY);
// set control point to 2nd one of this command
// "... the first control point is assumed to be the reflection of the second control point on the previous command relative to the current point."
controlX = current[1];
controlY = current[2];
break;
case 'q': // quadraticCurveTo, relative
// transform to absolute x,y
tempX = x + current[3];
tempY = y + current[4];
controlX = x + current[1];
controlY = y + current[2];
g.quadraticCurveTo(
controlX + l,
controlY + t,
tempX + l,
tempY + t
);
x = tempX;
y = tempY;
bounds.add(controlX, controlY);
bounds.add(tempX, tempY);
break;
case 'Q': // quadraticCurveTo, absolute
tempX = current[3];
tempY = current[4];
g.quadraticCurveTo(
current[1] + l,
current[2] + t,
tempX + l,
tempY + t
);
x = tempX;
y = tempY;
controlX = current[1];
controlY = current[2];
bounds.add(controlX, controlY);
bounds.add(tempX, tempY);
break;
case 't': // shorthand quadraticCurveTo, relative
// transform to absolute x,y
tempX = x + current[1];
tempY = y + current[2];
if (previous[0].match(/[QqTt]/) === null) {
// If there is no previous command or if the previous command was not a Q, q, T or t,
// assume the control point is coincident with the current point
controlX = x;
controlY = y;
}
else if (previous[0] === 't') {
// calculate reflection of previous control points for t
controlX = 2 * x - tempControlX;
controlY = 2 * y - tempControlY;
}
else if (previous[0] === 'q') {
// calculate reflection of previous control points for q
controlX = 2 * x - controlX;
controlY = 2 * y - controlY;
}
tempControlX = controlX;
tempControlY = controlY;
g.quadraticCurveTo(
controlX + l,
controlY + t,
tempX + l,
tempY + t
);
x = tempX;
y = tempY;
controlX = x + current[1];
controlY = y + current[2];
bounds.add(controlX, controlY);
bounds.add(tempX, tempY);
break;
case 'T':
tempX = current[1];
tempY = current[2];
// calculate reflection of previous control points
controlX = 2 * x - controlX;
controlY = 2 * y - controlY;
g.quadraticCurveTo(
controlX + l,
controlY + t,
tempX + l,
tempY + t
);
x = tempX;
y = tempY;
bounds.add(controlX, controlY);
bounds.add(tempX, tempY);
break;
case 'a':
drawArc(g, x + l, y + t, [
current[1],
current[2],
current[3],
current[4],
current[5],
current[6] + x + l,
current[7] + y + t
], bounds, l, t);
x += current[6];
y += current[7];
break;
case 'A':
drawArc(g, x + l, y + t, [
current[1],
current[2],
current[3],
current[4],
current[5],
current[6] + l,
current[7] + t
], bounds, l, t);
x = current[6];
y = current[7];
break;
case 'z':
case 'Z':
g.closePath();
break;
}
previous = current;
}
return bounds.translate(l, t);
};
return {
parse: parse,
render: render
};
})();vg.canvas.marks = (function() {
var parsePath = vg.canvas.path.parse,
renderPath = vg.canvas.path.render,
halfpi = Math.PI / 2,
sqrt3 = Math.sqrt(3),
tan30 = Math.tan(30 * Math.PI / 180),
tmpBounds = new vg.Bounds();
// path generators
function arcPath(g, o) {
var x = o.x || 0,
y = o.y || 0,
ir = o.innerRadius || 0,
or = o.outerRadius || 0,
sa = (o.startAngle || 0) - Math.PI/2,
ea = (o.endAngle || 0) - Math.PI/2;
g.beginPath();
if (ir === 0) g.moveTo(x, y);
else g.arc(x, y, ir, sa, ea, 0);
g.arc(x, y, or, ea, sa, 1);
g.closePath();
return arcBounds(sa, ea, ir, or, x, y);
}
function arcBounds(sa, ea, ir, or, cx, cy) {
var a, i, n, x, y, ix, iy, ox, oy,
xmin = Infinity, xmax = -Infinity,
ymin = Infinity, ymax = -Infinity;
var angles = [sa, ea],
s = sa - (sa%halfpi) + halfpi;
for (var i=0; i<4 && s<ea; ++i, s+=halfpi) {
angles.push(s);
}
for (i=0, n=angles.length; i<n; ++i) {
a = angles[i];
x = Math.cos(a); ix = ir*x; ox = or*x;
y = Math.sin(a); iy = ir*y; oy = or*y;
xmin = Math.min(xmin, ix, ox);
xmax = Math.max(xmax, ix, ox);
ymin = Math.min(ymin, iy, oy);
ymax = Math.max(ymax, iy, oy);
}
return new vg.Bounds()
.set(cx+xmin, cy+ymin, cx+xmax, cy+ymax);
}
function pathPath(g, o) {
return renderPath(g, parsePath(o.path), o.x, o.y);
}
function symbolPath(g, o) {
g.beginPath();
var size = o.size != undefined ? o.size : 100,
x = o.x, y = o.y, r, t, rx, ry,
bounds = new vg.Bounds();
if (o.shape == undefined || o.shape === "circle") {
r = Math.sqrt(size/Math.PI);
g.arc(x, y, r, 0, 2*Math.PI, 0);
g.closePath();
return bounds.set(x-r, y-r, x+r, y+r);
}
switch (o.shape) {
case "cross":
r = Math.sqrt(size / 5) / 2;
t = 3*r;
g.moveTo(x-t, y-r);
g.lineTo(x-r, y-r);
g.lineTo(x-r, y-t);
g.lineTo(x+r, y-t);
g.lineTo(x+r, y-r);
g.lineTo(x+t, y-r);
g.lineTo(x+t, y+r);
g.lineTo(x+r, y+r);
g.lineTo(x+r, y+t);
g.lineTo(x-r, y+t);
g.lineTo(x-r, y+r);
g.lineTo(x-t, y+r);