-
Notifications
You must be signed in to change notification settings - Fork 0
/
detail.js
868 lines (731 loc) · 24.6 KB
/
detail.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
/*
* stacked bar chart inspired from http://bl.ocks.org/mbostock/3886208
*/
//-----------Global Variables--------------------
var sites =[];
var stats_filter = {};
var details_filter = {};
var details;
var stats;
//SVG variables
//Measurements for small charts
var sm_margin = {top:5,right:10,bottom:10,left:10};
var sm_width = 200 - sm_margin.left - sm_margin.right;
var sm_height = 200 - sm_margin.top - sm_margin.bottom;
//measurements for large charts
var margin = {top:0,right:250,bottom:50,left:100};
var width = 1060 - margin.left - margin.right;
var height = 500 - margin.top - margin.bottom;
//Dictionary of English names for crisis values
var crisis_dict = {"turkish-revolt": "A Revolt in Turkey","pakistan-drought": "Drought in Pakistan", "ukraine-protest":"Protests in Ukraine", "haiyan":"Typhoon Haiyan"};
//--------------Scales--------------------------
//Stacked bar graph scales
var xScale_l, yScale_l,
color_l = d3.scale.ordinal().range(d3.scale.category20().range().concat(d3.scale.category20b().range().slice(0,3)));
//grouped bar graph scales
var xScale_s, yScale_s, yScale_s;
var parse = d3.time.format("%Y-%m-%d").parse;
var format = d3.time.format("%B");
//------------------End Global Variable declarations----------------------
//----------- Use Data -------------------------
/**
* Use data.
* @param error
* @param data
* @param crisisSummary
*/
function useData(error, data,crisisSummary){
/* Crisis summary */
// make summary global for use in tab changes
summary = crisisSummary;
// update crisis info section
resetSummary(summary);
var keys = Object.keys(crisis_dict);
details = data[0];
stats = data[1];
//build cross filter objects
//Populate Crossfilter objects
$(keys).each(function(i,k){
//construct stats key
key = k;
stats_key = k+"_stats";
details_key = k + "_details"
stats_filter[key] = crossfilter();
details_filter[key] = crossfilter();
//retrieve list of stats objects and keys
stats_objs = stats[stats_key];
sobj_keys = Object.keys(stats_objs);
//retrieve list of details objects and keys
d_objs = details[details_key];
dobj_keys = Object.keys(d_objs);
//loop over keys and constuct crossfilter records
s_records = [];
$(sobj_keys).each(function(i,k){
//create record for stats
row = stats_objs[k];
s_records.push({
"crisis": key,
"c_articles": row.crisis_count,
"baseline" : row.all_count,
"date_start" : row.date_start,
"date_end" : row.date_end,
"id" : row.id,
"type": row.type,
"domain" : row.name,
"month" : format(parse(row.date_start))
});
});
d_records = [];
$(dobj_keys).each(function(i,k){
//create record for stats
row = d_objs[k];
d_records.push({
"crisis": row.collection,
"domain": row.name,
"title" : row.title,
"type" : row.type,
"url" : row.url,
"date_start" : row.date_start,
"date_end" : row.date_end
});
});
stats_filter[key].add(s_records);
details_filter[key].add(d_records);
});
//---------End Loop -----------------
clear_charts();
clear_stacks();
build_initial_charts();
stack_chart();
}
//------------end useData()------------------------
/**
* Clear stacks.
*/
function clear_stacks(){
d3.select("svg").remove();
$('#stacked').empty();
$('#source').empty();
}
/**
* Clear charts.
*/
function clear_charts(){
$('.ui-dialog').hide();//use dialog parent container
d3.select("#traditional svg").remove();
d3.select("#independent svg").remove();
d3.select("#blog svg").remove();
$('#traditional').empty();
$('#independent').empty();
$('#blog').empty();
//$('#cloud').empty();
//$('#cloud').append("traditional");
//TODO: RE-ENABLE WHEN SUB-CHART IS IN USE AGAIN
// $('#traditional').append('<div class="overview_title">Traditional</div><br>');
// $('#independent').append('<div class="overview_title">Independent</div><br>');
// $('#blog').append('<div class="overview_title">Blog-Social</div><br>');
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PRIMARY CHART (I.E. STACK CHART)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//----------------------------------------------
/**
* Build initial charts.
*/
function build_initial_charts(){
//build out scales
xScale_l = d3.scale.ordinal().rangeRoundBands([0, width],.1);
yScale_l = d3.scale.pow().exponent(0.38).rangeRound([height, 10]);
//setup svgs
svg = d3.select("#stacked").append("svg")
.attr("width",width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate("+margin.left +","+margin.top+")");
}
//------------Build Stacked Chart -----------------
/**
* Stack Chart.
*/
function stack_chart(){
var crisis = window.crisis_select.value;
//Create data set for selected crisis
var dset = stats[crisis+"_stats"];
var data = [];
var keys = Object.keys(dset);
var names = [];
var months = [];
$(keys).each(function(i,k){
var obj = dset[k];
var source = obj.name;
var date = format(parse(obj.date_start));
if(jQuery.inArray(source, names) < 0) {
// console.log("...pushing name: "+source);
names.push(source);
}
if(jQuery.inArray(date,months)<0) {
// console.log("...pushing month: "+date);
months.push(date)
}
});
//Remove Google as it skews everything
var g_index = names.indexOf("Google");
if (g_index > -1) {
names.splice(g_index, 1);
}
/* Set the color domain */
color_l.domain(names);
crisis_filter = stats_filter[crisis];
mfilters = {};
$(months).each(function (i,k){
var dim = crisis_filter.dimension(function (d){return d.month});
var dim_filtered = dim.filter(k);
mfilters[k] = dim_filtered.top(Infinity);
//clear all filters
dim_filtered.filterAll();
});
$(months).each(function (i,k){
var total = 0;
set = mfilters[k];
var date = parse(set[0].date_start);
var label = format(date);
var source_counts = {};
var mappings = [];
$(set).each(function(i,k){
if(k.domain != 'Google'){
var y0 = 0;
var articles = parseInt(k.c_articles);
//check for nan
if(!(articles==articles))
articles = 0;
source_counts[k.domain] = articles;
total += articles;
// mappings.push({
mappings[label] = {
"label" : label,
"name" : k.domain,
"baseline" : k.baseline,
"y0" : y0,
"y1" : articles
};
data.push({
"sources":source_counts,
"mappings":mappings,
"x":label,
"total":total});
}
});
})
//------------------------------------------
/* In order to get the months correct, need to reverse the value instead of pull it directly */
var xMap = data.map(function (d) {return d.x;});
var orderX = xMap.slice().reverse();
xScale_l.domain(orderX);
//yScale domain is correct.
yScale_l.domain([0, d3.max(data, function(d){return d.total;})]);
/* Since the data repeats so much, we need to figure out the max total per month and just use that. */
var tmpData = [];
months.forEach(function(month){
// console.log("--- Finding Max for Month '"+month+"' ---");
data.forEach(function(monthData){
if (monthData && monthData.x && monthData.x === month) {
if (!tmpData[month] || tmpData[month].total < monthData.total) {
// console.log("... new top total: "+monthData.total);
tmpData[month] = monthData;
}
}
});
});
/* NOTICE: RE-ASSIGNING 'data' OBJECT */
console.log("--- SHOW ME WHATCHA WORKIN WITH ---");
data = [];
months.forEach(function(month){
console.log(month);
data.push(tmpData[month]);
});
console.log(data);
/* APPEND SOME y0 and y1 Info */
data.forEach(function(d) {
var y0 = 0;
d.ySites = color_l.domain().map(function(name) {
var resultCount = +d.sources[name];
return {label: d.x, name: name, baseline: +d.mappings[d.x].baseline, results: resultCount, y0: y0, y1: y0 += resultCount};
});
d.yMax = d.ySites[d.ySites.length - 1].y1;
});
// data.sort(function(a, b) { return b.total - a.total; });
// Initialize tooltip
var tip = d3.tip()
.html(function(d) {
var p = ""+(d.results/ d.baseline)*100;
var percentVal = p.slice(0,p.indexOf("."));
if (percentVal === "0") percentVal = "<1";
return "<span class='sourceName' style='color:" + color_l(d.name) + ";'>" + d.name + ": </span>" + numberWithCommas(d.results) + " results for "+ d.label+"<br><span class='tip'>Baseline: " + numberWithCommas(d.baseline) + " - " + percentVal + "% of coverage (click for more)</span>";
})
.direction('e')
.attr('class','d3-tip e');
var series = svg.selectAll(".series")
.data(data)
.enter().append("g")
.attr("class", "series")
.attr("transform", function (d) {return "translate("+xScale_l(d.x)+",0)";});
series.selectAll("rect")
.data(function (d) {return d.ySites;})
.enter().append("rect")
.attr("width", "70px")
.attr("y", function(d) { return yScale_l(d.y1);})
.attr("height", function(d) { return yScale_l(d.y0) - yScale_l(d.y1); })
.style("fill", function(d){ return color_l(d.name);})
.on("click", function (d){
console.log("--- d for month ---");
console.log(d);
buildAndShowSubChartDialog(d.label, d.name);
})
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
series.call(tip);
//Add Legends
var legend = svg.selectAll(".legend")
.data(names.slice())//removed .reverse()
.enter().append("g")
.attr("class", "legend")
.attr("transform", function (d, i) {
return "translate("+(margin.right)+"," + i *15 + ")";
});
legend.append("rect")
.attr("x", width - 120)
.attr("width", 10)
.attr("height", 10)
.style("fill", color_l)
.style("stroke", "grey");
legend.append("text")
.attr("x", width - 130)
.attr("y", 6)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function (d) { return d; })
.attr("fill", function(d){ return color_l(d); });
//Add Axes
var xAxis = d3.svg.axis()
.scale(xScale_l)
.orient("bottom")
.tickSize(6, 0, 0);
var yAxis = d3.svg.axis()
.scale(yScale_l)
.orient("left");
svg.append("g")
.attr("class", "axis")//instead of x axis
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "axis")//instead of y axis
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 3)
.attr("dy", ".71em")
.style("text-anchor", "end")
.style("font-size","11px")
.text("Articles Published Related to Crisis");
svg.selectAll('.axis line, .axis path')
.style({'stroke': '#CCC', 'fill': '#CCC', 'stroke-width': '1px'});
svg.selectAll('.axis text')
.style({'fill': '#CCC'});
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sub-Chart & Table (DIALOG)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Entry-point method when a bar is clicked.
* @param month
* @param siteName
*/
function buildAndShowSubChartDialog(month,siteName){
var crisis = window.crisis_select.value;
$(function() {
$( "#deeper_dialog" ).dialog({
height: 500,
width: 1000,
modal: false
});
});
$('.ui-dialog').show();//use dialog parent container
//update the data table
show_table(month,siteName);
//create new filter for data in month
var crisis_filter = stats_filter[crisis];
var dim = crisis_filter.dimension(function (d){return d.month});
var dim_filtered = dim.filter(month);
var urows = dim_filtered.top(Infinity);
dim_filtered.filterAll();
//BUILD THE CHARTS IN SEQUENCE, LETTING EACH ONE CALL THE NEXT !!!
//TODO: FIGURE OUT THE BASELINE CHART
// trad_group(urows);
}
//-------Show Table -----------------------
/**
* Show Table
* @param month
* @param siteName
*/
function show_table(month,siteName){
var crisis = window.crisis_select.value;
$('#source').empty();
$('#source').append(
"<table id='source_table' class='display'></table>");
//get data and create filter
var crisis_filter = details_filter[crisis];
var dim = crisis_filter.dimension(function (d){return d.month});
var dim_filtered = dim.filter(month);
var urows = dim_filtered.top(Infinity);
dim_filtered.filterAll();
//populate row
rows = [];
$(urows).each(function(i,k){
var anchor = "<a href ='" + k.url +"' target='_blank'>"+ k.title+"</a>";
rows.push([k.domain,anchor,month,k.date_start, k.date_end]);
});
var columns = [
{ "sTitle": "Domain" },
{ "sTitle": "Anchor"},
{ "sTitle": "Month" },
{ "sTitle": "Query Start Date" },
{ "sTitle": "Query End Date" }
];
var table = $('#source_table').dataTable( {
"aaData": rows,
"aoColumns": columns
} );
if (siteName && siteName.length > 0 && sites[siteName]){
console.log("... searching table for sample results from site: "+ siteName);
var tableFilters = $('#source_table_filter').find('input');
if (tableFilters && tableFilters.length > 0){
var tableFilter = tableFilters[0];
tableFilter.value = sites[siteName];
tableFilter.focus();
var e = $.Event('keyup');
e.keyCode= 13; // enter
$('input').trigger(e);
}
}
}
//------------Grouped bar graph code-----------------
/**
* SUB-CHART-2: Independent Group
* @param data
*/
function indi_group(data){
var crisis = window.crisis_select.value;
//-------Parse data----------------------------------
var type = 'Independent'
var final_data = [];
$(data).each(function (i,k){
//Check article's type
if(k.type == type){
var crisis_count = k.c_articles;
var baseline = k.baseline;
var label = k.domain;
var bars = [];
bars.push({"name":"Baseline", "value":baseline});
bars.push({"name":"Crisis Articles", "value":crisis_count});
final_data.push({"crisis_count":crisis_count,
"baseline" : baseline,
"label" : label,
"bars": bars
})
}
});
var keys = ["Baseline", "Crisis Articles"];
//----------Create SVG and scales ----------------
var m = {top: 20, right: 20, bottom: 30, left: 40};
var w = 300 - m.left - m.right;
var h = 300 - m.top - m.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, w], .1);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([h, 0]);
var color = d3.scale.ordinal()
.range(["red","blue"]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
var msvg = d3.select("#independent").append("svg")
.attr("width", w + m.left + m.right)
.attr("height", h + m.top + m.bottom)
.append("g")
.attr("transform", "translate(" + m.left + "," + m.top + ")");
//-------Set up Scale domains ------------
x0.domain(final_data.map(function(d) {return d.label;}));
x1.domain(keys).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(final_data, function(d) { return d3.max(d.bars, function(d) { return d.value; }); })]);
msvg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
msvg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Population");
var domains = msvg.selectAll(".domains")
.data(final_data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x0(d.label) + ",0)"; });
domains.selectAll("rect")
.data(function(d) { return d.bars; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("x", function(d) { return x1(d.name); })
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.style("fill", function(d) { return color(d.name); });
/* Legend */
var legend = msvg.selectAll(".legend")
.data(keys.slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", w - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", w - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
/** CALL SUB-CHART */
blog_group(data);
}
/**
* SUB-CHART-1: Traditional Group
* @param data
*/
function trad_group(data){
var crisis = window.crisis_select.value;
/** AS THIS IS THE FIRST IN THE SEQUENCE, CLEAR THE CHARTS. */
clear_charts();
//-------Parse data----------------------------------
var type = 'Traditional'
var final_data = [];
$(data).each(function (i,k){
//Check article's type
if(k.type == type){
var crisis_count = k.c_articles;
var baseline = k.baseline;
var label = k.domain;
var bars = [];
bars.push({"name":"Baseline", "value":baseline});
bars.push({"name":"Crisis Articles", "value":crisis_count});
final_data.push({"crisis_count":crisis_count,
"baseline" : baseline,
"label" : label,
"bars": bars
})
}
});
var keys = ["Baseline", "Crisis Articles"];
//----------Create SVG and scales ----------------
var m = {top: 20, right: 20, bottom: 30, left: 40};
var w = 300 - m.left - m.right;
var h = 300 - m.top - m.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, w], .1);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([h, 0]);
var color = d3.scale.ordinal()
.range(["red","blue"]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
var msvg = d3.select("#traditional").append("svg")
.attr("width", w + m.left + m.right)
.attr("height", h + m.top + m.bottom)
.append("g")
.attr("transform", "translate(" + m.left + "," + m.top + ")");
//-------Set up Scale domains ------------
x0.domain(final_data.map(function(d) {return d.label;}));
x1.domain(keys).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(final_data, function(d) { return d3.max(d.bars, function(d) { return d.value; }); })]);
msvg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
msvg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Population");
var domains = msvg.selectAll(".domains")
.data(final_data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x0(d.label) + ",0)"; });
domains.selectAll("rect")
.data(function(d) { return d.bars; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("x", function(d) { return x1(d.name); })
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.style("fill", function(d) { return color(d.name); });
/* Legend */
var legend = msvg.selectAll(".legend")
.data(keys.slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", w - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", w - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
console.log(data);
/** CALL SUB-CHART */
indi_group(data);
}
/**
* SUB-CHART-3: Blog-Social Group
* @param data
*/
function blog_group(data){
var crisis = window.crisis_select.value;
//-------Parse data----------------------------------
var type = 'Blogs-Social'
var final_data = [];
$(data).each(function (i,k){
//Check article's type
if(k.type == type){
var crisis_count = k.c_articles;
var baseline = k.baseline;
var label = k.domain;
var bars = [];
bars.push({"name":"Baseline", "value":baseline});
bars.push({"name":"Crisis Articles", "value":crisis_count});
final_data.push({"crisis_count":crisis_count,
"baseline" : baseline,
"label" : label,
"bars": bars
});
}
});
var keys = ["Baseline", "Crisis Articles"];
//----------Create SVG and scales ----------------
var m = {top: 20, right: 20, bottom: 30, left: 40};
var w = 300 - m.left - m.right;
var h = 300 - m.top - m.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, w], .1);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([h, 0]);
var color = d3.scale.ordinal()
.range(["red","blue"]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
var msvg = d3.select("#blog").append("svg")
.attr("width", w + m.left + m.right)
.attr("height", h + m.top + m.bottom)
.append("g")
.attr("transform", "translate(" + m.left + "," + m.top + ")");
//-------Set up Scale domains ------------
x0.domain(final_data.map(function(d) {return d.label;}));
x1.domain(keys).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(final_data, function(d) { return d3.max(d.bars, function(d) { return d.value; }); })]);
msvg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
msvg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Population");
var domains = msvg.selectAll(".domains")
.data(final_data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x0(d.label) + ",0)"; });
domains.selectAll("rect")
.data(function(d) { return d.bars; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("x", function(d) { return x1(d.name); })
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); })
.style("fill", function(d) { return color(d.name); });
/* Legend */
var legend = msvg.selectAll(".legend")
.data(keys.slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", w - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", w - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CRISIS SELECT
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function populateMediaSiteLookup(error, _sites){
_sites.forEach(function(d){
sites[d.site_name] = d.site_domain;
});
}
$('document').ready(function(){
addClassNameListener("crisis_select", function(){
var crisis = window.crisis_select.value;
console.log("### QUEUE NEW CRISIS ("+crisis+") AFTER CLASS CHANGE ###");
queue()
.defer(d3.json, "productiondata/con-data.json")//consolidated data
.defer(d3.csv, "/productiondata/"+crisis+"/summary.csv")//storypoints
.await(useData);
});
queue()
.defer(d3.tsv, "productiondata/media-sites.tsv")
.await(populateMediaSiteLookup);
});