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
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
/* Copyright 2020 Google LLC. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/


var ttSel = d3.select('body').selectAppend('div.tooltip.tooltip-hidden')

var colors = {
  m: '#7DDAD3',
  f: '#9B86EF',
  h: '#F0BD80',
  l: '#FF777B',
  grey: '#ccc',
}


var totalWidth = width = d3.select('#graph').node().offsetWidth
var r = 40

var sel = d3.select('#graph').html('')
  .append('div')

var extraWidth = d3.clamp(500, innerHeight - 150, innerWidth - 500)
var scale = extraWidth/500
scale = 1
sel.st({transform: `scale(${scale})`, transformOrigin: '0% 0%'})

var c = d3.conventions({
  sel,
  totalWidth,
  totalHeight: totalWidth,
  margin: {left: 25, right: 7},
  layers: 'sd',
})
var divSel = c.layers[1]

c.x.domain([1, 4]).clamp(true).interpolate(d3.interpolateRound)
c.y.domain([1, 4]).clamp(true).interpolate(d3.interpolateRound)

c.xAxis.ticks(3).tickFormat(d3.format('.1f'))
c.yAxis.ticks(3).tickFormat(d3.format('.1f'))
d3.drawAxis(c)

var axis2Sel= c.svg.append('g.axis').append('line')
  .translate(Math.round(c.y(2)) + .5, 1)
  .at({x2: c.width, stroke: '#000', opacity: 0})

var meanGPADiff = .6

var seed = new Math.seedrandom('hii')
var students = d3.range(150).map((d, index) => {
  var collegeGPA = d3.randomUniform.source(seed)(1, 4)()

  // if (index == 93) collegeGPA = 2.05
  // if (index == 87) collegeGPA = 2.15
  // if (index == 32) collegeGPA = 2.25
  if (index == 131) collegeGPA = 3.9

  // var hsGPA = collegeGPA*d3.randomNormal(1, .4)()
  var hsGPA = collegeGPA + d3.randomNormal.source(seed)(meanGPADiff, .8)()
  var hsGPAadjusted = hsGPA - meanGPADiff

  var rand = d3.randomUniform.source(seed)(0, 1)

  var isMale = rand() < .5
  var name = names[isMale ? 'm' : 'f'][Math.floor(d/2)]
  var lastName = names.last[d]
  var maleOffset = rand()*(isMale ? 1 : -1)*.6

  // if (index == 47) name = 'Mia'
  // if (index == 82) name = 'Mason'


  var compGPA0 = lerp(hsGPAadjusted, collegeGPA, rand()*.7) + maleOffset
  var compGPA1 = lerp(compGPA0, collegeGPA + maleOffset, rand()*1.1)
  var compGPA2 = compGPA1 + rand()/4 - 1/4/2
  // var compGPA0 = collegeGPA + d3.randomNormal.source(seed)(0, .5)()
  // var compGPA1 = collegeGPA + d3.randomNormal.source(seed)(0, .3)()

  if (index == 69){
    compGPA1 = 2.0
  }
  if (index == 37){
    compGPA1 = 2.0
  }


  var isLowIncome = rand() < .5

  var inteviewGPA = collegeGPA + d3.randomNormal.source(seed)(0, .15)() 
  var inteviewGPAbias = inteviewGPA + rand()*(isLowIncome ? -1 : 1)*.5

  // if (index == 115) name = 'Mason'
  // if (index == 32) name = 'Mia'

  if (name == 'Camila') name = 'Mia'


  return {name, index, lastName, collegeGPA, hsGPA, hsGPAadjusted, compGPA0, compGPA1, compGPA2, isMale, isLowIncome, inteviewGPA, inteviewGPAbias}
})

students = _.sortBy(students, d => d.collegeGPA)

students = students.filter(d => {
  return d3.entries(d).every(({key, value}) => {
    if (!key.includes('GPA')) return true

    return 1 < value && value < 4.0
  })
})


c.svg.append('path')
  .at({
    d: ['M', 0, c.height, 'L', c.width, 0].join(' '),
    stroke: '#ccc',
    strokeWidth: 2,
    strokeDasharray: '4 2'
  })

!(function(){
  // return window.annotationSel = d3.select(null)
  var isDrag = 0
  if (!isDrag) annotations.forEach(d => d.text = d.html ? '' : d.text)
  if (isDrag){
    d3.select('#sections').st({pointerEvents: 'none'})
  }

  // copy('window.annotations = ' + JSON.stringify(annotations, null, 2))
  var swoopy = d3.swoopyDrag()
    .x(d => c.x(d.x))
    .y(d => c.y(d.y))
    .draggable(isDrag)
    .annotations(annotations)
    .on('drag', d => {

    })


  var htmlAnnoSel = divSel.appendMany('div.annotation', annotations.filter(d => d.html))
    .translate(d => [c.x(d.x), c.y(d.y)]).st({position: 'absolute', opacity: 0})
    .append('div')
    .translate(d => d.textOffset)
    .html(d => d.html)
    .st({width: 150})



  var swoopySel = c.svg.append('g.annotations').call(swoopy)

  c.svg.append('marker')
      .attr('id', 'arrow')
      .attr('viewBox', '-10 -10 20 20')
      .attr('markerWidth', 20)
      .attr('markerHeight', 20)
      .attr('orient', 'auto')
    .append('path')
      .attr('d', 'M-6.75,-6.75 L 0,0 L -6.75,6.75')

  swoopySel.selectAll('path')
    .attr('marker-end', 'url(#arrow)')
    .st({'opacity': d => d.path == 'M 0 0' ? 0 : 1})
  window.annotationSel = swoopySel.selectAll('g')
    .st({fontSize: 12, opacity: d => d.slide == 0 ? 1 : 0})

  window.annotationSel = d3.selectAll('g.annotations g, div.annotation')

  swoopySel.selectAll('text')
    .each(function(d){
      d3.select(this)
        .text('')                        //clear existing text
        .tspans(d3.wordwrap(d.text, d.width || 20), 13) //wrap after 20 char
    })  
  })()



students = _.sortBy(students, d => d.collegeGPA)
var lineSel = c.svg.appendMany('path', students)
  .translate(d => [c.x(d.hsGPA), c.y(d.collegeGPA)])
  .at({
    // fill: d => d.hsGPA > d.collegeGPA ? 'blue' : 'orange',
    fill: '#eee',
    stroke: '#aaa',
    strokeWidth: .5,
    opacity: 0,
    // strokeWidth: 1/scale,
  })


var circleSel = c.svg.appendMany('g', students)
  .translate(d => [c.x(d.collegeGPA), c.y(d.hsGPA)])
  .call(d3.attachTooltip)
  .on('mouseover', d => {
    var html = ''
    html += `<div><b>${d.name} ${d.lastName}</b></div>`

    if (curSlide.circleFill == 'gender'){
      html += `<span style='background: ${colors[d.isMale ? 'm' : 'f']}'>${d.isMale ? 'Male' : 'Female'}</span>`
    }

    if (curSlide.circleFill == 'income'){
      html += `<span style='background: ${colors[d.isLowIncome ? 'l' : 'h']}'>${d.isLowIncome ? 'Low Income' : 'High Income'}</span>`
    }
    html += `
      <div><b>${d3.format('.2f')(d[curSlide.yKey]).slice(0, 4)}</b> ${curSlide.index ? 'Predicted' : 'High School'} GPA</div>
      <div><b>${d3.format('.2f')(d.collegeGPA).slice(0, 4)}</b> College GPA</div>`

    ttSel.html(html)
  })


var innerCircleSel = circleSel.append('circle')
  .at({
    r: 5,
    fill: '#eee',
    stroke: '#aaa'
  })

// var textSel = circleSel.append('text').text(d => d.isMale ? 'M' : 'F')
//   .at({textAnchor: 'middle', dy: '.33em', fontSize: 8, fill: '#eee'})
// var textSel2 = circleSel.append('text').text(d => d.isLowIncome ? 'L' : 'H')
//   .at({textAnchor: 'middle', dy: '.33em', fontSize: 8, opacity: 0})


c.svg.select('.y').selectAll('line').filter(d => d == 4)
  .remove()
c.svg.select('.y').selectAll('text').filter(d => d == 4)
  .select(function() {
    return this.parentNode.insertBefore(this.cloneNode(1), this.nextSibling);
  })
  .text('Actual College GPA')
  .at({x: c.width/2, y: c.height + 35, textAnchor: 'middle', fontWeight: 800})

var yLabelSel = divSel.st({pointerEvents: 'none'}).append('div.axis')
  .html('<b>High School GPA</b>')
  .translate([0, -9])
  .st({textAlign: 'left', maxWidth: 260})

// c.svg.append('text').text('Actual College GPA').st({fontWeight: 800})

var longLabel = 'high school GPA, essay, clubs, zip code, teacher recommendations, sports, AP scores, demonstrated interest, gender, SAT scores, interviews, portfolio, race, work experience'

var slides = [
  {
    yKey: 'hsGPA',
    isLineVisible: 0,
    yLabel: '<b>High School GPA</b>',
    circleFill: 'grey',
    circleFillDelay: d => 0,
  },

  {
    yKey: 'hsGPA',
    isLineVisible: true,
    yLabel: '<b>High School GPA</b>'
  },

  {
    yKey: 'hsGPAadjusted',
    yLabel: 'high school GPA'
  },

  {
    yKey: 'compGPA0',
    yLabel: 'high school GPA, essay, clubs, zip code'.replace('essay', '<span class="highlight blue">essay') + '</span>'
  },

  {
    yKey: 'compGPA1',
    yLabel: longLabel.replace('teacher', '<span class="highlight blue">teacher') + '</span>',
    circleFill: 'grey',
    circleFillDelay: d => 0,
    textFill: '#eee',
  },

  {
    yKey: 'compGPA1',
    yLabel: longLabel,
    circleFill: 'gender',
    circleFillDelay: (d, i) => i*20 + (d.isMale ? 0 : 2000),
    textFill: '#000',
  },

  {
    name: 'proxyHighlight',
    yKey: 'compGPA2',
    yLabel: longLabel,
    circleFill: 'gender',
    circleFillDelay: d => 0,
    textFill: '#000',
  },

  {
    textFill: '#eee',
    yLabel: 'Alumni interview',
    yKey: 'inteviewGPAbias',
    circleFill: 'grey',
    text2Opacity: 0,
  },

  {
    textFill: '#eee',
    yLabel: 'Alumni interview',
    yKey: 'inteviewGPAbias',
    circleFill: 'income',
    circleFillDelay: (d, i) => i*20 + (!d.isLowIncome ? 2000 : 0),
    text2Opacity: 1,
  },

  {
    textFill: '#eee',
    yLabel: 'Alumni interview, household income'.replace('household', '<span class="highlight blue">household') + '</span>',
    yKey: 'inteviewGPA',
    text2Opacity: 1,
  },
]

slides.forEach(d => {
  if (d.name == 'proxyHighlight'){
    var proxies = 'clubs, interviews, portfolio, sports'.split(', ')
    d.yLabel = d.yLabel
      .split(', ')
      .map(d => {
        if (d == 'gender') return `<span class='strikethrough'>gender</span>`
        if (!proxies.includes(d)) return d

        return `<span class='highlight yellow'>${d}</span>`
      })
      .join(', ')
  }


  if (d.yLabel[0] != '<') d.yLabel = '<b>Predicted College GPA</b> using ' + d.yLabel.replace('School', 'school')
})

var keys = []
slides.forEach(d => keys = keys.concat(d3.keys(d)))
_.uniq(keys).forEach(str => {
  var prev = null
  slides.forEach(d => {
    if (typeof(d[str]) === 'undefined'){
      d[str] = prev
    }
    prev = d[str]
  }) 
})

slides.forEach((d, i) => {
  d.circleFillFn = {
    grey: d => '#eee',
    gender: d => d.isMale ? colors.m : colors.f,
    income: d => d.isLowIncome ? colors.l : colors.h,
  }[d.circleFill]

  d.index = i
})




var gs = d3.graphScroll()
  .container(d3.select('.container-1'))
  .graph(d3.selectAll('container-1 #graph'))
  .eventId('uniqueId1')
  .sections(d3.selectAll('.container-1 #sections > div'))
  .offset(innerWidth < 900 ? 300 : 520)
  .on('active', updateSlide)


var prevSlide = -1
function updateSlide(i){
  var slide = slides[i]
  if (!slide) return
  curSlide = slide
  var {yKey} = slide

  lineSel.transition('yKey').duration(500)
    .at({
      d: d => [
        'M 5 0', 
        'C 0 0',  
        0, c.y(d['collegeGPA']) - c.y(d[yKey]),
        0, c.y(d['collegeGPA']) - c.y(d[yKey]),
        'S 0 0 -5.5 0'
      ].join(' ')
    })
    .translate(d => [c.x(d.collegeGPA), c.y(d[yKey])])


  circleSel.transition('yKey').duration(500)      
    .translate(d => [c.x(d.collegeGPA), c.y(d[yKey])])

  innerCircleSel.transition('colorFill').duration(30)
    .delay(slide.circleFillDelay)
    .at({
      fill: slide.circleFillFn, 
      stroke: d => d3.color(slide.circleFillFn(d)).darker(1.5)
    })

  axis2Sel.transition()
    .st({opacity: i == 5 ? 1 : 0})

  lineSel.transition('opacity').duration(500)
    .st({
      opacity: slide.isLineVisible ? 1 : 0
    })

  if (slide.yLabel) yLabelSel.html(slide.yLabel)


  annotationSel.transition()
    .st({opacity: d => i == d.slide ? 1 : 0})



  prevSlide = i
}

slide = slides[0]




d3.selectAll('.circle').each(function(){
  var d = d3.select(this).attr('class').split(' ')[0]

  d3.select(this)
    .st({
      backgroundColor: d3.color(colors[d]),
      borderColor: d3.color(colors[d]).darker(1.5),
    })

  
})




function lerp(a, b, t){ return a + t*(b - a) }



c.svg.selectAll('g.annotations').raise()



d3.selectAll('#sections img').attr('aria-hidden', true)