热力图效果图如下,支持缩放
setChart() {
const margin = {top: 80, right: 25, bottom: 30, left: 40},
width = 450 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
// append the svg object to the body of the page
const svg = d3.select("#data_div")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.call(d3.zoom().on("zoom", function (event) { // 控制缩放
svg.attr("transform", event.transform)
}))
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
//Read the data
const data = [
{group:'A', variable:'V1', value:'30'},
{group:'A', variable:'V2', value:'60'},
{group:'A', variable:'V4', value:'40'},
{group:'B', variable:'V1', value:'50'},
{group:'B', variable:'V2', value:'80'},
{group:'C', variable:'V3', value:'70'},
{group:'C', variable:'V1', value:'40'},
{group:'D', variable:'V3', value:'90'},
{group:'D', variable:'V4', value:'80'},
{group:'E', variable:'V4', value:'50'},
{group:'E', variable:'V5', value:'80'},
{group:'F', variable:'V5', value:'60'},
{group:'F', variable:'V6', value:'70'},
];
// Labels of row and columns -> unique identifier of the column called 'group' and 'variable'
const myGroups = Array.from(new Set(data.map(d => d.group)))
const myVars = Array.from(new Set(data.map(d => d.variable)))
// Build X scales and axis,Y scales and axis:
const x = d3.scaleBand()
.range([ 0, width ])
.domain(myGroups)
.padding(0.05);
svg.append("g")
.style("font-size", 15)
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x).tickSize(0)) // tickSize(0) 去掉坐标点的指示线
.select(".domain").remove() // 去掉X轴的坐标线
const y = d3.scaleBand()
.range([ height, 0 ])
.domain(myVars)
.padding(0.05);
svg.append("g")
.style("font-size", 15)
.call(d3.axisLeft(y).tickSize(0))
.select(".domain").remove()
// Build color scale
const myColor = d3.scaleSequential()
.interpolator(d3.interpolateInferno)
.domain([1,100])
// create a tooltip
const tooltip = d3.select("#data_div")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("background-color", "cyan")
.style("border", "solid")
.style("border-width", "1px")
.style("border-radius", "5px")
.style("padding", "5px")
// Three function that change the tooltip when user hover / move / leave a cell
const mouseover = function(event,d) {
tooltip
.style("opacity", 1)
d3.select(this)
.style("stroke", "black")
.style("opacity", 1)
}
const mousemove = function(event,d) {
tooltip
.html("The exact value of
this cell is: " + d.value).style("left", "120px")
.style("top", ((event.x)/2 + 11) + "px")
}
const mouseleave = function(event,d) {
tooltip
.style("opacity", 0)
d3.select(this)
.style("stroke", "none")
.style("opacity", 0.8)
}
// add the squares
svg.selectAll()
.data(data, function(d) {return d.group+':'+d.variable;})
.join("rect")
.attr("x", function(d) { return x(d.group) })
.attr("y", function(d) { return y(d.variable) })
.attr("rx", 4)
.attr("ry", 4)
.attr("width", x.bandwidth() )
.attr("height", y.bandwidth() )
.style("fill", function(d) { return myColor(d.value)} )
.style("stroke-width", 4)
.style("stroke", "none")
.style("opacity", 0.8)
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
// Add title to graph
svg.append("text")
.attr("x", 0)
.attr("y", -50)
.attr("text-anchor", "left")
.style("font-size", "22px")
.text("A d3.js heatmap");
// Add subtitle to graph
svg.append("text")
.attr("x", 0)
.attr("y", -20)
.attr("text-anchor", "left")
.style("font-size", "14px")
.style("fill", "grey")
.style("max-width", 400)
.text("A short description of the take-away message of this chart.");
}