{
let matrix = avg_matrix;
if(DataInfo == 'average'){
matrix = avg_matrix
}
if(DataInfo =='maximum'){
matrix = max_matrix
}
if(DataInfo =='minimum'){
matrix = min_matrix
}
if(DataInfo=='median'){
matrix = median_matrix
}
const margin = {top: 50, right: 120, bottom: 50, left: 120};
const visWidth = 500;
const visHeight = 500;
const svg = d3.create('svg')
.attr('width', visWidth + margin.left + margin.right)
.attr('height', visHeight + margin.top + margin.bottom);
const g = svg.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
g.append("text")
.attr("x", visWidth / 2)
.attr("y", -margin.top + 5)
.attr("text-anchor", "middle")
.attr("dominant-baseline", "hanging")
.attr("font-family", "sans-serif")
.attr("font-size", "16px")
.text("Titanic Fare Data by Travel Distance and Class Type");
// create scales
const x = d3.scalePoint()
.domain(Pclass)
.range([0,visHeight])
.padding(0.5)
const y = d3.scalePoint()
.domain(unique_embark)
.range([0,visWidth])
.padding(0.5)
// create and add axes
//define new tick for y-axis
const yText = {
"S": "Southampton",
"C": "Cherbourg",
"Q": "Queestown",
};
const xText = {
"1": "1st class",
"2": "2nd class",
"3": "3rd class",
};
const yAxis = d3.axisLeft(y).tickFormat(d => yText[d])
;
const xAxis = d3.axisBottom(x).tickFormat(d => xText[d])
;
// const xAxis = d3.axisBottom(x);
// const tooltip = d3.select('body')
// .append('div')
// .style('position', 'absolute')
// .style('z-index','10')
// .style('color','#3497db')
// .style('visibility','hidden')
// .style('font-size', '12px')
// .style('font-weight', 'bold')
// .text('test')
g.append("g")
.attr("transform", `translate(0, ${visHeight})`)
// add axis
.call(xAxis)
.call(g => g.select(".domain").remove())
// add grid lines
.call(g => g.selectAll('.tick line')
.clone()
.attr('stroke', '#d3d3d3')
.attr('y1', -visHeight)
.attr('y2', 0))
// add title
.append("text")
.attr("x", visWidth / 2)
.attr("y", 40)
.attr("fill", "black")
.attr("text-anchor", "middle")
.attr("font-size", "15px")
.text(" ");
g.append("g")
// add axis
.call(yAxis)
.call(g => g.select(".domain").remove())
// add grid lines
.call(g => g.selectAll('.tick line')
.clone()
.attr('stroke', '#d3d3d3')
.attr('x1', 0)
.attr('x2', visWidth))
// add title
.append("text")
.attr("x", -40)
.attr("y", visHeight / 2)
.attr("fill", "black")
.attr("dominant-baseline", "middle")
.attr("font-size", "15px")
.text(" ");
//draw circles to represent points
g.append("g")
.selectAll("circle")
.data(matrix)
.join("circle")
.attr("cx", d=>x(d.PClass))
.attr("cy", d=>y(d.Embarked))
.attr("fill", 'steelblue')
.attr("r", d=> point_radius(d.Fare))
.on('mouseenter', mouseEnter)
.on('mouseleave', mouseLeave);
// create tooltip
const tooltip = g.append('g')
.attr('visibility', 'hidden');
const tooltipHeight = 16;
// add a rectangle to the tooltip to serve as a background
const tooltipRect = tooltip.append('rect')
.attr('fill', 'black')
.attr('rx', 5)
.attr('height', tooltipHeight);
// add a text element to the tooltip to contain the label
const tooltipText = tooltip.append('text')
.attr('fill', 'white')
.attr('font-family', 'sans-serif')
.attr('font-size', 12)
.attr('y', 2) // offset it from the edge of the rectangle
.attr('x', 3) // offset it from the edge of the rectangle
.attr('dominant-baseline', 'hanging')
// handle hovering over a circle
function mouseEnter(event, d) {
// make the circle larger
d3.select(this)
.attr('r', point_radius(d.Fare) * 1.3);
// update the label's text and get its width
tooltipText.text(d.Fare);
const labelWidth = tooltipText.node().getComputedTextLength();
// set the width of the tooltip's background rectangle
// to match the width of the label, plus some extra space
tooltipRect.attr('width', labelWidth + 6);
// move the tooltip to the position of the circle (offset by a bit)
// and make the tooltip visible
const xPos = x(d.PClass) + point_radius(d.Fare) * 3;
const yPos = y(d.Embarked) - tooltipHeight / 2;
tooltip.attr('transform', `translate(${xPos},${yPos})`)
.attr('visibility', 'visible');
}
// handle leaving a circle
function mouseLeave(event, d) {
// reset the size of the circle
d3.select(this)
.attr('r', point_radius(d.Fare))
// make the tooltip invisible
tooltip
.attr('visibility', 'hidden');
}
return svg.node();
}