Search code examples
javascriptnodesdiagramfillgojs

GoJS: How can I change Node fill color?


I'm using GoJS to make the diagram.

My diagram configuration (the example from the official documentation):

function init() {
    //......
    // define the Node template
    myDiagram.nodeTemplate =
        $(go.Node, "Auto",
            new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
            // define the node's outer shape, which will surround the TextBlock
            $(go.Shape, "RoundedRectangle",
                {
                    parameter1: 20,  // the corner has a large radius
                    fill: $(go.Brush, "Linear", { 0: "rgb(254, 201, 0)", 1: "rgb(254, 162, 0)" }),
                    stroke: null,
                    portId: "",  // this Shape is the Node's port, not the whole Node
                    fromLinkable: true, fromLinkableSelfNode: true, fromLinkableDuplicates: true,
                    toLinkable: true, toLinkableSelfNode: true, toLinkableDuplicates: true,
                    cursor: "pointer"
                }),
            $(go.TextBlock,
                {
                    font: "bold 11pt helvetica, bold arial, sans-serif",
                    editable: true  // editing the text automatically updates the model data
                },
                new go.Binding("text").makeTwoWay())
        );
//......
}

I'm creating the nodes in the following way:

var nodeOperations = new Object();

    for (var i = 0; i < countState; i++) {           
            var json = {'id': i, 'loc': nodesCenters[i].x +' '+nodesCenters[i].y, 'text': markedStateTable['digitStates'][i] + ', ' + markedStateTable['namedStates'][i]};

            nodes.push(json);
    }

And now I need programmatically change the fill color for specific node. I'm trying this code:

var data = myDiagram.model.findNodeDataForKey(0);

    myDiagram.model.setDataProperty(data, "fill", "green");

But after that, my chart doesn't display. And there are no error in console. Should I set the new shape for the node? Or how can I do that? Thank you for your help!


Solution

  • Please specify the fill color in the nodeArray

    var nodeDataArray = [
        { key: 1, text: "Name", fill: "#ff5722", stroke: "#4d90fe", description: "geethu" }];
    

    Then add binding to the textBlock as

     $(go.Shape, "RoundedRectangle", {
                        fill: "#cce6ff" // the default fill, if there is no data-binding
            }, new go.Binding("fill", "fill"))
    
     myDiagram.nodeTemplate =
    
        $(go.Node, "Horizontal", {
                isTreeExpanded: false,
                click: showDetail
            },
            $(go.Panel, "Auto",
                $(go.Shape, "RoundedRectangle", {
                    fill: "#cce6ff", // the default fill, if there is no data-binding
                    stroke: "#6699ff",
                    height: 40,
                    strokeWidth: 2,
                    portId: "",
                    cursor: "pointer", // the Shape is the port, not the whole Node
                }, new go.Binding("fill", "fill")),
                $(go.TextBlock, {
                        editable: true
                    },
                    new go.Binding("text", "text"))
            ),
            $("TreeExpanderButton", { alignment: go.Spot.Bottom, alignmentFocus: go.Spot.Top }, { visible: true })
        );