display = {
const getX = (percentile) => {
return (150*percentile) / 100;
};
const getTextXValue = (x) => {
const stringLength = Math.floor((x/150)*100).toString().length;
switch (stringLength) {
case 1: return x + 7;
case 2: return x + 4;
default: return x;
}
};
const lineDecorators = [
{ cx: getX(0), cy: 30 },
{ cx: getX(50), cy: 30 },
{ cx: getX(100), cy: 30 },
{ cx: getX(0), cy: 60 },
{ cx: getX(50), cy: 60 },
{ cx: getX(100), cy: 60 },
{ cx: getX(0), cy: 90 },
{ cx: getX(50), cy: 90 },
{ cx: getX(100), cy: 90 },
{ cx: getX(0), cy: 120 },
{ cx: getX(50), cy: 120 },
{ cx: getX(100), cy: 120 },
];
const percentileData = [
{ label: 'Exit Velocity', cx: getX(63), cy: 30 },
{ label: 'xBA', cx: getX(29), cy: 60 },
{ label: 'K%', cx: getX(40), cy: 90 },
{ label: 'Sprint Speed', cx: getX(5), cy: 120 }
];
const svg = d3.create('svg')
.attr('width', 170)
.attr('height', 150);
const lines = svg.selectAll('line')
.data(percentileData)
.enter()
.append('line')
.style('stroke', 'rgb(155, 155, 155)')
.style('stroke-width', 2)
.attr('x1', 10)
.attr('x2', 160)
.attr('y1', d => d.cy)
.attr('y2', d => d.cy);
const decorators = svg.selectAll('#decor')
.data(lineDecorators)
.enter()
.append('circle')
.attr('id', 'decor')
.attr('cx', d => d.cx + 10 )
.attr('cy', d => d.cy)
.attr('r', 3)
.attr('fill', 'rgb(155, 155, 155)');
const circles = svg.selectAll('.ranks')
.data(percentileData)
.enter()
.append('circle')
.attr('class', 'ranks')
.attr('cx', d => d.cx + 10 )
.attr('cy', d => d.cy )
.attr('r', 10)
.attr('fill', d => d3.interpolateRdBu(1-(Math.floor((d.cx/150)*100) / 100)))
.attr('stroke', 'black');
const statLabels = svg.selectAll('.labels')
.data(percentileData)
.enter()
.append('text')
.attr('class', 'labels')
.attr('x', 5)
.attr('y', d => d.cy - 12)
.text(d => d.label)
.attr('font-family', 'sans-serif')
.attr('font-weight', 'bolder')
.attr('font-size', '.6rem')
.attr('fill', 'black');
const text = svg.selectAll('.percentRank')
.data(percentileData)
.enter()
.append('text')
.attr('class', 'percentRank')
.attr('x', d => getTextXValue(d.cx))
.attr('y', d => d.cy + 3)
.text(d => Math.floor((d.cx/150)*100))
.attr('font-weight', 'bolder')
.attr('font-size', '.7rem')
.attr('fill', d => (Math.floor((d.cx/150)*100) >= 70 ||
Math.floor((d.cx/150)*100) <= 30)
? 'white' : 'black');
return svg.node()
}