function draw_heatmap(div) {
const container = div.append('div')
.style('display','flex')
.style('flex-direction', 'column')
const title = container.append('div')
.append('p')
.text('员工绩效热力图')
.style('margin', '0 0 0 0')
.style('padding', '18px 16px')
.style('font-family', 'sans-serif')
.style('font-size', '14px')
.style('font-weight', 500)
.style('color', dashboardStyle.textColor)
const svg = container.append('div')
.style('display','flex')
.style('flex-direction', 'column')
.append('svg')
.attr('width', `${chart_layout_specific.heatmap.width}`)
.attr('height', `${chart_layout_specific.heatmap.height}`)
const chart_g = svg.append('g')
.attr('transform',`translate(${chart_layout_specific.heatmap.margin.left},${chart_layout_specific.heatmap.margin.top})`)
chart_g.append('g')
.call(yAxis_heatmap)
.call(g=>{
g.selectAll('.domain').remove()
})
.call(g=>{
g.selectAll('.domain, .tick line')
.attr('stroke', dashboardStyle.axisLine)
g.selectAll('text')
.attr('fill', dashboardStyle.axisText)
})
chart_g.append('g')
.attr('transform',`translate(0, ${chart_layout_specific.heatmap.innerHeight})`)
.call(xAxis_heatmap)
.call(g=>{
g.selectAll('.domain').remove()
})
.call(g=>{
g.selectAll('.domain, .tick line')
.attr('stroke', dashboardStyle.axisLine)
g.selectAll('text')
.attr('fill', dashboardStyle.axisText)
})
const chart_rects_g = chart_g
.append('g')
.attr('class', 'heatmap-rects-g')
chart_rects_g.selectAll('g')
.data(heatmap_data)
.join('g')
.attr('class', 'rects-horizontal-g')
.attr('transform',d=> `translate(0, ${yBandScale_heatmap(d[0])})`)
.selectAll('g')
.data(d=> d[1])
.join('g')
.attr('transform',d=> `translate(${xBandScale_heatmap(d[0])}, 0)`)
.call(g=>{
g.append('rect')
.attr('x', heatmap_attrs.gap/2)
.attr('y', heatmap_attrs.gap/2)
.attr('width', xBandScale_heatmap.bandwidth() - heatmap_attrs.gap)
.attr('height', yBandScale_heatmap.bandwidth() - heatmap_attrs.gap)
.attr('rx', 4)
.attr('fill', d=>colorScale_heatmap(d[1]))
})
.call(g=>{
g.append('g')
.call(
g=>{
g.append('text')
.attr('transform',`translate(${xBandScale_heatmap.bandwidth()*0.85}, ${yBandScale_heatmap.bandwidth()*0.44})`)
.text(d=>{
const ratio = d[1]/d3.sum(heatmap_sample_data, d=>d['value'])
return d3.format(".1%")(ratio)
})
.style("font", "13px sans-serif")
.style("font-weight", '600')
.style("text-anchor","end")
.attr('fill', d=>{
const bgColor = d3.color(colorScale_heatmap(d[1])).formatHex()
const contrast = colorContrast(bgColor, textColor)
if(contrast < contrastThreshold){
return '#fff'
}else{
return textColor
}
})
})
.call(
g=>{
g.append('text')
.attr('transform',`translate(${xBandScale_heatmap.bandwidth()*0.85}, ${yBandScale_heatmap.bandwidth()*0.44 + 15})`)
.text(d=>d[1])
.style("font", "13px sans-serif")
.style("font-weight", '600')
.style("text-anchor","end")
.attr('fill', d=>{
const bgColor = d3.color(colorScale_heatmap(d[1])).formatHex()
const contrast = colorContrast(bgColor, textColor)
if(contrast < contrastThreshold){
return '#fff'
}else{
return textColor
}
})
})
})
}