[英]How can I get this D3.js HTML element in my AngularJS so that I can manipulate it?
我正在嘗試使用D3.js圖形庫為我的網頁上的Javascript繪制圖形。 我可以使用HTML在SVG上繪制一條灰線。 我可以在我在Javascript中創建的新SVG容器上使用Javascript繪制一條紅線。 但我無法弄清楚如何在HTML中創建的SVG元素上繪制readline。
我嘗試了var svgContainer = document.getElementById("my_svg_widget")
但是沒有用。
如何在已在HTML文件中聲明的SVG元素上繪制紅線(與灰線的端點相同)
這是Plunker: https ://plnkr.co/edit/F6deVZU3NNADOeIdC5Iy ? p = preview
以下是我的文件供您參考。
使用Javascript:
myApp = angular.module('myApp', ['ui.router'] );
myApp.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
$stateProvider.state('myState', {url: '/myState', params: {slug: {value: null, squash: true} }, templateUrl: 'my-state-page1.html', controller: 'MyStateCtrl'} );
}
);
myApp.controller('MyStateCtrl', function($scope, $http) {
var self = this;
$scope.$watch(function() {return self.myDataFromAPI}, function (objVal) {
console.log('objVal = ', objVal);
x = objVal.origin.split('.');
console.log("X = ", x)
var svgContainer = d3.select("body")
.append("svg")
.attr("width", 1000)
.attr("height", 1000);
var line = svgContainer.append("line")
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", Number(x[2]))
.attr("y2", Number(x[3]))
.attr("stroke-width", 2)
.attr("stroke", "red");
},
true
);
self.httpFailure = function(response) {
console.log('Failure');
self.myDataFromAPI = null;
}
self.httpSuccess = function(response) {
console.log('\n\n\nGot the data from the API!');
self.myDataFromAPI = response.data;
console.log('\n\n\self.myDataFromAPI =', self.myDataFromAPI);
}
$http.get(
'https://httpbin.org/get'
).then(self.httpSuccess, self.httpFailure);
}
);
myApp.directive('mypMyDirective',function(){
return {
restrict:'E',
scope: {
myDataFromAPI: '='
},
controller: 'MyStateCtrl',
controllerAs: 'myStateCtrl',
bindToController: true,
templateUrl: 'myD3Diagram.html'
};
}
);
index.html的:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.18/angular-ui-router.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
HELLO!
<div ng-controller="MyStateCtrl as myStateCtrl">
<myp-my-directive mydatafromapi="myStateCtrl.myDataFromAPI"></myp-my-directive>
</div>
</body>
</html>
myD3Diagram.html:
<svg id="my_svg_widget" width="500" height="500">
<line x1="5" y1="5" x2="40" y2="40" stroke="gray" stroke-width="5" />
</svg>
在這里,每次調用watch函數時,你都會附加一個新的svg。
var svgContainer = d3.select("body")
.append("svg")
.attr("width", 1000)
.attr("height", 1000);
在你的watch函數中正確的方式而不是創建一個新的svg只需要選擇d3.select("svg")
或d3.select("#my_svg_widget")
來獲取svg已經存在並向其添加行。
$scope.$watch(function() {return self.myDataFromAPI}, function (objVal) {
console.log('objVal = ', objVal);
x = objVal.origin.split('.');
console.log("X = ", x)
var svgContainer = d3.select("svg");
var line = svgContainer.append("line")
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", Number(x[2]))
.attr("y2", Number(x[3]))
.attr("stroke-width", 2)
.attr("stroke", "red");
},
true
);
在這里工作代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.