我正尝试使用SVG和角度指令来创建图形以更改动态部分。现在,我已经做到了这一点:更改值时动画SVG路径
http://plnkr.co/edit/TcbK7kyzM3tapDISxndh?p=preview
app.directive('pieChart', function($document) {
return {
restrict: "E",
template: '<svg width="500" height="500">' +
'<path d="M100,200 a150,150 0 1,0 150,-150" stroke="black" stroke-width="10" fill="none"></path>' +
'</svg>',
scope: {
value: '='
},
link: function(scope, elem, attr) {
}
}
});
我希望我的图表看起来像这样当它是100%的值,而当值是 - 让我们说 - 45%,我会喜欢看这条线,但只有45%从顶尖中心开始。我可能不得不重新计算路径的路径值,但我想问一下,是否有可能在改变路径时使用JS使其在改变大小时变为动画效果?
预先感谢您,或者如果您有任何人知道这个东西的好教程,请链接到我。
编辑:我改变了指令为一个简单的条形图,但这只是例如,我知道这可以做到没有SVG,因为你可以使用divs,但我希望图表后更复杂。
这里有一个的jsfiddle http://jsfiddle.net/fg9e7eo4/1/
在我的例子,图表一直保持一个,我想使它动画只有一次,不是停留在这一点上。
顺便说一句,这是我想出来,使其工作指令:
testApp.directive('pieChart', function() {
var html =
'<svg width="510" height="20" style="background: #fff">' +
'<path d="{{path}}" stroke="red" stroke-width="10" fill="none">' +
'<animate dur="1s" repeatCount="indefinite" attributeName="d" values="{{path2}}"/>' +
'</path>' +
'</svg>';
return {
restrict: 'E',
template: html,
scope: {
'value': '='
},
link: function(scope, elem, attrs) {
scope.$watch('value', function(newValue, oldValue) {
scope.path = 'M 5 10, L ' + (newValue * 5) + ' 10';
scope.path2 = 'M 5 10, L ' + (oldValue * 5) + ' 10; M 5 10, L ' + (newValue * 5) + ' 10';
elem.children().children().beginElement();
});
}
};
});
您需要$观察attr – Dylan 2014-12-03 18:46:23
的值我得到的值是我在指令中调用的范围更新的值,但我不知道何时更改模板中的路径,如何从上一个'd '到新的 – 2014-12-03 18:49:08
您将必须使用一些缓动功能间隔地修改路径。有框架(如snap或d3)可以为你做到这一点 - [这里](http://bl.ocks.org/mbostock/31ec1817b2be2660c453)是一个使用d3的例子。 – user1620220 2014-12-03 19:12:13