/* 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 += `