/
pvc-d1.0.js
11173 lines (9342 loc) · 338 KB
/
pvc-d1.0.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
//VERSION TRUNK-20120215-patched-20121015-b
// ECMAScript 5 shim
if(!Object.keys) {
Object.keys = function(o){
if (o !== Object(o)){
throw new TypeError('Object.keys called on non-object');
}
var ret = [];
for(var p in o){
if(Object.prototype.hasOwnProperty.call(o,p)){
ret.push(p);
}
}
return ret;
};
}
/**
* Implements filter property if not implemented yet
*/
if (!Array.prototype.filter){
Array.prototype.filter = function(fun, ctx)
{
var len = this.length >>> 0;
if (typeof fun != "function"){
throw new TypeError();
}
var res = [];
for (var i = 0; i < len; i++){
if (i in this){
var val = this[i]; // in case fun mutates this
if (fun.call(ctx, val, i, this))
res.push(val);
}
}
return res;
};
}
// ----------------------------
var pvc = {
debug: false
};
// Begin private scope
(function(){
var arraySlice = pvc.arraySlice = Array.prototype.slice;
/**
* Utility function for logging messages to the console
*/
pvc.log = function(m){
if (pvc.debug && typeof console != "undefined"){
console.log("[pvChart]: " + m);
}
};
pvc.logError = function(m){
if (typeof console != "undefined"){
console.log("[pvChart ERROR]: " + m);
} else {
throw new Error("[pvChart ERROR]: " + m);
}
};
/**
* Evaluates x if it's a function or returns the value otherwise
*/
pvc.ev = function(x){
return typeof x == "function" ? x(): x;
};
/**
* Sums two numbers.
*
* If v1 is null or undefined, v2 is returned.
* If v2 is null or undefined, v1 is returned.
* Else the sum of the two is returned.
*/
pvc.sum = function(v1, v2){
return v1 == null ?
v2 :
(v1 == null ? v1 : (v1 + v2));
};
pvc.nonEmpty = function(d){
return d != null;
};
pvc.get = function(o, p, dv){
var v;
return o && (v = o[p]) != null ? v : dv;
};
// Creates an object whose prototype is the specified object.
pvc.create = (function(){
function dummyKlass(){}
var dummyProto = dummyKlass.prototype;
return function(baseProto){
dummyKlass.prototype = baseProto || {};
var instance = new dummyKlass();
dummyKlass.prototype = dummyProto;
return instance;
};
}());
pvc.number = function(d, dv){
var v = parseFloat(d);
return isNaN(v) ? (dv || 0) : v;
};
// null or undefined to 'dv''
pvc.nullTo = function(v, dv){
return v != null ? v : dv;
};
pvc.padMatrixWithZeros = function(d){
return d.map(function(v){
return v.map(function(a){
return typeof a == "undefined"?0:a;
});
});
};
pvc.padArrayWithZeros = function(a){
return a.map(function(d){
return d == null ? 0 : d;
});
};
pvc.cloneMatrix = function(m){
return m.map(function(d){
return d.slice();
});
};
/**
* ex.: arrayStartsWith(['EMEA','UK','London'], ['EMEA']) -> true
* arrayStartsWith(a, a) -> true
**/
pvc.arrayStartsWith = function(array, base){
if(array.length < base.length) {
return false;
}
for(var i = 0; i < base.length ; i++){
if(base[i] != array[i]) {
return false;
}
}
return true;
};
/**
* Joins arguments other than null, undefined and ""
* using the specified separator and their string representation.
*/
pvc.join = function(sep){
var args = [],
a = arguments;
for(var i = 1, L = a.length ; i < L ; i++){
var v = a[i];
if(v != null && v !== ""){
args.push("" + v);
}
}
return args.join(sep);
};
/**
* Calls function <i>fun</i> with context <i>ctx</i>
* for every own property of <i>o</i>.
* Function <i>fun</i> is called with arguments:
* value, property, object.
*/
pvc.forEachOwn = function(o, fun, ctx){
if(o){
for(var p in o){
if(o.hasOwnProperty(p)){
fun.call(ctx, o[p], p, o);
}
}
}
};
pvc.mergeOwn = function(to, from){
pvc.forEachOwn(from, function(v, p){
to[p] = v;
});
return to;
};
pvc.mergeDefaults = function(to, defaults, from){
pvc.forEachOwn(defaults, function(dv, p){
var v;
to[p] = (from && from.hasOwnProperty(p) && (v = from[p]) !== undefined) ? v : dv;
});
return to;
};
/*
pvc.forEachRange = function(min, max, fun, ctx){
for(var i = min ; i < max ; i++){
fun.call(ctx, i);
}
};
pvc.arrayInsertMany = function(target, index, source){
// TODO: is there a better way: without copying source?
target.splice.apply(target, [index, 0].concat(other));
return target;
};
pvc.arrayAppend = function(target, source){
for(var i = 0, L = source.length, T = target.length ; i < L ; i++){
target[T + i] = source[i];
}
return target;
};
*/
// Adapted from pv.range
pvc.Range = function(start, stop, step){
if (arguments.length == 1) {
stop = start;
start = 0;
}
if (step == null) {
step = 1;
}
if ((stop - start) / step == Infinity) {
throw new Error("range must be finite");
}
this.stop = stop;//-= (stop - start) * 1e-10; // floating point precision!
this.start = start;
this.step = step;
};
pvc.Range.prototype.forEach = function(fun, ctx){
var i = 0, j;
if (this.step < 0) {
while((j = this.start + this.step * i++) > this.stop) {
fun.call(ctx, j);
}
} else {
while((j = this.start + this.step * i++) < this.stop) {
fun.call(ctx, j);
}
}
};
pvc.Range.prototype.map = function(fun, ctx){
var result = [];
this.forEach(function(j){
result.push(fun.call(ctx, j));
});
return result;
};
/**
* Equals for two arrays
* func - needed if not flat array of comparables
**/
pvc.arrayEquals = function(array1, array2, func){
if(array1 == null){return array2 == null;}
var useFunc = typeof(func) == 'function';
for(var i=0;i<array1.length;i++)
{
if(useFunc){
if(!func(array1[i],array2[i])){
return false;
}
}
else if(array1[i]!=array2[i]){
return false;
}
}
return true;
};
/**
* Converts something to an array if it is not one already
* an if it is not equal (==) to null.
*/
pvc.toArray = function(thing){
return (thing instanceof Array) ? thing : ((thing != null) ? [thing] : null);
};
/**
* Creates a color scheme based on the specified colors.
* The default color scheme is "pv.Colors.category10",
* and is returned when null or an empty array is specified.
*/
// variable to represent a default color scheme
// (Added by CvK febr. 2012)
pvc.defaultColorScheme = null;
pvc.createColorScheme = function(colors){
if (colors == null || !colors.length){
return pvc.defaultColorScheme || pv.Colors.category10;
}
colors = pvc.toArray(colors);
return function() {
var scale = pv.colors(colors); // creates a color scale with a defined range
scale.domain.apply(scale, arguments); // defines the domain of the color scale
return scale;
};
};
/**
* Install a default colorscheme. The parameter should be an array containing
* approximately 10 colors.
* If you pass colors==null it will remove the default-color scheme and
* use original default-colors.
* (Added by CvK febr. 2012)
*/
pvc.setDefaultColorScheme = function(colors){
pvc.defaultColorScheme = (colors == null) ?
null :
pvc.createColorScheme(colors);
};
//convert to grayscale using YCbCr luminance conv
pvc.toGrayScale = function(color){
var avg = Math.round( 0.299 * color.r + 0.587 * color.g + 0.114 * color.b);
//var avg = Math.round( (color.r + color.g + color.b)/3);
return pv.rgb(avg,avg,avg,0.6).brighter();
};
pvc.removeTipsyLegends = function(){
try {
$('.tipsy').remove();
} catch(e) {
// Do nothing
}
};
pvc.compareNatural = function(a, b){
return (a < b) ? -1 : ((a > b) ? 1 : 0);
};
pvc.createDateComparer = function(parser, key){
if(!key){
key = pv.identity;
}
return function(a, b){
return parser.parse(key(a)) - parser.parse(key(b));
};
};
/* Protovis Z-Order support */
// Default values
pv.Mark.prototype._zOrder = 0;
pv.Panel.prototype._hasZOrderChild = false;
pv.Panel.prototype._needChildSort = false;
pv.Mark.prototype.zOrder = function(zOrder) {
if(!arguments.length){
return this._zOrder;
}
if(this._zOrder !== zOrder){
this._zOrder = zOrder;
if(this.parent){
this.parent._hasZOrderChild =
this.parent._needChildSort = true;
}
}
return this;
};
// Copy original methods
var markRender = pv.Mark.prototype.render,
panelAdd = pv.Panel.prototype.add;
// @replace
pv.Panel.prototype.add = function(){
var mark = panelAdd.apply(this, arraySlice.call(arguments));
// Detect new child with non-zero ZOrder
if(!this._hasZOrderChild && mark._zOrder !== 0){
this._hasZOrderChild = this._needChildSort = true;
}
return mark;
};
// @replace
pv.Mark.prototype.render = function(){
// ensure zOrder is up to date
sortChildren.call(this);
markRender.apply(this, arraySlice.call(arguments));
};
function sortChildren(){
// Sort children by their Z-Order
var children = this.children, L;
if(children && (L = children.length)){
var needChildSort = this._needChildSort;
if(needChildSort){
children.sort(function(m1, m2){
return pvc.compareNatural(m1._zOrder, m2._zOrder);
});
this._needChildSort = false;
}
// Fix childIndex and apply recursively
for(var i = 0 ; i < L ; i++){
var child = children[i];
if(needChildSort) {
child.childIndex = i;
}
if(child instanceof pv.Panel){
sortChildren.call(child);
}
}
}
}
/* Local Properties */
/**
* Adapted from pv.Layout#property.
* Defines a local property with the specified name and cast.
* Note that although the property method is only defined locally,
* the cast function is global,
* which is necessary since properties are inherited!
*
* @param {string} name the property name.
* @param {function} [cast] the cast function for this property.
*/
pv.Mark.prototype.localProperty = function(name, cast) {
if (!this.hasOwnProperty("properties")) {
this.properties = pv.extend(this.properties);
}
this.properties[name] = true;
this.propertyMethod(name, false, pv.Mark.cast[name] = cast);
return this;
};
/* TICKS */
/**
* An alternative implementation of QuantitativeScale#ticks
* that ensures that:
* (i) the returned ticks include the min. and max. domain values,
* (ii) the scale's domain is extended,
* when the calculated ticks so demand and
* (iii) the resulting ticks are cached.
* <br/>
* Only scales with numeric domains are treated specially.
* The 'syncScale', when not null and falsy,
* makes every case be treated solely by the protovis implementation.
* <br />
* In any case, the default of desiredTickCount is 5
* (which is different from that of the protovis implementation).
*/
pvc.scaleTicks = function(scale, syncScale, desiredTickCount, forceCalc){
/* This implementation uses PROTOVIS's
* implementation of QuantitativeScale#ticks
* as a way to not to deal with date scales
* and to ensure that its internal field 'tickFormat'
* is updated.
*
* For the cases when the ticks do not fully enclose the domain,
* this implementation copies & adapts PROTOVIS's
* implementation, and, unfortunately,
* ends up doing the same work twice.
*
* In either case, if the ticks domain is !=
* from the scale's domain the later is updated to the former.
*/
if(!desiredTickCount){
desiredTickCount = 5;
}
var ticks,
ticksCacheKey = syncScale + "|" + desiredTickCount;
if(!forceCalc &&
scale._ticksCache &&
(ticks = scale._ticksCache[ticksCacheKey])){
return ticks;
}
// Call PROTOVIS implementation
ticks = scale.ticks(desiredTickCount);
if(syncScale != null && !syncScale){
return ticks;
}
var T = ticks.length;
// Treat only well-formed, finite, numeric domains
if(T >= 2 && !(ticks[0] instanceof Date)){
// Assume numeric domain
// Check if scale's domain is "included" in the ticks domain
var doma = scale.domain(), // "doma/in"
domaBeg = doma[0],
domaEnd = doma[doma.length - 1],
// Is is an ascending or descending scale?
// Assuming the scale is monotonic...
domaAsc = domaBeg < domaEnd,
domaMin = domaAsc ? domaBeg : domaEnd,
domaMax = domaAsc ? domaEnd : domaBeg,
tickMin = domaAsc ? ticks[0] : ticks[T - 1],
tickMax = domaAsc ? ticks[T - 1] : ticks[0];
if((tickMin > domaMin) || (domaMax > tickMax)){
// Copied & Adapted PROTOVIS algorithm
// To recalculate ticks that include the scale's domain
// at both ends.
var domaSize = domaMax - domaMin,
// 1, 10, 100, 1000, ...
tickStep = pv.logFloor(domaSize / desiredTickCount, 10),
tickCount = (domaSize / tickStep);
err = desiredTickCount / tickCount;
if (err <= .15) tickStep *= 10;
else if (err <= .35) tickStep *= 5;
else if (err <= .75) tickStep *= 2;
// NOTE: this is the "BIG" change to
// PROTOVIS's implementation:
// ceil -> floor
// floor -> ceil
tickMin = Math.floor(domaMin / tickStep) * tickStep;
tickMax = Math.ceil (domaMax / tickStep) * tickStep;
// Overwrite PROTOVIS ticks
ticks = pv.range(tickMin, tickMax + tickStep, tickStep);
if(!domaAsc){
ticks = ticks.reverse();
}
}
if(tickMin < domaMin || domaMax < tickMax){
/* Update the scale to reflect the new domain */
if(doma.length !== 2){
pvc.log("Ticks forced extending a linear scale's domain, " +
"but it is not possible to update the domain because " +
"it has '" + doma.length + "' element(s).");
} else {
pvc.log("Ticks forced extending a linear scale's domain from [" +
[domaMin, domaMax] + "] to [" +
[tickMin, tickMax] + "]");
scale.domain(tickMin, tickMax);
}
} // else === && ===
}
// Cache ticks
(scale._ticksCache || (scale._ticksCache = {}))[ticksCacheKey] = ticks;
return ticks;
};
pvc.roundScaleDomain = function(scale, roundMode, desiredTickCount){
// Domain rounding
if(roundMode){
switch(roundMode){
case 'none':
break;
case 'nice':
scale.nice();
break;
case 'tick':
scale.nice();
pvc.scaleTicks(scale, true, desiredTickCount);
break;
default:
pvc.log("Invalid 'roundMode' argument: '" + roundMode + "'.");
}
}
};
/* PROPERTIES */
/**
* Returns the value of a property as specified upon definition,
* and, thus, without evaluation.
*/
pv.Mark.prototype.getStaticPropertyValue = function(name) {
var properties = this.$properties;
for (var i = 0, L = properties.length; i < L; i++) {
var property = properties[i];
if (property.name == name) {
return property.value;
}
}
//return undefined;
};
/**
* Function used to propagate a datum received, as a singleton list.
* Use this to prevent re-evaluation of inherited data property functions!
*/
pv.dataIdentity = function(datum){
//pvc.log("dataIdentity " + this.type + " datum: " +
// (typeof datum.describe == 'function' ? datum.describe() : datum));
return [datum];
};
/* ANCHORS */
/**
* name = left | right | top | bottom
* */
pv.Mark.prototype.addMargin = function(name, margin) {
if(margin != 0){
var staticValue = pvc.nullTo(this.getStaticPropertyValue(name), 0),
fMeasure = pv.functor(staticValue);
this[name](function(){
return margin + fMeasure.apply(this, arraySlice.call(arguments));
});
}
return this;
};
/**
* margins = {
* all:
* left:
* right:
* top:
* bottom:
* }
*/
pv.Mark.prototype.addMargins = function(margins) {
var all = pvc.get(margins, 'all', 0);
this.addMargin('left', pvc.get(margins, 'left', all));
this.addMargin('right', pvc.get(margins, 'right', all));
this.addMargin('top', pvc.get(margins, 'top', all));
this.addMargin('bottom', pvc.get(margins, 'bottom', all));
return this;
};
/* SCENE */
/**
* Iterates through all instances that
* this mark has rendered.
*/
pv.Mark.prototype.forEachInstances = function(fun, ctx){
var mark = this,
indexes = [],
instances = [];
/* Go up to the root and register our way back.
* The root mark never "looses" its scene.
*/
while(mark.parent){
indexes.unshift(mark.childIndex);
mark = mark.parent;
}
// mark != null
// root scene exists if rendered at least once
var scene = mark.scene;
if(scene){
var L = indexes.length;
function collectRecursive(scene, level, t){
if(level === L){
for(var i = 0, I = scene.length ; i < I ; i++){
fun.call(ctx, scene[i], t);
}
} else {
var childIndex = indexes[level];
for(var index = 0, D = scene.length ; index < D ; index++){
var instance = scene[index],
childScene = instance.children[childIndex];
// Some nodes might have not been rendered?
if(childScene){
var toChild = t.times(instance.transform)
.translate(instance.left, instance.top);
collectRecursive(childScene, level + 1, toChild);
}
}
}
}
collectRecursive(scene, 0, pv.Transform.identity);
}
return instances;
};
/* BOUNDS */
pv.Mark.prototype.toScreenTransform = function(){
var t = pv.Transform.identity;
var parent = this.parent; // TODO : this.properties.transform ? this : this.parent
if(parent){
do {
t = t.translate(parent.left(), parent.top())
.times(parent.transform());
} while ((parent = parent.parent));
}
return t;
};
pv.Transform.prototype.transformHPosition = function(left){
return this.x + (this.k * left);
};
pv.Transform.prototype.transformVPosition = function(top){
return this.y + (this.k * top);
};
// width / height
pv.Transform.prototype.transformLength = function(length){
return this.k * length;
};
})(); // End private scope
/**
* Equal to pv.Behavior.select but doesn't necessarily
* force redraw of component it's in on mousemove, and sends event info
* (default behavior matches pv.Behavior.select())
* @param {boolean} autoRefresh refresh parent mark automatically
* @param {pv.Mark} mark
* @return {function mousedown
**/
pv.Behavior.selector = function(autoRefresh, mark) {
var scene, // scene context
index, // scene context
r, // region being selected
m1, // initial mouse position
redrawThis = (arguments.length > 0)?
autoRefresh : true; //redraw mark - default: same as pv.Behavior.select
/** @private */
function mousedown(d, e) {
if(mark == null){
index = this.index;
scene = this.scene;
} else {
index = mark.index;
scene = mark.scene;
}
m1 = this.mouse();
r = d;
r.x = m1.x;
r.y = m1.y;
r.dx = r.dy = 0;
pv.Mark.dispatch("selectstart", scene, index, e);
}
/** @private */
function mousemove(e) {
if (!scene) return;
scene.mark.context(scene, index, function() {
// this === scene.mark
var m2 = this.mouse();
r.x = Math.max(0, Math.min(m1.x, m2.x));
r.y = Math.max(0, Math.min(m1.y, m2.y));
r.dx = Math.min(this.width(), Math.max(m2.x, m1.x)) - r.x;
r.dy = Math.min(this.height(), Math.max(m2.y, m1.y)) - r.y;
if(redrawThis){
this.render();
}
});
pv.Mark.dispatch("select", scene, index, e);
}
/** @private */
function mouseup(e) {
if (!scene) return;
pv.Mark.dispatch("selectend", scene, index, e);
scene = null;
}
pv.listen(window, "mousemove", mousemove);
pv.listen(window, "mouseup", mouseup);
return mousedown;
};
/**
*
* Implements support for svg detection
*
**/
(function($){
$.support.svg = $.support.svg ||
document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1");
})(jQuery);
/**
* A datum is the atomic data entity of the data model.
* A datum contains key properties, of specific data dimensions.
* A datum contains a value property.
* A datum belongs to a given data engine.
*
* datum.elem["series"].value;
* datum.elem.category.value;
*
* @constructs
*/
pvc.Datum = function(dataEngine, datumIndex, elemsByDim, value){
// TODO: hardcoded for 2 dimensions
this.engine = dataEngine;
this.index = datumIndex; // -1 => null datum
this.elem = elemsByDim;
this.value = value;
this._selected = false;
};
$.extend(pvc.Datum.prototype,
/**
* @lends Datum#
*/
{
// Called by engine on clear
_deselect: function(){
this._selected = false;
},
/**
* Changes the selected state of the datum,
* to the specified value, 'select'.
* Returns true if the selected state changed.
*/
setSelected: function(select){
// Normalize 'select'
select = (select == null) || !!select;
if(this._selected !== select){
this._selected = select;
if(this.index >= 0){ // not a null datum
this.engine._onDatumSelectedChanged(this, select);
}
return true;
}
return false;
},
/**
* Returns true if the datum is selected.
*/
isSelected: function(){
return this._selected;
},
/**
* Toggles the selected state of the datum.
*/
toggleSelected: function(){
this.setSelected(!this._selected);
},
describe: function(){
var s = ["DATUM #" + this.index];
this.engine._dimensionList.forEach(function(dimension){
var name = dimension.name,
elem = this.elem[name];
s.push(
"\t" + name + ": " +
JSON.stringify(elem.value) + "|" + elem.index);
}, this);
return s.join(" ");
},
toString: function(){
return '' + this.value;
}
});
pvc.DataDimension = Base.extend(
/**
* @lends DataDimension#
*/
{
// lazy loading of dimension values
// function() -> [unique dimension values]
_getValues: null,
// function(value, index) -> label
_calcLabel: null,
_values: null,
_labels: null, // cache, immutable
_elements: null, // cache, immutable
_rootElement: null, // cache, immutable
_maxDepth: 0, // cache, immutable
_invisibleIndexes: null,
_selectedIndexes: null,
_valueKeyToIndex: null, // cache, immutable
_visibleIndexes: null, // cache
_visibleValues: null, // cache
_visibleElements: null, // cache
_selectedValues: null, // cache
/**
* A dimension of data.
* @constructs
*/
constructor: function(name, index, definition){
this.name = name;
this.index = index;
this._invisibleIndexes = {};
// translator -> [values]
this._fetchValues = pvc.get(definition, 'fetchValues');
this._calcLabel = pvc.get(definition, 'calcLabel');
},
/**
* Returns the unique values.
*/
getValues: function(){
if(!this._values){
this._values = this._fetchValues.call(null);
}
return this._values;
},
/**
* Returns the leaf elements.
*/
getElements: function(){
if(!this._elements){
this.getElementTree();
}
return this._elements;
},
/**
* Returns the root node of the elements tree.
*/
getElementTree: function(){
if(!this._rootElement){
var treeInfo = this.createElementsTree();
this._elements = treeInfo.elements;
this._rootElement = treeInfo.root;
this._maxDepth = treeInfo.maxDepth;
}