缩放仅适用于一个元素

时间:2015-03-23 16:26:56

标签: javascript d3.js zoom

我使用d3.nest()创建了一个包含多个区域图的图表,这些图表由另一个函数着色。当我尝试添加this缩放功能时,我只能使用第一个区域图。

这里是Plunk

<!DOCTYPE html>
<meta charset="utf-8">
<style>

svg {
  font: 10px sans-serif;
}

.area {
  fill: steelblue;
  clip-path: url(#clip);
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.brush .extent {
  stroke: #fff;
  fill-opacity: .125;
  shape-rendering: crispEdges;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var margin = {top: 10, right: 10, bottom: 100, left: 40},
    margin2 = {top: 430, right: 10, bottom: 20, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom,
    height2 = 500 - margin2.top - margin2.bottom;

var x = d3.scale.linear().range([0, width]),
    x2 = d3.scale.linear().range([0, width]),
    y = d3.scale.linear().range([height, 0]),
    y2 = d3.scale.linear().range([height2, 0]);

var xAxis = d3.svg.axis().scale(x).orient("bottom"),
    xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
    yAxis = d3.svg.axis().scale(y).orient("left");

var brush = d3.svg.brush()
    .x(x2)
    .on("brush", brushed);

var area = d3.svg.area()
    .interpolate("basis")
    .x(function(d) { return x(d.distance); })
    .y0(height)
    .y1(function(d) { return y(d.elevation); });

var area2 = d3.svg.area()
    .interpolate("basis")
    .x(function(d) { return x2(d.distance); })
    .y0(height2)
    .y1(function(d) { return y2(d.elevation); });

// Map colors to limits
var color = d3.scale.ordinal()
    .domain([-10,-5,0,5,10])
    .range(['#a1d99b','#c7e9c0','#fdd0a2','#fdae6b','#fd8d3c','#e6550d']);

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom);

svg.append("defs").append("clipPath")
    .attr("id", "clip")
  .append("rect")
    .attr("width", width)
    .attr("height", height);

var focus = svg.append("g")
    .attr("class", "focus")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var context = svg.append("g")
    .attr("class", "context")
    .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

// Caculate the average gradient of a dataGroup.
function dataGroupGradient(dataGroup) {
    return d3.mean(dataGroup, function(value) {
        return parseFloat(value.gradient);
    });
}

var line_points = [];

d3.csv("first5km_Strade_Bianche.csv", function(error, data) {
  data.forEach(function(d) {
    d.distance = +d.distance;
    d.elevation = +d.elevation;
    d.latitude = +d.latitude;
    d.longitude = +d.longitude;
    line_points.push([d.latitude, d.longitude]);
  });

  // Split the data based on "group"
  var dataGroup = d3.nest()
    .key(function(d) {
        return d.group;
    })
    .entries(data);

  // To remove white space between dataGroups, append the first element of one
  // dataGroup to the last element of the previous dataGroup.
  dataGroup.forEach(function(group, i) {
    if(i < dataGroup.length - 1) {
      group.values.push(dataGroup[i+1].values[0])
    }
  })

  x.domain(d3.extent(data.map(function(d) { return d.distance; })));
  y.domain([0, d3.max(data.map(function(d) { return d.elevation; }))]);
  x2.domain(x.domain());
  y2.domain(y.domain());

  // Add a line and an area for each dataGroup
  dataGroup.forEach(function(d, i){
      focus.append("path")
          .datum(d.values)
          .attr("class", "area")
          .attr("d", area);
      });

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

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

  context.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area2);

  dataGroup.forEach(function(d, i){
    context.append("path")
        .datum(d.values)
        .attr("class", "area")
        .attr("d", area2);
    });

  // Fill the dataGroups with color
  svg.selectAll(".area")
      .style("fill", function(d) { return color(dataGroupGradient(d)); });

  context.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height2 + ")")
      .call(xAxis2);

  context.append("g")
      .attr("class", "x brush")
      .call(brush)
    .selectAll("rect")
      .attr("y", -6)
      .attr("height", height2 + 7);
});

function brushed() {
  x.domain(brush.empty() ? x2.domain() : brush.extent());
  focus.select(".area").attr("d", area);
  focus.select(".x.axis").call(xAxis);
}

function type(d) {
  d.distance = parseDate(d.distance);
  d.elevation = +d.elevation;
  return d;
}

</script>

1 个答案:

答案 0 :(得分:0)

刷子处理程序中的一个小错误:

function brushed() {
  x.domain(brush.empty() ? x2.domain() : brush.extent());
  focus.select(".area").attr("d", area);// <-- HERE!
  focus.select(".x.axis").call(xAxis);
}

focus.select(".area")的调用只选择一条路径。你想要所有的路径。因此,请使用selectAll()

focus.selectAll(".area").attr("d", area);

Updated Plunk