trigger event on second click

I've somehow managed to do create a stacked bar chart for this post .

Instead of hover method i've chosen to get on with click event as i was facing some bug with the hover event like the elements were not disappearing properly when i hover on to a element.

When i click on a bar all the bars are getting disappeared and the width of the bar is getting increased. But that when I click on the bar that's been expanded i want to reset it's width to the original width.

  • I wanted to know how to get back to it's original width[when i was using hover method i defined the width manually]
  • Need to how to code for the send click event for the expanded bar.
  • Please help.

    JS :

    var width = 750,
          height = 500;
    
      var x = d3.scale.ordinal()
                .rangeRoundBands([0, width], .1);
    
      var y = d3.scale.linear()
                .rangeRound([height, 0]);
    
      var color = d3.scale.ordinal()
                    .range(["#D70B16", "#154CEF", "#1A8A55"]);
    
      var xAxis = d3.svg.axis()
                    .scale(x)
                    .orient("bottom");
    
      var yAxis = d3.svg.axis()
                    .scale(y)
                    .orient("left")
                    .tickFormat(d3.format(".2s"));
    
      var svgContainer = d3.select("body")
                            .append("svg")
                            .attr("width", width)
                            .attr("height", height)
                            .append("g")
                            .attr("transform", "translate(" + 30 + "," + 30 + ")");
    
    
      function movein() {
        var allRect = d3.selectAll("rect")
                        // .transition()
                        // .duration(300)
                        .attr("opacity", 0)
                        .attr("display", "none")
    
        d3.select(this)
          .attr("opacity", 1)
          .attr("display", "block")
    
        d3.select(this)
          .transition()
          .delay(300)
          .duration(500)
          .attr("width", 300)
      };
    
      function moveout() {  
        d3.select(this)
          .transition()
          .duration(1000)
          .attr("width", 150)
    
          var allRect = d3.selectAll("rect")
                        .transition()
                        .delay(1000)
                        .duration(300)
                        .attr("opacity", 1)
                        .attr("display", "block")
      };
    
      d3.csv("data.csv", function(error, data) {
        color.domain(d3.keys(data[0]).filter(function(key) { return key !== "circle"; }));
    
        data.forEach(function(d) {
          var y0 = 0;
          d.hours = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
          d.total = d.hours[d.hours.length - 1].y1;
        });
    
        x.domain(data.map(function(d) {return d.circle;}));
        y.domain([0, d3.max(data, function(d) {return d.total;})])
    
        svgContainer.append("g")
                    .attr("class", "x axis")
                    .attr("transform", "translate(0," + height + ")")
                    .call(xAxis);
    
        svgContainer.append("g")
                    .attr("class", "y axis")
                    .call(yAxis)
                    .append("text")
                    .attr("transform", "rotate(-90)")
                    .attr("y", 6)
                    .attr("dy", ".71em")
                    .style("text-anchor", "end")
                    .text("Login Hours");
    
        var circle = svgContainer.selectAll(".circle")
                        .data(data)
                        .enter().append("g")
                        .attr("class", "g")
                        .attr("transform", function(d) { return "translate(" + x(d.circle) + ",0)"; });
    
        circle.selectAll("rect")
              .data(function(d) { return d.hours; })
              .enter()
              .append("rect")
              .attr("width", x.rangeBand())
              .attr("y", function(d) { return y(d.y1); })
              .attr("height", function(d) { return y(d.y0) - y(d.y1); })
              .on("click", movein)
              .on("click", moveout)
              .style("fill", function(d) { return color(d.name); });
    
      })
    

    DATA :

    state,value1, value2, value3
    state1, 80, 10, 20
    state2, 90, 5, 10
    state3, 70, 15, 35
    state4, 90, 3, 27
    state5, 50, 25, 55
    state6, 85, 8, 27
    
    链接地址: http://www.djcxy.com/p/84202.html

    上一篇: 从右到左反转条

    下一篇: 触发第二次点击事件