0

I want to triggle an event on a path with D3.js. Here a working example: http://jsfiddle.net/kwoxer/kpL1uyy2/

So how can I say that a specific path is triggered by the zoomIntoArea function. I mean at the end I have some pathes and I want to load one specific at startup without clicking on it. I already tried:

zoomIntoArea(d3.select("lines"))

and some others but for sure that does not give me back the correct element.

kwoxer
  • 3,734
  • 4
  • 40
  • 70
  • possible duplicate of [Center a map in d3 given a geoJSON object](http://stackoverflow.com/questions/14492284/center-a-map-in-d3-given-a-geojson-object) – Paulo Scardine Jan 21 '15 at 19:15
  • No it is not. Because I already have a function to center. But I want to activate it after the loading finished. – kwoxer Jan 21 '15 at 19:52

1 Answers1

2

You don't need the zoom behavior to explicitly zoom to a path, e.g.:

var bounds = path.bounds(d),
        dx = bounds[1][0] - bounds[0][0],
        dy = bounds[1][1] - bounds[0][1],
        x = (bounds[0][0] + bounds[1][0]) / 2,
        y = (bounds[0][1] + bounds[1][1]) / 2,
        scale = .9 / Math.max(dx / width, dy / height),
        translate = [
            width / 2 - scale * x, 
            height / 2 - scale * y];

svg.transition()
    .duration(750)
    .attr("transform", "translate(" + 
        translate + ")scale(" + 
        scale + ")");
Stephen Thomas
  • 13,843
  • 2
  • 32
  • 53
  • Thanks for your answer, but that is not what I was looking for. Well I need something in the d3.json ... that says center on the first path there. That needs to be triggered at the end or so. Or could you change the jsfiddle example with your code and show it to me. Maybe I just do not get your way? Thank you. – kwoxer Jan 21 '15 at 19:55
  • Ok, now I know where to put your code. Yeah that would be working indeed. But how can I can on using that function zoomIntoArea for that. I mean why again the same code. Thanks. – kwoxer Jan 21 '15 at 19:59
  • Alright now zoomIntoArea(featureCollection); is working. But I really have tested it that way before. Strange things happening here. Ok question is solved. Thank you. – kwoxer Jan 21 '15 at 20:01