【发布时间】:2014-03-23 22:08:29
【问题描述】:
我的数据有一个字段(我们称之为Kind),用于内部目的是一个短字符串(短格式),但它映射到相当冗长的字符串(长格式),用于演示。例如:
// Not the real values, but you get the idea...
var kind_map = {'c2a': "Collected Compound Allocations",
'dee': "Digital Extrapolated Exponents", ...};
在我的可视化小部件中,有一个饼图总结了与Kind 相关的数据。由于我打算显示长格式字符串,因此我已安排将它们显示在饼图之外(请参阅this question)。但是,正如您将在此example fiddle 中注意到的那样,在转换期间,图例项将恢复为其原始过滤器值,即短格式字符串。我想避免这种情况,但到目前为止一直没有成功。我也尝试过修改filterPrinter、filterHandler 等,但行为仍然存在。
我的代码如下:
cxf = crossfilter(raw_data); //raw_data comes from d3.csv, json, whatever
kind_D = cxf.dimension( function(d) { return d.Kind; } );
kind_G = kind_D.group().reduceSum( function(d) { return d.Value; });
kind_chart = dc.pieChart('#kind-chart');
kind_chart.width(a_width)
.height(a_height)
.radius(a_radius)
.dimension(kind_D)
.group(kind_G)
.legend( dc.legend().x(this.groups_base_dim).y(50).gap(10) );
// Expand the group's legend with verbose group names from the JSON config
var kind_name_render = function(chart) {
chart.selectAll(".dc-legend-item text")
.html( function (d, i) {
return kind_map[d.name];
});
}
kind_chart.on("postRedraw", kind_name_render);
kind_chart.on("postRender", kind_name_render);
【问题讨论】:
-
我相信问题是在预渲染时,图例不存在?检查此FIDDLE。预渲染事件被触发,但无论是否有过渡,选择都无法选择任何内容(检查控制台消息)。然而,在渲染后,该事件被触发并且选择成功。取消注释渲染后事件,您可以看到它的实际效果(控制台消息)。
-
感谢您的意见。我查看了
dc.legend的代码,如果我没记错的话,似乎有必要修改_legend.render函数。不是好消息... -
解决方法是使用映射值定义
dimension:kind_D = cfx.dimension( function(d) { return kind_map[d.Kind]; })(这使得kind_name_render函数变得不必要)。但是我担心这会增加内存使用量并降低性能,因为长字符串将用于所有过滤/分组操作中的比较。我的恐惧有根据吗?
标签: d3.js legend transitions dc.js