【发布时间】: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