ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – “插值”不是一个功能

2019-10-06 07:38:12  阅读:249  来源: 互联网

标签:javascript d3-js data-visualization


我是D3的新手并在几张图表上进行实验.在使用D3 V4构建折线图时,我遇到了以下错误.

d3.line(…).x(…).y(…).interpolate is not a function

我认为这个错误是由于D3 v4中的函数插值不可用.如果有人可以帮助我使用插值函数的替换函数,那将会很棒.

我的代码在以下链接中

https://ghostbin.com/paste/ztans

解决方法:

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

While lines are defined as a sequence of two-dimensional [x, y] points, and areas are similarly defined by a topline and a baseline, there remains the task of transforming this discrete representation into a continuous shape: i.e., how to interpolate between the points. A variety of curves are provided for this purpose […] Curves are typically not constructed or used directly, instead being passed to line.curve and area.curve. (emphases mine)

所以这:

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 onl oad(){

    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>

标签:javascript,d3-js,data-visualization
来源: https://codeday.me/bug/20191006/1859180.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有