计划制作一个可视化的神经网络设计工具,调研了多种前端画图的框架,包括gojs、draw2d、tensorspace、d3等,发现gojs的实现最符合我的需要,尽管基于jqueryui的样式稍微有些丑、商用版本的加个也很贵:(
GOJS是一个用于实现交互式图标的javascript库,同时支持typescript。GOJS可以用于创建大量不同类型的交互式图表,包括数据可视化、画图工具和图片编辑。他已经包含了许多内置的布局方式,包括树型、力导向型、雷达型、分层有向图以及大量自定义布局实例。
<script src="go.js"></script>
<script id="code">
function init() {
var $ = go.GraphObject.make; // for conciseness in defining templates
var myDiagram = $(go.Diagram, "myDiagramDiv", // create a Diagram for the DIV HTML element
{
// enable undo & redo
"undoManager.isEnabled": true
});
// define a simple Node template
myDiagram.nodeTemplate =
$(go.Node, "Auto", // the Shape will go around the TextBlock
$(go.Shape, "RoundedRectangle", { strokeWidth: 0, fill: "white" },
// Shape.fill is bound to Node.data.color
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 8 }, // some room around the text
// TextBlock.text is bound to Node.data.key
new go.Binding("text", "key"))
);
// but use the default Link template, by not setting Diagram.linkTemplate
// create the model data that will be represented by Nodes and Links
myDiagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "lightblue" },
{ key: "Beta", color: "orange" },
{ key: "Gamma", color: "lightgreen" },
{ key: "Delta", color: "pink" }
],
[
{ from: "Alpha", to: "Beta" },
{ from: "Alpha", to: "Gamma" },
{ from: "Beta", to: "Beta" },
{ from: "Gamma", to: "Delta" },
{ from: "Delta", to: "Alpha" }
]);
}
</script>