热图矩形内的标签
labels inside the heatmap rects
我想在框内显示热图框的值,而不仅仅是在工具提示中。
虽然根据https://github.com/dc-js/dc.js/issues/1303看来这个功能还没有添加,但是用d3.js可以实现吗?谢谢
热图:
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);
细节决定成败。
在方框上添加一些文字非常容易。让它们很好地定位和格式化将需要更多的工作。不管怎样,我都会尽力让你开始。
您可以使用 renderlet
事件。 pretransition
会更好,但我们需要从 rect
中窃取一些属性,因为图表的比例不可访问,并且这些属性在 pretransition
时并未全部初始化。
这是 dc.js 中注释的常见模式:等待图表事件,然后 select 现有元素并向其添加内容。元素select或为documented here;我们想要 g.box-group
.
其余的是标准的 D3 通用更新模式,但正如我提到的,我们无法访问天平。我们可以从 rect
中读取 x
、y
、width
、height
等属性,而不是试图复制该语言并可能弄错它已经居住在这些 g
中。
SVG 没有多行文本,因此我们必须创建 tspan elements inside of 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);
})
请注意,我放弃了并将文本向上轻推了 15 像素...可能有也可能没有更好的方法来使多行文本垂直居中。
即使没有描述,文字也不太合适。我会让你弄明白的。
但它在那里并且正确更新:
我想在框内显示热图框的值,而不仅仅是在工具提示中。
虽然根据https://github.com/dc-js/dc.js/issues/1303看来这个功能还没有添加,但是用d3.js可以实现吗?谢谢
热图:
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);
细节决定成败。
在方框上添加一些文字非常容易。让它们很好地定位和格式化将需要更多的工作。不管怎样,我都会尽力让你开始。
您可以使用 renderlet
事件。 pretransition
会更好,但我们需要从 rect
中窃取一些属性,因为图表的比例不可访问,并且这些属性在 pretransition
时并未全部初始化。
这是 dc.js 中注释的常见模式:等待图表事件,然后 select 现有元素并向其添加内容。元素select或为documented here;我们想要 g.box-group
.
其余的是标准的 D3 通用更新模式,但正如我提到的,我们无法访问天平。我们可以从 rect
中读取 x
、y
、width
、height
等属性,而不是试图复制该语言并可能弄错它已经居住在这些 g
中。
SVG 没有多行文本,因此我们必须创建 tspan elements inside of 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);
})
请注意,我放弃了并将文本向上轻推了 15 像素...可能有也可能没有更好的方法来使多行文本垂直居中。
即使没有描述,文字也不太合适。我会让你弄明白的。
但它在那里并且正确更新: