【问题标题】:Bar chart in D3.js with AngularJS带有 AngularJS 的 D3.js 中的条形图
【发布时间】:2016-01-06 16:39:51
【问题描述】:

在网页上加载时,d3 条形图出现错误
错误:
错误: 属性 y="NaN" 的值无效,错误: 属性 height="NaN"
的值无效 我试图通过编辑此代码来解决它
没有任何效果

var countriesData = data.countries;

    var datac=[];
    for (var key in countriesData) {
        datac.push({key: key, value: countriesData[key]});
    };
    console.log(datac); 
    var width = 250;
    var height = 250;
//console.log(data4); 
    //x and y Scales
    var xScale = d3.scale.ordinal()
        .rangeRoundBands([0, width], .1);

    var yScale = d3.scale.linear()
        .range([height, 0]);

    xScale.domain(datac.map(function(d) { return d.x; }));
    yScale.domain([0, d3.max(datac, function(d) { return d.y; })]);

    //x and y Axes
    var xAxis = d3.svg.axis()
        .scale(xScale)
        .orient("bottom");
        //.ticks();
    var yAxis = d3.svg.axis()
        .scale(yScale)
        .orient("left");
        //.ticks(function(d) { return d.x; });

    //create svg container
    var svg = d3.select("#barchart").select("svg").remove();


    svg = d3.select("#barchart")
        .append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
        .append("g")
        //.transition().duration(2000)
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");        


    //create bars
    svg.selectAll(".bar")
        .data(datac)
        .enter()
        .append("rect")
        .attr("class", "bar")
        .attr("x", function(d) { return xScale(d.x); })
        .attr("width", xScale.rangeBand())
        .attr("y", function(d) { return yScale(d.y); })
        .attr("height", function(d) { return height - yScale(d.y); });

    //drawing the x axis on svg
    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

    //drawing the y axis on svg
    svg.append("g")
        .attr("class", "y axis")
        .call(yAxis)
        .append("text")
        .attr("transform", "rotate(-90)")
        .attr("y", 6)
        .attr("dy", ".71em")
        .style("text-anchor", "end")
        .text("Reviews Number");

请帮忙!

【问题讨论】:

    标签: angularjs d3.js


    【解决方案1】:

    很难说,但我猜它是 xScale 的定义:

    xScale.domain(datac.map(function(d) { return d.x; }));
    

    应该是这样的:

    xScale.domain(d3.extent(datac, fucntion (d) {return d.x}))
    

    【讨论】:

    • 它不起作用,顺便说一下(y 和高度)的错误,错误:属性 y="NaN" 的值无效,错误:属性 height="NaN" 的值无效。不是 (x)。
    • 你能发布datac的样子吗?
    • 谢谢你,问题出在 datac 中,现在解决了 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    相关资源
    最近更新 更多