{
const chartWidth = 1000;
const chartHeight = 600;
const margin = { top: 80, right: 110, bottom: 60, left: 90 };
const targets = ['heartDisease', 'kidneyDisease', 'skinCancer'];
const factors = ['bmi', 'smoking', 'alcoholDrinking', 'stroke', 'physicalHealth',
'mentalHealth', 'diffWalking', 'sex', 'ageCategory', 'raceCategory',
'diabetic', 'physicalActivity', 'genHealth', 'sleepTime', 'asthma'];
const correlationMatrix = [];
targets.forEach(target => {
factors.forEach(factor => {
const correlation = pointBiserialCorrelation(data, target, factor);
correlationMatrix.push({target, factor, correlation});
});
});
const xScale = d3.scaleBand()
.domain(targets)
.range([margin.left, chartWidth - margin.right])
.padding(0.05);
const yScale = d3.scaleBand()
.domain(factors)
.range([margin.top, chartHeight - margin.bottom])
.padding(0.05);
const colorScale = d3.scaleSequential(d3.interpolateRdBu)
.domain([1, -1]);
const labelMap = {heartDisease: 'Heart Disease',
bmi: 'BMI',
smoking: 'Smoking',
alcoholDrinking: 'Alcohol Drinking',
stroke: 'Stroke',
physicalHealth: 'Physical Health',
mentalHealth: 'Mental Health',
diffWalking: 'Difficulty Walking',
sex: 'Sex',
ageCategory: 'Age Category',
raceCategory: 'Race',
diabetic: 'Diabetic',
physicalActivity: 'Physical Activity',
genHealth: 'General Health',
sleepTime: 'Sleep Time',
asthma: 'Asthma',
kidneyDisease: 'Kidney Disease',
skinCancer: 'Skin Cancer'
};
// Transform the label
const formatLabel = (label) => labelMap[label] || label;
// Create the SVG element for the heatmap
const svg = d3.create('svg')
.attr('width', chartWidth)
.attr('height', chartHeight);
// Create cells for the heatmap
svg.selectAll('rect')
.data(correlationMatrix)
.enter()
.append('rect')
.attr('x', d => xScale(d.target))
.attr('y', d => yScale(d.factor))
.attr('width', xScale.bandwidth())
.attr('height', yScale.bandwidth())
.attr('fill', d => colorScale(d.correlation)); // Set color based on correlation value
// Add labels for the correlation values inside the cells
svg.selectAll('text')
.data(correlationMatrix)
.enter()
.append('text')
.attr('x', d => xScale(d.target) + xScale.bandwidth() / 2)
.attr('y', d => yScale(d.factor) + yScale.bandwidth() / 2)
.attr('dy', '.35em')
.attr('text-anchor', 'middle')
.text(d => d3.format('.2f')(d.correlation)) // Round to 2 decimal places
.attr('fill', 'black');
// Disease names on the x-axis
svg.append('g')
.attr('transform', `translate(0, ${margin.top})`)
.call(d3.axisTop(xScale))
.selectAll('text')
.text(d => formatLabel(d));
// Factors on the y-axis
svg.append('g')
.attr('transform', `translate(${margin.left}, 0)`)
.call(d3.axisLeft(yScale))
.selectAll('text')
.text(d => formatLabel(d));
return svg.node();
}