relation.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. function Relation(legcerNo,regNo,data){
  2. this.legcerNo = legcerNo;
  3. this.regNo = regNo;
  4. this.data = data;
  5. this.zoomlevel = 1;
  6. this.w = 0;
  7. this.h = 0;
  8. this.index = -1;
  9. this.redrawflag = false;
  10. this.infovisdiv = null;
  11. this.svg = null;
  12. this.force = null;
  13. this.lines = null;
  14. this.nodes = null;
  15. this.mouseoverstatus = false;
  16. this.chargeVal = -1600;
  17. }
  18. Relation.prototype.init = function(){
  19. var thisClass = this;
  20. thisClass.w = $("#entrelation").width();
  21. thisClass.h = $("#entrelation").height();
  22. $("#entrelation-infovis").width(thisClass.w).append(
  23. '<div class="entrelation-controlls hidden-sm hidden-xs">'
  24. +'<div class="entrelation-item">'
  25. +'<div id="entrelation-fullscreen" class="entrelation-box" title_pos="left"><div class="entrelation-fullscreen"></div></div>'
  26. +'</div>'
  27. +'</div>'
  28. );
  29. $("#entrelation-fullscreen").click(function(){
  30. thisClass.initFullScreen();
  31. });
  32. if($(".entrelation-controlls").length>0){
  33. $(".entrelation-controlls").css({left:$("#entrelation").width()-$(".entrelation-controlls").width()});
  34. }
  35. //处理全屏
  36. $(document).on('webkitfullscreenchange mozfullscreenchange msfullscreenchange fullscreenchange', function(){
  37. if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement){
  38. //退出
  39. thisClass.cancelFullScreen();
  40. }
  41. });
  42. this.infovisdiv = document.getElementById("entrelation-infovis");
  43. document.onkeydown=function(event){
  44. var e = event || window.event || arguments.callee.caller.arguments[0];
  45. if(e && e.keyCode==122){
  46. event.preventDefault?event.preventDefault():window.event.returnValue = false;
  47. if(!!window.ActiveXObject || "ActiveXObject" in window){//ie下F11不能用,屏蔽掉
  48. if($(".exit").length == 0){
  49. return;
  50. }
  51. }
  52. thisClass.initFullScreen();
  53. }else if(e && e.keyCode==27){
  54. event.preventDefault?event.preventDefault():window.event.returnValue = false;
  55. thisClass.cancelFullScreen();
  56. }
  57. };
  58. $(window).resize(function(){
  59. if(!thisClass.redrawflag){
  60. //1秒后重绘
  61. thisClass.redrawflag=true;
  62. setTimeout(thisClass.redraw,1000);
  63. }
  64. firstresize=false;
  65. });
  66. if(document.getElementById("entrelation-infovis")){
  67. document.getElementById("entrelation-infovis").oncontextmenu=function(){return false;};
  68. }
  69. $("#entrelation-fullscreen").attr("title","进入全屏视图"+((!!window.ActiveXObject || "ActiveXObject" in window)?"":" (F11)"));
  70. ///////////////////////关系网实例////////////////////////////
  71. var htmls="<div style='position:absolute;top:0px;' class='legend'>";
  72. htmls= htmls+ "<div class='legend-text-bg' style='margin-top: 10px;'><div style='background-color: #ff9800;'></div><span style='margin-left: 20px;'>本企业</span></div>";
  73. htmls= htmls+ "<div class='legend-text-bg' style='margin-top: 10px;'><div style='background-color: #cddc39;opacity:0.5;'></div><span style='margin-left: 12px;'>其他企业</span></div>";
  74. htmls= htmls+ "<div class='legend-text-bg' style='margin-top: 10px;'><div style='background-color: #2196f3;'></div><span style='margin-left: 25px;'>个人</span></div>";
  75. htmls= htmls+ "<div class='legend-text-bg' style='margin-top: 10px;margin-bottom: 10px;'><div style='background-color: #cecece;'></div><span style='margin-left: 5px;'>注吊销企业</span></div>";
  76. htmls= htmls+ "<div><span style='font-size: 20px;font-weight: bold;color: #cecece;'>→</span><span style=''>投资关系(股东→企业)</span></div>";
  77. htmls= htmls+ "<div><span style='font-size: 20px;font-weight: bold;color: #cd93d7;'>→</span><span style=''>法定代表人</span></div>";
  78. htmls= htmls+ "<div style='margin:10px 0px;' class='hidden-sm hidden-xs'><span class='text-muted'>提示:点击鼠标右键可以删除不想看的节点。</span></div>";
  79. htmls= htmls+"</div>"
  80. $("#entrelation").append(htmls);
  81. try{
  82. this.dataProcess();
  83. this.makeRelation();
  84. }catch(e){}
  85. }
  86. Relation.prototype.dataProcess = function(){
  87. var reg = new RegExp(/(有限公司|有限责任公司|股份有限公司|总公司|分公司|公司|事务所|合伙企业)$|\(.*\)|\(.*\)/g);
  88. /*var provinces = [];
  89. $.ajax({
  90. url: "/js/provinceData.min.json",
  91. dataType: "json",
  92. cache: true,
  93. async: false,
  94. success: function(json){
  95. provinces = json;
  96. }
  97. });*/
  98. for(var i=0;i<this.data.nodes.length;i++){
  99. var nodeObj = this.data.nodes[i];
  100. nodeObj.text = $.trim(nodeObj.text);
  101. nodeObj.shortText = nodeObj.text;
  102. if(nodeObj.type == "e" || nodeObj.type == "ce"){
  103. var text = nodeObj.shortText;
  104. if(text.length > 2){//先替换结尾
  105. text = text.replace(reg,"");
  106. }
  107. if(text.length > 2){//再替换开头
  108. //text = this.filterEntName(text);
  109. }
  110. if(text.length > 2){//小于两个字符不生效
  111. nodeObj.shortText = text;
  112. }
  113. if(nodeObj.name == this.regNo){//先找本企业位置
  114. this.index = i;
  115. nodeObj["regcap"] = (typeof(d1) == "undefined")?0:d1;
  116. nodeObj["legcerno"] = this.legcerNo;
  117. }
  118. }
  119. }
  120. /*********************************连线处理***********************************/
  121. var minLink=-1,maxLink=-1;
  122. for(var i=0;i<this.data.links.length;i++){
  123. var linkObj = this.data.links[i];
  124. if(linkObj.target==this.index && this.data.nodes[linkObj.source].name==this.legcerNo){
  125. //this.data.nodes[linkObj.source].type="cp";
  126. linkObj.type = "cp"
  127. linkObj.isLegal = 1;
  128. }
  129. //
  130. this.data.nodes[linkObj.target]["istarget"] = 1;
  131. if(this.data.nodes[linkObj.source].name == this.data.nodes[linkObj.target].invlegcerno || this.data.nodes[linkObj.source].name == this.data.nodes[linkObj.target].legcerno){
  132. linkObj.isLegal = 1;
  133. }
  134. if(typeof(linkObj.invacconam) == "undefined"){
  135. continue;
  136. }
  137. var invacconam = new Number(linkObj.invacconam);
  138. if(minLink == -1 && maxLink == -1){
  139. minLink = invacconam;
  140. maxLink = invacconam;
  141. }else if(invacconam > maxLink){
  142. maxLink = invacconam;
  143. }else if(invacconam < minLink){
  144. minLink = invacconam;
  145. }
  146. }
  147. /////////////
  148. var linkSectionArray = this.getLineSectionArray(minLink,maxLink);
  149. for(var i=0;i<this.data.links.length;i++){
  150. var linksObj = this.data.links[i];
  151. linksObj["linkVal"] = 2;
  152. if(typeof(linkObj.invacconam) == "undefined"){
  153. continue;
  154. }
  155. var invacconam = new Number(linksObj.invacconam);
  156. for(var s in linkSectionArray){
  157. var sectionObj = linkSectionArray[s];
  158. if(invacconam >= sectionObj.minVal && invacconam < sectionObj.maxVal){
  159. linksObj["linkVal"] = sectionObj.linkVal;
  160. continue;
  161. }
  162. }
  163. }
  164. /*********************************节点处理***********************************/
  165. var minNode=-1,maxNode=-1;
  166. for(var i=0;i<this.data.nodes.length;i++){
  167. var nodeObj = this.data.nodes[i];
  168. if(nodeObj.type == "e" || nodeObj.type == "ce"){
  169. var regcap = nodeObj.regcap;
  170. if(minNode == -1 && maxNode == -1){
  171. minNode = regcap;
  172. maxNode = regcap;
  173. }else if(regcap > maxNode){
  174. maxNode = regcap;
  175. }else if(regcap < minNode){
  176. minNode = regcap;
  177. }
  178. }
  179. }
  180. var nodeSectionArray = this.getNodeSectionArray(minNode,maxNode);
  181. for(var i=0;i<this.data.nodes.length;i++){
  182. var nodeObj = this.data.nodes[i];
  183. nodeObj["nodeVal"] = 7;
  184. if(nodeObj.type == "e" || nodeObj.type == "ce"){
  185. var invacconam = new Number(nodeObj.invacconam);
  186. var regcap = new Number(nodeObj.regcap);
  187. for(var s in nodeSectionArray){
  188. var sectionObj = nodeSectionArray[s];
  189. if(regcap >= sectionObj.minVal && regcap < sectionObj.maxVal){
  190. nodeObj["nodeVal"] = sectionObj.nodeVal;
  191. continue;
  192. }
  193. }
  194. }
  195. }
  196. //计算点之间的距离
  197. var chargeArray = [[0,10],[10,20],[20,30],[30,40],[40,50],[50,60],[60,70],[70,80],[80,90],[90,100],[100,110]];
  198. var nodeCount = this.data.nodes.length;
  199. for(var i in chargeArray){
  200. if(nodeCount >= chargeArray[i][0] && nodeCount < chargeArray[i][1]){
  201. this.chargeVal += i*100;
  202. break;
  203. }else if(i == chargeArray.length){
  204. this.chargeVal += i*100;
  205. }
  206. }
  207. }
  208. Relation.prototype.filterEntName = function(text){
  209. this.allProvinces = "河南省、青海省、山西省、黑龙江省、安徽省、广西壮族自治区、西藏自治区";
  210. this.commonReplace = function(t){
  211. if(t.length > 1 && text.startWith(t)){
  212. var textCopy = text.replace(new RegExp("^"+t),"");
  213. if(textCopy.length > 2){
  214. text = textCopy;
  215. }
  216. return true;
  217. }
  218. return false;
  219. };
  220. this.replaceStart = function(t,f){
  221. var flag = false;
  222. var minus = null;
  223. if(this.commonReplace(t)){//替换省、市、县、区
  224. return true;
  225. }
  226. if(f){//不替换
  227. flag = false;
  228. }else if(t.endWith("维吾尔自治区")){
  229. flag = true;
  230. minus = 6;
  231. }else if(t.endWith("回族自治区") || t.endWith("壮族自治区") || t.endWith("特别行政区")){
  232. flag = true;
  233. minus = 5;
  234. }else if(t.endWith("自治州") || t.endWith("自治县") || t.endWith("自治区")){
  235. flag = true;
  236. minus = 3;
  237. }else if(t.endWith("地区")){
  238. flag = true;
  239. minus = 2;
  240. }else if(t.endWith("省") || t.endWith("市") || t.endWith("区")){
  241. flag = true;
  242. minus = 1;
  243. }
  244. if(flag){
  245. return this.commonReplace(t.substring(0,t.length-minus));
  246. }
  247. return false;
  248. };
  249. this.replaceCity = function(citys){
  250. if(citys){
  251. for(var c in citys){//市
  252. var c_name = citys[c].n;
  253. if(this.replaceStart(c_name)){
  254. this.replaceAreas(citys[c].s);
  255. return true;
  256. }else if(this.replaceAreas(citys[c].s)){
  257. return true;
  258. }
  259. }
  260. }
  261. };
  262. this.replaceAreas = function(areas){
  263. if(areas){
  264. for(var a in areas){//县、区
  265. var a_name = areas[a].n;
  266. if(this.replaceStart(a_name,true)){
  267. return true;
  268. }
  269. }
  270. }
  271. };
  272. for(var p in provinces){//省
  273. var p_name = provinces[p].n;
  274. if(this.allProvinces.indexOf(p_name) == -1){
  275. continue;
  276. }
  277. if(this.replaceStart(p_name)){
  278. this.replaceCity(provinces[p].s);
  279. break;
  280. }else if(this.replaceCity(provinces[p].s)){
  281. break;
  282. }
  283. }
  284. return text;
  285. }
  286. Relation.prototype.getLineSectionArray = function(minLink,maxLink){
  287. var countSection = 6;
  288. var section = (maxLink - minLink) / countSection;
  289. var sectionArr = [];
  290. for(var i=2;i<=countSection;i++){
  291. var minVal = minLink;
  292. if(sectionArr.length > 0){
  293. minVal = sectionArr[sectionArr.length - 1].maxVal;
  294. }
  295. var maxVal = minLink+section*i;
  296. if(i == countSection){
  297. maxVal += section;
  298. }
  299. var sectionObj = {linkVal:i,minVal:minVal,maxVal:maxVal};
  300. sectionArr.push(sectionObj);
  301. }
  302. return sectionArr;
  303. }
  304. Relation.prototype.getNodeSectionArray = function(minNode,maxNode){
  305. var countSection = 7;
  306. var section = (maxNode - minNode) / countSection;
  307. var sectionArr = [];
  308. for(var i=1;i<=countSection;i++){
  309. var minVal = minNode;
  310. if(sectionArr.length > 0){
  311. minVal = sectionArr[sectionArr.length - 1].maxVal;
  312. }
  313. var maxVal = minNode+section*i;
  314. if(i == countSection){
  315. maxVal += section;
  316. }
  317. var sectionObj = {nodeVal:6+i,minVal:minVal,maxVal:maxVal};
  318. sectionArr.push(sectionObj);
  319. }
  320. return sectionArr;
  321. }
  322. Relation.prototype.launchFullScreen = function(element) {
  323. if(element){
  324. if (element.requestFullscreen) {
  325. element.requestFullscreen();
  326. } else if (element.msRequestFullscreen) {
  327. element.msRequestFullscreen();
  328. } else if (element.mozRequestFullScreen) {
  329. element.mozRequestFullScreen();
  330. } else if (element.webkitRequestFullscreen) {
  331. // 对 Chrome 特殊处理,
  332. // 参数 Element.ALLOW_KEYBOARD_INPUT 使全屏状态中可以键盘输入。
  333. if ( window.navigator.userAgent.toUpperCase().indexOf( 'CHROME' ) >= 0 ) {
  334. element.webkitRequestFullScreen( Element.ALLOW_KEYBOARD_INPUT );
  335. }else {
  336. // Safari 浏览器中,如果方法内有参数,则 Fullscreen 功能不可用。
  337. element.webkitRequestFullScreen();
  338. }
  339. }
  340. }
  341. }
  342. Relation.prototype.cancelFullScreen = function(){
  343. this.h = $("#entrelation-infovis").height();
  344. $("#entrelation-fullscreen").attr("title","进入全屏视图"+((!!window.ActiveXObject || "ActiveXObject" in window)?"":" (F11)"));
  345. //force.linkDistance(120).charge(-200).size([w,h]).resume();
  346. $("#entrelation-infovis").css({width:this.w,height:this.h});
  347. //设置svg标签的宽度与高度
  348. d3.select("svg").attr("width", this.w).attr("height", this.h);
  349. $(".entrelation-controlls").css({left:$("#entrelation").width()-$(".entrelation-controlls").width()-30});
  350. if(document.exitFullscreen) {
  351. document.exitFullscreen();
  352. } else if (document.msExitFullscreen) {
  353. document.msExitFullscreen();
  354. } else if (document.mozCancelFullScreen) {
  355. document.mozCancelFullScreen();
  356. } else if (document.webkitExitFullscreen) {
  357. document.webkitExitFullscreen();
  358. }
  359. }
  360. Relation.prototype.initFullScreen = function(){
  361. var thisClass = this;
  362. var fullscreenElement =
  363. document.fullscreenElement ||
  364. document.mozFullScreenElement ||
  365. document.webkitFullscreenElement ||
  366. document.msFullscreenElement;
  367. $(".entrelation-fullscreen").toggleClass("exit");
  368. if(!fullscreenElement || fullscreenElement==null){
  369. this.redrawflag = true;
  370. $("#entrelation-fullscreen").attr("title","退出全屏视图 (F11)");
  371. this.launchFullScreen(this.infovisdiv);
  372. var timeout = 100;
  373. if(!!window.ActiveXObject || "ActiveXObject" in window){
  374. timeout = 200;
  375. }
  376. setTimeout(function(){
  377. if(thisClass.force != null){
  378. //重绘
  379. var w=document.body.clientWidth;
  380. var h=document.body.clientHeight;
  381. //if(w>1200){
  382. //w= 1200
  383. //}
  384. $("#entrelation-infovis").css({width:w,height:h});
  385. d3.select("svg").attr("width", w).attr("height", h);
  386. $(".entrelation-controlls").css({left:w-$(".entrelation-controlls").width() - 70});
  387. thisClass.force.linkDistance(h).charge(-700).size([w,h]).resume();
  388. }
  389. },timeout);
  390. }else{
  391. this.cancelFullScreen();
  392. }
  393. }
  394. //重绘
  395. Relation.prototype.redraw = function(){
  396. if(this.force != undefined){
  397. d3.select("svg").attr("width",this.w).attr("height", this.h);
  398. this.force.size([this.w,this.h]).resume();
  399. $("#entrelation-infovis").css({width:this.w,height:this.h});
  400. //
  401. this.redrawflag = false;
  402. }
  403. }
  404. Relation.prototype.removeNode = function(obj){
  405. if(d3.event.which != 3){
  406. return;
  407. }
  408. this.highlightObject(obj,false);
  409. d3.select(obj).remove();
  410. var name = obj.__data__.name;
  411. var removeAloneNode = function(removeNodeName){
  412. var isRemoveAloneNode = true;
  413. d3.selectAll("path").each(function (i){
  414. if(removeNodeName == this.__data__.source.name || removeNodeName == this.__data__.target.name){
  415. isRemoveAloneNode = false;
  416. return true;
  417. }
  418. });
  419. if(isRemoveAloneNode){
  420. d3.selectAll("rect").each(function (){
  421. if(this.__data__.name == removeNodeName){
  422. d3.select(this).remove();
  423. return true;
  424. }
  425. });
  426. d3.selectAll("text").each(function (){
  427. if(this.__data__.name == removeNodeName){
  428. d3.select(this).remove();
  429. return true;
  430. }
  431. });
  432. }
  433. }
  434. d3.selectAll("path").each(function (){
  435. if(name == this.__data__.target.name){
  436. var fromNodeName = this.__data__.source.name;
  437. d3.select(this).remove();
  438. removeAloneNode(fromNodeName);
  439. }else if(name == this.__data__.source.name){
  440. var toNodeName = this.__data__.target.name;
  441. d3.select(this).remove();
  442. removeAloneNode(toNodeName);
  443. }
  444. });
  445. d3.selectAll("rect").each(function (){
  446. if(name == this.__data__.name){
  447. d3.select(this).remove();
  448. }
  449. });
  450. d3.selectAll("text").each(function (){
  451. if(name == this.__data__.name){
  452. d3.select(this).remove();
  453. }
  454. });
  455. }
  456. Relation.prototype.tick = function(e){
  457. var thisClass = this;
  458. this.lines.attr("d", function(d){
  459. var sy = d.source.y,
  460. x = d.target.x,
  461. y = d.target.y;
  462. if(sy > y){
  463. sy -= 12;
  464. }else{
  465. sy += 5;
  466. }
  467. var line = new geo.LineSegment(d.source.x, sy, x, y);
  468. for (var e in d.target.edge) {
  469. var ix = line.intersect(d.target.edge[e].offset(x, y));
  470. if (ix.in1 && ix.in2) {
  471. x = ix.x;
  472. y = ix.y;
  473. break;
  474. }
  475. }
  476. var dx = x - d.source.x,
  477. dy = y - sy,
  478. dr = Math.sqrt(dx * dx + dy * dy),
  479. theta = Math.atan2(dy, dx) + Math.PI / 7.85,
  480. d90 = Math.PI / 2,
  481. dtxs = x - Math.cos(theta),
  482. dtys = y - Math.sin(theta);
  483. return "M" + d.source.x + "," + sy + "A" + dr + "," + dr + " 0 0 1," + x + "," + y + "A" + dr + "," + dr + " 0 0 0," + d.source.x + "," + sy + "M" + dtxs + "," + dtys + "l" + (3.5 * Math.cos(d90 - theta) - 10 * Math.cos(theta)) + "," + (-3.5 * Math.sin(d90 - theta) - 10 * Math.sin(theta)) + "L" + (dtxs - 3.5 * Math.cos(d90 - theta) - 10 * Math.cos(theta)) + "," + (dtys + 3.5 * Math.sin(d90 - theta) - 10 * Math.sin(theta)) + "z";
  484. });
  485. this.nodes.attr("transform", function(d) {
  486. return "translate(" + d.x + "," + d.y + ")scale(" + thisClass.zoomlevel+ ")";
  487. });
  488. }
  489. Relation.prototype.zoomed = function() {
  490. this.svg.attr("transform",
  491. "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
  492. }
  493. Relation.prototype.highlightObject = function(obj,flag){
  494. if(this.mouseoverstatus){
  495. return;
  496. }
  497. if(!flag){
  498. d3.selectAll(".node").each(function (){
  499. d3.select(this).style("opacity","1");
  500. });
  501. d3.selectAll("path").each(function (){
  502. d3.select(this).style("opacity","1");
  503. });
  504. return;
  505. }
  506. var name = obj.__data__.name;
  507. d3.selectAll(".node").each(function (){
  508. if(name != this.__data__.name){
  509. d3.select(this).style("opacity","0.2");
  510. }
  511. });
  512. d3.selectAll("path").each(function (){
  513. var targetName = this.__data__.target.name;
  514. var sourceName = this.__data__.source.name;
  515. if(name != targetName && name != sourceName){
  516. d3.select(this).style("opacity","0.2");
  517. }else{
  518. d3.selectAll(".node").each(function (){
  519. if(targetName == this.__data__.name || sourceName == this.__data__.name){
  520. d3.select(this).style("opacity","1");
  521. }
  522. });
  523. }
  524. });
  525. }
  526. Relation.prototype.makeRelation = function(){
  527. var thisClass = this;
  528. this.force = d3.layout.force()
  529. .nodes(this.data.nodes)
  530. .links(this.data.links)
  531. .size([this.w, this.h])
  532. .linkDistance(function(p){
  533. return Math.floor(Math.random()*60)+90;
  534. })
  535. .chargeDistance(600)
  536. .charge(this.chargeVal)
  537. .linkStrength(1)
  538. .on('tick',function(){
  539. thisClass.tick();
  540. });
  541. var dragstatus = false;
  542. var drag = this.force.drag()
  543. .on("dragstart",function(d,i){
  544. d.fixed = true; //拖拽开始后设定被拖拽对象为固定
  545. d3.event.sourceEvent.stopPropagation(); // Prevent panning
  546. })
  547. .on("drag",function(d,i){
  548. dragstatus = true;
  549. thisClass.mouseoverstatus = true;
  550. })
  551. .on("dragend",function(d,i){
  552. thisClass.mouseoverstatus = false;
  553. setTimeout(function(){
  554. dragstatus = false;
  555. },500)
  556. });
  557. //处理缩放
  558. var zoom = d3.behavior.zoom().scaleExtent([0.4, 10]).on("zoom", function(){
  559. thisClass.zoomed();
  560. });
  561. this.svg = d3.select("#entrelation-infovis").append("svg:svg")
  562. .attr("width", this.w)
  563. .attr("height", this.h)
  564. .call(zoom)
  565. .append("g");
  566. this.lines = this.svg.append("svg:g")
  567. .selectAll("path")
  568. .data(this.force.links())
  569. .enter().append("path")
  570. .style("fill", function(p){
  571. return (p.type=="cp" || p.isLegal)?"#cd93d7":"#cecece";
  572. })
  573. .style("stroke", function(p){
  574. return (p.type=="cp" || p.isLegal)?"#cd93d7":"#cecece";
  575. })
  576. .attr("stroke-width",function (e,i){
  577. return e.linkVal;
  578. });
  579. this.lines.each(function (d,i){
  580. var invacconam = new Number(d.invacconam);
  581. if(invacconam > 0){
  582. var lenInvacconam = (invacconam + "").length;
  583. if(lenInvacconam > 4 && (invacconam + "").indexOf(".") > -1 && (lenInvacconam - (invacconam + "").indexOf(".")) > 4){
  584. invacconam = (new Number(invacconam)).toFixed(4);
  585. }
  586. d3.select(this).append("title").text("投资金额:"+invacconam+"万元");
  587. }
  588. });
  589. this.nodes = this.svg.append("svg:g")
  590. .selectAll(".node")
  591. .data(this.force.nodes())
  592. .enter()
  593. .append("g")
  594. .call(this.force.drag)
  595. .attr('class', 'node')
  596. .on('mouseover', function(d) {
  597. thisClass.highlightObject(this,true);
  598. })
  599. .on('mouseout', function(d) {
  600. thisClass.highlightObject(this,false);
  601. })
  602. .on("mousedown",function (e,i){
  603. d3.event.preventDefault();
  604. thisClass.removeNode(this);
  605. });
  606. this.nodes.append("rect")
  607. .attr("rx", 5)
  608. .attr("ry", 5)
  609. .attr('height', 20)
  610. .attr("fill", function(p){
  611. if(p.type == "e" && (p.opstate == "11" || p.opstate == "07")){
  612. return "#cecece";
  613. }
  614. return p.type=="p"?"#2196f3":p.type=="ce"?"#ff9800":"#cddc39";
  615. })
  616. .attr("stroke", function(p){
  617. if(p.type == "e" && (p.opstate == "11" || p.opstate == "07")){
  618. return "#cecece";
  619. }
  620. return p.type=="p"?"#2196f3":p.type=="ce"?"#ff9800":"#cddc39";
  621. }).style("opacity",function(p){
  622. if(p.type=="e" && (p.opstate != "11" || p.opstate != "07")){
  623. return "0.5";
  624. }
  625. return "1";
  626. });
  627. this.nodes.each(function (d,i){
  628. if(d.type == "ce" || d.type == "e"){
  629. var title = "";
  630. if(d.text != d.shortText){
  631. title = d.text;
  632. }
  633. var regcap = d.regcap;
  634. if(regcap > 0){
  635. var lenRegcap = (regcap + "").length;
  636. if(lenRegcap > 4 && (regcap + "").indexOf(".") > -1 && (lenRegcap - (regcap + "").indexOf(".")) > 4){
  637. regcap = (new Number(regcap)).toFixed(4);
  638. }
  639. if(title != ""){
  640. title += "\n";
  641. }
  642. title += "注册资本:"+regcap+"万元";
  643. }
  644. if(title != ""){
  645. d3.select(this).append("title").text(title);
  646. }
  647. }
  648. });
  649. this.nodes.append('text')
  650. .text(function(d){
  651. return d.shortText;
  652. })
  653. .style("cursor",function (d,i){
  654. if(d.type == "e"){
  655. return "pointer";
  656. }else{
  657. return "auto";
  658. }
  659. })
  660. .on("click",function (d,i){
  661. if(!dragstatus && d.type == "e"){
  662. window.open("/enterpriseInfoByRegNO/"+d.name+".html");
  663. }
  664. });
  665. setTimeout(function() {
  666. thisClass.nodes.each(function(d) {
  667. var node = d3.select(this),
  668. text = node.selectAll('text'),
  669. bounds = {},
  670. first = true;
  671. text.each(function() {
  672. var box = this.getBBox();
  673. if (first || box.x < bounds.x1) {
  674. bounds.x1 = box.x;
  675. }
  676. if (first || box.y < bounds.y1) {
  677. bounds.y1 = box.y;
  678. }
  679. if (first || box.x + box.width > bounds.x2) {
  680. bounds.x2 = box.x + box.width;
  681. }
  682. if (first || box.y + box.height > bounds.y2) {
  683. bounds.y2 = box.y + box.height;
  684. }
  685. first = false;
  686. }).attr('text-anchor', 'middle');
  687. var padding = {"left":3,"right":3,"top":2,"bottom":2},
  688. margin = {"left":3,"right":3,"top":2,"bottom":2},
  689. oldWidth = bounds.x2 - bounds.x1;
  690. bounds.x1 -= oldWidth / 2;
  691. bounds.x2 -= oldWidth / 2;
  692. bounds.x1 -= padding.left;
  693. bounds.y1 -= padding.top;
  694. bounds.x2 += padding.left + padding.right;
  695. bounds.y2 += padding.top + padding.bottom;
  696. var width = bounds.x2 - bounds.x1;
  697. var height = bounds.y2 - bounds.y1;
  698. node.select('rect')
  699. .attr('x', bounds.x1)
  700. .attr('y', bounds.y1)
  701. //.attr('height', height)
  702. .attr('width', width);
  703. d.edge = {
  704. left : new geo.LineSegment(bounds.x1, bounds.y1, bounds.x1, bounds.y2),
  705. right : new geo.LineSegment(bounds.x2, bounds.y1, bounds.x2, bounds.y2),
  706. top : new geo.LineSegment(bounds.x1, bounds.y1, bounds.x2, bounds.y1),
  707. bottom : new geo.LineSegment(bounds.x1, bounds.y2, bounds.x2, bounds.y2)
  708. };
  709. });
  710. thisClass.force.start();
  711. },10);
  712. //var diagonal = d3.svg.diagonal().projection(function(d) { return [d.y, d.x]; });
  713. }