我的HTML代码:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>Test App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.bundle.js"></script>
</head>
<header>
<h1>Test App</h1>
</header>
<body ng-controller="myController">
Search Names: <input type="text" ng-model="search">
<ol>
<li ng-repeat="datas in collection | filter:search track by $index">{{datas}}</li>
</ol>
<form ng-submit="addName()">
<input type="text" ng-model="names" placeholder="Enter name" required>
<input type="text" ng-model="number" placeholder="Enter number for chart" required>
<input type="submit" value="Submit">
</form>
<div style="width:20%;">
<canvas id="myChart" width="400" height="400"></canvas>
</div>
</body>
</html>
我的javascript文件:
var myApp =angular.module("myApp", []);
myApp.controller("myController", function($scope) {
$scope.collection = [];
$scope.addName = function () {
$scope.collection.push($scope.names);
$scope.names = '';
$scope.number = this.number;
};
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, $scope.number],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
});
我是角度和javascript的新手。我试图从表单获取用户值amd想要在数据集中的数据中使用,以便我的图形值将根据用户输入。
请在我错的地方帮忙。感谢
答案 0 :(得分:0)
你不能这样使用,因为数据是图表中的一个数组,
你可以这样做,
struct
并在图表中指定值
<强> DEMO 强>
无论如何,我推荐的是有很多图表库可用于支持angularjs,如HighCharts-ng和nvd3。尝试使用其中一个。