forked from idescat/visual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual.max.js
902 lines (851 loc) · 26.7 KB
/
visual.max.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
/*
Visual
Copyright (c) 2013 Institut d'Estadistica de Catalunya (Idescat)
http://www.idescat.cat (https://github.com/idescat/visual)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var VisualJS={
version: "0.5.6",
show: true, //To be used when a callback function is specified: "false" means "don't run VisualJS.chart()", that is, load everything but don't draw.
old: false, //You can change it to true programmatically if you already know the browser is IE<9
fixed: null,
width: 500,
bwidth: 500, //body width
height: 500,
normal: 500, //If less than this value, apply mini style; otherwise, normal style (see setup)
scripts: [],
ticks: [],
map: {},
container: {}, //To allow multiple direct embeddings, particular features of every container are saved here
func: {}, //Space for external functions
callback: null, //Or specify a default callback function when the user hasn't specified one
/* Functions */
draw: function(){
var chart=false;
if(typeof VisualJS.chart==="function"){ //can be undefined if "cmap" && old browser
VisualJS.tooltip();
VisualJS.show && VisualJS.chart();
window.onresize=function(){
VisualJS.canvas();
};
chart=true;
}
if(VisualJS.callback!==null){
VisualJS.callback.call( {id: VisualJS.id, chart: chart} );
}
},
tooltip: function(){
var d=document;
if(!d.getElementById(VisualJS.setup.tooltipid)){
var tt=d.createElement("div");
tt.id=VisualJS.setup.tooltipid;
tt.style.display="none";
d.body.appendChild(tt);
}
},
getsize: function(id){
var
vsetup=VisualJS.setup,
html=vsetup.html,
headingElement=html.heading,
footerElement=html.footer,
w=window,
d=document,
e=d.documentElement,
g=d.getElementsByTagName("body")[0],
vis=d.getElementById(id),
h=vis.getElementsByTagName(headingElement)[0].clientHeight,
f=vis.getElementsByTagName(footerElement)[0].clientHeight,
bheight=w.innerHeight || e.clientHeight || g.clientHeight
;
if(typeof bheight!=="undefined" && typeof h!=="undefined" && typeof f!=="undefined"){
if(VisualJS.fixed===null){ //Normal case: full page for visualization (embedded via iframe)
VisualJS.bwidth=w.innerWidth || e.clientWidth || g.clientWidth;
VisualJS.width=VisualJS.bwidth-vsetup.padding.w;
VisualJS.height=bheight-vsetup.padding.h-h-f;
}else{ //Embed visualization on a page via script
VisualJS.bwidth=e.clientWidth || g.clientWidth;
VisualJS.width=VisualJS.fixed[0]-vsetup.padding.w;
VisualJS.height=VisualJS.fixed[1]-vsetup.padding.h-h-f;
}
}
// We take into account width because height has little impact on label space
VisualJS.visualsize=(VisualJS.width<VisualJS.normal) ? vsetup.mini : vsetup.normal;
},
atext: function (s) {
return String(s).replace(/&/g, "&"); //More general .replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
},
getHeading: function (o) {
if(VisualJS.autoheading===false){
return o.title;
}
var
t=[],
add=function(s, nw){
if(typeof s==="string"){
if(nw===true){
s='<span class="'+VisualJS.setup.nowrapclass+'">' + s + "</span>";
}
t.push(s);
}
}
;
if(o.time!==null && typeof o.time==="object"){
var
start=VisualJS.tformat(o.time[0]),
end=VisualJS.tformat(o.time[o.time.length-1]),
time=start+"–"+end
;
}else{
var time=VisualJS.tformat(o.time);
}
add(o.title, false);
add(o.geo, true);
if(time!==null) {
add(time, true);
}
return VisualJS.atext(t.join(". "));
},
// Add "script" to scripts' array
// check: false adds script without verifying its existance (used when a parent lib is already missing and existing conditions cannot be met).
addJS: function (script, check) {
if(!check || !script.exists.call()) {
VisualJS.scripts.push(script.js);
return true;
}
return false;
},
/* html: tooltip html content
x, y: mouse coordinates
Returns: posTT (tooltip coordinates [x,y])
*/
showTooltip: function(html, x, y) {
var
tt=document.getElementById(VisualJS.setup.tooltipid),
visRightLimit=VisualJS.bwidth-VisualJS.setup.margin, //Visual right limit
pos={} //Final tooltip position
;
tt.innerHTML=html;
tt.style.display="block"; //Paint to get width
var ttHalfWidth=tt.clientWidth/2; //Half of tooltip width
//Default: tooltip top and centered
pos.x=x-ttHalfWidth;
pos.y=y-tt.clientHeight-5; //5 to avoid cursor
if(x + ttHalfWidth > visRightLimit){ //Outside right: --> move to left
pos.x-= (x + ttHalfWidth)-visRightLimit;
}else if(pos.x<VisualJS.setup.margin){ //Outside left --> move to right
pos.x+=VisualJS.setup.margin-pos.x ;
}//Outside top --> move down
if(pos.y<VisualJS.setup.margin){
pos.y+=tt.clientHeight*1.75;
}//Outside bottom not possible
tt.style.left=pos.x+"px";
tt.style.top=pos.y+"px";
},
format: function(n){
if(typeof n==="undefined" || n===null){
return VisualJS.setup.i18n.text.na[VisualJS.lang];
}
if(typeof n==="number"){
var
s=n.toFixed(VisualJS.container[VisualJS.id].dec),
rgx=/(\d+)(\d{3})/,
x=s.split("."),
x1=x[0],
x2=(x.length>1) ? VisualJS.setup.i18n.text.dec[VisualJS.lang] + x[1] : ""
;
while(rgx.test(x1)){
x1=x1.replace(rgx, "$1" + VisualJS.setup.i18n.text.k[VisualJS.lang] + "$2");
}
return x1+x2;
}
return "";
},
tformat: function(t){
if(!t){//undefined, null, "", 0
return null;
}
//Formatted dates are string numbers
if(isNaN(t)){
return t;
}
switch(t.length){
case 5:
var f="quarter";
break;
case 6:
var f="month";
break;
default:
return t;
}
var label=VisualJS.setup.i18n.text[f];
if(typeof label==="undefined"){
return t;
}
var text=label[VisualJS.lang];
if(typeof text==="undefined"){
return t;
}
return text[t.slice(4)-1]+" <span>"+t.slice(0,4)+"</span>";
},
tooltipText: function(id, l, v) {
var
d=VisualJS.container[id].dec,
lab=" "+VisualJS.container[id].unit.label,
si=(typeof v==="number") ? VisualJS.container[id].unit.symbol : "",
va=VisualJS.format(v),
t=(va!== VisualJS.setup.i18n.text.na[VisualJS.lang]) ?
( (VisualJS.container[id].unit.position==="end") ? va+lab+" "+si : si+va+lab )
: // Value not available
va
;
return l ? "<strong>"+t+"</strong> "+l : t; //no need to atext()
},
iframe: function(o, css){
var
vsetup=VisualJS.setup,
clas=(typeof o.clas==="string") ? o.clas : vsetup.clas,
html="<html><head>",
old=vsetup.func.old("ie9"),
create=function(){
var
d=document,
iframe=d.createElement("iframe"),
e=d.getElementById(o.id)
;
//iframe.seamless="seamless";
iframe.frameBorder="0";
iframe.scrolling="no";
e.parentNode.insertBefore(iframe, e.nextSibling); //we insert iframe after script
return iframe;
},
content=function(iframe, html){
if(typeof iframe !=="undefined"){
var iframeDoc;
if(iframe.contentDocument){
iframeDoc=iframe.contentDocument;
}
else if(iframe.contentWindow){
iframeDoc=iframe.contentWindow.document;
}
else if(window.frames[iframe.name]){
iframeDoc=window.frames[iframe.name].document;
}
if(iframeDoc){
iframeDoc.open();
iframeDoc.write(html);
iframeDoc.close();
}
}
}
;
if(typeof css==="string"){
if(css.indexOf("{")===-1){ //No "{"? We assume it's a URI
html+= '<link href="'+ css +'" rel="stylesheet" type="text/css"\/>';
}else{
html+= '<style type="text/css">'+ css +'<\/style>';
}
}
html+= '<script type="text/javascript" src="'+ VisualJS.setup.main.visual +'"><\/script>';
html+= '<script type="text/javascript" src="'+ VisualJS.setup.main.setup +'"><\/script>';
html+= '<script type="text/javascript" src="'+ VisualJS.setup.main.lazy +'"><\/script>';
html+= '<\/head><body><div id="'+ o.id +'" class="'+ clas +'"><\/div><script>window.setTimeout(function(){VisualJS.old='+ old +'; visual('+ JSON.stringify(o) +');},1);<\/script><\/body><\/html>';
content(create(), html);
},
//if o is array, then loop
load: function (o) {
if(typeof VisualJS.setup==="undefined"){
window.alert("Visual: Setup not found (visual.setup.js)!");
}
if(Object.prototype.toString.call(o)!=="[object Array]"){
VisualJS.get(o);
}else{
for(var i=0, len=o.length; i<len; i++){
VisualJS.get(o[i]);
}
}
},
//o: object passed thru visual(o)
get: function (o) {
var
vsetup=VisualJS.setup,
html=vsetup.html,
scanvas=vsetup.canvas,
headingElement=html.heading,
footerElement=html.footer,
ie8=VisualJS.old||vsetup.func.old("ie9") //Means: less than IE9
;
VisualJS.id=(typeof o.id==="string") ? o.id : vsetup.id;
if(typeof o.fixed==="object"){
VisualJS.fixed=o.fixed;
}
if(typeof o.unit==="object"){
VisualJS.container[VisualJS.id]={
unit: {
label: (typeof o.unit.label==="string") ? o.unit.label : scanvas.unit.label,
symbol: (typeof o.unit.symbol==="string") ? o.unit.symbol: scanvas.unit.symbol,
position: (typeof o.unit.position==="string") ? o.unit.position : scanvas.unit.position
}
};
}else{
VisualJS.container[VisualJS.id]={unit: scanvas.unit};
}
VisualJS.container[VisualJS.id].dec=(typeof o.dec==="number") ? o.dec : scanvas.dec;
VisualJS.show=(typeof o.show==="boolean") ? o.show : VisualJS.show;
VisualJS.autoheading=(typeof o.autoheading==="boolean") ? o.autoheading : scanvas.autoheading;
VisualJS.legend=(typeof o.legend==="boolean") ? o.legend: scanvas.legend;
VisualJS.lang=o.lang || vsetup.i18n.lang;
VisualJS.callback=(typeof o.callback==="function") ? o.callback: VisualJS.callback;
if(typeof o.grid==="object"){
VisualJS.grid={
width: (typeof o.grid.width==="number") ? o.grid.width : scanvas.grid.width
};
}else{
VisualJS.grid=scanvas.grid;
}
if(typeof o.axis==="object"){
VisualJS.axis={
x: (typeof o.axis.x==="boolean") ? o.axis.x : scanvas.axis.x,
y: (typeof o.axis.y==="boolean") ? o.axis.y : scanvas.axis.y
};
}else{
VisualJS.axis=scanvas.axis;
}
var
selector="#" + VisualJS.id,
canvas=selector + " ."+vsetup.canvasclass //Currently, only used in Flot
;
if(o.type==="cmap"){
if(ie8){
document.getElementById(VisualJS.id).innerHTML="<p>"+vsetup.i18n.text.oldbrowser[VisualJS.lang]+"</p>";
}else{
if(typeof o.by!=="string"){
return;
}
VisualJS.addJS( vsetup.lib.maps, true );
VisualJS.addJS( vsetup.lib.d3, true );
VisualJS.addJS( vsetup.map[o.by], true );
///////// CHART
VisualJS.chart=function(){
var
map=VisualJS.map[o.by],
mwidth=map.area[0],
mheight=map.area[1],
//hasGroup: grouped property exists, is object (array), has content and data seems to include a group property
hasGroup=(typeof o.grouped==="object" && o.grouped.length>0 && o.data[0].hasOwnProperty("group")),
hasValues=(!hasGroup && o.data[0].hasOwnProperty("val")),
num=(hasGroup) ? o.grouped.length : ((hasValues) ? vsetup.colors.map.max : 1),
colors=VisualJS.func.colors( vsetup.colors.map.base, num, "fill", "q" ),
visual=d3.select(selector),
projection=d3.geo[map.projection](),
//Support for projections that don't support the center method (albersUSA, for example).
proj=(typeof map.center==="object" && typeof projection.center==="function") ? projection.center(map.center) : projection,
xy=proj
.scale(map.scale)
.translate([mwidth/2, mheight/2]),
path=d3.geo.path().projection(xy),
tooltip=d3.select("#" + vsetup.tooltipid)
;
VisualJS.canvas=function(){
visual.html("<"+headingElement+"></"+headingElement+"><"+footerElement+"></"+footerElement+">");
d3.select(selector+" "+headingElement).html(VisualJS.getHeading(o));
d3.select(selector+" "+footerElement).html(VisualJS.atext(o.footer || ""));
VisualJS.getsize(VisualJS.id);
var
id=VisualJS.id,
valors=d3.map(),
val=[],
groups, //key: id, value: group
setGroups=function(g,r){},
legend=function(){},
checkGrouped,
groupLabel,
min=(typeof o.filter==="number") ? o.filter : scanvas.filter,
max=1-min,
inf,
sup,
hh=VisualJS.height/mheight,
ww=VisualJS.width/mwidth,
width=Math.min(
Math.round( mwidth*hh ),
VisualJS.width
),
height=Math.min(
Math.round( mheight*ww ),
VisualJS.height
),
left=Math.floor( (VisualJS.width-width)/2 ),
topbottom=Math.floor( (VisualJS.height-height)/2 ),
scale=(hh<ww) ? hh : ww,
vis=visual
.insert("svg:svg", footerElement)
.attr("width", width)
.attr("height", height)
;
if(hasGroup){
groups=d3.map();
setGroups=function(g, r){
g.set(r.id, r.group);
};
checkGrouped=function(g, v, p, inf, sup){
return "q" + (g.get(p[map.id])-1);
};
groupLabel=function(g, p){
var
em=o.grouped[(g.get(p[map.id])-1)],
ret=p[map.label]
;
if(typeof em!=="undefined"){
ret+=" <em>" + em + "</em>";
}
return ret;
};
}else{
if(hasValues){
checkGrouped=function(g, v, p, inf, sup){
var quantize=d3.scale.quantize()
.domain([inf, sup])
.range(d3.range(num).map(function(i) { return "q" + i; }))
;
return quantize(v.get(p[map.id]));
};
legend=VisualJS.func.legend;
}else{
checkGrouped=function(g, v, p, inf, sup){
return (v.get(p[map.id])!=="") ? "" : "q"+(num-1);
};
}
groupLabel=function(g, p){
return p[map.label];
};
}
for (var i=0, odata=o.data, len=odata.length; i<len; i++){
var r=odata[i];
if(r.hasOwnProperty("val")){
valors.set(r.id, r.val);
}else{ //If no val property on data (for example, grouped info), then do not print value on tooltip.
valors.set(r.id, "");
}
val.push(r.val);
setGroups(groups, r); //Does nothing if no groups
}
val.sort(function(a, b) {
return a-b;
});
if(
Object.prototype.toString.call(o.filter)==="[object Array]" &&
o.filter.length===2 &&
typeof o.filter[0]==="number" &&
typeof o.filter[1]==="number" &&
o.filter[0]<o.filter[1]
){
inf=o.filter[0];
sup=o.filter[1];
}else{
inf=d3.quantile(val, min);
sup=d3.quantile(val, max);
}
vis.style("margin-left", left+"px");
vis.style("margin-top", topbottom+"px");
vis.style("margin-bottom", topbottom+"px");
vis.append("svg:g")
.attr("class", vsetup.areaclass)
.attr("transform", "scale("+scale+")")
.selectAll("path")
.data(map.features)
.enter().append("svg:path")
.attr("class", function(d) {
return checkGrouped(groups, valors, d.properties, inf, sup);
})
.attr("d", path)
.on("mousemove", function(d){
if(hasValues || typeof valors.get(d.properties[map.id])!=="undefined"){
VisualJS.showTooltip(
VisualJS.tooltipText(
id,
groupLabel(groups, d.properties),
valors.get(d.properties[map.id])
),
d3.event.pageX,
d3.event.pageY
);
}
})
.on("mouseout", function(){return tooltip.style("display", "none");})
;
if(VisualJS.legend && typeof map.legend==="object") { //If legend specified (array), draw it
legend(
id,
VisualJS.tooltipText(id, null, sup),
VisualJS.tooltipText(id, null, inf),
colors[colors.length-1],
colors[0],
vis,
tooltip,
map.area,
map.legend
);
}
};
VisualJS.canvas();
}
}
}else{
//(o.type==="tsline" || o.type==="tsbar" || o.type==="bar" || o.type==="rank" || o.type==="pyram")
if( VisualJS.addJS( vsetup.lib.jquery, true ) ){ //No jQuery? Add Flot without checking
var hasFlot=false;
VisualJS.addJS( vsetup.lib.jquery.flot, false );
}else{ //Has jQuery but not Flot?
if( VisualJS.addJS( vsetup.lib.jquery.flot, true ) ){
var hasFlot=false;
}else{
var hasFlot=true;
}
}
if(ie8){
VisualJS.addJS( vsetup.lib.excanvas, true);
}
var
transform=function(){}, //Local function in load(). It will be redefined.
series=[],
ticks=[],
opt=[],
stacked=o.stacked || false,
ts=function(){
var fbars=function(si){
return; //When stacked an undefined is expected in bars (null or false won't work)
}
if(stacked){
VisualJS.addJS( vsetup.lib.jquery.flot.stack, hasFlot ); //Check plugin only if we have Flot
}else{
if(o.type==="tsbar"){
VisualJS.addJS( vsetup.lib.jquery.flot.orderbars, hasFlot ); //Check plugin only if we have Flot
var fbars=function(si){
return si.bars;
}
}
}
transform=function(d,t,b){ // Local in load(), not ts().
for(var i=0, len=t.length; i<len; i++){
ticks.push([i,t[i]]);
VisualJS.ticks.push([i,t[i]]); //keep original ticks
}
for(var i=0, len=d.length; i<len; i++){
for(var data=[], v=d[i].val, vlen=v.length, j=0; j<vlen; j++){
data.push([j,v[j]]);
}
if(o.type!=="tsbar" || stacked || len===1){//if tsbar with one series (len===1) must be treated like stacked (even though it's not)
series.push({label: d[i].label, data: data});
}else{
series.push({label: d[i].label, data: data, bars: { show: true, barWidth: 0.2, order: i+1, lineWidth: 2 }}); //barWidth should probably be computed dynamically considering number of series (this value allows only for a max of 3 series)
}
}
for (var i=0, slen=series.length; i<slen; i++){
opt.push(
{
data: series[i].data,
label: series[i].label,
bars: fbars(series[i]),
shadowSize: 4
}
);
}
shlegend=(slen>1);
};
return VisualJS.getHeading(o);
}
;
switch(o.type){
case "pyram":
VisualJS.addJS( vsetup.lib.jquery.flot.pyramid, hasFlot ); //Check plugin only if we have Flot
Array.max=function(a){
return Math.max.apply(Math, a);
};
var
transform=function(d,t,b){
max=Math.max( Array.max(d[0].val) , Array.max(d[1].val) ),
series[0]={label: d[0].label, data: [], pyramid: {direction: "L"}};
series[1]={label: d[1].label, data: []};
for(var i=0, len=b.length; i<len; i++){
series[0].data[i]=[ b[i] , d[0].val[i] ];
series[1].data[i]=[ b[i] , d[1].val[i] ];
}
},
shlegend=true,
stack=false,
lines=false,
points=false,
bars=false,
heading=VisualJS.getHeading(o)
;
break;
case "rank":
var
data=[],
transform=function(d,t,b){
for(var i=0, len=d.length; i<len; i++){
//Include in reverse order
ticks[i]=[i,d[len-i-1][0]];
data[i]=[d[len-i-1][1],i];
}
series={data: data};
},
shlegend=false, //Currently only one series allowed when rank (no series loop)
stack=false, //See previous line
lines=false,
points=false,
bars=true,
heading=VisualJS.getHeading(o)
;
break;
case "bar":
VisualJS.addJS( vsetup.lib.jquery.flot.categories, hasFlot ); //Check plugin only if we have Flot
var
transform=function(d,t,b){
series=d;
shlegend=(series.length>1);
},
stack=true,
lines=false,
points=false,
bars=true,
heading=VisualJS.getHeading(o)
;
break;
case "tsline":
var
heading=ts(),
stack=null,
lines=true,
points=true,
bars=false
;
break;
case "tsbar":
var
heading=ts(),
stack=true,
lines=false,
points=false,
bars=true
;
break;
}
VisualJS.chart=function(){
transform(o.data, o.time, o.by);
$.fn.UseTooltip=function (id) {
var previousPoint=[];
$(this).bind("plothover", function (event, pos, item) {
if (item) {
if (previousPoint!=[item.seriesIndex, item.dataIndex]) {
previousPoint=[item.seriesIndex, item.dataIndex];
var
x=item.datapoint[0],
y=item.datapoint[1],
itemlab=(o.type!=="bar") ? item.series.label : series[x][0],
label=(o.type!=="rank") ? itemlab : ticks[y][1],
tick=(o.type!=="rank" && o.type!=="bar") ?
(
(stacked || series.length===1) ?
ticks[x][1]
:
(
(o.type==="pyram") ? series[pos.x<0 ? 0 : 1].data[item.dataIndex][0] : ticks[item.dataIndex][1] // item.series.yaxis.ticks[item.dataIndex].label won't work in pyram if axis : {y: false}
)
)
: false
,
val=(o.type==="pyram") ? Math.abs(x) :
(o.type!=="rank") ?
(
(o.type!=="tsbar") ?
y :
(stacked || series.length===1) ? series[item.seriesIndex].data[x][1] : y
)
: x
;
VisualJS.showTooltip(
VisualJS.tooltipText(
id,
(tick) ? label+" ("+tick+")" : label,
val
),
pos.pageX, //item.pageX
pos.pageY //item.pageY
);
}
}else{
$("#"+vsetup.tooltipid).hide();
previousPoint=[];
}
});
};
shlegend=VisualJS.legend && shlegend;
var setup={
colors: vsetup.colors.series,
series: {
stack: stack,
bars: {
show: bars,
barWidth: 0.7,
align: "center",
fill: 0.9
},
lines: {
show: lines
},
points: {
show: points,
radius: 1
}
},
legend: {
show: shlegend
},
grid: {
borderWidth: VisualJS.grid.width,
hoverable: true,
clickable: false,
mouseActiveRadius: 10
},
xaxis:{ show: VisualJS.axis.x },
yaxis:{ show: VisualJS.axis.y }
}
;
VisualJS.canvas=function(){
$(selector).html("<"+headingElement+"></"+headingElement+"><"+footerElement+"></"+footerElement+">");
$(selector+" "+headingElement).html(heading);
$(selector+" "+footerElement).html(VisualJS.atext(o.footer || ""));
VisualJS.getsize(VisualJS.id);
$(selector+" "+headingElement).after('<div class="'+vsetup.canvasclass+' '+VisualJS.visualsize+'" style="width: '+VisualJS.width+'px; height: '+VisualJS.height+'px;"></div>');
var ticklen=ticks.length;
switch(o.type){
case "pyram":
setup.series.pyramid={show: true, barWidth: 1};
setup.xaxis.max=max*(1.02); //Increase area by 2% of the longest bar
setup.xaxis.tickFormatter=function(val, axis) {
return VisualJS.format(val);
}
$.plot(
canvas,
series,
setup
);
break;
case "rank":
setup.series.bars.horizontal=true;
setup.yaxis.ticks=( (VisualJS.height/ticklen) > 11) ? ticks.slice(0) : 0; //If too many categories and not enough height, remove y-labels
setup.xaxis.max=o.data[0][1]*(1.02); //Increase area by 2% of the longest bar
setup.xaxis.tickFormatter=function(val, axis) {
return VisualJS.format(val);
}
setup.yaxis.autoscaleMargin=0;
setup.series.bars.barWidth=0.5;
$.plot(
canvas,
[series],
setup
);
break;
case "bar":
setup.xaxis.mode="categories";
setup.xaxis.tickLength=0;
setup.yaxis.tickFormatter=function(val, axis) {
return VisualJS.format(val);
}
$.plot(
canvas,
[series],
setup
);
break;
//Time series
case "tsline":
case "tsbar":
setup.yaxis.tickFormatter=function(val, axis) {
return VisualJS.format(val);
}
var
ratio=VisualJS.width/ticklen,
xticks=[],
digcrit="01" //first month
;
switch(VisualJS.ticks[0][1].length){ //Assuming all time periods follow the same pattern
case 4: //Annual time series (4 digits)
// Magic rule: Only one year of every two must be displayed if width (mini) is small in comparison with # of ticks
if(ratio<25){
for(var i=0; i<ticklen; i++){
xticks[i]=(i % 2) ?
[ ticks[i][0], "" ]
:
[ ticks[i][0], ticks[i][1] ]
;
}
setup.xaxis.ticks=xticks;
}else{
setup.xaxis.ticks=ticks;
}
break;
case 5: //quarterly (5 digits)
digcrit="1"; //first quarter
case 6: //monthly (6 digits)
//Magic rule: do not show month/quarter when width is small in comparison with # of ticks
if(ratio<35){
for(var i=0; i<ticklen; i++){
xticks[i]=(VisualJS.ticks[i][1].slice(4)!==digcrit) ?
[ VisualJS.ticks[i][0], "" ]
:
[ VisualJS.ticks[i][0], VisualJS.ticks[i][1].slice(0,4) ]
//Formatting time
ticks[i][1]=VisualJS.tformat(VisualJS.ticks[i][1]);
}
setup.xaxis.ticks=xticks;
}else{
for(var i=0; i<ticklen; i++){
//Formatting time
ticks[i][1]=VisualJS.tformat(VisualJS.ticks[i][1]);
}
setup.xaxis.ticks=ticks;
}
break;
default: //leave ticks alone
setup.xaxis.ticks=ticks;
}
$.plot(
canvas,
opt,
setup
);
}
$(canvas).UseTooltip(VisualJS.id);
}
VisualJS.canvas();
}
}
if(VisualJS.scripts.length && typeof LazyLoad==="object"){
LazyLoad.js(VisualJS.scripts, VisualJS.draw);
}else{ //If no Lazyload, user must manually include the required libs
VisualJS.draw();
}
}
};
if(typeof visual!=="function"){
//Create the visual alias
var visual=VisualJS.load;
} //If you already have a visual() function, use VisualJS.load({...});