我想在框内显示HeatMap框的值,而不仅仅是在工具提示中。
尽管尚未根据https://github.com/dc-js/dc.js/issues/1303添加此功能,但是使用d3.js是否可以实现?谢谢。
HeatMap:
heatMap
.width(1000)
.height(400)
.dimension(dimension)
.group(filtered_heatMap_group)
.margins({
left: 200,
top: 30,
right: 10,
bottom: 50
})
.keyAccessor(function (d) {
return d.key[0];
})
.valueAccessor(function (d) {
return d.key[1];
})
.colorAccessor(function (d) {
return +d.value.color;
})
.title(function (d) {
return "Manager: " + d.key[1] + "\n" +
"FTE: " + d.value.toolTip + "\n" +
"Date: " + d.key[0] + "";
})
.rowOrdering(d3.descending);
答案 0 :(得分:1)
细节决定成败。
在框上输入一些文字非常容易。使它们正确定位和格式化将需要更多工作。无论如何,我都会尽力让您入门。
您可以使用renderlet
事件。 pretransition
会更好,但是我们将需要从rect
中窃取一些属性,因为无法访问图表的比例,并且这些属性并非在pretransition
时都已初始化。< / p>
这是dc.js中注释的常见模式:等待图表事件,然后选择现有元素并向其中添加内容。元素选择器是documented here;我们想要g.box-group
。
其余的是标准的D3常规更新模式,除了我提到的那样,我们无法使用秤。我们可以尝试从x
来读取y
,width
,height
,rect
之类的属性,而不是尝试复制该语言并可能弄错它已经驻留在这些g
中。
SVG没有多行文字,因此我们必须在tspan元素内创建text元素。
将它们放在一起:
.on('renderlet.label', function(chart) {
var text = chart.selectAll('g.box-group')
.selectAll('text.annotate').data(d => [d]);
text.exit().remove();
// enter attributes => anything that won't change
var textEnter = text.enter()
.append('text')
.attr('class', 'annotate')
textEnter.selectAll('tspan')
.data(d => [d.key[1], d.value.toolTip, d.key[0]])
.enter().append('tspan')
.attr('text-anchor', 'middle')
.attr('dy', 10);
text = textEnter.merge(text);
// update attributes => position and text
text
.attr('y', function() {
var rect = d3.select(this.parentNode).select('rect');
return +rect.attr('y') + rect.attr('height')/2 - 15;
});
text.selectAll('tspan')
.attr('x', function() {
var rect = d3.select(this.parentNode.parentNode).select('rect');
return +rect.attr('x') + rect.attr('width')/2;
})
.text(d => d);
})
请注意,我已经放弃并微调了15px的文字...也许有或没有更好的方法来获得多行文字的垂直居中。
即使没有说明,文本也不太适合。我会让你知道。
但是它在那里并且可以正确更新: