javascript - 在 Django 页面中使用时,Infovis 不会布局图表

标签 javascript python django infovis

我正在尝试集成an exampleJavaScript InfoVis Toolkit绘制到我的 Django 应用程序中。

现在,我只想在我的 Django 页面中显示上述示例图。我创建了下面给出的模板。

但它没有按预期工作。原始图表在网页加载后立即布局。

我的图表不是,需要手动将节点移动到它们之间的链接。

您可以在 video 中了解它是如何工作的.

我应该在模板和/或脚本和/或 CSS 文件中更改哪些内容,以便在加载页面后立即布置图表(如原始示例所示)?

这是我的 Django 模板:

<!DOCTYPE html>

{% block prehtml %}
{% endblock %}

<html>
<head>
    <title>{% block title %}{% endblock %}</title>
    <link href="{{ MEDIA_URL }}css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="{{ MEDIA_URL }}css/graphs.css" rel="stylesheet" media="screen">
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript" src="{{ MEDIA_URL }}js/jit.js"></script>
    <script type="text/javascript">
        var labelType, useGradients, nativeTextSupport, animate;

        (function() {
          var ua = navigator.userAgent,
              iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
              typeOfCanvas = typeof HTMLCanvasElement,
              nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
              textSupport = nativeCanvasSupport 
                && (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
          //I'm setting this based on the fact that ExCanvas provides text support for IE
          //and that as of today iPhone/iPad current text support is lame
          labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
          nativeTextSupport = labelType == 'Native';
          useGradients = nativeCanvasSupport;
          animate = !(iStuff || !nativeCanvasSupport);
        })();

        var Log = {
          elem: false,
          write: function(text){
            if (!this.elem) 
              this.elem = document.getElementById('log');
            this.elem.innerHTML = text;
            this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
          }
        };


        function init(){
          // init data
          var json = [
              {
                "adjacencies": [
                    "graphnode21", 
                    {
                      "nodeTo": "graphnode1",
                      "nodeFrom": "graphnode0",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode13",
                      "nodeFrom": "graphnode0",
                      "data": {
                        "$color": "#909291"
                      }
                    }, {
                      "nodeTo": "graphnode14",
                      "nodeFrom": "graphnode0",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode15",
                      "nodeFrom": "graphnode0",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode16",
                      "nodeFrom": "graphnode0",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode17",
                      "nodeFrom": "graphnode0",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }
                ],
                "data": {
                  "$color": "#83548B",
                  "$type": "circle",
                  "$dim": 10
                },
                "id": "graphnode0",
                "name": "graphnode0"
              }, {
                "adjacencies": [
                    {
                      "nodeTo": "graphnode2",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode4",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#909291"
                      }
                    }, {
                      "nodeTo": "graphnode5",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#909291"
                      }
                    }, {
                      "nodeTo": "graphnode6",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#909291"
                      }
                    }, {
                      "nodeTo": "graphnode7",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#909291"
                      }
                    }, {
                      "nodeTo": "graphnode8",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#909291"
                      }
                    }, {
                      "nodeTo": "graphnode10",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode11",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#909291"
                      }
                    }, {
                      "nodeTo": "graphnode12",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#909291"
                      }
                    }, {
                      "nodeTo": "graphnode13",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode14",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode15",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode16",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#909291"
                      }
                    }, {
                      "nodeTo": "graphnode17",
                      "nodeFrom": "graphnode1",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }
                ],
                "data": {
                  "$color": "#EBB056",
                  "$type": "circle",
                  "$dim": 11
                },
                "id": "graphnode1",
                "name": "graphnode1"
              }, {
                "adjacencies": [
                    {
                      "nodeTo": "graphnode5",
                      "nodeFrom": "graphnode2",
                      "data": {
                        "$color": "#909291"
                      }
                    }, {
                      "nodeTo": "graphnode9",
                      "nodeFrom": "graphnode2",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode18",
                      "nodeFrom": "graphnode2",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }
                ],
                "data": {
                  "$color": "#416D9C",
                  "$type": "circle",
                  "$dim": 7
                },
                "id": "graphnode2",
                "name": "graphnode2"
              }, {
                "adjacencies": [
                    {
                      "nodeTo": "graphnode5",
                      "nodeFrom": "graphnode3",
                      "data": {
                        "$color": "#909291"
                      }
                    }, {
                      "nodeTo": "graphnode9",
                      "nodeFrom": "graphnode3",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode10",
                      "nodeFrom": "graphnode3",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode12",
                      "nodeFrom": "graphnode3",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }
                ],
                "data": {
                  "$color": "#416D9C",
                  "$type": "square",
                  "$dim": 10
                },
                "id": "graphnode3",
                "name": "graphnode3"
              }, {
                "adjacencies": [],
                "data": {
                  "$color": "#83548B",
                  "$type": "square",
                  "$dim": 11
                },
                "id": "graphnode4",
                "name": "graphnode4"
              }, {
                "adjacencies": [
                  {
                    "nodeTo": "graphnode9",
                    "nodeFrom": "graphnode5",
                    "data": {
                      "$color": "#909291"
                    }
                  }
                ],
                "data": {
                  "$color": "#C74243",
                  "$type": "triangle",
                  "$dim": 8
                },
                "id": "graphnode5",
                "name": "graphnode5"
              }, {
                "adjacencies": [
                    {
                      "nodeTo": "graphnode10",
                      "nodeFrom": "graphnode6",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode11",
                      "nodeFrom": "graphnode6",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }
                ],
                "data": {
                  "$color": "#83548B",
                  "$type": "circle",
                  "$dim": 11
                },
                "id": "graphnode6",
                "name": "graphnode6"
              }, {
                "adjacencies": [],
                "data": {
                  "$color": "#EBB056",
                  "$type": "triangle",
                  "$dim": 12
                },
                "id": "graphnode7",
                "name": "graphnode7"
              }, {
                "adjacencies": [],
                "data": {
                  "$color": "#C74243",
                  "$type": "star",
                  "$dim": 10
                },
                "id": "graphnode8",
                "name": "graphnode8"
              }, {
                "adjacencies": [],
                "data": {
                  "$color": "#83548B",
                  "$type": "circle",
                  "$dim": 12
                },
                "id": "graphnode9",
                "name": "graphnode9"
              }, {
                "adjacencies": [
                  {
                    "nodeTo": "graphnode11",
                    "nodeFrom": "graphnode10",
                    "data": {
                      "$color": "#909291"
                    }
                  }
                ],
                "data": {
                  "$color": "#70A35E",
                  "$type": "triangle",
                  "$dim": 11
                },
                "id": "graphnode10",
                "name": "graphnode10"
              }, {
                "adjacencies": [],
                "data": {
                  "$color": "#70A35E",
                  "$type": "circle",
                  "$dim": 11
                },
                "id": "graphnode11",
                "name": "graphnode11"
              }, {
                "adjacencies": [],
                "data": {
                  "$color": "#83548B",
                  "$type": "triangle",
                  "$dim": 10
                },
                "id": "graphnode12",
                "name": "graphnode12"
              }, {
                "adjacencies": [
                  {
                    "nodeTo": "graphnode14",
                    "nodeFrom": "graphnode13",
                    "data": {
                      "$color": "#557EAA"
                    }
                  }
                ],
                "data": {
                  "$color": "#EBB056",
                  "$type": "star",
                  "$dim": 7
                },
                "id": "graphnode13",
                "name": "graphnode13"
              }, {
                "adjacencies": [],
                "data": {
                  "$color": "#EBB056",
                  "$type": "triangle",
                  "$dim": 12
                },
                "id": "graphnode14",
                "name": "graphnode14"
              }, {
                "adjacencies": [
                    {
                      "nodeTo": "graphnode16",
                      "nodeFrom": "graphnode15",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode17",
                      "nodeFrom": "graphnode15",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }
                ],
                "data": {
                  "$color": "#83548B",
                  "$type": "triangle",
                  "$dim": 11
                },
                "id": "graphnode15",
                "name": "graphnode15"
              }, {
                "adjacencies": [
                  {
                    "nodeTo": "graphnode17",
                    "nodeFrom": "graphnode16",
                    "data": {
                      "$color": "#557EAA"
                    }
                  }
                ],
                "data": {
                  "$color": "#C74243",
                  "$type": "star",
                  "$dim": 7
                },
                "id": "graphnode16",
                "name": "graphnode16"
              }, {
                "adjacencies": [],
                "data": {
                  "$color": "#416D9C",
                  "$type": "circle",
                  "$dim": 7
                },
                "id": "graphnode17",
                "name": "graphnode17"
              }, {
                "adjacencies": [
                    {
                      "nodeTo": "graphnode19",
                      "nodeFrom": "graphnode18",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }, {
                      "nodeTo": "graphnode20",
                      "nodeFrom": "graphnode18",
                      "data": {
                        "$color": "#557EAA"
                      }
                    }
                ],
                "data": {
                  "$color": "#EBB056",
                  "$type": "triangle",
                  "$dim": 9
                },
                "id": "graphnode18",
                "name": "graphnode18"
              }, {
                "adjacencies": [],
                "data": {
                  "$color": "#70A35E",
                  "$type": "circle",
                  "$dim": 8
                },
                "id": "graphnode19",
                "name": "graphnode19"
              }, {
                "adjacencies": [],
                "data": {
                  "$color": "#C74243",
                  "$type": "star",
                  "$dim": 8
                },
                "id": "graphnode20",
                "name": "graphnode20"
              }
          ];
          // end
          // init ForceDirected
          var fd = new $jit.ForceDirected({
            //id of the visualization container
            injectInto: 'graph',
            //Enable zooming and panning
            //by scrolling and DnD
            Navigation: {
              enable: true,
              //Enable panning events only if we're dragging the empty
              //canvas (and not a node).
              panning: 'avoid nodes',
              zooming: 10 //zoom speed. higher is more sensible
            },
            // Change node and edge styles such as
            // color and width.
            // These properties are also set per node
            // with dollar prefixed data-properties in the
            // JSON structure.
            Node: {
              overridable: true
            },
            Edge: {
              overridable: true,
              color: '#23A4FF',
              lineWidth: 0.4
            },
            //Native canvas text styling
            Label: {
              type: labelType, //Native or HTML
              size: 10,
              style: 'bold'
            },
            //Add Tips
            Tips: {
              enable: true,
              onShow: function(tip, node) {
                //count connections
                var count = 0;
                node.eachAdjacency(function() { count++; });
                //display node info in tooltip
                tip.innerHTML = "<div class=\"tip-title\">" + node.name + "</div>"
                  + "<div class=\"tip-text\"><b>connections:</b> " + count + "</div>";
              }
            },
            // Add node events
            Events: {
              enable: true,
              type: 'Native',
              //Change cursor style when hovering a node
              onMouseEnter: function() {
                fd.canvas.getElement().style.cursor = 'move';
              },
              onMouseLeave: function() {
                fd.canvas.getElement().style.cursor = '';
              },
              //Update node positions when dragged
              onDragMove: function(node, eventInfo, e) {
                  var pos = eventInfo.getPos();
                  node.pos.setc(pos.x, pos.y);
                  fd.plot();
              },
              //Implement the same handler for touchscreens
              onTouchMove: function(node, eventInfo, e) {
                $jit.util.event.stop(e); //stop default touchmove event
                this.onDragMove(node, eventInfo, e);
              },
              //Add also a click handler to nodes
              onClick: function(node) {
                if(!node) return;
                // Build the right column relations list.
                // This is done by traversing the clicked node connections.
                var html = "<h4>" + node.name + "</h4><b> connections:</b><ul><li>",
                    list = [];
                node.eachAdjacency(function(adj){
                  list.push(adj.nodeTo.name);
                });
                //append connections information
                $jit.id('inner-details').innerHTML = html + list.join("</li><li>") + "</li></ul>";
              }
            },
            //Number of iterations for the FD algorithm
            iterations: 200,
            //Edge length
            levelDistance: 130,
            // Add text to the labels. This method is only triggered
            // on label creation and only for DOM labels (not native canvas ones).
            onCreateLabel: function(domElement, node){
              domElement.innerHTML = node.name;
              var style = domElement.style;
              style.fontSize = "0.8em";
              style.color = "#ddd";
            },
            // Change node styles when DOM labels are placed
            // or moved.
            onPlaceLabel: function(domElement, node){
              var style = domElement.style;
              var left = parseInt(style.left);
              var top = parseInt(style.top);
              var w = domElement.offsetWidth;
              style.left = (left - w / 2) + 'px';
              style.top = (top + 10) + 'px';
              style.display = '';
            }
          });
          // load JSON data.
          fd.loadJSON(json);
          // compute positions incrementally and animate.
          fd.computeIncremental({
            iter: 40,
            property: 'end',
            onStep: function(perc){
              Log.write(perc + '% loaded...');
            },
            onComplete: function(){
              Log.write('done');
              fd.animate({
                modes: ['linear'],
                transition: $jit.Trans.Elastic.easeOut,
                duration: 2500
              });
            }
          });
          // end
        }
    </script>
    {% block scripts %}{% endblock %}
