简体   繁体   English

D3 js - onClick of circle 我想在它的背景中添加该区域

[英]D3 js - onClick of circle I want to add the area in background of that

I have a d3 chart, In the chart on click of the circle I am making circle size bigger d3.select(this).attr("r", 9);我有一个 d3 图表,在点击圆圈的图表中,我使圆圈大小变大d3.select(this).attr("r", 9);

My requirement is I want to add the area in background with blue color on top of the circle as depicted in below image,我的要求是我想在圆圈顶部添加蓝色背景区域,如下图所示,

在此处输入图片说明

I tried to add an area with color on the background but it's not working.我试图在背景上添加一个带有颜色的区域,但它不起作用。 Please suggest to me.请给我建议。 Thanks.谢谢。

My on Click code -我的点击代码 -

.on("click", function(d) {
        d3.selectAll(".green-circle").attr("r", 5);
        d3.selectAll(".red-circle").attr("r", 5);
        d3.select(this).attr("r", 9);
      })

I tried to add line onClick but it adds the content inside circle I tried to get the parentNode also but no luck.我试图添加行 onClick 但它添加了圆圈内的内容我也试图获取 parentNode 但没有运气。

  d3.select(this).append("line")
              .attr("id", "focusLineX")
              .attr("class", "focusLine");

<circle cx="568.2359387326708" cy="100" r="9" class="green-circle" style="fill: transparent; stroke: black; stroke-width: 0.25;">
<line id="focusLineX" class="focusLine"></line>
</circle>

My Code sand box - https://codesandbox.io/s/polished-bush-iqvrx我的代码沙盒 - https://codesandbox.io/s/polished-bush-iqvrx

You need to add the new object to your chart, not to your circle.您需要将新对象添加到您的图表中,而不是您的圆圈中。 Based on your code that you have shared (thank you for doing it, it makes it much easier to help!) you should be adding to g object.根据您共享的代码(感谢您这样做,它使帮助变得更加容易!)您应该添加到g对象。

I am not sure if the changes I made have reflected, I suppose you can't see them, so here is what I have changed so far:我不确定我所做的更改是否已反映出来,我想您看不到它们,所以这是我到目前为止所做的更改:

Declare a rect variable somewhere, I did it right after newX variable:在某处声明一个rect变量,我在newX变量之后就newX

// A function that updates the chart when the user zoom and thus new boundaries are available
var newX = "";
var rect;

And then within the on("click") function, put this code in the beginning:然后在on("click")函数中,将此代码放在开头:

    if (rect) rect.remove();
    rect = g
      .append("rect")
      .attr("x", xScale(d.startTime) - 5)
      .attr("y", 0)
      .attr("width", 10)
      .attr("height", height)
      .attr("data", d.startTime)  // Need to save original x Position for scroll to work properly
      .style("fill", "navy");

I am using rectangle with navy fill, but feel free to play with the styles, this is just to give you an idea how you could be doing what you are looking for.我正在使用带有navy填充的矩形,但可以随意使用这些样式,这只是为了让您了解如何做您正在寻找的东西。

I hope this is helpful, let me know if you have any questions.我希望这对您有所帮助,如果您有任何问题,请告诉我。

* UPDATE * Also following line needs to be added to the end of the updateChart function to make bar scroll with the rest of the graphics: * UPDATE *还需要将以下行添加到updateChart函数的末尾,以使条形图与其余图形一起滚动:

  if (rect) rect.attr("x", newX(rect.attr("data")));

* UPDATE 2 * So for zoom+scrolling both working properly you need to use newX function everywhere, as this function provides with the x coordinate updated according to both zoom and scroll values. * UPDATE 2 *因此, newX缩放+滚动都正常工作,您需要在任何地方使用newX函数,因为该函数提供了根据缩放和滚动值更新的x坐标。

Also in the updateChart function you need to subtract half of the column width as well, so that column does not jump to the right.同样在updateChart函数中,您还需要减去列宽的一半,以便该列不会向右跳。

Working code can be found her: CodeSandbox可以找到她的工作代码: CodeSandbox

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

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