当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


d3.js curveMonotoneX()用法及代码示例


这个d3.curveMonotoneX内插器假设根据x坐标对数据进行排序,否则对数据进行相应的排序。假定x为单调性,此曲线方法将生成一个三次样条,该样条可保持y的单调性。

用法:

d3.curveMonotoneX()

参数:此方法不接受任何参数。

返回值:此方法不返回任何值。

范例1:



<!DOCTYPE html> 
<html> 
  
<head> 
    <meta charset="utf-8"> 
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"> 
    </script> 
</head> 
  
<body> 
    <h1 style="text-align:center; color:green;"> 
        GeeksforGeeks 
    </h1> 
  
    <center> 
        <svg id="gfg" width="250" height="250"></svg> 
    </center> 
  
    <script> 
        var data = [ 
          {x:0, y:0}, 
          {x:1, y:3}, 
          {x:2, y:15}, 
          {x:5, y:1}, 
          {x:6, y:15}, 
          {x:7, y:5}, 
          {x:8, y:19}]; 
  
        var xScale = d3.scaleLinear() 
          .domain([0, 8]).range([25, 200]); 
        var yScale = d3.scaleLinear() 
          .domain([0, 20]).range([200, 25]); 
  
        var line = d3.line() 
          .x((d) => xScale(d.x)) 
          .y((d) => yScale(d.y)) 
          .curve(d3.curveMonotoneX); 
  
        d3.select("#gfg") 
          .append("path") 
          .attr("d", line(data)) 
          .attr("fill", "none") 
          .attr("stroke", "green"); 
    </script> 
</body> 
</html>

输出:

范例2:通过x轴对未排序的点进行排序,然后绘制曲线。

<!DOCTYPE html> 
<html> 
  
<head> 
    <meta charset="utf-8"> 
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"> 
    </script> 
</head> 
  
<body> 
    <h1 style="text-align:center; color:green;"> 
        GeeksforGeeks 
    </h1> 
  
    <center> 
        <svg id="gfg" width="250" height="200"></svg> 
    </center> 
  
    <script> 
        var points = [ 
          {xpoint:75,  ypoint:150}, 
          {xpoint:25,  ypoint:5}, 
          {xpoint:150, ypoint:115}, 
          {xpoint:100, ypoint:5}, 
          {xpoint:200, ypoint:150}]; 
  
        // Sorting the points by x axis 
        points.sort((a, b) => a.xpoint - b.xpoint); 
  
        var Gen = d3.line() 
          .x((p) => p.xpoint) 
          .y((p) => p.ypoint) 
          .curve(d3.curveMonotoneX); 
  
        d3.select("#gfg") 
          .append("path") 
          .attr("d", Gen(points)) 
          .attr("fill", "none") 
          .attr("stroke", "green"); 
  
    </script> 
</body> 
</html>

输出:




相关用法


注:本文由纯净天空筛选整理自taran910大神的英文原创作品 D3.js curveMonotoneX() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。