简体   繁体   English

D3 v4散点图图表数据点在缩放时未更新

[英]D3 v4 Scatterplot chart data points are not updating on zoom

I am working on scatter-plot chart with d3 v4 and wanted to apply zooming so that I can see the data points when they overlap each other, the problem I am facing here is both x and y axis are working fine but data points are not updating for some reason. 我正在使用d3 v4绘制散点图,并希望应用缩放功能,以便在彼此重叠时可以看到数据点,这里我面临的问题是xy轴都可以正常工作,但数据点却不能由于某种原因进行更新。

I am trying to select the dots/circles and draw them again but for some reason I couldn't select them, I have no clue why the dots are not updating, any help is much appreciated. 我正在尝试选择点/圆并再次绘制它们,但是由于某些原因我无法选择它们,我不知道为什么点没有更新,非常感谢您的帮助。

Note: my application is in Angular 4 , since I couldn't find the angular 2/4 template here in plunker I had to do in JavaScript . 注意:我的应用程序在Angular 4 ,因为我在plunker中找不到必须在JavaScript中完成的angular 2/4模板。

You just missed 2 minor things: 您只是错过了2个小事情:

  1. In the zoom function, the scales to be used would be the new X and Y scales , as you've to transform the circles in the new domain. 缩放功能中,要使用的比例尺将是新的X和Y比例尺 ,因为您必须在新域中变换圆。

     circles.attr('cx', (d) => xNewScale(d.duration)) .attr('cy', (d) => yNewScale(d.priority)); 
  2. Selection : 选择

     var circles = svg.selectAll('dot') .data(chartData) .... .append('svg:title') .html(...) 

would actually return the "titles" as the current selection and so now, the variable circles is a node group of titles whose attributes (cx and cy) get changed in the zoom function which is not what we want as we want to change the attributes of the <circle> s. 实际上将返回“标题”作为当前选择,因此,现在,可变圆圈是标题的节点组,其属性(cx和cy)在缩放功能中发生了变化,这不是我们想要的,因为我们要更改属性的<circle> And so, changing the above to the following: 因此,将以上内容更改为以下内容:

// Add the scatterplot
var circles = svg.selectAll("dot")
  .data(chartData)
  .enter()
  ....
  .attr('fill' ..);

// NOTE THE CHANGE HERE
circles
  .append('svg:title')
  .html(d => {
    return 'Date : ' + timeFormat(new Date(d.start)) + ',<br> Description: ' + d.description + ',<br> Impact: ' + d.priority + ',<br> Hours: ' + Math.round(d.duration)
  });

Similarly, I've made changes to the red circles (outer). 同样,我对红色圆圈(外部)进行了更改。

Comprising all of the above, here's a fork of your code: 包含以上所有内容,这是您的代码的一部分:

https://plnkr.co/edit/61zf86q6cpIT4MIyhZKo?p=preview https://plnkr.co/edit/61zf86q6cpIT4MIyhZKo?p=preview

Hope this helps. 希望这可以帮助。

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

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