javascript - d3v4 结帐图表 - 确保图例和圆圈的配色方案正确
问题描述
所以我制作了这个原型图——并想创建一种鸟瞰图——金字塔类型的图表。
例如,对于foursquare——餐馆、肉店、咖啡馆——会有不同的相关属性——比如计数、切金、流行度
这是图表的第 3 版。 http://jsfiddle.net/0ht35rpb/33/
这是版本 4 https://jsfiddle.net/pLo6mubx/
var color = d3.scaleOrdinal()
.range(["#a8e6cf", "#dcedc1", "#ffd3b6", "#ffaaa5"]);
- 但我不确定颜色是否正确 - 好像 A 系列中的计数很小 - 查看其他系列时是否一致 - 圆圈的大小可能会改变 - 但它实际上只是它的层。我想确保数据正确表示 - 并且颜色在整个圆圈本身和图例中都是正确的。- 此外,如果有一种方法可以清理对角指向的标签 - 随着组的整体大小可能发生变化,它会变得混乱 - 所以这会导致角度发生变化。
所以图表本身应该是正确的,因为它调用了数据索引
circles.enter().append("circle")
.attr("cy", height / 2)
.attr("cx", 0)
.attr("r", function(d) {
return scale(Math.sqrt(d.value));
})
.style("fill", function(d) {
return color(d.index);
});
但是图例只使用了一系列数据,并且只是循环显示了颜色。
ring.enter().append("circle")
.attr("cy", function(d, i) {
return (vertical * i) + ringRadius;
})
.attr("r", ringRadius)
.attr("width", ringRadius*2)
.attr("height", ringRadius*2)
.style("fill", function(d, i) {
return color(i);
});
- 修复指针标签位移 - 角度以奇数和锐角脱落 - 好像标签 200,500,4000,200 - 需要位于每个父金字塔的相对位置 - 然后偏移到像100px一样的权利?
- 检查和/或纠正——图例和圆圈本身的颜色——我不确定图例是否真正代表了颜色的使用方式——图表上的颜色应该始终保持一致——但是我认为圆圈绘制填充颜色的方式以及图例的方式 - 依赖于不同的索引 - 请检查/更正
6 月 8 日 - 最新版本 - https://jsfiddle.net/e8bmyvra/1/ - 剩余“修复指针标签位移”
解决方案
So from your updates it seems like the remaining problem is fixing the labels for the numbers so that they have the same angle and snap to the centre of the circles instead of the circumferences. We can actually accomplish this with some minor modifications of your code. Specifically at two places:
When drawing the lines that point to the number labels, we remove the code that displaces the lines according to the circle radius. For
y1
we give it the y-value for the centre of the circle, and forx2
andy2
we give them a fixed displacement. i.e.newGroups.append("g").attr("class", "datumLine") .append("line") .attr("class", "datumValue") .attr("y2", 30) // <---- Adjusted this value slightly .attr("y1", function(d) { return height / 2; // <---- note that we remove the dependence on radius here }) .attr("x2", function(d) { return (height / 2) - 70; // <---- Likewise we use a fixed displacement here });
For the portion where we add the numbers
200, 500, 4000, 2000
, we likewise remove the code that displaces the numbers according to the circle radius and give it a fixed value (the same value we use for thex2
attribute in the code above). We also adjust the rotation slightly. i.e.newGroups.append("text") .attr("class", "datumValue") .attr("y", 10) .text(function(d) { return fetchValue(d.items, focus); }) .attr("transform", function(d) { return "translate(" + ((height / 2) - 70) + ",20) rotate(-60)"; // <- Note changes here });
The exact numbers used in the example above, such as displacement and rotation, can be altered as desired. This is just an example of values that avoids the problem of the lines intersecting adjacent circles. Here is a jsfiddle of the results https://jsfiddle.net/a8qod3z0/:
Let me know if this is your intended output, or if you had something different in mind and we can work on that!
推荐阅读
- flutter - 如何在键盘出现后控制 AlertDialog 调整大小
- ios - 升级firebase核心后颤动Ios构建失败
- windows - Windows 中的 Meteor 服务器日志在哪里?
- kubernetes - 为内存达到 90% 且未暴露于 Internet(后端微服务)的 kubernetes pod 配置通知/警报的方法
- sql-server - 检索过程可选参数列表的 SQL 语句
- r - R Shiny:减少过滤数据帧的重复代码
- sql-server - 如何在 SQL Server 中导入大型 CSV 文件
- python - 删除 django 搜索中的重复结果
- asp.net-mvc-routing - 为什么 Blazor SPA 中的路由消息不明确?
- java - 配置警告中不存在此类片段