-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_pattern_analyzer.html
More file actions
1364 lines (1125 loc) · 45.4 KB
/
Copy pathtime_pattern_analyzer.html
File metadata and controls
1364 lines (1125 loc) · 45.4 KB
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Keywords" content="" />
<meta name="Description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<title>Day and Night</title>
<script type="text/javascript" src="lib/d3.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<style type="text/css">
div#day-length text {
fill: gray;
font-family: Helvetica, sans-serif;
font-size: 10px;
}
li
{
display:inline;
padding-right:20px;
}
</style>
<script type="text/javascript">
$(document).ready(function ()
{
var yearDropDown = $("#Year").val();
var crimeTypeDropDown = $("#incident").val();
var suburbDropDown = $("#suburbName").val();
plotCrimes(yearDropDown,crimeTypeDropDown,suburbDropDown);
$("#suburbName").change(function()
{
yearDropDown = $("#Year").val();
crimeTypeDropDown = $("#incident").val();
suburbDropDown = $("#suburbName").val();
plotCrimes(yearDropDown,crimeTypeDropDown,suburbDropDown);
});
$("#incident").change(function()
{
yearDropDown = $("#Year").val();
crimeTypeDropDown = $("#incident").val();
suburbDropDown = $("#suburbName").val();
plotCrimes(yearDropDown,crimeTypeDropDown,suburbDropDown);
});
$("#Year").change(function()
{
yearDropDown = $("#Year").val();
crimeTypeDropDown = $("#incident").val();
suburbDropDown = $("#suburbName").val();
plotCrimes(yearDropDown,crimeTypeDropDown,suburbDropDown);
});
});
function plotCrimes(year,crimeType,suburb)
{
var fetchSunData =[];
var crimeData = [];
var monthCrime = [];
$.getJSON("http://ec2-54-211-171-126.compute-1.amazonaws.com/Hello911/api/sunMovementData",
function (data) {
// On success, 'data' contains a list of sun movement data.
$.each(data, function (key, val) {
// Format the text to display.
var parsebTwilight = val.bTwilight.split(":");
var parseSunrise = val.sunrise.split(":");
var parsesunTransit = val.sunTransit.split(":");
var parseSunset = val.sunset.split(":");
var parseeTwilight = val.eTwilight.split(":");
var str = {date:new Date(val.date), bTwilight: [parsebTwilight[0], parsebTwilight[1]], sunrise:[parseSunrise[0], parseSunrise[1]], sunTransit:[parsesunTransit[0], parsesunTransit[1]], sunset: [parseSunset[0], parseSunset[1]], eTwilight:[parseeTwilight[0], parseeTwilight[1]]};
fetchSunData.push(str);
});
$.getJSON("http://ec2-54-211-171-126.compute-1.amazonaws.com/Hello911/api/crimePlot?year="+year+"&crimeType="+crimeType+"&Suburb="+suburb,
function (data) {
$.each(data, function (key, val) {
var parseTime = val.crimeTime.split(":");
var str = {Crime:val.crimeType, Date: new Date(val.crimeDate),Time:[parseTime[0],parseTime[1]] };
crimeData.push(str);
});
$.getJSON("http://ec2-54-211-171-126.compute-1.amazonaws.com/Hello911/api/monthPlot?year=" + year + "&crimeType=" + crimeType + "&Suburb=" + suburb,
function (data) {
$.each(data, function (key, val) {
var newMonth = val.month - 1;
var str = { month:newMonth, monthName: val.monthName, days: val.days, crime: val.hourAggregate };
monthCrime.push(str);
});
dayNightViz(fetchSunData, crimeData, monthCrime, crimeType, suburb);
});
});
});
}
</script>
<style type="text/css">
.popup {
zoom:1.0;
position:relative;
text-decoration:none;
}
.popup span {
position: absolute;
top:7px;
color: #000000;
left:2px;
width:350px;
padding:4px;
border:1px solid #333333;
border-radius:4px;
left:-999em;
background-color: #e2e2e2;
opacity: 0.9;
z-index:990;
}
.popup:hover {visibility:visible}
.popup:hover span {left:70px;}
* html .popup span {position:absolute;}
</style>
</head>
<body>
<div id="header">
<div id="colHeader">
<a href="index.html"><img src="images/logo.png" alt="logo" style="float: left;padding-left: 50px"></a>
<h2>Information Visualization of Seattle Crime data</h2>
</div>
</div>
<div id="content">
<div id="colMenu">
<div id="menu1">
<ul>
<li id="menu-01"><a href="index.html">About</a></li> <li><p>|</p></li>
<li id="menu-03"><a href="time_pattern_analyzer.html" class="current">Yearly Crime Pattern Analyzer</a></li><li><p>|</p></li>
<li id="menu-04"><a href="week_analyzer.html">Weekly Crime Pattern Analyzer</a></li><li><p>|</p></li>
<li id="menu-02"><a href="seattle_walkability.html">Seattle Walkability</a></li><li><p>|</p></li>
<li id="menu-05"><a href="reference.html">Reference</a></li>
</ul>
</div>
</div>
<div id="contentcenter">
<br><br>
</div>
<div id="filter">
<div style="float:left; width:400px;height:50px">
<h2>Yearly Crime Pattern Analyzer</h2><p><a class="popup" href="#"><img src="images/help.png" style="padding-left: 10px">
<span>This visualization shows the crimes in Seattle over the period of year occurred during daylight and darkness.
<br>The x-axis is the 'Time of the day' and y-axis is the 'Month'. The months and times can be compared by clicking on a particular time or month on the axes.</span></a></p>
</div>
<ul style="float:left;list-style:none;display:inline;white-space:nowrap" >
<li>Year to Analyze:
<select id="Year" >
<option>2012</option>
</select>
</li>
<li>Type of Incident:
<select id="incident" >
<option>ALL</option>
<option>ASSAULTS</option>
<option>AUTO THEFTS</option>
<option>COMMERCIAL BURGLARIES</option>
<option>GUN CALLS</option>
<option>HOMICIDE</option>
<option>LIQUOR VIOLATIONS</option>
<option>NARCOTICS COMPLAINTS</option>
<option>RESIDENTIAL BURGLARIES</option>
<option>ROBBERY</option>
<option>THEFT</option>
<option>WEAPONS CALLS</option>
</select>
</li>
<li>Suburb:
<select id="suburbName" >
<option>ALL</option>
<option>Ballard</option>
<option>Beacon Hill</option>
<option>Belltown</option>
<option>Broadview</option>
<option>Capitol Hill</option>
<option>Central District</option>
<option>Delridge</option>
<option>Edmonds</option>
<option>First Hill</option>
<option>Georgetown</option>
<option>Green Lake</option>
<option>Greenwood</option>
<option>Haller Lake</option>
<option>International District</option>
<option>Lake City</option>
<option>Leschi</option>
<option>Madison Park</option>
<option>Madison Valley</option>
<option>Madrona</option>
<option>Magnolia</option>
<option>Maple Leaf</option>
<option>Mount Baker</option>
<option>North Park</option>
<option>Northgate</option>
<option>Phinney Ridge</option>
<option>Queen Anne</option>
<option>Rainier Valley</option>
<option>Shoreline</option>
<option selected="selected">University District</option>
<option>Wallingford</option>
<option>West Seattle</option>
<option>White Center</option>
<option>Yesler Terrace</option>
</select>
</li>
</ul>
<hr style="clear:both">
</div>
<div id="day-length" ></div>
<script type="text/javascript">
function dayNightViz(sunData, crimeData, monthCrime, crimeTypeValue, suburb)
{
var width = 900;
var height = 450;
var padding = 40;
var paddingX = 40;
var paddingY = 40;
var lineGraphHeight = height / 3;
var symbolSize = 7;
var teethLength = 12;
var yDisplacement = 6;
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
// color selections for curves and clickable box
// var curveColor = ["Peru", "SteelBlue", "Violet", "Teal", "Orange", "Turquoise", "Khaki", "LightCoral", "Goldenrod", "IndianRed", "BurlyWood", "Pink", "RosyBrown",
// "Chartreuse", "Chocolate", "Coral", "DarkMagenta", "Fuchsia", "Gainsboro", "GhostWhite", "Indigo", "LemonChiffon", "Lime", "Linen" ,"OliveDrab"];
///////////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var curveColor = ["DarkSlateBlue", "BlueViolet", "SteelBlue", "CornflowerBlue", "Violet", "Turquoise", "LightBlue", "Pink", "Moccasin", "Gainsboro", "Lime", "GreenYellow",
"BurlyWood", "DarkOrange" ,"OrangeRed", "HotPink", "RosyBrown", "SeaGreen", "Olive", "Sienna", "Green", "DarkOrchid", "Blue", "MidnightBlue", ];
///////////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//Fake monthly data should be get from jason
// Fake hour data should be get from jason
// [ { hour: 0, section: "0 - 1", crime: getRandomCrime()}, .....]
var hourCrime = getRandomCrimeForHour();
//////////////////////////////////////////////////
function getRandomCrimeForHour() {
var data = new Array();
for (var i = 0; i < 24; i++) {
data.push({ hour: i, section: i + " am - " + (i + 1) + " pm", crime: getRandomCrime(12)}); ///////////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// console.log("test crime for hour" + data[i].section);
}
return data;
}
function getRandomCrime(d) {
var monthCrime = new Array();
for(var i = 0; i < d; i++) {
monthCrime[i] = Math.round(Math.random() * 500);
}
return monthCrime;
}
function yAxisLabel(d) {
if (d == 12) { return "noon"; }
if (d < 12) { return d; }
return (d - 12);
}
// The labels along the x axis will be positioned on the 15th of the
// month
// �إ�Map<month, data>
function midMonthDates() {
return d3.range(0, 12).map(function(i) { return new Date(2012, i, 15) });
}
// The labels along the x axis will be positioned on the 1st of the
// month
function firstMonthDates() {
var data = new Array();
for (var i = 0; i < 12; i++) {
data.push(new Date(2012, i, 1));
}
data.push(new Date(2012, 11, 31));
// return d3.range(0, 13).map(function(i) { return new Date(2012, i, 1) });
return data;
}
/////////////////////////////////////////////
// start a SVG
d3.select("#yearCrimeViz").remove();
var dayLength = d3.select("#day-length").
append("svg:svg").
attr("width", width + paddingX * 2 + lineGraphHeight * 2).
attr("height", height * 2 + paddingY * 2).
attr("id","yearCrimeViz");
var xScale = d3.time.scale().domain([new Date(2012, 0, 1, 0, 0), new Date(2012, 0, 1, 23, 59)]).range([0, width]);//Date + time
var yScale = d3.time.scale().domain([new Date(2012, 0, 1), new Date(2012, 11, 31)]).range([0, height]);//Date
var xGradientScale = d3.time.scale().domain([new Date(2012, 0, 1, 0, 0), new Date(2012, 0, 1, 23, 59)]).range([0, 1]);//Date + time
/////////click group//////////
var clickGroup = dayLength.append("svg:g") ////
.attr("transform", "translate("+ paddingX + ", " + paddingY + ")");
///////////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var intentMonth = new Array();
var defaultMonth = 5;
var defaultHour = 5;
intentMonth.push(defaultMonth); // default
var intentHour = new Array();
intentHour.push(defaultHour); // default
var monthClickDemo = clickGroup.selectAll("rect.monthClickDemo").
data(monthCrime).enter().
append("rect").
attr("id", function(d, i) { return "monthClick" + i; }).
attr("x", -30).
attr("y", function(d, i) {return yScale(new Date(2012, i, 1));}).
attr("rx", 5).
attr("ry", 5).
attr("width", width + 60).
attr("height", function(d, i) {return yScale(new Date (2012, i, d.days)) - yScale(new Date (2012, i, 0))}).
attr("fill", function (d, i) { return curveColor[d.month];}).
attr("fill-opacity", "0.5").
attr("stroke", "white").
attr("stroke-width", "1px").
attr("stroke-opacity", "0.1").
transition().
delay(function(d, i) { return i * 50}).
duration(700).
attr("x", -10).
attr("width", width + 20).
attr("fill-opacity", "0");
var hourClickDemo = clickGroup.selectAll("rect.hourClickDemo").
data(hourCrime).enter().
append("rect").
attr("id", function(d, i) { return "hourClick" + i; }).
attr("x", function(d, i) {return xScale(new Date(2012, 0, 1, i));}).
attr("y", -20).
attr("rx", 5).
attr("ry", 5).
attr("width", function(d, i) {return xScale(new Date(2012, 0, 1, 1)) - xScale(new Date(2012, 0, 1, 0))}).
attr("height", height + 40).
attr("fill", function (d, i) { return curveColor[d.hour];}).
attr("fill-opacity", "0.5").
attr("stroke", "white").
attr("stroke-width", "1px").
attr("stroke-opacity", "0.5").
transition().
delay(function(d, i) { return i * 50}).
duration(700).
attr("y", -10).
attr("height", height + 20).
attr("fill-opacity", "0");
var clickLength = 20;
var monthClick = clickGroup.selectAll("rect.monthClick").
data(monthCrime).enter().
append("rect").
attr("id", function(d, i) { return "monthClick" + i; }).
attr("x", -clickLength).
attr("y", function(d, i) {return yScale(new Date(2012, i, 1));}).
attr("rx", 5).
attr("ry", 5).
attr("width", width + (2 * clickLength)).
attr("height", function(d, i) {return yScale(new Date (2012, i, d.days)) - yScale(new Date (2012, i, 0))}).
attr("fill", function (d, i) { if(i == defaultMonth) {return curveColor[i];
} else {return "white";}}).
attr("fill-opacity", function (d, i) { if(i == defaultHour) {return 1;
} else {return 0;}}).
attr("stroke", "white").
attr("stroke-width", "1px").
attr("stroke-opacity", "0.1").
on("click", monthOnClick).
on("mouseover", monthOnHover).
on("mouseout", monthOnOut);
var hourClick = clickGroup.selectAll("rect.hourClick").
data(hourCrime).enter().
append("rect").
attr("id", function(d, i) { return "hourClick" + i; }).
attr("x", function(d, i) {return xScale(new Date(2012, 0, 1, i));}).
attr("y", -clickLength).
attr("rx", 5).
attr("ry", 5).
attr("width", function(d, i) {return xScale(new Date(2012, 0, 1, 1)) - xScale(new Date(2012, 0, 1, 0))}).
attr("height", height + (2 * clickLength)).
// attr("fill", function (d, i) {return curveColor[i];}).
// attr("fill-opacity", "1").
attr("fill", function (d, i) { if(i == defaultHour) {return curveColor[i];
} else {return "white";}}).
attr("fill-opacity", function (d, i) { if(i == defaultHour) {return 1;
} else {return 0;}}).
attr("stroke", "white").
attr("stroke-width", "1px").
attr("stroke-opacity", "0.1").
on("click", hourOnClick).
on("mouseover", hourOnHover).
on("mouseout", hourOnOut);
///////////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function monthOnHover(d) {
if (intentMonth.lastIndexOf(d.month) == -1) {
d3.select(this).attr("fill", function(d) {return curveColor[d.month]; })
.style("cursor", "pointer").attr("fill-opacity", "1");
}else {
d3.select(this).attr("fill", function(d) {return curveColor[d.month ]; })
.style("cursor", "pointer").attr("fill-opacity", "0.5");
}
}
function monthOnOut(d) {
if (intentMonth.lastIndexOf(d.month) == -1) {
d3.select(this).attr("fill", "white")
.attr("fill-opacity", "0");
}else {
d3.select(this).attr("fill", function(d) {return curveColor[d.month]; })
.attr("fill-opacity", "1");
}
}
function monthOnClick(d) {
// select
if(intentMonth.lastIndexOf(d.month) == -1) {
intentMonth.push(d.month);
d3.select(this).attr("fill", function(d) {return curveColor[d.month]; })
.attr("fill-opacity", "1");
} else {
intenMonth = remove(intentMonth, d.month);
d3.select(this).attr("fill", "white")
.attr("fill-opacity", "0");
}
d3.select("#monthGroup").remove();
drawMonthCurves(intentMonth);
}
function hourOnHover(d) {
if (intentHour.lastIndexOf(d.hour) == -1) {
d3.select(this).attr("fill", function(d) {return curveColor[d.hour]; })
.style("cursor", "pointer").attr("fill-opacity", "1");
}else {
d3.select(this).attr("fill", function(d) {return curveColor[d.hour]; })
.style("cursor", "pointer").attr("fill-opacity", "0.5");
}
}
function hourOnOut(d) {
if (intentHour.lastIndexOf(d.hour) == -1) {
d3.select(this).attr("fill", "white")
.attr("fill-opacity", "0");
}else {
d3.select(this).attr("fill", function(d) {return curveColor[d.hour]; })
.attr("fill-opacity", "1");
}
}
function hourOnClick(d) {
if(intentHour.lastIndexOf(d.hour) == -1) {
intentHour.push(d.hour);
d3.select(this).attr("fill", function(d) {return curveColor[d.hour]; })
.attr("fill-opacity", "1");
} else {
console.log("remove");
intenHour = remove(intentHour, d.hour);
d3.select(this).attr("fill", "white")
.attr("fill-opacity", "0");
}
d3.select("#hourGroup").remove();
drawHourCurves(intentHour);
}
function remove (array, element) {
var index = array.lastIndexOf(element);
array.splice(index, 1);
return array;
}
///////////////////////////////////////////////////////////////////
// create a group to hold the axis-related elements
var axisGroup = dayLength.append("svg:g").
attr("transform", "translate("+paddingX+","+paddingY+")");
// draw the x and y tick marks. Since they are behind the visualization, they
// can be drawn all the way across it. Because the <g> has been
// translated, they stick out the left side by going negative.
///////////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Title
axisGroup.append("svg:text").
attr("id", "title1").
text("Change in Crimes with daylight ").
attr("x", width / 2).
attr("y", -25).
// attr("dy", yDisplacement).
style("font-family", "sans-serif").
style("font-size", "15px").
style("text-anchor", "middle");
// attr("class", "axis yAxisLeft");
///////////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// y axis ���k���
axisGroup.selectAll(".yTicks").
data(firstMonthDates).
enter().append("svg:line").
attr("x1", -5).
attr("y1", yScale).
attr("x2", width + 5).
attr("y2", yScale).
attr("stroke", "lightgray").
attr("class", "yTicks");
// draw the text for the labels. Since it is the same on top and
// bottom, there is probably a cleaner way to do this by copying the
// result and translating it to the opposite side
//��label
axisGroup.selectAll("text.yAxisLeft").
data(midMonthDates).
enter().
append("svg:text").
text(function(d, i) { return monthNames[i]; }).
attr("x", -15).
attr("y", yScale).
attr("dy", yDisplacement).
attr("text-anchor", "end").
attr("class", "axis yAxisLeft");
//�klabel
axisGroup.selectAll("text.yAxisRight").
data(midMonthDates).
enter().
append("svg:text").
text(function(d, i) { return monthNames[i]; }).
attr("x", width + 15).
attr("y", yScale).
attr("dy", yDisplacement).
attr("text-anchor", "start").
attr("class", "yAxisRight");
// x axis �W�U���
axisGroup.selectAll(".xTicks").
data(d3.range(0,25)).
enter().append("svg:line").
// Round and add 0.5 to fix anti-aliasing effects (see above)
attr("x1", function(d) { return xScale(new Date(2012, 0, 1, d)); }).
attr("y1", -3).
attr("x2", function(d) { return xScale(new Date(2012, 0, 1, d)); }).
attr("y2", height + 3).
attr("stroke", "lightgray").
attr("class", "xTicks");
//�Wlabel
axisGroup.selectAll("text.xAxisTop").
data(d3.range(0, 25)).
enter().
append("svg:text").
text(yAxisLabel).
attr("y", -7).
attr("x", function(d) { return xScale(new Date(2012, 0, 1, d)); }).
attr("class", "xAxisTop").
attr("text-anchor", "middle");
//�Ulabel
axisGroup.selectAll("text.xAxisBottom").
data(d3.range(0, 25)).
enter().
append("svg:text").
text(yAxisLabel).
attr("y", height + 15).
attr("x", function(d) { return xScale(new Date(2012, 0, 1, d)); }).
attr("class", "xAxisBottom").
attr("text-anchor", "middle");
// create a group for the sunrise and sunset paths
var lineGroup = dayLength.append("svg:g").
attr("transform", "translate("+ paddingX + ", " + paddingY + ")");
// draw the background. The part of this that remains uncovered will
// represent the daylight hours.
// Background
lineGroup.append("svg:rect").
attr("x", 0).
attr("y", 0).
attr("height", height).
attr("width", width).
attr("fill", "lightblue");
// The meat of the visualization is surprisingly simple. sunriseLine
// and sunsetLine are areas (closed svg:path elements) that use the date
// for the x coordinate and sunrise and sunset (respectively) for the y
// coordinate. The sunrise shape is anchored at the top of the chart, and
// sunset area is anchored at the bottom of the chart.
// Sunrise
var sunriseLine = d3.svg.area().
y(function(d) { return yScale(d.date); }).
x0(0).
x1(function(d) { return xScale(new Date(2012, 0, 1, d.sunrise[0], d.sunrise[1])); }).
interpolate("linear");
lineGroup.append("svg:path").
attr("d", sunriseLine(sunData)).
attr("fill", "steelblue");
// Sunset
var sunsetLine = d3.svg.area().
x0(width).
x1(function(d) { return xScale(new Date(2012, 0, 1, d.sunset[0], d.sunset[1])); }).
y(function(d) { return yScale(d.date); }).
interpolate("linear");
lineGroup.append("svg:path").
attr("d", sunsetLine(sunData)).
attr("fill", "steelblue");
// Noon Line
lineGroup.append("svg:line").
attr("y1", 0).
attr("x1", d3.round(xScale(new Date(2012, 0, 1, 12))) + 0.5).
attr("y2", height).
attr("x2", d3.round(xScale(new Date(2012, 0, 1, 12))) + 0.5).
attr("stroke", "white");
// create a group for the sunrise and sunset background color
var gradientBackgroundGroup = dayLength.append("svg:g") ////
.attr("transform", "translate("+ paddingX + ", " + paddingY + ")");
console.log("test gradient" + xGradientScale(new Date(2012,0,1,23,59)));
// color gradient for loop
var defs = gradientBackgroundGroup.append("svg:defs")
.selectAll(".gradient")
.data(d3.range(0,sunData.length)).enter() ///
.append("svg:linearGradient")
// .attr('gradientUnits', 'userSpaceOnUse')
.attr("id", function(d, i) { return "gradient" + i; })
.attr("x1", "0%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "0%")
.selectAll("stop")
.data(getData)
.enter()
.append("stop")
.attr("offset", function(d) { return d.offset; })
.attr("stop-color", function(d) { return d.color; });
// get the color gradient map by given day
function getData(d) {
return getGradientData(sunData[d]) ;
}
// {date: new Date(2012, 1, 21), bTwilight: [6, 33], sunrise: [7, 04], sunTransit: [12, 23], sunset: [17, 43], eTwilight: [18, 14]},
// get the color gradient map by given day and night info array
function getGradientData(d) {
// console.log("time2per: %d : %d", d.bTwilight[0], d.bTwilight[1]);
var gradientData = [];
gradientData.push({offset: 0, color: "rgba(76,88,152,0.5)"}); // dark night
// night blue to dark blue
gradientData.push({offset: xGradientScale(new Date(2012, 0, 1, d.bTwilight[0], d.bTwilight[1])), color: "#718ec7"}); //start twilight
// darkblue to orange
gradientData.push({offset: xGradientScale(new Date(2012, 0, 1, d.sunrise[0], d.sunrise[1])), color: "#FF9B0D"}); //start sunrise
// orange to yellow (15 min)
gradientData.push({offset: xGradientScale(new Date(2012, 0, 1, d.sunrise[0], d.sunrise[1])) + 0.01, color: "#FFED00"}); //sun rising
// yellow to light blue (30 min)
gradientData.push({offset: xGradientScale(new Date(2012, 0, 1, d.sunrise[0], d.sunrise[1])) + 0.03, color: "#ADD5F7"}); //sunrise over
// light blue to white
gradientData.push({offset: xGradientScale(new Date(2012, 0, 1, d.sunTransit[0], d.sunTransit[1])), color: "white"}); //sun transit
// white to white
gradientData.push({offset: xGradientScale(new Date(2012, 0, 1, d.sunset[0], d.sunset[1])) - 0.03, color: "white"}); //start sunset
// white to yellow
gradientData.push({offset: xGradientScale(new Date(2012, 0, 1, d.sunset[0], d.sunset[1])), color: "#FFED00"}); //start sunset
// white to red
gradientData.push({offset: xGradientScale(new Date(2012, 0, 1, d.eTwilight[0], d.eTwilight[1])), color: "#FF9B0D"}); //sunset over
// red to dark blue
gradientData.push({offset: xGradientScale(new Date(2012, 0, 1, d.eTwilight[0], d.eTwilight[1])) + 0.01, color: "#718ec7"}); //sunset over
// dark blue to night blue
gradientData.push({offset: 1, color: "rgba(76,88,152,0.5)"}); // dark night
return gradientData;
}
// draw the gradient background
var rect = gradientBackgroundGroup.selectAll("rect").
data(sunData).
enter().
append("rect").
attr("x", 0).
attr("y", function(d, i) {return yScale(d.date);}).
attr("width", width).
attr("height", function(d, i) {return yScale(new Date (2012, 0, 3)) - yScale(new Date (2012, 0, 1))}).
attr("fill", function(d, i) {return "url(#gradient" + i + ")";});
// create a group for crime plot
var plotGroup = dayLength.append("svg:g").
attr("transform", "translate("+ paddingX + ", " + paddingY + ")");
// Plot Symbols
plotGroup.selectAll("path").
data(crimeData).
enter().
append("svg:path").
attr("d", d3.svg.symbol().
type("dot").
size(symbolSize)).
attr("transform", time2Position).
attr("stroke", "darkred").
attr("stroke-width", "0.5px").
attr("fill", "darkred").
attr("opacity","0.5");
function time2Position(d) {
return "translate(" + xScale(new Date(2012, 0, 1, d.Time[0], d.Time[1])) + "," + yScale(d.Date) + ")";
}
// Type: circle, cross, diamond, square, triangle-down, triangle-up
// Return Type by Crime type
function crimeType(d) {
switch(d.Crime) {
case "shooting":
return "dot";
break;
case "robbery":
return "cross";
break;
}
}
// Return Color by Crime type
function crimeColor(d) {
switch(d.Crime) {
case "shooting":
return "red";
break;
case "robbery":
return "yellow";
break;
}
}
///////////////// Variables & Scales /////////////////
var maxMonthlyCrime = d3.max(monthCrime, function(d) { return d3.max(d.crime); });
var maxHourCrime = d3.max(hourCrime, function(d) { return d3.max(d.crime); });
// console.log("testmaxHourCrime=" + maxHourCrime);
// Max lable value
var maxMonthYaxisValue = getMaxLabelValue(maxMonthlyCrime);
var maxHourXaxisValue = getMaxLabelValue(maxHourCrime);
// Scale factor
var monthLineYscale = d3.scale.linear().domain([0, getMaxLabelValue(maxMonthlyCrime)]).range([0, lineGraphHeight]);
var hourLineXscale = d3.scale.linear().domain([0, getMaxLabelValue(maxHourCrime)]).range([0, lineGraphHeight]);
console.log("read monthHourCrime, month= %d, crime= %d, max= %d", monthCrime[0].month, monthCrime[0].crime[11], maxMonthlyCrime);
// get Maximum quantity of Y-Axis
// If the maximum values of crime is 569
// returns 600
function getMaxLabelValue(d) {
var temp = d;
var countDigit = 0;
var ceiling = 1;
while(temp >= 10) {
temp = temp / 10;
countDigit++;
}
for(var j = 1; j <= (temp % 10); j++) {
ceiling++;
}
return ceiling * Math.pow(10, countDigit);
}
console.log("read monthHourCrime ceiling %d", getMaxLabelValue(maxMonthlyCrime));
////////Bottom Curve Graph
///////////////////////////////////
var monthPaddingX = paddingX;
var monthPaddingY = paddingY + height + 30;
// create a group to hold the axis-related elements of bottom graph
var axismonthCurveGroup = dayLength.append("svg:g").
attr("transform", "translate("+monthPaddingX+","+monthPaddingY+")");
///////////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Title
axismonthCurveGroup.append("svg:text").
attr("id", "titleBotom").
text("Monthly Crimes").
attr("x", width / 2).
attr("y", lineGraphHeight + 40).
// attr("dy", yDisplacement).
style("font-family", "sans-serif").
style("font-size", "15px").
style("text-anchor", "middle");
// attr("class", "axis yAxisLeft");
///////////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// x axis �W�U���
axismonthCurveGroup.selectAll(".xTicks").
data(d3.range(0,25)).
enter().append("svg:line").
// Round and add 0.5 to fix anti-aliasing effects (see above)
attr("x1", function(d) { return xScale(new Date(2012, 0, 1, d)); }).
attr("y1", -3).
attr("x2", function(d) { return xScale(new Date(2012, 0, 1, d)); }).
attr("y2", lineGraphHeight + 3).
attr("stroke", "lightgray").
attr("class", "xTicks");
//�Ulabel
axismonthCurveGroup.selectAll("text.xAxisBottom").
data(d3.range(0, 25)).
enter().
append("svg:text").
text(yAxisLabel).
attr("y", lineGraphHeight + 15).
attr("x", function(d) { return xScale(new Date(2012, 0, 1, d)); }).
attr("class", "xAxisBottom").
attr("text-anchor", "middle");
// y axis ���k���
axismonthCurveGroup.selectAll(".yTicks").
data(d3.range(0,11)).
enter().append("svg:line").
attr("x1", -5).
attr("y1", function(d) { return d * lineGraphHeight / 10}).
attr("x2", width + 5).
attr("y2", function(d) { return d * lineGraphHeight / 10}).
attr("stroke", "lightgray").
attr("class", "yTicks");
//��label
axismonthCurveGroup.selectAll("text.yAxisLeft").
data(d3.range(0, 1.1 * maxMonthYaxisValue , maxMonthYaxisValue / 10)).
enter().
append("svg:text").
text(function(d, i) { return d; }).
attr("x", -15).
attr("y", function(d, i) { return lineGraphHeight - i * lineGraphHeight / 10}).
attr("dy", yDisplacement).
attr("text-anchor", "end").
attr("class", "axis yAxisLeft");
//�klabel
axismonthCurveGroup.selectAll("text.yAxisRight").
data(d3.range(0, 1.1 * maxMonthYaxisValue , maxMonthYaxisValue / 10)).
enter().
append("svg:text").
text(function(d, i) { return d; }).
attr("x", width + 15).
attr("y", function(d, i) { return lineGraphHeight - i * lineGraphHeight / 10}).
attr("dy", yDisplacement).
attr("text-anchor", "start").
attr("class", "yAxisRight");
// opaque rect for decoration
axismonthCurveGroup.append("svg:rect").
attr("x", 0).
attr("y", 0).
attr("width", width).
attr("height", lineGraphHeight).
attr("fill", "white").
attr("fill-opacity", "0.8");
/////////////////////////////////////////////////
// create a group to draw curve elements of bottom graph
// var monthCurveGroup = dayLength.append("svg:g").
// attr("id", "monthGroup").
// attr("transform", "translate("+ monthPaddingX +","+ monthPaddingY +")");
//
// From filter about what month should be draws.
// 2 means Feb
// var intentMonth = [2, 3, 4, 6, 12];
function remove (array, element) {
var index = array.lastIndexOf(element);
array.splice(index, 1);
// console.log(array.toString());
return array;
}
// input: [2, 4, .....], 1 means January
// return: [
// { month: "Feb", crime: [.....]},
// { month: "Apr", crime: [.....]}, ]
function monthDrawData(d) {
var data = d.sort(function(a,b){return a-b});
// console.log("test sort" + data[1]);
var result = [];
for (var i = 0; i < data.length; i++) {
result.push(monthCrime[data[i]
]);
// console.log("result: " + result[i].monthName);
}
return result;
}
// var curveColor = ["Peru", "SteelBlue", "Violet", "Pink", "Orange", "Maroon", "LightSkyBlue", "LightCoral", "Khaki", "Lavender", "BurlyWood", "Azure", "plum",
// "Chartreuse", "Chocolate", "Coral", "DarkMagenta", "Fuchsia", "Gainsboro", "GhostWhite", "Indigo", "LemonChiffon", "Lime", "Linen" ,"OliveDrab"];
//
///////////call by filter
drawMonthCurves(intentMonth);
///////////call by filter
// input intentMonth;
function drawMonthCurves(d) {
var monthCurveGroup = dayLength.append("svg:g").
attr("id", "monthGroup").
attr("transform", "translate("+ monthPaddingX +","+ monthPaddingY +")");
for(var i = 0; i < monthDrawData(d).length; i++) {
var monthLegend = monthCurveGroup.append("svg:text").
text(monthDrawData(intentMonth)[i].monthName).
attr("x", xScale(new Date(2012, 0, 1, i , 30))).
attr("y", 12).
style("font-size", "10px").
style("fill", curveColor[monthDrawData(intentMonth)[i].month]).
style("text-anchor", "middle").
attr("dy", 0);
var monthCurveName = monthCurveGroup.append("svg:text").
text(monthDrawData(intentMonth)[i].monthName).