简体   繁体   English

d3.js beeswarm plot 上的条件颜色

[英]Conditionnal colours on d3.js beeswarm plot

I'm trying to color a specific dot in my beeswarm plot.我正在尝试为我的 beeswarm plot 中的特定点着色。 My condition is: color the dot if the id matches the id in the params.我的条件是:如果 id 与参数中的 id 匹配,则为点着色。

Therefore, i'm guessing something like this: .attr("fill", function(d) {if("myCondition")return "red"; }) on the svg因此,我猜是这样的: svg 上的.attr("fill", function(d) {if("myCondition")return "red"; })

My data looks like this: { id: 1, name: ItemA, size: 15 }我的数据如下所示: { id: 1, name: ItemA, size: 15 }

When i'm on the page of ItemA, its corresponding dot on the plot should be coloured red.当我在 ItemA 的页面上时,它在 plot 上的对应点应该是红色的。

What should "myCondition" be to have this result? “myCondition”应该是什么才能得到这个结果?

Here's the code on d3's website to fiddle with (where you can try colouring according to Year or Acceleration for example) https://observablehq.com/@d3/beeswarm这是 d3 网站上要摆弄的代码(例如,您可以在其中尝试根据年份或加速度着色) https://observablehq.com/@d3/beeswarm

I'm quite sure my line of code has to be added in the const dot = svg.append("g") at the end我很确定我的代码行必须添加到最后的const dot = svg.append("g")

For those interested, i did these modifications: In the d3.js file:对于那些感兴趣的人,我做了这些修改: 在 d3.js 文件中:

const idFetch = d3.map(data, item) //create an array of my data's ID

and in the svg creation:并在 svg 创建中:

.attr("fill",function(d) {if(idFetch[d] === activeSampleId) return 'red'}) //With active sample ID an option, equal to the ID of the page I'm on (item/{activeSampleId})

Then upon calling the Beeswarm function, in the options:然后在调用 Beeswarm function 时,在选项中:

item: d => d.id,
activeSampleId: this.sampleId, //Vue options API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM