【问题标题】:I want joint.js library to read my JSON and display it as a cell: rect and circle我希望joint.js 库读取我的JSON 并将其显示为一个单元格:rect 和circle
【发布时间】:2013-12-04 15:00:17
【问题描述】:

我有一个像这样的 json 数据结构:

var json1 = {
"places": [ { "id":0, "x":0.0, "y":0.0, "width":10.0, "height":10.0 },
            { "id":1, "x":50.0, "y":0, "width":10.0, "height":10.0 },
            { "id":2, "x":0.0, "y":30.0, "width":10.0, "height":10.0 },
            { "id":3, "x":50.0, "y":30.0, "width":10.0, "height":10.0 } ],
"transitions": [ { "id":0, "x":20.0, "y":20.0, "width":20.0, "height":10.0, "label":"Hello" } ],
"ptlinks": [ { "src":0, "dst":0, "expr":"x=0" },
             { "src":1, "dst":0, "expr":"y=0" } ],
"tplinks": [ { "src":0, "dst":1 },
             { "src":0, "dst":3 } ],
"name"": "Client"
}

我想使用这些数据来绘制一个图形,其中元素转换为矩形,并放置为带有链接的圆形......

    <script language="javascript">
var graph = new joint.dia.Graph;


var paper = new joint.dia.Paper({
    el: $('#main_petri'),
    width: 960,
    height: 500,
    model: graph
});

var rect = new joint.shapes.basic.Rect({
    position: { x: 100, y: 30 },
    size: { width: 100, height: 30 },
    attrs: { rect: { fill: '#FFFFFF' }, text: { text: '#', fill: '#000000' } }
});
    var rect2 = rect.clone();
rect2.translate(0,50);

var link = new joint.dia.Link({
        source: { id: rect.id },
        target: { id: rect2.id }
    });
graph.addCells([rect, rect2, link]);

如何在jointjs中使用JSON(位置、大小...)?

【问题讨论】:

    标签: javascript json jointjs


    【解决方案1】:

    您可以遍历您的位置/过渡和链接并创建 JointJS 元素/链接。比如:

    _.each(json1.places, function(p) {
        graph.addCell(new joint.shapes.pn.Place({
            id: 'place' + p.id,
            position: { x: p.x, y: p.y },
            size: { width: p.width, height: p.height }
        }));
    });
    _.each(json1.transitions, function(t) {
        graph.addCell(new joint.shapes.pn.Transition({
            id: 'transition' + t.id,
            position: { x: t.x, y: t.y },
            size: { width: t.width, height: t.height },
            attrs: { '.label': { text: t.label } }
        }));
    });
    _.each(json1.ptlinks, function(l) {
        graph.addCell(new joint.dia.Link({
            source: { id: 'place' + l.src },
            target: { id: 'transition' + l.dst },
            labels: [ { position: .5, attrs: { text: { text: l.expr } } } ]
        }));
    });
    _.each(json1.tplinks, function(l) {
        graph.addCell(new joint.dia.Link({
            source: { id: 'transition' + l.src },
            target: { id: 'place' + l.dst },
            labels: [ { position: .5 } ]
        }));
    });
    

    【讨论】:

    • 非常感谢 - 我会试试看。
    • 我知道这不是发布此评论的地方,但我不明白为什么我不能“提问”......“抱歉,我们不再接受来自此帐户的问题。 '解释是“Stack Exchange 有自动过滤器来禁止来自过去提供许多低质量问题的帐户的问题......”,太棒了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    相关资源
    最近更新 更多