</head>
<body onload="init();">
    <div class="hero-unit">
        <h1>{{ topic.title }}</h1>
    </div>  
    <h2>Схема</h2>
        <div id="graph"></div>
    <h2>Высказывания</h2>

    <ul>
        {% for argelement in arg_elements %}
            <li>{{ argelement.contents }}</li>
        {% endfor %}
    </ul>


    {% block content %}
    {% endblock %}
</body>
</html>

最佳答案

可能是反对Log有异常(exception)吗?尝试添加<div id='log'></div>翻页或剪切Log从页面。

控制台有异常吗?

关于javascript - 在 Django 页面中使用时,Infovis 不会布局图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17243138/

相关文章:

javascript - 只有当我使用 IE11 时,事件 addUserSignedOut 才会自动触发

javascript - react native : Cannot read property 'push' of undefined - Have NavigatorIOS and ES6 bind(this)

python - 使用嵌入式 Python/C 函数编译文件 .c

python - Spring python : how to import another xml resource from within an xml config file

python - 将嵌套在两个字典下的列表转换为 DataFrame

python - Django 错误 : cannot import name current_datetime

JavaScript 工作队列

python - 创建受密码保护的 zip 文件?

python - Django - 自定义管理操作日志记录

php - Jquery、Ajax、PHP 数字增量器