简体   繁体   English

D3将点添加到多个堆积的折线图中

[英]D3 adding dots to multiple stacked line charts

I'm creating multiple line-graphs that are stacked by "symbol" from a csv-file. 我正在创建多个折线图,这些折线图由csv文件中的“符号”堆叠。

This is how the csv looks-like: 这是csv的样子:

symbol,date,price
Agency,2015/5/6,33
Agency,2015/5/7,29
Agency,2015/5/8,32
test,2015/5/6,23
test,2015/5/7,19
test,2015/5/8,22
example,2015/5/6,43
example,2015/5/7,49
example,2015/5/8,42

So for each symbol contained in the csv-file a separate graph gets drawn. 因此,对于csv文件中包含的每个符号,都会绘制一个单独的图形。 This all works fine so far. 到目前为止,一切正常。

But I'm struggling to add dots to each line. 但是我正在努力为每行添加点。

This is the part that's creating the dots (line 142 - 148 in my fiddle ): 这是创建点的部分( 我的小提琴中的第142-148行 ):

var circles = svg.selectAll("dot")
    .data(data)
    .enter().append("circle")
        .attr("r", 2)
        .attr("cx", function(d) { return x(d.date); })
        .attr("cy", function(d) { return y(d.price); })
        .style("fill", "black");

But this code creates the dots populated by the values of the last "symbol" off the csv and not separate for each "symbol" . 但是,此代码创建了由csv中最后一个“符号”的值填充的点,并且没有为每个“符号”分开

I researched a lot here on stackoverflow, but none of the solutions or hints I found worked for me. 我在这里对stackoverflow进行了很多研究,但是我发现的任何解决方案或提示都不适合我。

In this example http://jsbin.com/isuris/1/edit?html,output I found this part: 在此示例http://jsbin.com/isuris/1/edit?html,输出中,我发现了这一部分:

// add circles
pathContainers.selectAll('circle')
.data(function (d) { return d; })
.enter().append('circle')
.attr('cx', function (d) { return xScale(d.x); })
.attr('cy', function (d) { return yScale(d.y); })
.attr('r', 3); 

So I tried to change line 143 of my fiddle http://fiddle.jshell.net/doyL1L0p/1/ like this: 因此,我尝试更改小提琴http://fiddle.jshell.net/doyL1L0p/1/的第143行,如下所示:

.data(function (d) { return d; })

But this didn't work, the "wrong" dots disappear. 但这没有用,“错误”的点消失了。

So I tried this: 所以我尝试了这个:

.data(function (d) { return d.key; })

because the separate "symbol"-value gets displayed by using "d.key". 因为使用“ d.key”显示了单独的“ symbol”值。 But know I get this error in the console: 但是知道我在控制台中收到此错误:

Unexpected value NaN for attribute cx
Unexpected value NaN for attribute cy

Then I tried to alter lines 146 and 147: 然后,我尝试更改146和147行:

        .attr("cx", function(d) { return x(d.date); })
        .attr("cy", function(d) { return y(d.price); })

but without success. 但没有成功。

I tried many other posted "solutions" off stackoverflow and others I found in the web, but all without success again. 我尝试过从stackoverflow上找到许多其他发布的“解决方案”,以及在网络上找到的其他解决方案,但所有尝试都没有成功。

Can anyone please give me a hint on how to solve this? 谁能给我一个关于如何解决这个问题的提示?

Heres the fiddle again: 这是小提琴:

http://fiddle.jshell.net/doyL1L0p/1/ http://fiddle.jshell.net/doyL1L0p/1/

Based on some editing of your fiddle, it seems that they ARE actually being displayed... but they're out of range of the canvas. 根据您对小提琴的一些编辑,似乎它们实际上已在显示中……但是它们超出了画布的范围。 If you look at the outcome here , you can see that the newly added point near the top is displayed, on all of the graphs. 如果在此处查看结果,则可以看到在所有图形上都显示了靠近顶部的新添加的点。

I'll see if I can work on a proper fix, because obviously this is not the desired behaviour. 我将查看是否可以进行适当的修复,因为显然这不是所需的行为。

UPDATE 更新

It looks like the problem is that you aren't updating your domain when you display your dots, so they are using the last domain which was set (hence why only last show normally). 看起来问题在于您在显示点时没有更新域,因此它们使用的是设置的最后一个域(因此为什么通常只有最后一个显示)。 In addition, you are displaying dots for the entire data set on each graph (although most of them are outside the domain). 此外,您将在每个图形上显示整个数据集的点(尽管大多数都在域外)。

I would actually recommend trying to parse your data into an array for each unique symbol, it make things much more straightforward for you. 我实际上建议您尝试将每个唯一符号的数据解析为数组,这对您来说将变得更加简单。 For example, you could simply set the domain using d3.extent . 例如,您可以简单地使用d3.extent设置域。

SUMMARY 摘要

A couple of solutions spring to mind: 我想到了几种解决方案:

  1. Setting up separate axes/domains per graph. 为每个图形设置单独的轴/域。

  2. Putting your dots into ag element per symbol, then adding data to them via a nested selection. 将点放入每个符号的ag元素中,然后通过嵌套选择将数据添加到它们中。

This works, but you really should read up a bit more on (nested) selections. 可行,但是您确实应该多读一些(嵌套的)选择。 Ideally, you would probably want to separately generate each graph, out of scope of the others. 理想情况下,您可能希望单独生成每个图,而超出其他图的范围。 This solution is badly designed, but hopefully it should help to give you an idea of how your code is working. 这个解决方案的设计很糟糕,但是希望它可以帮助您了解代码的工作方式。

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

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