我是AngularJS的新手。我已经通过使用Chart.js和HTML创建了水平条形图。现在,我想添加AngularJS,以便在页面上动态显示图表。有人可以指导我如何开始吗?谢谢。
HTML:
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.min.js"></script>
<canvas id="myChart"></canvas>
JavaScript:
var config = {
type: 'horizontalBar',
data: {
labels: ["A", "B", "C", "D", "E"],
datasets: [{
label: "Dataset 1",
backgroundColor: "rgba(154,178,96,0.5)",
hoverBackgroundColor: "rgba(154,178,96,1)",
data: [10, 15, 5, 81, 55],
}, {
label: "Dataset 2",
backgroundColor: "rgba(197,213,167,0.5)",
hoverBackgroundColor: "rgba(197,213,167,1)",
data: [90, 85, 95, 19, 45]
}]
},
options: {
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true
}]
}
}
};
var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, config);
答案 0 :(得分:0)
任何处理DOM的jQuery代码都应封装在custom directive中,以便在AngularJS框架实例化元素时执行。
app.directive("myChartJs", function()
var config = {
//....
};
return {
link: postLink,
};
function postLink(scope,elem,attrs) {
var ctx = elem[0].getContext("2d");
new Chart(ctx, config);
}
});
<canvas id="myChart" my-chart-js>
</canvas>
要使用jQuery,只需确保将其加载到angular.js
文件之前即可。
有关更多信息,请参见