2013-01-02 114 views
1

我最近發現這個Streamgraph http://bl.ocks.org/1963983D3 streamgraph層淡入淡出功能

enter image description here

我想實現在例如淡入淡出功能到我自己的streamgraph但我有沒有成功。

礦井顯然比那個例子中的礦井複雜得多。任何意見或建議將非常感謝!

謝謝。

 var customPalette = [ 
      "#ff7f0e", "#2ca02c", "#00FFFF", "#d62728", "#9467bd", 
      "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf" 
     ]; 

     var format = d3.time.format("%y"); 

     //creating margins around the graph 
     var margin = {top: 20, right: 30, bottom: 30, left: 200}, 
      width = 1200 - margin.left - margin.right, 
      height = 500 - margin.top - margin.bottom; 

     //OUTPUT RANGE 
     var x = d3.time.scale() 
      .range([0, width]); 

     //OUTPUT RANGE 
     var y = d3.scale.linear() 
      .range([height, 0]); 

     //assining custom colors to layers 
     var colours = d3.scale.ordinal().range(customPalette); 

     var xAxis = d3.svg.axis() 
      .scale(x) 
      .orient("top") 
      .ticks(d3.time.years); 

     var yAxis = d3.svg.axis() 
      .scale(y) 
      .orient("left"); 

     //ctreate stack layout 
     var stack = d3.layout.stack() 
      .offset("wiggle") 
      .order("reverse") 
      .values(function(d) { return d.values; }) 
      .x(function(d) { return d.date; }) 
      .y(function(d) { return d.amount; }); 

     //creates array of datya elements for stacked bar graph 
     var nest = d3.nest() 
      .key(function(d) { return d.age; }); 

     //create area 
     var area = d3.svg.area() 
      //adds curviture 
      .interpolate("monotone") 
      .x(function(d) { return x(d.date); }) 
      .y0(function(d) { return y(d.y0); }) 
      .y1(function(d) { return y(d.y0 + d.y); }); 

     var svg = d3.select("body").append("svg") 
      //defines length of x-axis 
      .attr("width", width + margin.left + margin.right) 
      //defines height of y-axis 
      .attr("height", height + margin.top + margin.bottom) 
      .append("g") 
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 


     d3.csv("data6.csv", function(data) { 
      data.forEach(function(d) { 
      // Convert strings to numbers 
      d.date = format.parse(d.date); 
      d.amount = +d.amount; 
      }); 

      //returns an array of objects with a key feild (0-20yrs....) 
      //and a value array which contains associated records 
      var layers = stack(nest.entries(data)); 

      //.extent() returns min and max values of argument 
      x.domain(d3.extent(data, function(d) { return d.date; })); 
      // 
      y.domain([0, d3.max(data, function(d) { return d.y0 + d.y; })]); 

      svg.selectAll(".layer") 
       .data(layers) 
      .enter().append("path") 
       .attr("class", "layer") 
       .attr("d", function(d) { return area(d.values); }) 
       .style("fill", function(d, i) { return colours(i); }); 


     svg.append("g") 
       .attr("class", "x axis") 
       .attr("transform", "translate(0," + 0 + ")") 
       .call(xAxis); 

     svg.append("g") 
       .attr("class", "y axis") 
       .call(yAxis); 

     }); 

回答

2

我終於得到它的工作!我發現這個方便的代碼在這裏 http://bl.ocks.org/3480186

我剪下了大塊的淡出效果。很高興。

svg.selectAll(".layer").attr("opacity", 1) 
.on("mouseover", function(d, i) { 
    svg.selectAll(".layer").transition() 
     .duration(500) 
     .attr("opacity", function(d, j) { 
     return j != i ? 0.2 : 1; 
     }); 
    d3.select(this) 
    .classed("hover", true); 
}) 
.on("mouseout", function(d, i) { 
    svg.selectAll(".layer") 
    .transition() 
     .duration(500) 
     .attr("opacity", 1); 
    d3.select(this) 
    .classed("hover", false); 
}); 
+0

+1回答你自己的問題! (這樣所有的人都可以看到它) – VividD