-
Notifications
You must be signed in to change notification settings - Fork 0
/
PfStatus.html
1161 lines (980 loc) · 39 KB
/
PfStatus.html
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
{% load staticfiles %}
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<body>
<link rel="stylesheet" href="{% static 'jquery-ui-1.11.4.custom/spin.css' %}">
<link rel="stylesheet" href="{% static 'jquery-ui-1.11.4.custom/jquery-ui.css' %}">
<link rel="stylesheet" href="{% static 'jquery-ui-1.11.4.custom/component.css' %}">
<link rel="stylesheet" href="{% static 'jquery-ui-1.11.4.custom/buttons_tymp.css' %}">
<link rel="stylesheet" href="{% static 'jquery-ui-1.11.4.custom/table_tymp.css' %}">
<link rel="stylesheet" href="{% static 'jquery-ui-1.11.4.custom/PfStatus.css' %}">
<link rel="stylesheet" href="{% static 'jquery-ui-1.11.4.custom/set1.css' %}">
<script src="{% static 'jquery-ui-1.11.4.custom/external/jquery/jquery.js' %}"></script>
<script src="{% static 'jquery-ui-1.11.4.custom/d3.min.js' %}"></script>
<script src="{% static 'jquery-ui-1.11.4.custom/jquery-ui.js' %}"></script>
<script src="{% static 'jquery-ui-1.11.4.custom/external/jquery/d3-legend.js' %}"></script>
<script src="{% static 'jquery-ui-1.11.4.custom/jquery.ui.slider.js' %}"></script>
<script src="{% static 'jquery-ui-1.11.4.custom/snap.svg-min.js' %}"></script>
<link href='https://fonts.googleapis.com/css?family=Bitter:400,400italic' rel='stylesheet' type='text/css'>
<script>
//global VARIABLES
var timerStart = Date.now();
console.log('BEGIN ? '+timerStart);
window.master_array = new Array();
window.bm_array = new Array();
window.bm_Data_ = [];
//this will store the fund selected by the user
window.current_MF_Data_ = [];
window._BM_MF_FromDt_idx = -1;
window._BM_MF_ToDt_idx = -1;
var colors = d3.scale.category20();
//to be read from a static file
window.BM1 = 'CNX Nifty';
window.BM2 = 'CNX Midcap';
window.BM3 = 'CNX Smallcap';
window.dispCircleRadius = 8;
window.dispBMName = '';
window.dispMFName = '';
// will enable removal of ite ms via click on legend
window.RemoveLegendClick = 1;
window.pieRadiusDiff = 80;
window.doughRadiusOuterDiff = 10;
window.doughRadiusInnerDiff = 70;
window.XOffset = 150;
window.YOffset = 25;
window.Mnth = {};
window.MnthRev = {};
Mnth["Jan"]='01';
Mnth["Feb"]='02';
Mnth["Mar"]='03';
Mnth["Apr"]='04';
Mnth["May"]='05';
Mnth["Jun"]='06';
Mnth["Jul"]='07';
Mnth["Aug"]='08';
Mnth["Sep"]='09';
Mnth["Oct"]='10';
Mnth["Nov"]='11';
Mnth["Dec"]='12';
MnthRev[1]='Jan';
MnthRev[2]='Feb';
MnthRev[3]='Mar';
MnthRev[4]='Apr';
MnthRev[5]='May';
MnthRev[6]='Jun';
MnthRev[7]='Jul';
MnthRev[8]='Aug';
MnthRev[9]='Sep';
MnthRev[10]='Oct';
MnthRev[11]='Nov';
MnthRev[12]='Dec';
window.isDragging = false;
//given that both BM and MF will have the same Date range
//we can read the current BM values itself
initGlobalSetBM();
function findIndex(findDt){
for(ctr = 0 ;ctr < bm_Data_.length; ctr++){
if( (bm_Data_[ctr].Date).getTime() >= findDt.getTime() )
return ctr;
}
return -1;
}
// to be called initially
function initGlobalSetBM(){
//FOR NOW use 0 index .. but we need to have a map
// for MF data and bm index
window.doughRec = {{ll_dough|safe}};
window.doughRecStock = {{ll_dough1|safe}};
window.stockAlloc = {{ll_alloc|safe}};
window.idxRec = {{ll_ap|safe}};
window.stockRec = {{ll_line|safe}};
calcPfLine();
calcCompIndex();
calcPeriodicReturns();
//input dough
doughList = Object.keys(doughRec);
doughListStock = Object.keys(doughRecStock);
window.doughIP = [];
window.doughStockIP = [];
for(cr =0 ; cr < doughList.length; cr++){
var singleObj = {};
singleObj['asset'] = doughList[cr];
singleObj['share'] = doughRec[doughList[cr]];
doughIP.push(singleObj);
}
for(cr =0 ; cr < doughListStock.length; cr++){
var singleObj = {};
singleObj['asset'] = doughListStock[cr];
singleObj['share'] = doughRecStock[doughListStock[cr]];
doughStockIP.push(singleObj);
}
if(window._BM_MF_FromDt_idx == -1 && window._BM_MF_ToDt_idx == -1){
_BM_MF_FromDt_idx = Math.round( bm_Data_.length*(0.5) );
_BM_MF_ToDt_idx = bm_Data_.length-1;
}
}
var _sort_asc = function (date1, date2) {
// This is a comparison function that will result in dates being sorted in
// ASCENDING order. As you can see, JavaScript's native comparison operators
// can be used to compare dates. This was news to me.
if (date1 > date2) return 1;
if (date1 < date2) return -1;
return 0;
}
function calcPfLine(){
stock = '';
window.dateDict = {};
window.pfForMo = {};
window.NiftyForMo = {};
window.MCForMo = {};
window.SCForMo = {};
window.pfChange = [];
window.bm_Data_ = [];
window.minClose = 0;
window.maxClose = 0;
StockMaster = {};
StockDict = {};
for (ctr = 0; ctr < stockRec.length; ctr++){
if( (Object.keys(StockDict )).length > 0 && stockRec[ctr].Symbol != stock ){
StockMaster[stock] = StockDict;
StockDict = {};
}
stock = stockRec[ctr].Symbol;
// the below is just to ensure i get a distinct list of all dates
dateDict[stockRec[ctr].Date] = 1;
StockDict[stockRec[ctr].Date] = stockRec[ctr].perChg;
}
//the last dict always will go here, given that the loop runs out before comp
StockMaster[stock] = StockDict;
//console.log( 'Num '+(Object.keys(StockMaster)).length+' '+StockMaster['ASHOKLEY'] );
var dateList = Object.keys(dateDict);
dateList = dateList.sort(_sort_asc);
var stockList = Object.keys(stockAlloc);
cumuChg = 0.0;
// now find out daily returns line
for (cr = 0 ; cr < dateList.length ; cr++){
dailyPfReturn = 0.0;
for (inn = 0 ; inn < stockList.length ; inn ++){
locDict = StockMaster[stockList[inn]];
dailyPfReturn = dailyPfReturn + parseFloat ( locDict[dateList[cr]] )*parseFloat (stockAlloc[stockList[inn]]);
}
if( isNaN(dailyPfReturn) )dailyPfReturn=0.0;
var singleObj = {};
singleObj['asset'] = 'Portfolio';
singleObj['Date'] = new Date(dateList[cr]);
singleObj['daily'] = dailyPfReturn;
singleObj['cumuChg'] = parseFloat (dailyPfReturn) + parseFloat(cumuChg);
//console.log(' PF '+singleObj['asset']+' '+singleObj['Date']+' '+singleObj['cumuChg']);
pfForMo[new Date(dateList[cr])] = parseFloat (dailyPfReturn) + parseFloat(cumuChg);
pfChange.push(singleObj);
cumuChg = dailyPfReturn + cumuChg;
if( cumuChg > maxClose ) maxClose = cumuChg;
if( cumuChg < minClose ) minClose = cumuChg;
}
//last value will be lost
bm_Data_ = pfChange;
}
function calcPeriodicReturns(){
min = (d3.min(pfChange , function(d) { return d.Date; })).getTime();
max = (d3.max(pfChange , function(d) { return d.Date; })).getTime();
diff = max - min;
DateList = Object.keys(pfForMo);
maxIdx = DateList.length - 1;
msDay = 60*60*24*1000;
window.tableArr = [];
mo1 = pfForMo[DateList[maxIdx]] - pfForMo[DateList[maxIdx-22]];
mo3 = pfForMo[DateList[maxIdx]] - pfForMo[DateList[maxIdx-67]];
mo6 = pfForMo[DateList[maxIdx]] - pfForMo[DateList[maxIdx-113]];
mo12 = pfForMo[DateList[maxIdx]] - pfForMo[DateList[maxIdx-225]];
mo11 = NiftyForMo[DateList[maxIdx]] - NiftyForMo[DateList[maxIdx-22]];
mo31 = NiftyForMo[DateList[maxIdx]] - NiftyForMo[DateList[maxIdx-67]];
mo61 = NiftyForMo[DateList[maxIdx]] - NiftyForMo[DateList[maxIdx-113]];
mo121 = NiftyForMo[DateList[maxIdx]] - NiftyForMo[DateList[maxIdx-225]];
if ( (maxIdx - 675) > 0){
mo36 = pfForMo[DateList[maxIdx]] - pfForMo[DateList[maxIdx-675]];
mo361 = NiftyForMo[DateList[maxIdx]] - NiftyForMo[DateList[maxIdx-675]];
var singleObj = {};
singleObj['period'] = '3 Yrs';
singleObj['assetR'] = mo36;
singleObj['mktR'] = mo361;
tableArr.push(singleObj);
}
if ( (maxIdx - 1125) > 0){
mo60 = pfForMo[DateList[maxIdx]] - pfForMo[DateList[maxIdx-1125]];
mo601 = NiftyForMo[DateList[maxIdx]] - NiftyForMo[DateList[maxIdx-1125]];
var singleObj = {};
singleObj['period'] = '5 Yrs';
singleObj['assetR'] = mo60;
singleObj['mktR'] = mo601;
tableArr.push(singleObj);
}
var singleObj = {};
singleObj['period'] = '1 Mnth';
singleObj['assetR'] = mo1;
singleObj['mktR'] = mo11;
tableArr.push(singleObj);
var singleObj = {};
singleObj['period'] = '3 Mnth';
singleObj['assetR'] = mo3;
singleObj['mktR'] = mo31;
tableArr.push(singleObj);
var singleObj = {};
singleObj['period'] = '6 Mnth';
singleObj['assetR'] = mo6;
singleObj['mktR'] = mo61;
tableArr.push(singleObj);
var singleObj = {};
singleObj['period'] = '1 Yr';
singleObj['assetR'] = mo12;
singleObj['mktR'] = mo121;
tableArr.push(singleObj);
}
function calcCompIndex(){
StockMaster = {};
StockDict = {};
idx = '';
window.idxChange = [];
window.idxMaster = {};
for (ctr = 0; ctr < idxRec.length; ctr++){
if( (Object.keys(StockDict )).length > 0 && idxRec[ctr].idx_name != idx ){
StockMaster[idx] = StockDict;
StockDict = {};
}
idx = idxRec[ctr].idx_name;
StockDict[idxRec[ctr].idx_dt] = idxRec[ctr].perChg;
}
StockMaster[idx] = StockDict;
var dateList = Object.keys(dateDict);
dateList = dateList.sort(_sort_asc);
var idxList = Object.keys(StockMaster);
var inn = 0;
// now find out daily returns line
for (inn = 0 ; inn < idxList.length; inn++){
cumuChg = 0;
for (cr = 0 ; cr < dateList.length ; cr++){
sml = StockMaster[idxList[inn]];
dailyPfReturn = parseFloat ( sml[dateList[cr]] );
if( isNaN(dailyPfReturn) )dailyPfReturn=0.0;
var singleObj = {};
singleObj['asset'] = idxList[inn];
singleObj['Date'] = new Date(dateList[cr]);
singleObj['daily'] = dailyPfReturn;
singleObj['cumuChg'] = parseFloat (dailyPfReturn) + parseFloat(cumuChg);
//console.log(' IDX '+singleObj['asset']+' '+singleObj['Date']+' '+singleObj['daily']+' '+singleObj['cumuChg']);
if( idxList[inn] == BM1 ){
NiftyForMo[new Date(dateList[cr])] = parseFloat (dailyPfReturn) + parseFloat(cumuChg);
}
idxChange.push(singleObj);
cumuChg = dailyPfReturn + cumuChg;
if( cumuChg > maxClose ) maxClose = cumuChg;
if( cumuChg < minClose ) minClose = cumuChg;
}
console.log(' IDX '+ idxList[inn]+' '+ idxChange.length);
idxMaster[idxList[inn]] = idxChange;
window.idxChange = [];
}
}
function retDailyData( data, dt){
console.log('DIG '+dt);
for(ctr = 0 ; ctr < data.length; ctr++)
{
lcDt = data[ctr].Date;
if ( dt.getFullYear() == lcDt.getFullYear() && dt.getMonth() == lcDt.getMonth() && dt.getDate() == lcDt.getDate() ) {
console.log('DIG1 '+dt+lcDt);
var per = data[ctr];
vik = Math.round(per.daily*100*100)/100 ;
if (vik > 0)
return "<p style="+"\"color:black\""+">"+per.asset+": "+ vik + "% </p>";
else
return "<p style="+"\"color:red\""+">"+per.asset+": "+ vik + "% </p>";
}
}
return "<p style="+"\"color:lawngreen\""+">"+per.asset+": Unchanged</p>";
}
function calcCumuChg( timeSeries ){
cumuChg = 0.0;
locArr = [];
for (ctr = 0 ; ctr < timeSeries.length ; ctr++){
var singleObj = {};
singleObj['asset'] = timeSeries[ctr].asset;
singleObj['Date'] = timeSeries[ctr].Date;
singleObj['daily'] = timeSeries[ctr].daily;
singleObj['cumuChg'] = cumuChg;
cumuChg += timeSeries[ctr].daily;
locArr.push(singleObj);
}
return locArr;
}
function _reScaleBM_MF_axes( area) {
//both slices will have window prefix since we intend to use them outside this function
//defensive coding
sliced_bm_Data_ = pfChange;
bmNifty = idxMaster[BM1];
bmMC = idxMaster[BM2];
bmSC = idxMaster[BM3];
if(_BM_MF_FromDt_idx >= 0 && _BM_MF_ToDt_idx >= 0){
sliced_bm_Data_ = calcCumuChg ( sliced_bm_Data_.slice( _BM_MF_FromDt_idx, _BM_MF_ToDt_idx ) );
bmNifty = calcCumuChg ( bmNifty.slice( _BM_MF_FromDt_idx, _BM_MF_ToDt_idx ) );
bmMC = calcCumuChg ( bmMC.slice( _BM_MF_FromDt_idx, _BM_MF_ToDt_idx ) );
bmSC = calcCumuChg ( bmSC.slice( _BM_MF_FromDt_idx, _BM_MF_ToDt_idx ) );
}
else{
sliced_bm_Data_.forEach(function(d) {
d.Date = new Date(d.Date);
d.cumuChg = +d.cumuChg;
});
bmNifty.forEach(function(d) {
d.Date = new Date(d.Date);
d.cumuChg = +d.cumuChg;
});
bmMC.forEach(function(d) {
d.Date = new Date(d.Date);
d.cumuChg = +d.cumuChg;
});
bmSC.forEach(function(d) {
d.Date = new Date(d.Date);
d.cumuChg = +d.cumuChg;
});
}
var locDtList = [];
var locChg = [];
locChg.push( d3.min(sliced_bm_Data_, function(d) { return d.cumuChg; }));
locChg.push( d3.max(sliced_bm_Data_, function(d) { return d.cumuChg; }));
locChg.push( d3.min(bmNifty , function(d) { return d.cumuChg; }));
locChg.push( d3.max(bmNifty, function(d) { return d.cumuChg; }));
locChg.push( d3.min(bmMC , function(d) { return d.cumuChg; }));
locChg.push( d3.max(bmMC , function(d) { return d.cumuChg; }));
locChg.push( d3.min(bmSC , function(d) { return d.cumuChg; }));
locChg.push( d3.max(bmSC , function(d) { return d.cumuChg; }));
locChg = locChg.sort(_sort_asc);
locDtList.push( d3.min(sliced_bm_Data_, function(d) { return d.Date; }));
locDtList.push( d3.max(sliced_bm_Data_, function(d) { return d.Date; }));
locDtList.push( d3.min(bmNifty , function(d) { return d.Date; }));
locDtList.push( d3.max(bmNifty, function(d) { return d.Date; }));
locDtList.push( d3.min(bmMC , function(d) { return d.Date; }));
locDtList.push( d3.max(bmMC , function(d) { return d.Date; }));
locDtList.push( d3.min(bmSC , function(d) { return d.Date; }));
locDtList.push( d3.max(bmSC , function(d) { return d.Date; }));
locDtList = locDtList.sort(_sort_asc);
window.minDate = locDtList[0];
window.maxDate = locDtList[locDtList.length-1];
window.minClose = locChg[0];
window.maxClose = locChg[locChg.length-1];
console.log('date '+minDate+' '+maxDate+' '+minClose+' '+maxClose);
var line = d3.svg.line()
.x(function(d) { return xScaler(d.Date); })
.y(function(d) { return yScaler(d.cumuChg); });
var svg = d3.select(area);
//clear existing
svg.selectAll("path")
.remove();
xScaler.domain([minDate, maxDate]);
yScaler.domain([minClose, maxClose]);
//Date picker - set min and max Dates for the calendar to show
// also setup call back function for Date closing ..if that happens
// then you need call this _reScaleBM_MF_axes after resetting start and end index
$(function() {
$( "#FromDt" ).datepicker(
{ minDate: bm_Data_[0].Date,
maxDate: bm_Data_[bm_Data_.length-1].Date ,
beforeShowDay: $.datepicker.noWeekends ,
dateFormat: 'd M yy',
constrainInput: true,
onClose: function(selectedDate) {
//need to pass via Date class since the selectedDate comes in as string
//*** whenever comparing or working with DATES lets stick to conversion into Date object***
if( null !== selectedDate){
console.log('CON11 '+new Date(selectedDate).getMonth()+new Date(selectedDate).getDate());
var locStore = _BM_MF_FromDt_idx;
_BM_MF_FromDt_idx = findIndex( new Date(selectedDate) );
console.log('CON1 '+locStore+' '+_BM_MF_FromDt_idx+' '+_BM_MF_ToDt_idx);
//set the dragslider values to the ones selected by the Date
if ( _BM_MF_FromDt_idx < _BM_MF_ToDt_idx) {
$( "#dragslider-range" ).dragslider( "values" , [ _BM_MF_FromDt_idx , _BM_MF_ToDt_idx] )
//now call the rescale function
_reScaleBM_MF_axes(".chart2");
}
else{
console.log("sorry from's always going to be lesser than TO!!");
_BM_MF_FromDt_idx = locStore;
$( "#FromDt" ).datepicker({ dateFormat: 'd M yy' });
$( "#FromDt" ).datepicker( "setDate", bm_Data_[_BM_MF_FromDt_idx].Date );
}
}
}
}
);
});
$(function() {
$( "#ToDt" ).datepicker(
{ minDate: bm_Data_[0].Date,
maxDate: bm_Data_[bm_Data_.length-1].Date ,
beforeShowDay: $.datepicker.noWeekends ,
dateFormat: 'd M yy',
onClose: function(selectedDate) {
if( null !== selectedDate){
dateConst = (selectedDate.split("/"));
var locStore = _BM_MF_ToDt_idx;
//_BM_MF_ToDt_idx = findIndex( new Date( dateConst[1]+'/'+dateConst[0]+'/'+dateConst[2] ) );
_BM_MF_ToDt_idx = findIndex( new Date(selectedDate) );
//set the dragslider values to the ones selected by the Date
//but also check if the end dt > start ...
if ( _BM_MF_FromDt_idx < _BM_MF_ToDt_idx) {
$( "#dragslider-range" ).dragslider( "values" , [ _BM_MF_FromDt_idx , _BM_MF_ToDt_idx] )
//now call the rescale function
_reScaleBM_MF_axes(".chart2");
}else{
console.log("sorry from's always going to be lesser than TO!!");
_BM_MF_ToDt_idx = locStore;
$( "#ToDt" ).datepicker({ dateFormat: 'd M yy' });
$( "#ToDt" ).datepicker( "setDate", bm_Data_[_BM_MF_ToDt_idx].Date );
}
}
}
}
);
});
svg.select(".y.axis").transition().duration(500).ease("sin-in-out") // https://github.com/mbostock/d3/wiki/Transitions#wiki-d3_ease
.call(yAxis);
svg.select(".x.axis").transition().duration(500).ease("sin-in-out") // https://github.com/mbostock/d3/wiki/Transitions#wiki-d3_ease
.call(xAxis);
//add benchmark
window.benchMarkPath = svg.append("path")
.datum(sliced_bm_Data_)
.attr("class", "line")
.attr("id","Portfolio")
.attr("d", line)
.attr("data-legend", "Portfolio")
.attr("stroke","turquoise")
.attr("transform", "translate("+marOffsetX+","+marOffsetY+")");
window.NiftyPath = svg.append("path")
.datum(bmNifty)
.attr("class", "line")
.attr("id",BM1)
.attr("d", line)
.attr("data-legend", BM1)
.attr("stroke","lawngreen")
.attr("transform", "translate("+marOffsetX+","+marOffsetY+")");
window.MCPath = svg.append("path")
.datum(bmMC)
.attr("class", "line")
.attr("id",BM2)
.attr("d", line)
.attr("data-legend", BM2)
.attr("stroke","brown")
.attr("transform", "translate("+marOffsetX+","+marOffsetY+")");
window.SCPath = svg.append("path")
.datum(bmSC)
.attr("class", "line")
.attr("id",BM3)
.attr("d", line)
.attr("data-legend", BM3)
.attr("stroke","steelblue")
.attr("transform", "translate("+marOffsetX+","+marOffsetY+")");
//now set to and from Date calendars and also dragslider positions
svg.selectAll(".legend").remove();
if ( _BM_MF_FromDt_idx < _BM_MF_ToDt_idx) {
$( "#FromDt" ).datepicker({ dateFormat: 'd M yy' });
$( "#ToDt" ).datepicker({ dateFormat: 'd M yy' });
$( "#FromDt" ).datepicker( "setDate", bm_Data_[_BM_MF_FromDt_idx].Date );
$( "#ToDt" ).datepicker( "setDate", bm_Data_[_BM_MF_ToDt_idx].Date );
$( "#dragslider-range" ).dragslider( "values" , [ _BM_MF_FromDt_idx , _BM_MF_ToDt_idx] )
}
else{
//better error handling ..for now just console.log
console.log("sorry from dt's always going to be earlier than To Date ..never went to school ..sucker ?");
}
legend = svg.append("g")
.attr("class","legend")
.attr("transform","translate(80,30)")
.style("font-size","15px")
.attr("data-style-padding",10)
.call(d3.legend);
//console();
}
</script>
<!--
<div id = "HDR1">
<p id="HeaderButton">
<button class="btn btn-7 btn-7a ">Fund Of Funds</button>
<button class="btn btn-7 btn-7a ">Fund House</button>
<button class="btn btn-7 btn-7a ">Fund Manager</button>
<button class="btn btn-7 btn-7a ">Synthetic Portfolios</button>
</p>
</div>
-->
<div id="PieBarContainer">
<div id="outer">
<h2 id="GyanHdr">Stock Allocation</h2>
<div id="inner">
</div>
</div>
<div id="Header">
<h2 id="GyanHdr">Sectoral Allocation</h2>
<div id="inner1">
</div>
</div>
<div id="Returns">
<table class="table1">
<thead>
<tr>
<th scope="col" abbr="Starter">Time Frame</th>
<th scope="col" abbr="Medium">Portfolio Returns</th>
<th scope="col" abbr="Business">NIFTY Returns</th>
</tr>
</thead>
<tbody id="tbody">
<script>
var numRows = tableArr.length;
if(numRows == 5)$('#Returns .table1').css("font-size", "20px");
if(numRows == 6)$('#Returns .table1').css("font-size", "16px");
if(numRows == 7)$('#Returns .table1').css("font-size", "12px");
var html = '';
for (ctr =0; ctr < numRows; ctr++){
html = html + "<tr><td>"+tableArr[ctr].period+"</td>";
if(tableArr[ctr].assetR < 0){
html = html + "<td style="+"\"color:red\""+">"+Math.round( (parseFloat(tableArr[ctr].assetR)*100) )+"%</td>";
}
else{
html = html + "<td style="+"\"color:black\""+">"+Math.round( (parseFloat(tableArr[ctr].assetR)*100) )+"%</td>";
}
if(tableArr[ctr].mktR < 0){
html = html + "<td style="+"\"color:red\""+">"+Math.round( (parseFloat(tableArr[ctr].mktR)*100) )+"%</td>";
}
else{
html = html + "<td style="+"\"color:black\""+">"+Math.round( (parseFloat(tableArr[ctr].mktR)*100) )+"%</td>";
}
html = html + "</tr>";
}
var inner = document.getElementById("tbody");
console.log('SLACK '+html);
inner.innerHTML = html;
</script>
</tbody>
</table>
</div>
</div>
<script>
$(function(){
width = parseFloat( $("#outer").css("width") );
height = parseFloat( $("#outer").css("height") );
console.log('WODTH '+width+' '+height+' '+screen.width+' '+screen.height);
var radius = (height/2);
var radiusSector = (height/2)-20;
var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
var doughnutArcStock = d3.svg.arc()
.outerRadius(radius - doughRadiusOuterDiff)
.innerRadius(radius - doughRadiusInnerDiff);
var doughnutArcSector = d3.svg.arc()
.outerRadius(radiusSector - doughRadiusOuterDiff)
.innerRadius(radiusSector - doughRadiusInnerDiff);
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d.share; });
var svg = d3.select("#inner").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + ( (width)/2 - 30 ) + "," + ( ((height) / 2)) + ")");
var svg1 = d3.select("#inner1").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + ( (width)/2 - 30 ) + "," + ( ((height) / 2)) + ")");
//d3.csv("data.csv", type, function(error, data) {
doughIP.forEach(function(d) {
d.share = +d.share;
});
doughStockIP.forEach(function(d) {
d.share = +d.share;
});
var g1 = svg.selectAll("#outer .arc")
.data(pie( doughStockIP ))
.sort(null)
.enter().append("g");
g1.append("path")
.attr("d", doughnutArcStock)
.attr("data-legend", function(d,i) { return doughStockIP[i].asset; })
.style("fill", function(d) { return color(d.data.asset); });
g1.append("text")
.attr("transform", function(d) { return "translate(" + doughnutArcStock.centroid(d) + ")"; })
.attr("dy", ".35em")
.text(function(d) { return d.data.share; });
g1.append("g")
.attr("class","legend")
.attr("transform", "translate(" + ((width)/2 - XOffset + 40) + "," + ( YOffset - ((height) / 2) ) + ")")
.style("font-size","15px")
.attr("data-style-padding",10)
.call(d3.legend);
var g2 = svg1.selectAll("#Header .arc")
.data(pie( doughIP ))
.sort(null)
.enter().append("g");
g2.append("path")
.attr("d", doughnutArcSector)
.attr("data-legend", function(d,i) { return doughIP[i].asset; })
.style("fill", function(d) { return color(d.data.asset); });
g2.append("text")
.attr("transform", function(d) { return "translate(" + doughnutArcSector.centroid(d) + ")"; })
.attr("dy", ".35em")
.text(function(d) { return d.data.share; });
g2.append("g")
.attr("class","legend")
.attr("transform", "translate(" + ((width)/2 - XOffset -60) + "," + ( YOffset - ((height) / 2) ) + ")")
.style("font-size","15px")
.attr("data-style-padding",10)
.call(d3.legend);
function type(d) {
d.population = +d.population;
return d;
}
});
</script>
<div id="LineChartContainer">
<div id="Label"></div>
<!-- <div id = "Label"> <span class="input input--kaede"> <span class="input input--kaede"><input class="input__field input__field--kaede" type="text" id="search" /><label class="input__label input__label--kaede" for="search"><span class="input__label-content input__label-content--kaede">Choose Benchmark</span> </label></span></div>
-->
<div id = "MFvsBM">
<svg class="chart2" id="OK"></svg>
<script>
window.MFVsBMHeight = parseFloat( $("#MFvsBM").css("height") );
window.MFVsBMWidth = parseFloat( $("#MFvsBM").css("width") );
var margin = {top: 20, right: 30, bottom: 20, left: 30},
width = MFVsBMWidth - margin.left - margin.right,
height = MFVsBMHeight - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse;
window.marOffsetX = margin.right;
window.marOffsetY = margin.top;
window.xScaler = d3.time.scale()
.range([0, width]);
window.yScaler = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(xScaler)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(yScaler)
.orient("left")
.tickValues([-2,-1,-0.50,-0.20,0,0.10,0.30,0.50,1 ,2])
.innerTickSize(-width)
.outerTickSize(0)
.tickPadding(10);
var svg = d3.select(".chart2")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + (margin.top) + ")");
//.attr("transform", "translate(0," + margin.top + ")");
//the below 2 are used to cut off the hover messages to the frame of the chart
//else it kee
//window.LineChartBegin = parseFloat($("#PieBarContainer").css("height"))+parseFloat($("#Label").css("height"))+parseFloat($("#LineChartContainer").css("margin-top"))+margin.top+15;
//window.LineChartEnd = parseFloat($("#PieBarContainer").css("height"))+parseFloat($("#Label").css("height"))+parseFloat($("#LineChartContainer").css("margin-top"))+margin.top+MFVsBMHeight+15;
window.LineChartBegin = 0;
window.LineChartEnd = MFVsBMHeight;
//add hover line
// Hover line.
var hoverLineGroup = svg.append("g")
.attr("class", "hover-line");
window.hoverLine = hoverLineGroup
.append("line")
.attr("x1", 0).attr("x2", 0)
.attr("y1", 0).attr("y2", height);
// Hide hover line by default.
hoverLine.style("opacity", 0.1);
window.tipLineChart = d3.select("body")
.append("div-tool") // declare the tooltip div
.attr("class", "d3-tip") // apply the 'tooltip' class
.style("opacity", 0);
circleBenchMark = svg.append("circle")
.attr("opacity", 0.00001)
.attr({ r: dispCircleRadius, fill: 'SpringGreen' });
ltOffset = parseFloat( $("#LineChartContainer").css("margin-left") ) + parseFloat( $("#LineChartContainer").css("width") ) - parseFloat( $("#MFvsBM").css("width") )+10;
d3.select(".chart2").on("mouseover", function() {
console.log('mouseover bugs');
}).on("mousemove", function() {
var xCoOrd = d3.mouse(this)[0];
var yCoOrd = d3.mouse(this)[1];
if( $("#FromDt").datepicker( "widget" ).is(":visible") || $("#ToDt").datepicker( "widget" ).is(":visible") ) return 1;
//console.log('GHOST BUSTERS '+xCoOrd+' '+yCoOrd+' '+ltOffset+' '+(MFVsBMWidth+ltOffset)+' '+LineChartBegin+' '+LineChartEnd);
if( xCoOrd < (ltOffset) || xCoOrd > (MFVsBMWidth+ltOffset) || yCoOrd < LineChartBegin || yCoOrd > LineChartEnd ){
tipLineChart.style("opacity",0);
hoverLine.style("opacity", 0.001);
circleBenchMark.attr("opacity", 0.001);
}
else{
console.log("JACK BLACK ENTRY: " + xScaler.invert(xCoOrd - marOffsetX) );
//check if the mouse is near the width end
xOff = 0.0;
yOff = 0.0;
// no idea why this is needed defaultOffset i mean ..
defaultOffset = 20;
hoverWidth = parseFloat($('.d3-tip').css("width"));
xOff = xCoOrd+defaultOffset ;
yOff = yCoOrd+defaultOffset;
if (MFVsBMWidth - xCoOrd < 200)
{
xOff = xCoOrd - (hoverWidth+defaultOffset);
yOff = yCoOrd+defaultOffset;
}
if (xCoOrd < 200)
{
xOff = xCoOrd + (hoverWidth+defaultOffset);
yOff = yCoOrd+defaultOffset;
}
if (LineChartEnd - yCoOrd < 50)
{
xOff = xCoOrd+defaultOffset;
yOff = yCoOrd-hoverWidth;
}
if (yCoOrd < 50)
{
xOff = xCoOrd+defaultOffset;
yOff = yCoOrd + hoverWidth;
}
circleBenchMark.attr("opacity", 0.4);
tipLineChart.transition().duration(200).style("opacity",1);
if ( xCoOrd >= (MFVsBMWidth - marOffsetX ))
hoverLine.attr("x1", MFVsBMWidth - marOffsetX ).attr("x2", MFVsBMWidth - marOffsetX ).style("opacity", 0.3);
else
hoverLine.attr("x1", xCoOrd - marOffsetX ).attr("x2", xCoOrd - marOffsetX ).style("opacity", 0.3);
locDt = new Date ( xScaler.invert(xCoOrd - marOffsetX) );
var formHtml = '';
formHtml += "<strong><p style="+"\"color:steelblue\""+">" +locDt.getDate()+" "+MnthRev[(locDt.getMonth())+1]+","+locDt.getFullYear() +"</p></strong>";
formHtml += retDailyData( sliced_bm_Data_ , locDt );
formHtml += retDailyData( bmNifty , locDt );
formHtml += retDailyData( bmMC , locDt );
formHtml += retDailyData( bmSC , locDt );
tipLineChart .html( formHtml)
.style("left", (xOff ) + "px")
.style("top", (yOff + LineChartEnd + 15) + "px");
// Use event.pageX / event.pageY here
}
var pathLength = benchMarkPath.node().getTotalLength();
//console.log("bm length " + pathLength );
//since our hover line has an offset from the xAxis our search
//needs to begin from there rather than plain xCoord
var x = xCoOrd - margin.left;
var beginning = x,
end = pathLength,
target;
while (true) {
target = Math.floor((beginning + end) / 2);
posBM = benchMarkPath.node().getPointAtLength(target);
if ((target === end || target === beginning) && posBM.x !== x) {
break;
}
if (posBM.x > x) end = target;
else if (posBM.x < x) beginning = target;
else break; //position found
}
circleBenchMark.attr("opacity", 0.5)
.attr("cx", posBM.x )
.attr("cy", posBM.y)
}) .on("mouseout", function() {
console.log('bill');
circleBenchMark.attr("opacity", 0.001);
tipLineChart.transition().duration(200).style("opacity",0);
hoverLine.style("opacity",0);
});
// Hover line.
svg.append("g")
.attr("class", "x axis")
//.attr("transform", "translate("+marOffsetX+"," + height + ")")
.attr("transform", "translate(0," +( height ) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 2)
.attr("dy", "1em")
.style("text-anchor", "end")
.text("% Daily Change (%)");
//call the final rendering
_reScaleBM_MF_axes(".chart2");
</script>
<script>
$(function() {
var begDate = new Date();
var endDate = new Date();
$("#dragslider-range").width( DragSliderWidth );
$( "#dragslider-range" ).dragslider({
range: true,
min: 0,
rangeDrag: true,
max: bm_Data_.length-1,
//values: [ 0 , bm_Data_.length-2 ],
//the below is the call back function when the dragslider's moved
slide: function( event, ui ) {
//if the below is true then this is called when we click on the range rather
//than drag the handle
if(!isDragging){
begDate = bm_Data_[ui.values[ 0 ]].Date;
endDate = bm_Data_[ui.values[ 1 ]-1].Date;
_BM_MF_FromDt_idx = ui.values[ 0 ];
_BM_MF_ToDt_idx = ui.values[ 1 ];
}
else
{
console.log('NOW GONE BUST!!!');
}
},
change : function ( event, ui ){
console.log('HMM MOVING VIA THIS !!'+_BM_MF_FromDt_idx+' '+ui.values[ 0 ]);
//over ride if isDragging true
},
stop: function( event, ui ) {
//rather than call it every movement in the above call it once after user lets go of handle
//if()
if(!isDragging)
{
_reScaleBM_MF_axes(".chart2");
}
}
});
$( "#dragslider-range .ui-dragslider-range" ).unbind('mousedown');
// $( "#dragslider-range .ui-dragslider-range" ).mousedown(function( event ) {
// console.log('OK MAYBE THIS TIME');
// isDragging = true;
// });