我想要使用下拉菜单显示两个图表。但我尝试了两种选择(使用JQuery / AngularJS),但我很难做到这一点。
我有两张图表
<div ng-controller="ExampleCtrl">
<nvd3-discrete-bar-chart
data="exampleData"
id="exampleId"
width="800"
height="400"
tooltips="true"
showXAxis="true"
showYAxis="true">
<svg></svg>
</nvd3-discrete-bar-chart>
<nvd3-discrete-bar-chart
data="exampleData2"
id="exampleId2"
width="800"
height="400"
tooltips="true"
showXAxis="true"
showYAxis="true">
<svg></svg>
</nvd3-discrete-bar-chart>
</div>
我想使用下拉菜单更改图表
<select id="leave" onchange="leaveChange()">
<option value="1">Second</option>
<option value="2">First</option>
</select>
选择图表的功能是:
function leaveChange() {
if (document.getElementById("leave").value != "1"){
document.getElementById("test").data="exampleData2";
}
else{
document.getElementById("test").data="exampleData";
}
}
使用Jquery: http://jsfiddle.net/K3Lwj/3/
答案 0 :(得分:1)
您使用Angular ng-options
选择菜单的想法是我认为是一个很好的选择,因为您使用Angular散布普通javascript的方式似乎并不是解决问题的特别“角度”方式。例如,在您使用onclick
的地方,我建议使用Angular的ng-change
并将select
放在控制器内。
这是一个例子,主要基于你的原创,并使用更多的Angular方法来解决这个问题:
<div ng-app='nvd3TestApp'>
<div ng-controller="ExampleCtrl">
<select ng-init="selectedChart.chart = chartOptions[0]; updateChart()" ng-model="selectedChart.chart" ng-change="updateChart()" ng-options="c.name for c in chartOptions track by c.id"></select>
<nvd3-discrete-bar-chart data="exampleData" id="exampleId" width="800" height="400" tooltips="true" showXAxis="true" showYAxis="true">
<svg></svg>
</nvd3-discrete-bar-chart>
</div>
</div>
angular.module("nvd3TestApp", ['nvd3ChartDirectives']);
function ExampleCtrl($scope) {
$scope.chartOptions = [{
id: 1,
name: "Option 1"
}, {
id: 2,
name: "Option 2"
}];
var d1 = [{
key: "Cumulative Return",
values: [
["A", -29.765957771107],
["B", 0],
["C", 32.807804682612],
["D", 196.45946739256],
["E", 0.19434030906893],
["F", -98.079782601442],
["G", -13.925743130903],
["H", -5.1387322875705]
]
}];
var d2 = [{
key: "Cumulative Return",
values: [
["A", -29.765957771107],
["B", 0],
["C", 32.807804682612],
["D", 196.45946739256],
["E", 0.19434030906893],
["F", -98.079782601442],
["G", -13.925743130903],
["H", -5.1387322875705]
]
}, {
key: "Cumulative Return2",
values: [
["A", 10.765957771107],
["B", 0],
["C", -32.807804682612],
["D", 96.45946739256],
["E", 0.19434030906893],
["F", -38.079782601442],
["G", -43.925743130903],
["H", -3.1387322875705]
]
}];
$scope.updateChart = function () {
if ($scope.selectedChart.chart === undefined || $scope.selectedChart.chart.id === 1) {
$scope.exampleData = d1;
}
if ($scope.selectedChart.chart !== undefined && $scope.selectedChart.chart.id === 2) {
$scope.exampleData = d2;
}
};
$scope.$on('tooltipShow.directive', function (event) {
//console.log('scope.tooltipShow', event);
});
$scope.$on('tooltipHide.directive', function (event) {
//console.log('scope.tooltipHide', event);
});
}
阅读Scott Allen关于使用ngOptions的文章是值得的。 http://odetocode.com/blogs/scott/archive/2013/06/19/using-ngoptions-in-angularjs.aspx
关于初始化选择值,请参阅此stackoverflow帖子:
How to have a default option in Angular.js select box