【问题标题】:d3 line rendering off paged3线渲染关闭页面
【发布时间】:2016-07-22 05:27:38
【问题描述】:

我正在尝试使用 d 和 angular 指令制作一个简单的折线图。 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 和线条正在渲染,线条只是屏幕的渲染。

感谢您的帮助!

【问题讨论】:

    标签: javascript angularjs d3.js


    【解决方案1】:

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

    为防止代码重复,请从宽度/高度中减去边距,并将结果定义为宽度/高度。现在您不必到处说“- margin”了。

    //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]);
    

    【讨论】:

      【解决方案2】:

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

      我还省略了 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]);
      

      希望这对其他人有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-10
        • 2012-11-23
        • 2020-03-18
        • 1970-01-01
        • 2022-10-04
        • 1970-01-01
        相关资源
        最近更新 更多