我目前正在一個項目中,我們需要根據一些輸入數據生成一些SVG。目前,所有這些SVG代都是使用d3庫在JavaScript中實現的。請注意,我的目標是能夠重複使用此邏輯,而不是全部實施。使用jint從c調用javascript使用d3#
我的問題是,我希望能夠從C#調用此JavaScript。
我一直在使用PhantomJS嘗試和我能夠生成SVG,但是我不滿意,因爲
- 每次我想調用JavaScript啓動一個新的進程,我 已經注意到,它使用很多內存(在我的情況下,我看到100 MB 這是我的情況太多)
- 這似乎有點不穩定。我已經 有一些情況下,過程只是掛起
- 發展(在JavaScript端)是非常令人沮喪,因爲這是很難調試
因爲我不滿意PhantomJS我一直在使用jint還試圖和這似乎真的很好合作。不幸的是,我還沒有完全掌握運行的例子。目前,我正在使用AngleSharp來提供DOM,以便D3有一個寫入數據的地方。這使我下面的例子:
static void TestJint()
{
//We require a custom configuration with JavaScript and CSS
var config = Configuration.Default.WithJavaScript().WithCss();
//Let's create a new parser using this configuration
var parser = new HtmlParser(config);
//This is our sample source, we will do some DOM manipulation
var source = "<!doctype html> <html><head></head> <body> </body></html>";
var document = parser.Parse(source);
var jintEngine = new Engine();
jintEngine.SetValue("document", document.Implementation);
jintEngine = jintEngine.Execute(File.ReadAllText("d3.min.js"));
jintEngine = jintEngine.Execute("function testFunc() { d3.select(\"body\").append(\"span\").text(\"Hello, world!\"); return 42;}");
var res = jintEngine.Invoke("testFunc").ToObject();
}
的問題是,該線VAR解析度= jintEngine.Invoke( 「testFunc」)ToObject();拋出異常。
如果我嘗試用
jintEngine = jintEngine.Execute("function testFunc() { d3.select(\"body\"); return 42;}");
更換線
jintEngine = jintEngine.Execute("function testFunc() { d3.select(\"body\").append(\"span\").text(\"Hello, world!\"); return 42;}");
那麼函數能夠沒有任何異常運行。通過對邏輯進行一點處理,我得出結論,它是導致異常的.append(\「span \」)。
我有點卡住了,所以我希望有人可能有一個想法,可以指出我在正確的方向。