如何为arbor js中的每个节点文本添加换行符

时间:2013-02-12 17:42:55

标签: javascript canvas arbor.js

我有一个网络,有14个节点,每个节点都有一个标签,还有用于将这些节点相互连接的边。我试图为其中一个节点输入一个长标签,不幸的是,似乎arborjs只在水平方向上显示节点上的标签,所以我试图在标签文本“\ n”中添加一个新的行字符并将其渲染为空间,所以我想知道是否有人知道如何在arbor js中为节点设置多个线路标签? 这是代码:

<script language="javascript" type="text/javascript">
    var sys = arbor.ParticleSystem(1000, 700,0.01);
    sys.parameters({gravity:false});
    sys.renderer = Renderer("#viewport") ;
    var data = {
    nodes:{
    STRUCTURE:{'color':'black','shape':'rect','label':'STRUCTURE', },
    Engineering:{'color':'salmon','shape':'rect','label':'Engineering'},
    Architecture:{'color':'salmon','shape':'rect','label':'Architecture'},
    ArtsSciences:{'color':'salmon','shape':'rect','label':'Arts & Sciences'},
    EarthEnergy:{'color':'salmon','shape':'rect','label':'Earth & Energy'},
    SustainableDesign:s{'color':'lightskyblue','shape':'rect','label':'Sustainable Design'},
    sutabledesignleaf1:{'color':'lawngreen','shape':'rect','label':'Earthen Structures'},
    MaterialsStructures:{'color':'lightskyblue','shape':'rect','label':'Materials & Structures'},
    MaterialsStructuresleaf1:{'color':'lawngreen','shape':'rect','label':'AEROSPACE/MECH. ENGINEERING'},
    LithosphereDynamics:{'color':'lightskyblue','shape':'rect','label':'Lithosphere Dynamics'},
    Energy:{'color':'lightskyblue','shape':'rect','label':'Energy'},
    LithosphereDynamicsleaf1:{'color':'lawngreen','shape':'rect','label':'Structure/Tectonophy'},
    Energyleaf1:{'color':'lawngreen','shape':'rect','label':'Structural Control on Reservoirs'},
    ArtsSciencesleaf1:{'color':'lawngreen','shape':'rect','label':'VARIOUS THEMES Market Structure'},
    },
    edges:{
    STRUCTURE:{ Engineering:{}, Architecture:{} , ArtsSciences:{}, EarthEnergy:{}},
    ArtsSciences:{ArtsSciencesleaf1:{}},
    EarthEnergy:{Energy:{},LithosphereDynamics:{}},
    Energy:{Energyleaf1:{}},
    LithosphereDynamics:{LithosphereDynamicsleaf1:{}},
    Engineering:{MaterialsStructures:{}},
    MaterialsStructures:{MaterialsStructuresleaf1:{}},
    Architecture:{SustainableDesign:{}},
    SustainableDesign:{sutabledesignleaf1:{}}
    }
    };
    sys.graft(data);
        var canvas =  document.selectElementById('viewport') ;  
    var context  =  canvas.getContext('2d');
      context.font = '40pt Calibri';
    context.fillStyle = 'blue';
</script>

假设在这段代码中,对于STRUCTURE节点的标签,我们有“STRUCTURE”,但是当我想要一个长文本谎言“kaskdjhkjahdkjhaskjdhjkahskjdhakjshdkjahdkjhaskjdhkjahsdkjhakjsdhkjashdkjhasdkjhkjshdkjhakjdhkajshdk”时它不会将它分成两行,它将显示为一行,并且即使我放了一个新的线条字符,它也会将它视为一个空格,它会再次显示在一行中,任何帮助都会受到赞赏。 提前致谢

3 个答案:

答案 0 :(得分:1)

感谢您的回答,昨晚,我发现不幸的是,在画布上没有办法fillText()在多行中绘制文字,所以我必须为此编写我的包装,这是我写的函数,它现在正在运作,

function wrap_text(var rawstring,var line_width)
                {
                    var strarray  = new Array() ;  
                    var temp_str = new Array() ; 
                    var j = 0 ;var i = 0 ;  
                    strarray = text.split(' ');                 
                    var temp = strarray[j] ;
                    j++;
                    while(j < strarray.length)
                    {
                        while(temp.length <30)
                        {
                            temp = temp+" "+strarray[j];
                            j++;
                        }   
                        temp_str[i]=temp ; i++;
                        var temp = "" ;
                    }
                    return temp_str; 
                }
                var wrap_result = wrap_text('kjakjhkjashd kajasd asdmbdmnad nmauhiqwe kbawem mnbasdm',20);

                for (var i = 0; i <wrap_result.length ; i--) 
                {
                    console.log(wrap_result[i]);
                }

答案 1 :(得分:1)

我知道这篇帖子已经晚了2年,虽然我仍然遇到同样的问题但无法在任何地方找到解决方案。主要问题是canvas fillText不支持多行。所以你必须在标签上添加多行。诀窍是将返回分解为一个数组,并为每个新行循环遍历数组。

为了保持代码清洁,我宁愿不直接编辑文件中的函数。要使多行工作,请将以下函数添加到自定义JS中,该函数将覆盖Cytoscape内部标签呈现。适用于cytoscape.js和amp; cytoscape.min.js

//function to override the default label rending to support multiple lines
;(function($$){ 'use strict';
  var CanvasRenderer = $$('renderer', 'canvas');
    CanvasRenderer.prototype.drawText = function(context, element, textX, textY) {
    var style = element._private.style;
    var parentOpacity = element.effectiveOpacity();
    if( parentOpacity === 0 ){ return; }

    var text = this.setupTextStyle( context, element );

    if ( text != null && !isNaN(textX) && !isNaN(textY) ) {

      var lineWidth = 2  * style['text-outline-width'].value; // *2 b/c the stroke is drawn centred on the middle
      if (lineWidth > 0) {
        context.lineWidth = lineWidth;
        context.strokeText(text, textX, textY);
      }

      //START NEW CODE
      //remove old label rendinging
      //context.fillText(text, textX, textY);

      //explode the text into an array split up by line returns
      var lines = text.split("\n");

      //loop through each of the lines
      for (var index = 0; index < lines.length; ++index) {
        //render the multiple lines
        context.fillText(lines[index], textX, (textY + (style['font-size'].pxValue * 1.2 * index)));
      }
      //END NEW CODE
    }
  };

})( cytoscape );

答案 2 :(得分:0)

此问题确实与canvas.fillText()有关,而不是arbor.js,请参阅this question。简而言之,这是fillText函数的限制。

那就是说,你可以相对简单地解决这个问题。以下全部假设您将渲染基于"atlas" example,但您可以从我想到的想法中找出答案。

      // draw the text
      if (label){
        ctx.font = "bold 11px Arial"
        ctx.textAlign = "center"

        // if (node.data.region) ctx.fillStyle = palette[node.data.region]
        // else ctx.fillStyle = "#888888"
        ctx.fillStyle = "#888888"

        // ctx.fillText(label||"", pt.x, pt.y+4)
        ctx.fillText(label||"", pt.x, pt.y+4)
      }

查看上面的代码,了解文本的呈现方式。我们可以使用基于您想要渲染的多行的循环替换行ctx.fillText(label||"", pt.x, pt.y+4)。将您的标签值设为line 1\nline 2\nline 3,然后使用String.split()将其设置为如下数组:

var lines = label.split("\n");

现在,循环浏览lines并为每个呈现ctx.fillText()。如果你的项目基于示例,你可能需要调整布局以使其看起来正确,但希望这能让你走上正确的轨道。