简体   繁体   English

d3在折线图的最高和最低值上添加圆圈

[英]d3 adding circles on the highest and lowest value on line chart

I know when you want to add circles (dots) on a line chart that this is the way to go 我知道您想在折线图上添加圆圈(点)时就是这样

pathContainers.selectAll('circle')
.data(function (d) { return d; })
.enter().append('circle')
.attr('cx', function (d) { return xScale(d.x); })
.attr('cy', function (d) { return yScale(d.y); })
.attr('r', 3); 

But i would like to put circles only on the highest and lowest point. 但是我只想在最高点和最低点上画圈。 How can i accomplisch that? 我该怎么做?

here i found a JSBIN of a d3 chart with the above code inside, perhaps this is a good example to work with 在这里,我发现了d3图表的JSBIN,其中包含上面的代码,也许这是一个很好的示例

JSBIN LINE CHART JSBIN线图

Change the radius to : 将半径更改为:

.attr('r', function (d) { 
  return d.y == yExtents[0] || d.y == yExtents[1] ? 3 : 0;;
})

Like that, only the highest points and lowest will have a circle with a visible radius, the other points will have a circle of radius of 0 这样,只有最高点和最低点将具有可见半径的圆,其他点将具有半径为0的圆

See http://jsbin.com/forezavuhu/1/edit?html,output 参见http://jsbin.com/forezavuhu/1/edit?html,输出

For these kind of things d3.js provides the very convenient selection.filter(selector) function that allows to 对于这类事情,d3.js提供了非常方便的selection.filter(selector)函数,该函数允许

apply operators to a subset of elements. 将运算符应用于元素的子集。

As selector you can also pass a function with which you're able to determine the min and max data values. 作为selector您还可以传递一个function ,通过该function可以确定最小和最大数据值。

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

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