fishBone.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import html2canvas from "html2canvas"
  2. import $ from "jquery";
  3. (function(window,$,html2canvas){
  4. var fishBone = {};
  5. fishBone.gapStep = 250; // 两块的间隙
  6. fishBone.style = '#038dc5';// 颜色设定
  7. fishBone.option = {};
  8. fishBone.dowload = true; //是否提供下载
  9. fishBone.axisTpl = `<div class="fish-bone-axis"></div><div class="fish-bone-node dateNodeEnd"></div><div class="zhanwei"></div>`;
  10. fishBone.blockTpl = `
  11. <div class="fish-bone-block">
  12. <div class="fish-bone-line upper-line"></div>
  13. <div class="fish-bone-line lower-line"></div>
  14. <div class="fish-bone-node active-node"></div>
  15. <div class="fish-bone-content active-content label-content">
  16. </div>
  17. <div class="fish-bone-content date-conent active-content">
  18. </div>
  19. </div>
  20. `;
  21. /**line left 21
  22. * node left 20
  23. * label-content left 9
  24. * date-content left 14
  25. *
  26. * left should be increased by gapStep
  27. *
  28. * upperline bottom 50
  29. * lowerline top 50
  30. * node top 49.5
  31. * label-content bottom 54
  32. * date-content 54
  33. *
  34. */
  35. // 各元素的位置
  36. var lineLeft = 147,nodeLeft = 140,labelContentLeft = 50,
  37. dateContentLeft = 108,upperLineBottom = 206,lowerLineTop = 211,
  38. nodeTop1 = 197,nodeTop2 = 195,labelContentBottom = 230,dateContentTop = 210;
  39. //初始化插件
  40. fishBone.init = function(container,option){
  41. this.style = option.color?option.color:this.style;
  42. this.dowload = option.dowload?option.dowload:this.dowload;
  43. var data = option.data;
  44. var len = data.length;
  45. var tpl = '';
  46. tpl+=this.axisTpl;
  47. for(var i=0;i<len;i++){
  48. tpl+='<div class="fish-bone-block">';
  49. // tpl+='<div class="fish-bone-line upper-line" style="left:'+(lineLeft+this.gapStep*i)+'px;bottom:'+upperLineBottom+'px;"></div>';
  50. // tpl+='<div class="fish-bone-line lower-line" style="left:'+(lineLeft+this.gapStep*i)+'px;top:'+lowerLineTop+'px;"></div>';
  51. // tpl+='<div class="fish-bone-node" style="top:'+nodeTop+'px;left:'+(nodeLeft+this.gapStep*i)+'px;"></div>'
  52. if(i%2==0){
  53. var pos1 = 'top';
  54. var pos2 = 'bottom';
  55. var line = 'topline'
  56. }else{
  57. var pos1 = 'bottom';
  58. var pos2 = 'top';
  59. var line = 'bottomLine'
  60. }
  61. // if(data[i].label){
  62. tpl+='<div class="fish-bone-content label-content" style="left:'+(labelContentLeft+this.gapStep*i)+'px;'+pos1+':'+labelContentBottom+'px;background-color:'+data[i].backgroundColor+'">';
  63. tpl+=data[i].label;
  64. tpl+='</div>'
  65. tpl+='<div class="fish-bone-node labelNode" style="top:'+nodeTop2+'px;left:'+(nodeLeft+this.gapStep*i)+'px;border-color:'+data[i].lineColor+'"></div>'
  66. if(i%2==0){
  67. tpl+='<div class="fish-bone-line fish-bone-line1 lower-line" style="left:'+(lineLeft+this.gapStep*i)+'px;top:'+lowerLineTop+'px;background-color:'+data[i].lineColor+'"></div>';
  68. }else{
  69. tpl+='<div class="fish-bone-line fish-bone-line2 upper-line" style="left:'+(lineLeft+this.gapStep*i)+'px;bottom:'+upperLineBottom+'px;background-color:'+data[i].lineColor+'"></div>';
  70. }
  71. // }
  72. // if(data[i].date){
  73. // tpl+='<div class="fish-bone-node dateNode" style="top:'+nodeTop1+'px;left:'+(nodeLeft+this.gapStep*i)+'px;background-color:'+data[i].lineColor+'"></div>'
  74. tpl+='<div class=" date-conent" style="left:'+(dateContentLeft+this.gapStep*i)+'px;'+pos2+':'+dateContentTop+'px;">';
  75. tpl+=data[i].date;
  76. tpl+='</div>';
  77. // }
  78. tpl+='</div>';
  79. }
  80. tpl+='<div class="fish-bone-node dateNode"></div>';
  81. tpl+='<div class="start">Start</div>';
  82. $(container).html(tpl);
  83. // 增加轴的长度
  84. var scale = Math.ceil(len/3);
  85. $(".fish-bone-axis").css({"width":(660*scale+"px")});
  86. $(".dateNodeEnd").css({"left":((660*scale+44)+"px")});
  87. $(".zhanwei").css({"left":((660*scale+60)+"px")});
  88. //设置节点的边框颜色
  89. var color = this.style;
  90. // $(".fish-bone-node").css({"border-color":color});
  91. //插入下载按钮
  92. // if(this.dowload){
  93. // var $dowload = $('<span class="icon fish-bone-download">&#xe87b;</span>')
  94. // .css({"left":"850px","top":"15px"});
  95. // $(container).append($dowload);
  96. // }
  97. this.attachClickEvent(container);
  98. }
  99. // 绑定点击事件
  100. fishBone.attachClickEvent = function(container){
  101. var color = this.style;
  102. $(".fish-bone-block").on("click",function(){
  103. $(".fish-bone-content").css({"background-color":"#f5f5f5"});
  104. $(".fish-bone-node").removeClass("active-node");
  105. $(this).find(".fish-bone-content").css({"background-color":color});
  106. $(this).find(".fish-bone-node").addClass("active-node");
  107. });
  108. $(container+" .fish-bone-download").on("click",function(){
  109. $(container+" .fish-bone-content").removeClass("active-content").css({"background-color":"#f5f5f5"});
  110. $(container+" .fish-bone-node").removeClass("active-node");
  111. html2pic(container);
  112. });
  113. }
  114. // 转图片
  115. function html2pic(container){
  116. var el = $(container).get(0);
  117. var copy = $(container).clone();
  118. if(html2canvas){
  119. $width = $(".fish-bone-axis").width()
  120. // $(container).width($width);
  121. copy.width($width);
  122. // $height = $(container).parent().height()
  123. // $(container).height($height)
  124. $('body').append(copy);
  125. el = copy.get(0)
  126. html2canvas(el, {
  127. onrendered: function(canvas) {
  128. var url = canvas.toDataURL();
  129. var triggerDownload = $("<a>").attr("href", url).attr("download", "下载.png").appendTo("body");
  130. triggerDownload[0].click();
  131. triggerDownload.remove();
  132. copy.remove();
  133. }
  134. });
  135. }
  136. }
  137. window.fishBone = fishBone;
  138. })(window,$,html2canvas);