0

I want to make a d3 map in which whenever I click a state then that particular state should zoom-in leaving behind other states and when I click that particular state once again then it zoom-out and become a part of the country.Here I am implementing it on India's map whose name is india.geojson Here is my code--

        var centered;
        var canvas= d3.select("body").append("svg")
                .attr("width", 1000)
                .attr("height", 1000)
                .attr("fill", "yellow");
var i;
for( i=0;i<5000;i++){               
        d3.json("india.geojson", function(data){

                    var tip = d3.tip()
                        .attr('class', 'd3-tip')
                        .offset([-10, 0])
                        .html(function(d) {
                            return ("State :"+d.properties.NAME_1);
                        });

                    canvas.call(tip);

                    var color = d3.scale.linear()
                                .domain([1,50])
                                .range(["red","blue"]);

                    var group=canvas.selectAll("g")
                        .data(data.features)
                        .enter()
                        .append("g")
                        .on('mouseover', tip.show)
                        .on('mouseout', tip.hide)

                    var projection = d3.geo.mercator().scale(1000).translate([-1000,700]);

                    var path= d3.geo.path().projection(projection);

                    var areas= group.append("path")
                            .attr("d", path)
                            .attr("class", "area")
                            .on("click",clicked)
                            .attr("fill",function(d){return color(d.properties.ID_1) ;});


            function clicked(d) {
                  var x, y, k,p;

                if(d.properties.ID_1 == 29){
                            //--------------------------------

                            var canvas= d3.select("body").append("svg")
                                            .attr("width", 500)
                                            .attr("height", 500)
                                            .attr("fill", "yellow");

                            d3.json("Rajasthan.geojson", function(data){
                                            var tip = d3.tip()
                                            .attr('class', 'd3-tip')
                                            .offset([-10, 0])
                                            .html(function(d) {
                                                return ("State :"+d.properties.NAME_1);
                                             });

                                            canvas.call(tip);

                                    //var color = d3.scale.linear()
                                        //      .domain([1,50])
                                            //  .range(["red","blue"]);

                                            var group1=canvas.selectAll("g")
                                                .data(data.features)
                                                .enter()
                                                .append("g")
                                                .on('mouseover', tip.show)
                                                .on('mouseout', tip.hide);

                                            var projection1 = d3.geo.mercator().scale(1000).translate([-1000,700]);

                                            var path1= d3.geo.path().projection(projection);

                                            var areas1= group1.append("path")
                                                    .attr("d", path)
                                                    .attr("class", "area")
                                                    .on("click",clicked)
                                                    .attr("fill",function(d){return color(d.properties.ID_1) ;});

                                            if (d && centered !== d) {
                                                    var centroid = path.centroid(d);
                                                    x = centroid[0];
                                                    y = centroid[1];
                                                    k = 4;
                                                    centered = d;

                                            } 
                                            else {
                                                    x = 500/ 2;
                                                    y = 500 / 2;
                                                    k = 1;
                                                    centered = null;
                                                    p=1;
                                            }

                                            group1.selectAll("path")
                                                .classed("active", centered && function(d) { return d === centered; });

                                            group1.transition()
                                                    .duration(50)
                                                    .attr("transform", "translate(" + 500/ 2 + "," + 500 / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")")
                                                    .style("stroke-width", 3.5 / k + "px");         
                                if(p==1)
                                        continue;

                            //--------------------------------------
                            });                                       
                }}}

});

Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204
lokesh
  • 108
  • 3
  • 17
  • Lars Kottoff its different from the link that you had given – lokesh Oct 26 '15 at 21:54
  • Take a look at this: http://bl.ocks.org/mbostock/2206590 – Mark Oct 27 '15 at 00:01
  • @Lars Kottoff This is not what I am asking.In the above link as we see when ever we click on a state then whole country get zoom-in but yes the state in which we click gots highlighted. But I want that whenever I click on a state then that particular state gets zoom-in leaving behind other states, means after zoomin I will see that state only in zoomed-in format and when I click on it again then I should get the real Map – lokesh Oct 27 '15 at 04:42

0 Answers0