【问题标题】:"Interpolate" is not a function“插值”不是函数
【发布时间】:2017-03-05 01:08:56
【问题描述】:

我是 D3 的新手,正在尝试一些图表。在使用 D3 V4 构建折线图时,我遇到了以下错误。

d3.line(...).x(...).y(...).interpolate 不是函数

我认为这个错误是由于函数 interpolate 在 D3 v4 中不可用。如果有人可以帮助我使用插值函数的替换函数,那就太好了。

我的代码在下面的链接中

https://ghostbin.com/paste/ztans

【问题讨论】:

    标签: javascript d3.js data-visualization


    【解决方案1】:

    在 D3 v4.x 中,线生成器采用 curve 来定义插值:

    虽然线被定义为二维 [x, y] 点的序列,并且区域也类似地由顶线和基线定义,但仍然存在将这种离散表示转换为连续形状的任务:即,如何在点之间进行插值。为此提供了各种曲线 [...] 曲线通常不直接构建或使用,而是传递给 line.curve面积.曲线。 (强调我的)

    所以,这个:

    var lineFun = d3.line()
        .x(function(d){return d.month*50})
        .y(function(d){return height - (10* d.Sales)})
        .interpolate("basis")
    

    应该是:

    var lineFun = d3.line()
        .x(function(d){return d.month*50})
        .y(function(d){return height - (10* d.Sales)})
        .curve(d3.curveBasis);
    

    这是您的更改代码:

    var w = 700;
    var height = 300;
    var padding = 2;
    var border = 2
    
    var dataset=[5,7,2,6,1,10,8,9,11,13,16,40,15,20,25,35,36,25,28,18,17,4,22,5,3,35,46,57];
    
    var monthlySales =[
        {
            "month":1,
            "Sales":10
        },
        {
            "month":2,
            "Sales":25
        },
        {
            "month":3,
            "Sales":12
        },
        {
            "month":4,
            "Sales":16
        },
        {
            "month":5,
            "Sales":17
        }
        ];
    
    onload();
    
    function onload(){
    
        var svg = d3.select("body")
                    .append("svg")
                    .attr("width",w)
                    .attr("height",height)
    
        svg.selectAll("rect")
            .data(dataset)
            .enter()
            .append("rect")
            .attrs({
                x : function(d,i){
                    return (i * (w/dataset.length)); },
                y : function(d){ return (height- (d*4))},
                width: (w/dataset.length)-padding,
                height:function(d){ return(d*4); },
                fill : function(d){return "rgb(0,"+(d*10)+",0)" ;}
            });
    
        svg.selectAll("text")
            .data(dataset)
            .enter()
            .append("text")
            .text(function(d){ return d})
            .attrs({
    
                x: function(d,i){ return (i * (w/dataset.length)) + ((((w/dataset.length) - padding)/2))},
                y: function(d) {return (height-(d*4))},
                "text-anchor" : "middle"
            })
    
    
        var lineFun = d3.line()
            .x(function(d){return d.month*50})
            .y(function(d){return height - (10* d.Sales)})
            .curve(d3.curveBasis);
    
    
        var svgLine = d3.select("body").append("svg")
            .attr("width",w)
            .attr("height",height);
    
        var svgPath = svgLine.append("path")
            .attrs({
                d: lineFun(monthlySales),
                "stroke": "purple",
                "stroke-width":2,
                "fill" :"none"
            })
    
        svgLine.selectAll("text")
            .data(monthlySales)
            .enter()
            .append("text")
            .text(function(d){return d.Sales})
            .attrs({
                x : function(d){return d.month*50 - 10},
                y : function(d){return height-(10*d.Sales) + 10},
                "font-size":"12px",
                "fill" : "#666666",
                "font-family":"sans-serif",
                "dx":".35em",
                "text-anchor":"start",
                "font-weight": function(d,i){
                    if(i==0 || i == monthlySales.length-1){
                        return "bold"
                    }
                    else{
                        return "normal"
                    }
                }
            })
    
    
    
    }
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <script src="https://d3js.org/d3-selection-multi.v1.min.js"></script>

    【讨论】:

      猜你喜欢
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      相关资源
      最近更新 更多