2

I'm building a pie chart at the moment, and it's mostly done.

Anyway, I'm trying to implement some hover effects on each pie slice, but I've come across a problem I'm unable to solve. When you hover over an element the text increases in size, and also uses a filter to add a black background. The issue with this is that the text is sometimes being hidden behind other elements, as if those elements are on top of it. My best solution to fix this was adding visibility and making it !important.

.pieChart svg>g text.hover {
  font-size: 1.3em;
  fill: #fff;
  filter: url(#pieTextFilter);
  visibility: visible !important;
}

This, however, doesn't solve my problem.

Here is a jsfiddle that represents the problem (hover you mouse of slice 4): http://jsfiddle.net/tinygram/22o1epyp/3/

If you're familiar with D3, it might be important to note that this happens only after I update the graph. If you look at the bottom of my jsfiddle, you'll see that I'm starting the graph and then running it again with some updated data. I have noticed that this adds a new and at the end of the svg in the dom. I honestly don't know if this is important, but I thought I should mention it.

user2767260
  • 283
  • 2
  • 10
  • 1
    The order of the elements in SVG is determined by the order in which you add the elements. So anything that should appear on top should be last in the DOM. See e.g. [this question](http://stackoverflow.com/questions/14167863/how-can-i-bring-a-circle-to-the-front-with-d3). – Lars Kotthoff Feb 07 '15 at 21:03
  • Thank you Lars. You've helped me greatly the past two days. – user2767260 Feb 08 '15 at 01:22

1 Answers1

1

As @LarsKotthoff mentions in his comment, it's all in the order. You initially build a pie chart with 4 slices, you then add a 5th slice. So the enter selection first adds 4 slices and 4 labels. On the addition of the 5th slice, it updates the first 4 slices/labels and enters a 5th slice/label. This addition of a 5th slice is then on top of the 4th label.

See this fiddle. The problem goes away because I've invoked the exit:

tests(data);
tests({});
tests(data2);

A better fix might just be to remove all the texts before update:

tests(data);
d3.selectAll('text').remove();
tests(data2);
Mark
  • 106,305
  • 20
  • 172
  • 230
  • Thanks Mark. I ended up doing something very similar to your suggestion. I made sure to keep texts below the paths, even when one was added. I ran into a problem keeping them in order relative to each other, but ultimately got that figured out as well. – user2767260 Feb 08 '15 at 01:23