本文介绍了如何在cytoscape.js中创建自定义样式映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将自定义映射器添加到新的cytoscape.js?

Is there a way to add a custom mapper to the new cytoscape.js?

我知道有data(nodeKey),但是它使用了nodeKey的值de novo 。可以设置自己的映射吗?

I know there is data(nodeKey), but that uses nodeKey's value de novo. Can I set a mapping of my own?

谢谢!

推荐答案

这是我的自定义映射器:

Here is my custom mapper:

    /* Converts element attributes to their appropriate mapped values
     * Any non-matching attributes will be matched to the "other" mapping
     *     if exists
        * data: data
        * elementType: nodes or edges
        * attr: some key under data.nodes[i].data
        * mapping: obj mapping oldVal: newVal for attr
        * (toType): new values will be put into this attr, if attr 
        *   shouldn't be touched
    */
    function mapAttr(elementType, attr, mapping, toType){
        for(var i=0; i < data[elementType].length; i++){
            element = data[elementType][i]['data'][attr];
            toType = toType ? toType : attr;
            if( mapping[element] ){
                data[elementType][i]['data'][toType] = mapping[element];
            }else if(mapping['other']){
                data[elementType][i]['data'][toType] = mapping['other'];
            }
        }
    }

示例:

    var nodeShapeMapper = {
        Rearrangement: "hexagon",
        Gene: "octagon",
        Molecule: "triangle",
        other: "ellipse"
    };
    mapAttr('nodes', 'ntype', nodeShapeMapper, 'shape');

这会根据nodeShapeMapper [ntype]

This generates values for the "shape" node attribute according to nodeShapeMapper[ntype]

这篇关于如何在cytoscape.js中创建自定义样式映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:46