d3线渲染关闭页面

时间:2016-07-22 05:27:39

标签: javascript angularjs d3.js

我试图用d和一个角度指令制作一个简单的折线图。 svg区域显示在页面上,但是线条部分正在渲染页面。

我正在使用d3 v4

我确实缩放了x和y数据。我的console.log显示x和y值应该都适合svg的宽度和高度。我已经研究了一段时间,似乎无法找到任何有效的解决方案。

这是角度代码:

    graphics.directive('lineVis', function(){
return{
    restrict: 'E',
    scope: {
        val: '=',
        grouped: '='
    },
    link: function(scope, element, attrs){
        var margin = 25,
            width = 960,
            height = 500;
            data = [{_id:16998.0, total: 1854},{_id:16999.0, total: 2157},{_id:17000.0, total: 2389},{_id:17001.0, total: 2187},{_id:17002.0, total: 1973},{_id:17003.0, total: 1050}];

        console.log(data.length);
        var x = d3.scaleLinear()
            .range([margin, width-margin])
            .domain(d3.extent(data, function(d) {return d._id;}));
            //[d3.min(data, function(d){return d._id}), d3.max(data, function(d){return d._id})])

        var y = d3.scaleLinear()
            .range([margin, height-margin])
            .domain([0, d3.max(data, function(d){
                return d.total;
            })]);

        var line = d3.line()
            .x(function(d){
                console.log('Y: '+ y(d.total) + ' X: ' + x(d._id));
                return (d._id);
            })
            .y(function(d){
                return y(d.total);
            })


        var svg = d3.select(element[0])
            .append('svg')
            .attr('width', width+margin)
            .attr('height', height+margin);

        svg.selectAll('*').remove() //remove all previous elements

        svg.append('path')
            .attr('class', 'line')
            .attr('stroke', '#777')
            .attr('d', line(data));
        }   
    }
});

和css:

    .line {
        fill: none;
        stroke: steelblue;
        stroke-width: 2px;
    }

我知道html正在运行,因为svg和line正在渲染,该行只是渲染屏幕。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您忘记将g元素附加到svg,转换为边距值。看看Mike的例子https://gist.github.com/mbostock/3019563

要防止代码重复,请从宽度/高度中减去边距,并将结果定义为宽度/高度。现在你不必一直在说“ - 边缘”。

//in case you want different values per side
var margin = {top: 25, right: 25, bottom: 25, left: 25};

var width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var svg = d3.select(element[0])
            .append('svg')
            .attr('width', width+margin)
            .attr('height', height+margin);
            .append('g')
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var x = d3.scale.linear()
    .range([0, width]);

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

答案 1 :(得分:0)

谢谢Eric!我同意这使代码更易于阅读和更灵活。 (以及实际添加边距)

我也在d3.line()上省略了x刻度 应该是:

    var line = d3.line()
        .x(function(d){
            console.log('Y: '+ y(d.total) + ' X: ' + x(d._id));
            return x(d._id); // <-- added x call
        })
        .y(function(d){
            return y(d.total);
        })

这条线现在呈现!但它现在倒了......宝贝步骤

编辑:

当我说倒置时,我的意思是线图从右上角开始

解决方案:

为y缩放器切换.domain:

    var y = d3.scaleLinear()
            .range([0, height])
            .domain([d3.max(data, function(d){
                return d.total;
            }), 0]);

希望这有助于其他人!