簡體   English   中英

如何在Go.js中的colorchange上更新json

[英]How to update json on colorchange in Go.js

我正在使用Go.js實現流程圖並將json數據保存到sql中。 但是我無法更新json數據。 這是我的代碼

myDiagram.addDiagramListener("ChangedSelection", function (e1) {
      var sel = e1.diagram.selection;
      var str = "";
      if (sel.count === 0) {
        str = "Selecting nodes in the main Diagram will display information here.";
        info.innerHTML = str;
        return;
      } else if (sel.count > 1) {
        str = sel.count + " objects selected.";
        info.innerHTML = str;
        return;
      }
      // One object selected, display some information
        var elem = sel.first();

        var shape = elem.findObject("SHAPE");
        var txtblock = elem.findObject("TEXT");
        str += "<h3>Selected Node:</h3>";
        str += "<p>Figure: " + shape.figure + "</p>";
        str += "<p>Text: " + txtblock.text + "</p>";
        var strokeColor = shape.stroke;
        str += '<p style="float: left; margin-right: 10px;">Color: <input type="text" id="custom" /></p>';
        info.innerHTML = str;

      // Initialize color picker
      $("#custom").spectrum({
          color: strokeColor,

          // Change colors by constructing a gradient
          change: function(color) {
            var c = color.toRgb();
            var r,g,b;
            var grad1 = new go.Brush(go.Brush.Linear);
            r = Math.min(c.r + 10, 255);
            g = Math.min(c.g + 10, 255);
            b = Math.min(c.b + 10, 255);
            grad1.addColorStop(0, "rgb(" + r +","+ g +","+ b + ")");
            grad1.addColorStop(0.5, color.toRgbString());
            r = Math.max(c.r - 30, 0);
            g = Math.max(c.g - 30, 0);
            b = Math.max(c.b - 30, 0);
            grad1.addColorStop(1, "rgb(" + r +","+ g +","+ b+  ")");
            shape.fill = grad1;
            shape.stroke = "rgb(" + r +","+ g +","+ b+  ")";
            alert(myDiagram.model.toJson());
            txtblock.stroke = (r < 100 && g < 100 && b < 100) ? "white" : "black";
          }
      });

    });

我需要這樣的json數據

 { "class": "go.GraphLinksModel",
  "linkFromPortIdProperty": "fromPort",
  "linkToPortIdProperty": "toPort",
  "nodeDataArray": [ {"category":"Start", "text":"Start", "key":-1, "loc":"-30 -301", "color":"rgb(196,0,0)"} ],
  "linkDataArray": [  ]} 

如果我靜態添加顏色元素,則可以使用。 但不要在顏色變化時更新jsondata。

我找到了答案。 我必須在節點形狀中創建一個新對象以更改顏色。

new go.Binding("stroke", "color").makeTwoWay()),

我的完整節點功能將如下所示。

myDiagram.nodeTemplateMap.add("Start",
      GO(go.Node, "Spot", nodeStyle(),
        GO(go.Panel, "Auto",
          GO(go.Shape, "Circle",new go.Binding("fill", "color"),
            { minSize: new go.Size(40, 60), fill: "#79C900", stroke: null ,name: "SHAPE"},
            new go.Binding("stroke", "color").makeTwoWay()),
          GO(go.TextBlock, "Start",
            { margin: 5, font: "bold 11pt Helvetica, Arial, sans-serif", name: "TEXT", stroke: lightText })
        ),
        // three named ports, one on each side except the top, all output only:
        makePort("L", go.Spot.Left, true, false),
        makePort("R", go.Spot.Right, true, false),
        makePort("B", go.Spot.Bottom, true, false)
      ));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM