Improved load GraphML graphs.

This commit is contained in:
Unick Soft 2020-03-02 21:05:17 +02:00
parent fa79baf2ea
commit fe4f14970b
4 changed files with 50 additions and 10 deletions

View File

@ -851,7 +851,6 @@ Application.prototype.Test = function ()
}
Application.prototype.SetAdjacencyMatrixSmart = function (matrix, separator)
{
if (separator === undefined)
@ -1002,6 +1001,10 @@ Application.prototype.LoadGraphFromString = function (str)
this.LoadUserSettings(userSettings["data"]);
this.SetDefaultTransformations();
this.graph = graph;
if (this.graph.isNeedReposition())
{
this.graph.VertexesReposition(new Point(this.GetRealWidth(), this.GetRealHeight()), this.graph.vertices);
}
this.AutoAdjustViewport();
this.updateMessage();
this.redrawGraph();

View File

@ -25,8 +25,8 @@ function BaseEdge(vertex1, vertex2, isDirect, weight)
BaseEdge.prototype.SaveToXML = function ()
{
return "<edge " +
"vertex1=\"" + this.vertex1.id + "\" " +
"vertex2=\"" + this.vertex2.id + "\" " +
"source=\"" + this.vertex1.id + "\" " +
"target=\"" + this.vertex2.id + "\" " +
"isDirect=\"" + this.isDirect + "\" " +
"weight=\"" + this.weight + "\" " +
"useWeight=\"" + this.useWeight + "\" " +
@ -41,14 +41,26 @@ BaseEdge.prototype.SaveToXML = function ()
BaseEdge.prototype.LoadFromXML = function (xml, graph)
{
var attr = xml.attr('vertex1');
this.vertex1 = graph.FindVertex(parseInt(typeof attr !== 'undefined' ? attr : xml.attr('graph1')));
if (typeof attr === 'undefined')
{
attr = xml.attr('source');
}
this.vertex1 = graph.FindVertex(typeof attr !== 'undefined' ? attr : xml.attr('graph1'));
var attr = xml.attr('vertex2');
this.vertex2 = graph.FindVertex(parseInt(typeof attr !== 'undefined' ? attr : xml.attr('graph2')));
if (typeof attr === 'undefined')
{
attr = xml.attr('target');
}
this.vertex2 = graph.FindVertex(typeof attr !== 'undefined' ? attr : xml.attr('graph2'));
this.isDirect = xml.attr('isDirect') == "true";
this.weight = parseFloat(xml.attr('weight'));
if (isNaN(this.weight))
{
this.weight = 1;
}
this.hasPair = xml.attr('hasPair') == "true";
this.useWeight = xml.attr('useWeight') == "true";
this.id = parseInt(xml.attr('id'));
this.id = xml.attr('id');
this.text = xml.attr("text") == null ? "" : xml.attr("text");
this.arrayStyleStart = xml.attr("arrayStyleStart") == null ? "" : xml.attr("arrayStyleStart");
this.arrayStyleFinish = xml.attr("arrayStyleFinish") == null ? "" : xml.attr("arrayStyleFinish");

View File

@ -12,6 +12,7 @@ function BaseVertex(x, y, vertexEnumType)
this.upText = "";
this.vertexEnumType = vertexEnumType;
this.model = new VertexModel();
this.hasUndefinedPosition = false;
};
BaseVertex.prototype.position = new Point(0, 0);
@ -30,10 +31,18 @@ BaseVertex.prototype.SaveToXML = function ()
BaseVertex.prototype.LoadFromXML = function (xml)
{
this.position = new Point(parseFloat(xml.attr('positionX')), parseFloat(xml.attr('positionY')));
this.id = parseInt(xml.attr('id'));
var xmlX = xml.attr('positionX');
var xmlY = xml.attr('positionY');
this.hasUndefinedPosition = (typeof xmlX === 'undefined') || (typeof xmlY === 'undefined');
this.position = new Point(parseFloat(xmlX), parseFloat(xmlY));
this.id = xml.attr('id');
this.mainText = xml.attr('mainText');
this.upText = xml.attr('upText');
if (typeof this.mainText === 'undefined')
this.mainText = this.id;
if (typeof this.upText === 'undefined')
this.upText = "";
}
BaseVertex.prototype.SetId = function (id)
@ -46,3 +55,8 @@ BaseVertex.prototype.diameterFactor = function ()
{
return 1.0 + (this.mainText.length ? this.mainText.length / 8.0 : 0);
}
BaseVertex.prototype.IsUndefinedPosition = function ()
{
return this.hasUndefinedPosition;
}

View File

@ -866,11 +866,11 @@ Graph.prototype.LoadFromXML = function (xmlText, additionalData)
$nodes = $xml.find( "node" );
var vertexs = [];
$nodes.each(function(){
var vertex = new BaseVertex();
vertex.LoadFromXML($(this));
vertexs.push(vertex);
vertexs.push(vertex);
});
this.vertices = vertexs;
@ -1030,3 +1030,14 @@ Graph.prototype.isMulti = function ()
{
return this.isMultiGraph;
}
Graph.prototype.isNeedReposition = function ()
{
var res = false;
for (var i = 0; i < this.vertices.length; i++)
{
res = res || this.vertices[i].IsUndefinedPosition();
}
return res;
}