问题描述
- 从 mxGraph图表示例
- 能够使用编辑器绘制图表
- 已启用本地存储
- 能够从本地存储中获取XML
- Pointed index.html from mxGraph diagram sample
- Able to draw the diagrams using editor
- Enabled local storage
- Able to get the XML from local storage
Editor.useLocalStorage = true;
var temp = localStorage['testing.xml'];
console.log(temp);
但是,我想在不使用本地存储的情况下动态获取XML,所以我使用了 mxGraph-保存功能在本地不起作用,但是它抛出了图形未定义
But, I want to get the XML dynamically without using local storage, So I used the code from mxGraph -Save functionality not working locally but it's throwing graph is not defined
let encoder = new mxCodec();
let result = encoder.encode(graph.getModel());
let xml = mxUtils.getXml(result);
代码:
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=5,IE=9" ><![endif]-->
<!DOCTYPE html>
<html>
<head>
<title>Grapheditor</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" type="text/css" href="styles/grapheditor.css">
<script type="text/javascript">
// Parses URL parameters. Supported parameters are:
// - lang=xy: Specifies the language of the user interface.
// - touch=1: Enables a touch-style user interface.
// - storage=local: Enables HTML5 local storage.
// - chrome=0: Chromeless mode.
var urlParams = (function(url)
{
var result = new Object();
var idx = url.lastIndexOf('?');
if (idx > 0)
{
var params = url.substring(idx + 1).split('&');
for (var i = 0; i < params.length; i++)
{
idx = params[i].indexOf('=');
if (idx > 0)
{
result[params[i].substring(0, idx)] = params[i].substring(idx + 1);
}
}
}
return result;
})(window.location.href);
// Default resources are included in grapheditor resources
mxLoadResources = false;
</script>
<script type="text/javascript" src="js/Init.js"></script>
<script type="text/javascript" src="deflate/pako.min.js"></script>
<script type="text/javascript" src="deflate/base64.js"></script>
<script type="text/javascript" src="jscolor/jscolor.js"></script>
<script type="text/javascript" src="sanitizer/sanitizer.min.js"></script>
<script type="text/javascript" src="../../../src/js/mxClient.js"></script>
<script type="text/javascript" src="js/EditorUi.js"></script>
<script type="text/javascript" src="js/Editor.js"></script>
<script type="text/javascript" src="js/Sidebar.js"></script>
<script type="text/javascript" src="js/Graph.js"></script>
<script type="text/javascript" src="js/Format.js"></script>
<script type="text/javascript" src="js/Shapes.js"></script>
<script type="text/javascript" src="js/Actions.js"></script>
<script type="text/javascript" src="js/Menus.js"></script>
<script type="text/javascript" src="js/Toolbar.js"></script>
<script type="text/javascript" src="js/Dialogs.js"></script>
</head>
<body class="">
<div class="actions" style="margin-bottom:25px">
<input type="button" value="Save" onclick="getxml()" />
</div>
</br></br></br></br></br>
<div class="geEditor">
<script type="text/javascript">
function getxml(){
console.log("Show XML");
var temp = localStorage['testing.xml'];
console.log(temp);
/* var graph = new Graph();
var encoder = new mxCodec();
//var result = encoder.encode(this.graph.getModel());
//var xmlString = encodeURIComponent(mxUtils.getXml(result));
var xml = mxUtils.getXml(Editor.prototype.getGraphXml());
console.log("Direct XML");
console.log(xmlString); */
//console.log(xml);
/* var graph = new Graph();
//var encoder = new mxCodec();
var node = encoder.encode(graph.getModel());
var graphXML =mxUtils.getPrettyXml(node); */
//var graphXMLWhole = mxUtils.getPrettyXml(Editor.prototype.getGraphXml());
//mxUtils.popup(mxUtils.getPrettyXml(node), true)
//var graph = this.editorUi.editor.graph;
/* var editorui = EditorUi;
var xmlString = mxUtils.getXml(editorUi.editor.getGraphXml());
*/
//console.log(xmlString);
/* var encoder = new mxCodec();
var node = encoder.encode(graph.getModel());
var testString = mxUtils.getXml(node);
console.log(testString);
*/
/* var encoder = new mxCodec();
//let graph = new Graph();
var editor = this.editor || new Editor();
var graph = this.editor.graph || new Graph(); */
/* // var model = graph.getxml();
var result = encoder.encode(graph.getModel()); //where graph is the object you are using
var xml = mxUtils.getXml(result);
console.log(xml);
//var xml = mxUtils.getXml(result); //now the global variable 'xml' is assigned with the xml value of the graph
*/
var encoder = new mxCodec();
var node = encoder.encode(graph.getmodel());
var xml = mxutils.getxml(node); //mxutils.popup(xml, true); return xml;
// var xml = mxUtils.getXml(this.editor.getGraphXml())
console.log(xml);
}
// Extends EditorUi to update I/O action states based on availability of backend
(function()
{
var editorUiInit = EditorUi.prototype.init;
Editor.useLocalStorage = true;
EditorUi.prototype.init = function()
{
editorUiInit.apply(this, arguments);
this.actions.get('export').setEnabled(false);
// Updates action states which require a backend
if (!Editor.useLocalStorage)
{
mxUtils.post(OPEN_URL, '', mxUtils.bind(this, function(req)
{
var enabled = req.getStatus() != 404;
this.actions.get('open').setEnabled(enabled || Graph.fileSupport);
this.actions.get('import').setEnabled(enabled || Graph.fileSupport);
this.actions.get('save').setEnabled(true)
this.actions.get('saveAs').setEnabled(true);
this.actions.get('export').setEnabled(enabled);
}));
}
};
console.log("on init");
// Adds required resources (disables loading of fallback properties, this can only
// be used if we know that all keys are defined in the language specific file)
mxResources.loadDefaultBundle = false;
var bundle = mxResources.getDefaultBundle(RESOURCE_BASE, mxLanguage) ||
mxResources.getSpecialBundle(RESOURCE_BASE, mxLanguage);
// Fixes possible asynchronous requests
mxUtils.getAll([bundle, STYLE_PATH + '/default.xml'], function(xhr)
{
// Adds bundle text to resources
mxResources.parse(xhr[0].getText());
// Configures the default graph theme
var themes = new Object();
themes[Graph.prototype.defaultThemeName] = xhr[1].getDocumentElement();
// Main
new EditorUi(new Editor(urlParams['chrome'] == '0', themes));
}, function()
{
document.body.innerHTML = '<center style="margin-top:10%;">Error loading resource files. Please check browser console.</center>';
});
})();
</script>
</div>
</body>
</html>
推荐答案
在www/index.html
中,似乎您要仿效的示例对我来说不是很清楚Graph
的位置,因此您可以检索它但我建议您采用以下解决方法来解决您的问题:
Well in the www/index.html
where it seems your example to be taken from it is not very clear to me where the Graph
exists so you can retrieve it but I propose you the following workaround to solve your problem:
1)添加一个额外的变量,该变量保持图形的"xml"状态,您可以随时对其进行检索
在/examples/grapheditor/www/js/Graph.js
中添加以下变量
Graph.xml = "";
2)在Graph.js的this.graph.click事件上,添加代码以进行检索,以在每次发生更改时更新保存图的xml状态的Graph.xml变量.的
2) On the this.graph.click event of Graph.js add code to retrieve to update the Graph.xml variable holding the xml state of the graph each time there is a change so instead of
this.graph.click = mxUtils.bind(this, function(me)
{
graphClick.apply(this.graph, arguments);
if (this.currentState != null && !this.graph.isCellSelected(this.currentState.cell) &&
mxEvent.isTouchEvent(me.getEvent()) && !this.graph.model.isVertex(me.getCell()))
{
this.reset();
}
});
添加以下内容:
this.graph.click = mxUtils.bind(this, function(me)
{
let encoder = new mxCodec();
let result = encoder.encode(this.graph.getModel()); //where graph is the object you are using
Graph.xml = mxUtils.getXml(result);
graphClick.apply(this.graph, arguments);
if (this.currentState != null && !this.graph.isCellSelected(this.currentState.cell) &&
mxEvent.isTouchEvent(me.getEvent()) && !this.graph.model.isVertex(me.getCell()))
{
this.reset();
}
});
因此,当您想要画布的xml状态时,应调用Graph.xml
,这在本地环境中对我有用
So when you want the xml state of the canvas you should call the Graph.xml
, this worked for me in a local environment
这篇关于如何从mxGrpah图获取XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!