【发布时间】:2015-08-12 07:47:15
【问题描述】:
我正在使用 nvd3.js 创建一个简单的堆叠条形图,如 here
所述我在 angular 指令 中添加了链接中提到的代码,如下所示:
app.directive('stackBar', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
nv.addGraph(function() {
var chart = nv.models.multiBarChart()
/*.transitionDuration(350)*/
.reduceXTicks(true) //If 'false', every single x-axis tick label will be rendered.
/*.rotateLabels(0) */ //Angle to rotate x-axis labels.
.showControls(true) //Allow user to switch between 'Grouped' and 'Stacked' mode.
.groupSpacing(0.1) //Distance between each group of bars.
chart.xAxis
.tickFormat(d3.format(',f'));
chart.yAxis
.tickFormat(d3.format(',.1f'));
d3.select(element[0])
.datum(exampleData())
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
//Generate some nice data.
function exampleData() {
return stream_layers(3,10+Math.random()*100,.1).map(function(data, i) {
return {
key: 'Stream #' + i,
values: data
};
});
}
}
}
});
这是我的 HTML:
<td class="centered" colspan="5">
<div stack-bar>
</div>
</td>
但是,我收到以下错误:
Uncaught ReferenceError: stream_layers is not defined
知道我哪里出错了吗?
此外,“transitonDuration”也不起作用,因此我将其注释掉。我一开始以为,这可能是d3版本的问题,但我使用的是最新版本,问题仍然存在。
编辑:
黄峰的回答帮助我摆脱了错误。但是,我没有得到任何图表,而是得到了很多文本。这是屏幕截图:
知道为什么吗?
此外,该指令位于 ng-repeat 中,这就是为什么屏幕截图中存在多行的原因。
【问题讨论】:
标签: javascript angularjs d3.js angularjs-directive nvd3.js