我正在尝试将下图(dagre-d3)集成到我的 vuejs 应用程序中。
http://www.samsarin.com/project/dagre-d3/latest/demo/hover.html
我已经成功地在 vuejs 组件中生成了图表。但我无法实现图中提供的工具提示功能。他们正在使用一个名为 Tipsy 的库,当我在纯 html 中运行图形时,它可以正常工作。但是当我在 vue 应用程序中使用时,它会抛出以下错误。
[Vue warn]: Error in mounted hook: "TypeError: $(...).tipsy is not a function"
found in
---> <DagView> at
如果我评论下面的部分图生成没有任何问题,但工具提示没有显示。
<script>
export default {
name: 'TreeView',
mounted () {
/* eslint-disable */
// Create a new directed graph
var g = new dagreD3.graphlib.Graph().setGraph({})
var Tree_JSON =
{
"nodes":
[
{
"label": "start",
"job_id": "11111"
},
{
"label": "End"
}
],
"edges":
[
{
"startEdge": "start",
"endEdge": "end"
}
]
}
var TreeData = Tree_JSON.nodes;
// Add states to the graph, set labels, and style
Object.keys(TreeData).forEach(function(key) {
var value = TreeData[key]
value.label = TreeData[key].label
value.rx = value.ry = 5
g.setNode(TreeData[key].label, value)
})
var TreeEdges = Tree_JSON.edges;
// Add states to the graph, set labels, and style
Object.keys(TreeEdges).forEach(function(key) {
g.setEdge(TreeEdges[key].startEdge, TreeEdges[key].endEdge, { label: ""} )
})
// Create the renderer
var render = new dagreD3.render()
// Set up an SVG group so that we can translate the final graph.
var svg = d3.select("svg"),
inner = svg.append("g")
// Set up zoom support
var zoom = d3.behavior.zoom().on("zoom", function() {
inner.attr("transform", "translate(" + d3.event.translate + ")" +
"scale(" + d3.event.scale + ")")
})
svg.call(zoom)
// Simple function to style the tooltip for the given node.
var styleTooltip = function(name, description) {
return "<p class='name'>" Test label "</p><p class='description'>" Desc "</p>"
}
// Run the renderer. This is what draws the final graph.
render(inner, g)
inner.selectAll("g.node")
.attr("title", function(v) {
return styleTooltip(v, "Execution Time: "+g.node(v).label + " <br /> Description: "+g.node(v).label)
})
//.each(function(v) { $(this).tipsy({ gravity: "w", opacity: 1, html: true }) })
inner.selectAll("g.node")
.on("click", function(v) {
console.log("Nodes --> "+ v + " -- "+ g.node(v).label)
// whatever
})
// Center the graph
var initialScale = 1.2
zoom
.translate([(svg.attr("width") - g.graph().width * initialScale) / 50, 20])
.scale(initialScale)
.event(svg)
svg.attr('height', g.graph().height * initialScale + 40)
svg.attr('width', g.graph().width * initialScale + 40)
}
}