简体   繁体   English

在d3.js的工具提示中绘制

[英]Plot within a tooltip in d3.js

I would like to create a d3-based plot which graphs a plot within a tooltip. 我想创建一个基于d3的图,该图在工具提示中绘制一个图。 Unfortunately, I haven't found any examples on the web. 不幸的是,我在网上找不到任何示例。 Here is a sample JSON file. 这是一个示例JSON文件。

[{"x":[0.4],
  "y":[0.2],
  "scatter.x":[0.54,0.9297,0.6024,-1.9224,2.2819],
  "scatter.y":[0.4139,1.1298,-0.1119,2.3624,-1.1947]},
 {"x":[0.1],
  "y":[0.9],
  "scatter.x":[-0.8566,-0.5806,-0.9326,0.8329,-0.5792],
  "scatter.y":[-0.5462,-0.7054,1.0264,-3.4874,-1.0431]}] 

The idea is to have a scatter plot for (x,y) coordinates first. 这个想法是首先为(x,y)坐标创建一个散点图。 However, when one mouses over a point, a different scatter plot within a tooltip appears based on [scatter.x, scatter.y] coordinates for that respective point. 但是,当一个鼠标悬停在一个点上时,会基于相应点的[scatter.x,scatter.y]坐标显示工具提示中的另一个散点图。

I can do the scatter plots separately but have been struggling to put them together. 我可以分别绘制散点图,但一直在努力将它们放在一起。 Could anyone shed some light on this and/or provide a minimal example? 任何人都可以对此有所了解和/或提供一个最小的例子吗?

This was too long for a comment but I'm not certain if it's the answer you were looking for. 这个评论太长了,但我不确定这是否是您要的答案。 One of the issues you might find is that your nested data is formatted differently-- one uses JSON objects with x and y , while the other uses two arrays of points. 您可能会发现的问题之一是,嵌套数据的格式不同-一个使用带有xy JSON对象,另一个使用两个点数组。

My solution to this would be to create an extensible function: 我对此的解决方案是创建一个可扩展的函数:

function makeScatterPlot(elem, width, height, data, fill)

elem , width , height , and data are the core parameters: which element to attach the chart to, the size of the chart, and the data for the chart (in the JSON object format). elemwidthheightdata是核心参数:将图表附加到哪个元素,图表的大小以及图表的数据(以JSON对象格式)。

This function would generate all necessary items for the chart and add the chart to the provided element. 此功能将为图表生成所有必要的项目,并将图表添加到提供的元素中。

Then you want to bind to mouseover of your main chart, and in that function you'll have to do a bit of data modification to re-organize the two arrays into the JSON object structure. 然后,您想绑定到主图表的mouseover ,并且在该函数中,您需要做一些数据修改才能将两个数组重新组织为JSON对象结构。

function mainMouseover(d){
  var newData = [];
  for (var i = 0; i < d["scatter.x"].length; i++){
    var t = {x: [0], y: [0]};
    t.x[0] = d["scatter.x"][i];
    t.y[0] = d["scatter.y"][i];
    newData.push(t);
  }
  var newG = mainG.append("g").attr("transform", "translate(200,200)");
  makeScatterPlot(newG, 100,100, newData, "red");
}

Of course, you would modify the translate to match wherever you want your tooltip to be. 当然,您可以修改translate以匹配您希望工具提示出现的任何地方。

Putting this all together you get the following (very crude) fiddle . 将所有这些放在一起,您将得到以下(非常粗糙的) 提琴 Hover over either of the black dots to see the sub-chart. 将鼠标悬停在两个黑点上可以查看子图。 Obviously this needs quite a bit of work to be a solid example (ie remove the sub-chart on mouseout ), but hopefully it will set you in the right direction. 显然,这需要大量工作才能成为一个可靠的示例(即,删除mouseout上的子图表),但希望它将为您设定正确的方向。

If the tooltip chart is significantly different styling-wise compared to your main chart it may not be the best idea to use an extensible function, and you could just create another custom function instead. 如果工具提示图表的样式与主图表相比明显不同,则使用扩展功能可能不是最好的主意,而您可以创建另一个自定义功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM