【发布时间】:2020-08-12 00:09:15
【问题描述】:
我一直在使用 mithril.js 使用框架,并希望添加 this script 以将交互式树绘制到我网站上的组件中。
到目前为止,我只是将script标签内的JS代码放入我的应用程序中,我知道通常情况下,生成的DOM对象是通过调用svg.node()获得的。但我知道我不能从对象的view 方法返回它,而是需要返回m(...) 形式的东西。我尝试查看this one 之类的来源,但无法在其中找到解决我的问题的方法。 有没有一种已知的方法可以将 D3 SVG 图形合并到 mihtril.js 中?
如果你想看看我现在的代码:
export default class TreeModal extends Modal {
content() {
var treeData = ... // some data
... // Other code; irrelevant to this question
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom).append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Some other code adding more functionality
// At this point, svg contains the SVG object which I wish to display
return svg.node(); // Raises ILLEGAL CONSTRUCTOR error; how to adapt this for Mithril.js?
}
}
提前感谢所有帮助!
更新:似乎Modal 对我的问题至关重要,因为我使用的API 实际上要求我在Modal 的任何子类中实现content() 方法。我查看了Modal.js,相关位是:
export default class Modal {
view() {
return (
<div>
...
{this.content()}
...
</div>
)
}
}
理想情况下,该解决方案不必覆盖Modal 的view() 方法,只需在TreeModal 中包含对content() 的更改。
【问题讨论】:
标签: javascript d3.js svg mithril.js