waffleChart = {
let total = 16,
rectDim = {width : 12, height: 4},
rectVal = total/(rectDim.width * rectDim.height),
waffDim = {width : 10, height: 20},
gap = {horizontal:15, vertical: 10},
wafHeight = height *6.5,
data_ = [];
const wafDatRemap = (data) =>
{
let prevSource = null,
sourceCount = 0,
currentRow = 1,
currentCol = 1,
maxCols = 40;
data.forEach((d,i)=>{
if (prevSource === d.source){
sourceCount += 1
}
else{
sourceCount += 1
for (let index = 1; index < sourceCount+1; ++index) {
data_.push({
source : d.source,
prize_count: sourceCount,
value : d.value,
xPos : index < maxCols ? index : 1,
yPos : index < maxCols ? currentRow : currentRow + 1 < sourceCount ? currentRow += 1 : currentRow += 2
})
}
prevSource = d.source
sourceCount = 0
currentCol = 1
currentRow += 1
}
})
}//------------------------------------------------------------------------------------------------------------- fxn1✅
wafDatRemap(waffleData)
//console.log(waffleData,data_)
let wafColor = d3.scaleOrdinal()
.domain(data_)
.range(g_colors), //-------------------------------------------------------------------- color✅
container = d3.create('svg')
.attr('class', 'chart')
.attr('width', width)
.attr('height', wafHeight)
.style('font-family', 'OpenSans')
.style('font-size', '.8rem')
.style('background-color','grey'),//-------------------------------------------------- container✅
waffle = container.append('g')
.attr('class','#waffleContainer')
.attr('transform',`translate(${margin.left},${margin.top*2+margin.bottom})`)
.attr('width',width)
.attr('height',wafHeight)
.append('g')
.selectAll('rect')
.data(data_)
.enter().append('rect')
.attr('width', rectDim.width)
.attr('height', rectDim.height)
.attr('fill',d => wafColor(d.source))
.attr('rx', 1)
.attr('ry', 10)
.attr('x',(d,i) => d.xPos*gap.horizontal)
.attr('y', (d,i) => d.yPos*gap.vertical),
yAxis = container.append('g')
.attr('class','#waffleContainer')
.attr('transform',`translate(-30,${margin.top*2+11+margin.bottom})`)
.attr('width',width)
.attr('height',wafHeight)
.append('g')
.selectAll('text')
.data(data_)
.enter().append('text')
.attr("font-family", "OpenSans")
.attr('font-size','15')
.attr('font-weight','200')
.attr('fill','#444444')
.attr('x',margin.left)
.attr('y', (d,i) => d.yPos*gap.vertical)
.text(d => d.value),//------------------------------------------------------------------- waffle✅
legend = container.append('g')
.attr('class','#legend')
.attr('transform',`translate(${width-3*(margin.right+margin.left+margin.left)}, ${wafHeight/6})`)
.attr('width', width)
.attr('height', wafHeight)
.append('g')
.selectAll('div')
.data(data_.map(d=>d.source).filter((value, index, array) => array.indexOf(value) === index))
.enter()
.append('g')
.attr('transform',(d,i)=> 'translate(20,'+i*(margin.top)+')'),
title = container.append('g')
.attr('class','#title')
.append('g')
.attr('transform','translate(100,0)'),
writeUp = container.append('g')
.attr('class','#write_up')
.attr('max-width',width/2)
.attr('max-height',wafHeight/2)
.attr('transform',`translate(${width/6},${wafHeight- margin.bottom})`),
conclusion = 'There are 16 institutions which award literary prizes. 17 prizes were awarded by Columbia university.'; //while a single prize was awarded by 5 other institutions.';
legend.append('rect')
.attr('width', rectDim.width)
.attr('height', rectDim.height)
.attr('rx', 1)
.attr('ry', 10)
.style('fill', (d,i)=>wafColor(i))
legend.append('text')
.attr('x',rectDim.width+5)
.attr('y',rectDim.height-1)
.text(d => d) //---------------------------------------------------------------------------------------- legend✅
title.append('text')
.attr('transform',`translate(${margin.left*4},${margin.top*2})`)
.attr("font-family", "OpenSans")
.attr('domainant-baseline','middle')
.text(' Prize awarding institutions')
.attr('font-size','30')
.attr('font-weight','600')
.attr('fill','#444444'); //------------------------------------------------------------------------------- title✅
/*writeUp.append('text')
.attr("font-family", "OpenSans")
.attr('domainant-baseline','middle')
.text(conclusion.split('.'))
.attr('font-size','15')
.attr('font-weight','300')
.attr('max-width',width/2)
.attr('max-height',wafHeight/2)
.attr('fill','#444444'); */
return container.node()
}