-
Notifications
You must be signed in to change notification settings - Fork 0
/
d2v.js
2110 lines (1828 loc) · 54.6 KB
/
d2v.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
/*!
* d2v - Data Driven Visualisations JavaScript Library v0.1.1
*
*/
dv2 = {version: "0.1.0"};
function Cell() {
this._data = null;
this._dims = {};
this._col = null; //parent CellCollection
}
Cell.prototype.data = function(data){
if (!arguments.length) return this._data;
this._data = data
return this;
};
Cell.prototype.dim = function(dims){
if (!arguments.length) return this._dims;
this._dims = dims
if(this._col){
this._col.dims_changed(this,dims);
}
return this;
};
Cell.prototype.setDim = function(dim,value){
this._dims[dim] = value;
if(this._col){
var a = {};
a[dim] = value;
this._col.dims_changed(this, a);
}
return this;
};
Cell.prototype.getDim = function(keys){
if( ! (keys instanceof Array)) keys = [keys];
var value = null;
try{
if(!keys[1]){
value = this._dims[keys[0]];
}else{
value = this._dims[keys[0]][keys[1]];
}
}catch(e){
}
return value;
};
Cell.prototype.col = function(col){
if (!arguments.length) return this._col;
this._col = col
return this;
};
function CellCollection(dataarray) {
this.cells = [];
this.dimsrange = {};
if(dataarray) this.data(dataarray);
}
/**
* Adds a Cell to the CellCollection
* @param {Cell} cell The Cell to add
* @returns {CellCollection}
*/
CellCollection.prototype.addCell = function(cell){
this.cells.push(cell);
cell.col(this);
return this;
};
/**
* Creates all cells for this collection from a data Array or another CellCollection
* @param {Array or CellCollection} dataarray The data to create the cells from
* @returns {CellCollection}
*/
CellCollection.prototype.data = function(dataarray){
if(dataarray instanceof CellCollection){
this.cells = clone(dataarray.cells);
}
for(var i = 0, data; data = dataarray[i]; i++){
var cell = new Cell().data(data);
this.addCell(cell);
}
return this;
};
/**
* Arranges the cells
* @param {Arrangement} arrangement The Arranagment to use.
* @returns {CellCollection}
*/
CellCollection.prototype.arrange = function(arrangement){
arrangement.arrange(this);
return this;
};
/**
* Projects the cells
* @param {Projection} projection The Projection to use.
* @returns {CellCollection}
*/
CellCollection.prototype.project = function(projection){
projection.project(this);
return this;
};
/**
* Called when a part of the cells data changes
* @param {Object} value The new value
* @param {String} nme The name of the atribute that changed
* @param {String} dim The dim of the atribute that changed
*/
CellCollection.prototype.dims_changed = function(cell, dims){
for(dim in dims){
if(!this.dimsrange[dim]) this.dimsrange[dim] = [];
this.dimsrange[dim][0] = MyMin(this.dimsrange[dim][0], dims[dim]);
this.dimsrange[dim][1] = MyMax(this.dimsrange[dim][1], dims[dim]);
}
}
function CombineArgArrays(){
var all = [];
for(var i = 0, total = 0; i < arguments.length; i++) {
arg = arguments[i];
if( arg instanceof Array){
all = all.concat(arg)
}else if(arg != undefined && arg != null){
all.push(arg);
}
}
return all;
}
function MyMax(){
var all = CombineArgArrays.apply(this,arguments);
return Math.max.apply(Math,all);
}
function MyMin(){
var all = CombineArgArrays.apply(this,arguments);
return Math.min.apply(Math,all);
}
/****************************************/
/* Arrangement
/****************************************/
function Arrangement(turnoffdefaults) {
if (!(this instanceof arguments.callee))
return new Arrangement(turnoffdefaults); // allows dropping of the "new" keyword
//this._data=null;
this._name=null;
this._arrangementsDependantOn={};
this.dimention={};
this.turnoffdefaults = turnoffdefaults;
/* Defauls:
"x": Constant(0),
"y": Constant(0),
"dx": Constant(1),
"dy": Constant(1)
};
*/
this.dimentionOrder = [];
/*
this._range={
"x":[],
"y":[]
};
this._nodes;
this.needsrecalc = true;
*/
}
/*
Arrangement.prototype.data = function(newdata) {
if (!arguments.length) return this._data;
this._data = newdata;
needsrecalc = true;
return this;
};
*/
Arrangement.prototype.name = function(newname){
if (!arguments.length) return this._name;
this._name = newname;
return this;
};
Arrangement.prototype.arrangementsDependantOn = function(newarrangementsDependantOn) {
if (!arguments.length) return this._arrangementsDependantOn;
this._arrangementsDependantOn = newarrangementsDependantOn;
return this;
};
Arrangement.prototype.addArrangementDependantOn = function(dim,arrangement) {
if (!arguments.length) return this._arrangementsDependantOn;
this._arrangementsDependantOn[dim] = arrangement;
return this;
};
Arrangement.prototype.getArrangementsDependantOnArray = function() {
var a = [];
for(i in this._arrangementsDependantOn){
a.push(this._arrangementsDependantOn[i]);
}
return a;
};
function funcToFactory(func){
if(typeof func == "object"){
//Already a factory
return func;
}else if(typeof func == "function"){
//Is a function
return {make:function(){return func}};
}else{
//Is a constant
return {make:function(){return function(){return func}}};
}
}
/**
* dim sets the calculator function that will return the value for a named dimension of a cell.
* The parameter "func" is coerced into the calculator function using the following rules:
* - If func is a _constant_ the same value will be set for the dim for all cells
* - If func is a _function_ it is required to be function(cell,i) and will be called to cacluate the value for each cell.
* - If func is a _factory_ (i.e. an object with a make() function). make() is required to return a function(cell,i) which is then used to cacluate the value for each cell.
* The factory approach is useful to allow the function(cell,i) to be recreated/reset before each collection of cells is calculated.
*
* @param {String} dim The name of the dimention
* @param {func} func The calculator function. See above.
* @param {String} c To be documented
* @param {Boolean} dontoverwrite If true nothing will happen if the dim has already been set
*/
Arrangement.prototype.dim = function(dim,func,dontoverwrite) {
if(this._arrangementsDependantOn[dim]) delete this._arrangementsDependantOn[dim];
if (!arguments.length){
return this;
}
if (arguments.length == 1) {
return this.dimention[dim];
}
if (arguments.length >= 2) {
if (arguments.length == 3 && dontoverwrite && this.dimention[dim]){
//do nothing
}else{
if(func instanceof Array){
for(var i = 0; i < func.length; i++){
func[i] = funcToFactory(func[i]);
}
this.dimention[dim] = func
}else {
this.dimention[dim] = funcToFactory(func);
}
this.dimentionOrder.push(dim);
}
}
return this;
};
/**
* Arranges an Array of data or a CellCollection of data
* @param data {CellCollection || Array} the data to be arranged
* @returns {CellCollection} the original CellCollection or a new CellCollection, with the arrangement data added to it by this function.
*/
Arrangement.prototype.arrange = function(data){
//TODO SORT and GROUPxx
var collection = new CellCollection(data);
var calculators = {};
if(! this.turnoffdefaults){
this.dim("i", Increment(), true)
this.dim("translate", Stringer("translate({x.0},{y.0})"), true)
this.dim("width", SubstractDims("x.1","x.0"), true)
this.dim("height", SubstractDims("y.1","y.0"), true)
}
for(var dim in this.dimention){
func = this.dimention[dim];
if(func instanceof Array){
calculators[dim] = [];
for(var i = 0; i < func.length; i++){
calculators[dim].push(func[i].make());
}
}else{
calculators[dim] = this.dimention[dim].make();
}
}
//For each dimention
for(var j = 0, dim; dim = this.dimentionOrder[j]; j++){
var calculator = calculators[dim];
//for each cell
for(var i = 0, cell; cell = collection.cells[i]; i++){
var value;
//For each array element
if(calculator instanceof Array){
value = [];
for(var k = 0; k < calculator.length; k++){
value.push(calculator[k].call(this, cell, i, dim, k, collection));
cell.setDim(dim, value);
}
}else{
value = calculator.call(this, cell, i, dim, 0, collection);
cell.setDim(dim, value);
}
}
}
return collection;
}
C_translate_y = function(d,i){
return "translate(0," + d.getDim("y")[0] + ")";
};
C_translate_x = function(d,i){
return "translate(" + d.getDim("x")[0] + ",0)";
};
C_translate = function(d,i){
return "translate(" + d.getDim("x")[0] +"," + d.getDim("y")[0] + ")";
};
C_height = function(d,i){
return d.getDim("height");
};
C_width = function(d,i){
return d.getDim("width");
};
C_square = function(d,i){
return Math.min(d.getDim("width"),d.getDim("height"));
};
C_half_height = function(d,i){
return d.getDim("height") / 2;
};
C_half_width = function(d,i){
return d.getDim("width") / 2;
};
C_sum = function(f1,f2){
var ff1 = d3.functor(f1);
var ff2 = d3.functor(f2);
return function(d,i){
return ff1(d,i) + ff2(d,i)
}
};
C_prod = function(f1,f2){
var ff1 = d3.functor(f1);
var ff2 = d3.functor(f2);
return function(d,i){
return ff1(d,i) * ff2(d,i)
}
};
function CellCalculator() {}
CellCalculator.prototype.calc = function(cell,i){};
function CellCalculatorFactory() {}
/**
* @return {CellCalculator}
*/
CellCalculatorFactory.prototype.make = function() {};
function Constant(value) {
if (!(this instanceof arguments.callee)) return new Constant(value); //allows dropping of the "new" keyword
CellCalculatorFactory.call(this); // Call the parent constructor
this.value = value ? value : 0;
}
Constant.prototype = new CellCalculatorFactory(); // inherit CellCalculatorFactory
Constant.prototype.constructor = Increment; // correct the constuctor
/**
* @return {CellCalculator}
*/
Constant.prototype.make = function(){
var that = this;
return function(cell,i){
return that.value;
}
};
/**
* Plus
* @constructor
* @return {Plus}
*/
function Plus(value) {
if (!(this instanceof arguments.callee)) return new Plus(value); //allows dropping of the "new" keyword
CellCalculatorFactory.call(this); // Call the parent constructor
this.value = value ? value : 1;
}
Plus.prototype = new CellCalculatorFactory(); // inherit CellCalculatorFactory
Plus.prototype.constructor = Plus; // correct the constuctor
/**
* @return {CellCalculator}
*/
Plus.prototype.make = function(){
var that = this;
return function(cell, i, dim, j){
return cell._dims[dim][j-1] + that.value;
}
};
/**
* Stringer
* @constructor
* @return {Stringer}
*/
function Stringer(value) {
if (!(this instanceof arguments.callee)) return new Stringer(value); //allows dropping of the "new" keyword
CellCalculatorFactory.call(this); // Call the parent constructor
this.value = value ? value : "";
var regex = /\{.*?\}/ig;
var allmatches = value.match(regex);
this.toReplace = [];
for(var i = 0, match; match = allmatches[i]; i++){
var keys = match.replace("{","");
keys = keys.replace("}","");
keys = keys.split(".");
r = {
"match":match,
"keys":keys
}
this.toReplace.push(r);
}
}
Stringer.prototype = new CellCalculatorFactory(); // inherit CellCalculatorFactory
Stringer.prototype.constructor = Stringer; // correct the constuctor
/**
* @return {CellCalculator}
*/
Stringer.prototype.make = function(){
var that = this;
return function(cell, i, dim, j){
res = that.value;
for(var i = 0, r; r = that.toReplace[i]; i++){
var value = cell.getDim(r.keys);
res = res.replace(r.match,value);
}
return res;
}
};
/**
* SubstractDims
* @constructor
* @return {SubstractDims}
*/
function SubstractDims(a,b) {
if (!(this instanceof arguments.callee)) return new SubstractDims(a,b); //allows dropping of the "new" keyword
CellCalculatorFactory.call(this); // Call the parent constructor
this._a = a.split(".");
this._b = b.split(".");
}
SubstractDims.prototype = new CellCalculatorFactory(); // inherit CellCalculatorFactory
SubstractDims.prototype.constructor = SubstractDims; // correct the constuctor
/**
* @return {CellCalculator}
*/
SubstractDims.prototype.make = function(){
var that = this;
return function(cell, i, dim, j){
return cell.getDim(that._a) - cell.getDim(that._b);
}
};
/**
* Project
* @constructor
* @return {Stringer}
*/
function Project(from) {
if (!(this instanceof arguments.callee)) return new Project(from); //allows dropping of the "new" keyword
CellCalculatorFactory.call(this); // Call the parent constructor
this._from = from ? from : null;
this._to = null;
this._gutter = [0,0];
}
Project.prototype = new CellCalculatorFactory(); // inherit CellCalculatorFactory
Project.prototype.constructor = Project; // correct the constuctor
/**
* @return {CellCalculator}
*/
Project.prototype.make = function(){
var that = this;
var scale = null;
return function(cell, i, dim, j){
if(scale==null){
var from_range, to_range;
if(that._from instanceof Array){
from_range = that._from;
}else{
from_range = cell.col().dimsrange[that._from];
}
if(that._to instanceof Array){
to_range = that._to;
}else{
to_range = cell.col().dimsrange[that._to];
}
scale = d3.scale.linear()
.domain(from_range)
.range(to_range);
}
if(! (that._to instanceof Array)){
to_range = cell.getDim(that._to);
scale.range(to_range);
}
from_dims = cell.getDim(that._from);
to_dim = [];
for(var i = 0; i < from_dims.length; i++){
var from_dim = from_dims[i]
var gutter = that._gutter[i];
to_dim.push(scale(from_dim ) + gutter);
}
return to_dim;
}
};
/**
* to
* @param {Array} to
* @return {Project}
*/
Project.prototype.to = function(to){
if (!arguments.length) return this._to;
this._to = to;
return this;
};
/**
* from
* @param {Array} from
* @return {Project}
*/
Project.prototype.from = function(from){
if (!arguments.length) return this._from;
this._from = from;
return this;
};
/**
* gutter
* @param {Array} from
* @return {Project}
*/
Project.prototype.gutter = function(gutter){
if (!arguments.length) return this._gutter;
this._gutter = gutter;
return this;
};
////
/**
* Grow
* @constructor
* @return {Stringer}
*/
function Grow(from) {
if (!(this instanceof arguments.callee)) return new Grow(from); //allows dropping of the "new" keyword
CellCalculatorFactory.call(this); // Call the parent constructor
this._from = from ? from : null;
this._start = 0;
this._increment = 1;
this._gutter = [0,0];
}
Grow.prototype = new CellCalculatorFactory(); // inherit CellCalculatorFactory
Grow.prototype.constructor = Grow; // correct the constuctor
/**
* @return {CellCalculator}
*/
Grow.prototype.make = function(){
var that = this;
return function(cell, i, dim, j){
from_dims = cell.getDim(that._from);
to_dim = [];
for(var i = 0; i < from_dims.length; i++){
var from_dim = from_dims[i]
var gutter = that._gutter[i];
to_dim.push(that._start + (from_dim * that._increment) + gutter);
}
return to_dim;
}
};
/**
* start
* @param {Array} to
* @return {Project}
*/
Grow.prototype.start = function(start){
if (!arguments.length) return this._start;
this._start = start;
return this;
};
/**
* increment
* @param {Array} to
* @return {Project}
*/
Grow.prototype.increment = function(increment){
if (!arguments.length) return this._increment;
this._increment = increment;
return this;
};
/**
* from
* @param {Array} from
* @return {Project}
*/
Grow.prototype.from = function(from){
if (!arguments.length) return this._from;
this._from = from;
return this;
};
/**
* gutter
* @param {Array} from
* @return {Project}
*/
Grow.prototype.gutter = function(gutter){
if (!arguments.length) return this._gutter;
this._gutter = gutter;
return this;
};
/**
* Increment
* @constructor
* @return {Increment}
*/
function Increment(step, start) {
if (!(this instanceof arguments.callee)) return new Increment(step,start); //allows dropping of the "new" keyword
CellCalculatorFactory.call(this); // Call the parent constructor
this.step = step ? step : 1;
this.start = start ? start : 0;
}
Increment.prototype = new CellCalculatorFactory(); // inherit CellCalculatorFactory
Increment.prototype.constructor = Increment; // correct the constuctor
/**
* @return {CellCalculator}
*/
Increment.prototype.make = function(){
var that = this;
var j;
function init(){
j = that.start - that.step;
}
init();
return function(cell,i){
if(i==0) init();
j = j + that.step;
return j;
}
};
/**
* Will return an integer starting ar "start", incrementing by "step" each time "field" changes.
*/
function IncrementOnChange(field, step, start) {
if (!(this instanceof arguments.callee)) return new IncrementOnChange(field, step, start); //allows dropping of the "new" keyword
Increment.call(this, step, start); // Call the parent constructor
this._field = field;
that = this;
this.field = (typeof field === "function") ? field : function(d,i) {
return d._data[that._field];
};
}
IncrementOnChange.prototype = new Increment(); // inherit CellCalculatorFactory
IncrementOnChange.prototype.constructor = IncrementOnChange; // correct the constuctor
/**
* @return {CellCalculator}
*/
IncrementOnChange.prototype.make = function(){
var that = this;
var j;
var last_f;
function init(){
j = that.start - that.step;
last_f = null;
}
init();
return function(cell,i){
if(i==0) init();
if(last_f == null || last_f != that.field(cell)){
j = j + that.step;
last_f = that.field(cell)
}
return j;
}
};
/**
* Will return an integer starting ar "start", incrementing by "step" for each unique "field".
*/
function IncrementOnUnique(field, step, start) {
if (!(this instanceof arguments.callee)) return new IncrementOnUnique(field, step, start); //allows dropping of the "new" keyword
IncrementOnChange.call(this, field, step, start); // Call the parent constructor
}
IncrementOnUnique.prototype = new IncrementOnChange(); // inherit IncrementOnChange
IncrementOnUnique.prototype.constructor = IncrementOnUnique; // correct the constuctor
/**
* @return {CellCalculator}
*/
IncrementOnUnique.prototype.make = function(){
var that = this;
var incrementors;
var incrementFactory = new Increment(that.step, that.start);
function init(){
incrementors = {};
}
init();
return function(cell,i){
if(i==0) init();
if (incrementors[that.field(cell)] == null) {
incrementors[that.field(cell)] = incrementFactory.make();
}
return incrementors[that.field(cell)](cell,i);
}
};
/**
* Will Sort
*/
function Sorter(sorter) {
if (!(this instanceof arguments.callee)) return new Sorter(sorter); //allows dropping of the "new" keyword
this._sorter = sorter;
}
Sorter.prototype = new CellCalculatorFactory(); // inherit CellCalculatorFactory
Sorter.prototype.constructor = Sorter; // correct the constuctor
/**
* @return {CellCalculator}
*/
Sorter.prototype.make = function(){
var that = this;
var tosort = true;
var a =[];
function dosort(collection){
for(var i =0, cell; cell = collection.cells[i]; i++){
a.push(cell);
}
a = a.sort(that._sorter);
tosort=false;
}
function getpos(cell){
for(var i = 0; i < a.length; i++){
if(a[i]==cell) return i;
}
}
return function(cell, i, dim, k, collection){
if(tosort) dosort(collection);
return getpos(cell);
}
};
/**
* Will Sort
*/
function SorterUnique(sorter, field) {
if (!(this instanceof arguments.callee)) return new SorterUnique(sorter, field); //allows dropping of the "new" keyword
this._sorter = sorter;
this._field = field;
}
SorterUnique.prototype = new CellCalculatorFactory(); // inherit CellCalculatorFactory
SorterUnique.prototype.constructor = SorterUnique; // correct the constuctor
/**
* @return {CellCalculator}
*/
SorterUnique.prototype.make = function(){
var that = this;
var tosort = true;
var b ={};
function dosort(collection){
for(var i = 0, cell; cell = collection.cells[i]; i++){
var key = that._field(cell);
if(!b[key]) b[key]=[];
b[key].push(cell);
}
for(var key in b){
b[key] = b[key].sort(that._sorter);
}
tosort=false;
}
function getpos(cell){
var key = that._field(cell);
for(var i = 0; i < b[key].length; i++){
if(b[key][i]==cell) return i;
}
}
return function(cell, i, dim, k, collection){
if(tosort) dosort(collection);
return getpos(cell);
}
};
/**
* Will Sum two CellCalculators
*/
function SumFn(f1,f2) {
if (!(this instanceof arguments.callee)) return new SumFn(f1,f2); //allows dropping of the "new" keyword
CellCalculatorFactory.call(this); // Call the parent constructor
this.f1 = f1;
this.f2 = f2;
}
SumFn.prototype = new CellCalculatorFactory(); // inherit CellCalculatorFactory
SumFn.prototype.constructor = SumFn; // correct the constuctor
/**
* @return {CellCalculator}
*/
SumFn.prototype.make = function(){
var cc1 = f1 instanceof CellCalculatorFactory ? this.f1.make() : d3.functor(f1);
var cc2 = f2 instanceof CellCalculatorFactory ? this.f2.make() : d3.functor(f2);
return function(cell,i){
return cc1(cell,i) + cc2(cell,i);
}
};
/**
* Will return an integer starting ar "start", incrementing by "step" each time "field" changes.
*/
function MultFn(f1,f2) {
if (!(this instanceof arguments.callee)) return new MultFn(f1,f2); //allows dropping of the "new" keyword
SumFn.call(this); // Call the parent constructor
}
MultFn.prototype = new SumFn(); // inherit CellCalculatorFactory
MultFn.prototype.constructor = MultFn; // correct the constuctor
/**
* @return {CellCalculator}
*/
MultFn.prototype.make = function(){
var cc1 = this.f1.make();
var cc2 = this.f2.make();
return function(cell,i){
return cc1(cell,i) * cc2(cell,i);
}
};
/*
vis.incrementBy = function vis_arrangement_distributeUniqueFor(field, s) {
var f = typeof field === "function" ? field : function(d, i) { return d[field]; };
var last = {};
var step = (s != null ? s : 1);
func = function(d,i){
if (last[f(d)] == null) {
last[f(d)] = 0;
}else{
last[f(d)] = last[f(d)] + step;
}
return last[f(d)];
}
return func;
}
function vis_arrangement_distributeD() {
return 1;
}
*/
/****************************************/
/* vis.oldlib.spanalign
/****************************************/
function Allign() {
if (!(this instanceof arguments.callee)) return new Allign(); //allows dropping of the "new" keyword
CellCalculatorFactory.call(this); // Call the parent constructor
this._toLayout = null;
this._toCellsCollection = null;
this._matchTo = null;
this._matchFrom = null;
this._dims = [];
}
Allign.prototype = new CellCalculatorFactory(); // inherit CellCalculatorFactory
Allign.prototype.constructor = SumFn; // correct the constuctor
/**
* Set or Get the Layout to be alligned to
* @param {Layout} toLayout
* @return {Allign}
*/
Allign.prototype.toLayout = function(toLayout) {
if (!arguments.length) return this._toLayout;
this._toLayout = toLayout;
return this;
};
/**
* Set or Get the CellsCollection to be alligned to
* @param {Layout} toLayout
* @return {Allign}
*/
Allign.prototype.toCells = function(toCellsCollection) {
if (!arguments.length) return this._toCellsCollection;
this._toCellsCollection = toCellsCollection;
return this;
};
/**
* Set or Get the dims to allign
* @param {Array} dims A list of dims
* @return {Allign}
*/
Allign.prototype.dim = function(dim) {
if (!arguments.length) return this._dim;
this._dim = dim;
return this;
};
/**
* Set or Get the to data accessor to get the data that is being used to allign to
* @param {function} matchFrom A function that takes a cell and returns a data value
* @return {Allign}
*/
Allign.prototype.matchFrom = function(matchFrom) {
if (!arguments.length) return this._matchFrom;
this._matchFrom = matchFrom;
return this;
};
/**