-
Notifications
You must be signed in to change notification settings - Fork 2
/
proportion-graph-framework.js
161 lines (149 loc) · 5.78 KB
/
proportion-graph-framework.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
const baselines = [];
function getbaselines() {
pausetime = performance.now();
$.ajax({
url: baseURL,
error: function () {
startOver();
return;
},
data: {
all: 'true',
fo: 'json'
},
success: function( data ) {
let totalcount = data.search.hits;
for (var i = 0; i < proportionfacets.length; i++) {
let typefacets = data.facets.filter(obj => {
return obj.type === proportionfacets[i].typedesc;
});
let types = typefacets[0].filters
baselines.push([]);
let typename = proportionfacets[i].type;
$.each( types, function( key, value ) {
let typeconst = { 'count': value.count, 'proportion': value.count/totalcount };
typeconst[typename] = value.title;
baselines[i].push(typeconst);
});
//Build our target div...
$("#charts").append('<div class="card mb-3 facet-chart">');
$(".facet-chart").last().append('<div class="card-header">');
$(".facet-chart .card-header").last().append('Proportion of each <strong>' + capitalizeFirstLetter(typename) + '</strong> compared to that in the entire collection');
$(".facet-chart").last().append('<div class="card-body">');
$(".facet-chart .card-body").last().append('<div id="' + typename + '_proportions">');
}
}
});
}
function showFormatBaselineComps(searchkeys) {
for (p = 0; p < proportionfacets.length; p++) {
let type = proportionfacets[p].type
var alltypes = [];
var typeProportionsArray = [[type]];
let counttype = type + 'counts';
//grab all our search terms and formats first
for (i = 0; i < searchkeys.length; i++) {
var searchterm = searches[searchkeys[i]].term;
typeProportionsArray[0].push(searchterm);
var typecounts = searches[searchkeys[i]][counttype];
for (c = 0; c < typecounts.length; c++) {
if(alltypes.indexOf(typecounts[c][type]) === -1){
alltypes.push(typecounts[c][type]);
}
}
}
alltypes.sort();
for (i=0; i<alltypes.length; i++) {
var typevalues = [];
typevalues.push(alltypes[i]);
for (m=0; m<searchkeys.length; m++) {
var mysearchtype = searches[searchkeys[m]][counttype].filter(obj => {
return obj[type] === alltypes[i];
});
baselinecomp = baselines[p].filter(obj => {
return obj[type] === alltypes[i];
});
if (typeof mysearchtype[0] !== "undefined") {
var mycomparison = mysearchtype[0].proportion - baselinecomp[0].proportion;
} else {
var mycomparison = 0;
}
typevalues.push(mycomparison);
}
typeProportionsArray.push(typevalues);
}
var typeProportionsData = google.visualization.arrayToDataTable( typeProportionsArray );
var chartheight = (typeProportionsArray.length * 20) * searchkeys.length;
if (chartheight < 120) { chartheight = 120}
var typePropOptions = {
bars: 'horizontal',
height: chartheight,
//width: mywidth,
fontName: 'sans-serif',
hAxis: {
title: 'Relative Proportion',
maxValue: .7,
minValue: -.7,
viewWindow: {
max: .9,
min: -.9
}
},
vAxis: {
title: capitalizeFirstLetter(type)
}
};
var chart = new google.charts.Bar(document.getElementById(type + '_proportions'));
//Here comes the clicky bit...
function selectHandler() {
var selectedItem = chart.getSelection()[0];
if (selectedItem && selectedItem.row != null) {
var format = typeProportionsData.getValue(selectedItem.row, 0);
var term = typeProportionsData.getColumnLabel(selectedItem.column);
var searchroot = getSearchRoot(format);
var locurl = searchroot + term;
window.open(locurl,'_formatsearch');
}
}
google.visualization.events.addListener(chart, 'select', selectHandler);
chart.draw(typeProportionsData, google.charts.Bar.convertOptions(typePropOptions));
}
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function getSearchRoot(format) {
const formatlabels = [
{formatlabel: "newspaper", rootpart: "newspapers" },
{formatlabel: "manuscript/mixed material", rootpart: "manuscripts" },
{formatlabel: "book", rootpart: "books" },
{formatlabel: "photo, print, drawing", rootpart: "photos" },
{formatlabel: "archived web site", rootpart: "websites" },
{formatlabel: "film, video", rootpart: "film-and-videos" },
{formatlabel: "sound recording", rootpart: "audio" },
{formatlabel: "map", rootpart: "maps" },
{formatlabel: "notated music", rootpart: "notated-music" },
];
var selectedformat = formatlabels.filter(obj => {
return obj.formatlabel === format;
});
if (selectedformat.length > 0) {
var searchroot = "https://www.loc.gov/" + selectedformat[0].rootpart + "/?q=";
}
const origformatlabels = [
{formatlabel: "periodical", rootpart: "periodical" },
{formatlabel: "legislation", rootpart: "legislation" },
{formatlabel: "web page", rootpart: "web+page" },
{formatlabel: "event", rootpart: "event" },
{formatlabel: "personal narrative", rootpart: "personal+narrative" },
{formatlabel: "3d object", rootpart: "3d+object" },
{formatlabel: "software, e-resource", rootpart: "software,+e-resource" },
];
var selectedorigformat = origformatlabels.filter(obj => {
return obj.formatlabel === format;
});
if (selectedorigformat.length > 0) {
var searchroot = "https://www.loc.gov/search/?fa=original-format:" + selectedorigformat[0].rootpart + "&q=";
}
return searchroot;
}