JavaScript D3:颜色区域重叠

时间:2018-07-22 17:22:22

标签: javascript d3.js visualization data-visualization

在HTML页面中,我有两个数据数组(datadatax),每个数组由一个D3区域表示。每个区域都有不同的颜色(黄色区域和绿色区域)。我希望当两个区域相交时,可以通过不同的颜色看到它们的相交。因此,无论如何,必须由其他较高区域覆盖的区域是可见的。

我该怎么办?谢谢。

这是HTML页面:

<!DOCTYPE html>
<meta charset="utf-8">
<style>
  .area {
    fill: #ffe368;
    clip-path: url(#clip);
  }

  .areax {
    fill: #8cffa4;
    clip-path: url(#clip);
  }

  .zoom {
    cursor: move;
    fill: none;
    pointer-events: all;
  }
</style>
<svg width="1200" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
  var svg = d3.select("svg"),
    margin = {
      top: 20,
      right: 20,
      bottom: 110,
      left: 40
    },
    margin2 = {
      top: 430,
      right: 20,
      bottom: 30,
      left: 40
    },
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

  var parseDate = d3.timeParse("%b %Y");
  var x = d3.scaleTime().range([0, width]),
    x2 = d3.scaleTime().range([0, width]),
    y = d3.scaleLinear().range([height, 0]),
    y2 = d3.scaleLinear().range([height2, 0]);

  var xAxis = d3.axisBottom(x),
    xAxis2 = d3.axisBottom(x2),
    yAxis = d3.axisLeft(y);

  var brush = d3.brushX()
    .extent([
      [0, 0],
      [width, height2]
    ])
    .on("brush end", brushed);

  var zoom = d3.zoom()
    .scaleExtent([1, 10])
    .translateExtent([
      [0, 0],
      [width, height]
    ])
    .extent([
      [0, 0],
      [width, height]
    ])
    .on("zoom", zoomed);

  var area = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) {
      return x(d.date);
    })
    .y0(height)
    .y1(function(d) {
      return y(d.price);
    });

  var area2 = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) {
      return x2(d.date);
    })
    .y0(height2)
    .y1(function(d) {
      return y2(d.price);
    });

  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 + ")");

  var data = [
    {date: 'Jan 2000', price: 9}, 
    {date: 'Feb 2000', price: 20}, 
    {date: 'Mar 2000', price: 2},
    {date: 'Apr 2000', price: 9}, 
    {date: 'May 2000', price: 11}, 
    {date: 'Jun 2000', price: 12}, 
    {date: 'Jul 2000', price: 21}, 
    {date: 'Aug 2000', price: 9}, 
    {date: 'Sep 2000', price: 15}, 
    {date: 'Oct 2000', price: 6}, 
    {date: 'Nov 2000', price: 49}, 
    {date: 'Dec 2000', price: 48}, 
    {date: 'Jan 2001', price: 55}, 
    {date: 'Feb 2001', price: 20}, 
    {date: 'Mar 2001', price: 2}, 
    {date: 'Apr 2001', price: 11}, 
    {date: 'May 2001', price: 49}, 
    {date: 'Jun 2001', price: 9}, 
    {date: 'Jul 2001', price: 32}, 
    {date: 'Aug 2001', price: 31}, 
    {date: 'Sep 2001', price: 12}, 
    {date: 'Oct 2001', price: 34}, 
    {date: 'Nov 2001', price: 11}, 
    {date: 'Dec 2001', price: 22}
  ];

  var datax = [
    {date: 'Jan 2000', price: 55}, 
    {date: 'Feb 2000', price: 3}, 
    {date: 'Mar 2000', price: 22}, 
    {date: 'Apr 2000', price: 2}, 
    {date: 'May 2000', price: 11}, 
    {date: 'Jun 2000', price: 23}, 
    {date: 'Jul 2000', price: 21}, 
    {date: 'Aug 2000', price: 19}, 
    {date: 'Sep 2000', price: 15}, 
    {date: 'Oct 2000', price: 16}, 
    {date: 'Nov 2000', price: 9}, 
    {date: 'Dec 2000', price: 18}, 
    {date: 'Jan 2001', price: 55}, 
    {date: 'Feb 2001', price: 20}, 
    {date: 'Mar 2001', price: 2}, 
    {date: 'Apr 2001', price: 33}, 
    {date: 'May 2001', price: 31}, 
    {date: 'Jun 2001', price: 9}, 
    {date: 'Jul 2001', price: 32}, 
    {date: 'Aug 2001', price: 7}, 
    {date: 'Sep 2001', price: 12}, 
    {date: 'Oct 2001', price: 2}, 
    {date: 'Nov 2001', price: 2}, 
    {date: 'Dec 2001', price: 3}
  ];

  data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.price = +d.price;
  });

  datax.forEach(function(d) {
    d.date = parseDate(d.date);
    d.price = +d.price;
  });

  x.domain(d3.extent(data, function(d) {
    return d.date;
  }));
  y.domain([d3.min(data, function(d) {
    return d.price;
  }), d3.max(data, function(d) {
    return d.price;
  })]);

  x2.domain(x.domain());
  y2.domain(y.domain());

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

  focus.append("path")
    .datum(datax)
    .attr("class", "areax")
    .attr("d", area);

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

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

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

  context.append("path")
    .datum(datax)
    .attr("class", "areax")
    .attr("d", area2);

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

  context.append("g")
    .attr("class", "brush")
    .call(brush)
    .call(brush.move, x.range());

  svg.append("rect")
    .attr("class", "zoom")
    .attr("width", width)
    .attr("height", height)
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .call(zoom);

  function brushed() {
    if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
    var s = d3.event.selection || x2.range();
    x.domain(s.map(x2.invert, x2));
    focus.select(".area").attr("d", area);
    focus.select(".areax").attr("d", area);
    focus.select(".axis--x").call(xAxis);
    svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (s[1] - s[0]))
      .translate(-s[0], 0));
  }

  function zoomed() {
    if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
    var t = d3.event.transform;
    x.domain(t.rescaleX(x2).domain());
    focus.select(".area").attr("d", area);
    focus.select(".areax").attr("d", area);
    focus.select(".axis--x").call(xAxis);
    context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
  }
</script>

1 个答案:

答案 0 :(得分:3)

您可以通过应用遮罩来做到这一点。我在下面绘制了第三条路径,该路径与顶部的路径重复,但被底部的路径遮盖了。然后将此交点分别着色:

<!DOCTYPE html>
<meta charset="utf-8">
<style>
  .area {
    fill: #ffe368;
    opacity: 1;
    clip-path: url(#clip);
  }
  
  .areax {
    fill: #8cffa4;
    opacity: 1;
    clip-path: url(#clip);
  }
  
  .zoom {
    cursor: move;
    fill: none;
    pointer-events: all;
  }
</style>
<svg width="1200" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
  var svg = d3.select("svg"),
    margin = {
      top: 20,
      right: 20,
      bottom: 110,
      left: 40
    },
    margin2 = {
      top: 430,
      right: 20,
      bottom: 30,
      left: 40
    },
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

  var parseDate = d3.timeParse("%b %Y");

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

  var xAxis = d3.axisBottom(x),
    xAxis2 = d3.axisBottom(x2),
    yAxis = d3.axisLeft(y);

  var brush = d3.brushX()
    .extent([
      [0, 0],
      [width, height2]
    ])
    .on("brush end", brushed);

  var zoom = d3.zoom()
    .scaleExtent([1, 10])
    .translateExtent([
      [0, 0],
      [width, height]
    ])
    .extent([
      [0, 0],
      [width, height]
    ])
    .on("zoom", zoomed);

  var area = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) {
      return x(d.date);
    })
    .y0(height)
    .y1(function(d) {
      return y(d.price);
    });

  var area2 = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) {
      return x2(d.date);
    })
    .y0(height2)
    .y1(function(d) {
      return y2(d.price);
    });

  var defs = svg.append("defs")
  
  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 + ")");

  var data = [{
    date: 'Jan 2000',
    price: 9
  }, {
    date: 'Feb 2000',
    price: 20
  }, {
    date: 'Mar 2000',
    price: 2
  }, {
    date: 'Apr 2000',
    price: 9
  }, {
    date: 'May 2000',
    price: 11
  }, {
    date: 'Jun 2000',
    price: 12
  }, {
    date: 'Jul 2000',
    price: 21
  }, {
    date: 'Aug 2000',
    price: 9
  }, {
    date: 'Sep 2000',
    price: 15
  }, {
    date: 'Oct 2000',
    price: 6
  }, {
    date: 'Nov 2000',
    price: 49
  }, {
    date: 'Dec 2000',
    price: 48
  }, {
    date: 'Jan 2001',
    price: 55
  }, {
    date: 'Feb 2001',
    price: 20
  }, {
    date: 'Mar 2001',
    price: 2
  }, {
    date: 'Apr 2001',
    price: 11
  }, {
    date: 'May 2001',
    price: 49
  }, {
    date: 'Jun 2001',
    price: 9
  }, {
    date: 'Jul 2001',
    price: 32
  }, {
    date: 'Aug 2001',
    price: 31
  }, {
    date: 'Sep 2001',
    price: 12
  }, {
    date: 'Oct 2001',
    price: 34
  }, {
    date: 'Nov 2001',
    price: 11
  }, {
    date: 'Dec 2001',
    price: 22
  }];

  var datax = [{
    date: 'Jan 2000',
    price: 55
  }, {
    date: 'Feb 2000',
    price: 3
  }, {
    date: 'Mar 2000',
    price: 22
  }, {
    date: 'Apr 2000',
    price: 2
  }, {
    date: 'May 2000',
    price: 11
  }, {
    date: 'Jun 2000',
    price: 23
  }, {
    date: 'Jul 2000',
    price: 21
  }, {
    date: 'Aug 2000',
    price: 19
  }, {
    date: 'Sep 2000',
    price: 15
  }, {
    date: 'Oct 2000',
    price: 16
  }, {
    date: 'Nov 2000',
    price: 9
  }, {
    date: 'Dec 2000',
    price: 18
  }, {
    date: 'Jan 2001',
    price: 55
  }, {
    date: 'Feb 2001',
    price: 20
  }, {
    date: 'Mar 2001',
    price: 2
  }, {
    date: 'Apr 2001',
    price: 33
  }, {
    date: 'May 2001',
    price: 31
  }, {
    date: 'Jun 2001',
    price: 9
  }, {
    date: 'Jul 2001',
    price: 32
  }, {
    date: 'Aug 2001',
    price: 7
  }, {
    date: 'Sep 2001',
    price: 12
  }, {
    date: 'Oct 2001',
    price: 2
  }, {
    date: 'Nov 2001',
    price: 2
  }, {
    date: 'Dec 2001',
    price: 3
  }];

  data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.price = +d.price;
  });

  datax.forEach(function(d) {
    d.date = parseDate(d.date);
    d.price = +d.price;
  });

  x.domain(d3.extent(data, function(d) {
    return d.date;
  }));
  y.domain([d3.min(data, function(d) {
    return d.price;
  }), d3.max(data, function(d) {
    return d.price;
  })]);

  x2.domain(x.domain());
  y2.domain(y.domain());

  focus.append("path")
    .datum(datax)
    .attr("class", "areax")
    .attr("d", area);
    
  focus.append("path")
    .datum(data)
    .attr("class", "area")
    .attr("d", area)

  focus.append("path")
    .datum(data)
    .attr("d", area)
    .attr("class", "intersection")
    .style("mask", "url(#mask)")
    .style("clip-path", "url(#clip)")
    .style("fill", "red");

  defs.append("mask")
    .attr("id", "mask")
    .attr("x", 0)
    .attr("y", 0)
    .attr("width", width)
    .attr("height", height)
    .append("path")
    .attr("class","mask")
    .datum(datax)
    .attr("d", area)
    .style("fill", "white");

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

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

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

  context.append("path")
    .datum(datax)
    .attr("class", "areax")
    .attr("d", area2);

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

  context.append("g")
    .attr("class", "brush")
    .call(brush)
    .call(brush.move, x.range());

  svg.append("rect")
    .attr("class", "zoom")
    .attr("width", width)
    .attr("height", height)
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .call(zoom);
    
  function brushed() {
    if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
    var s = d3.event.selection || x2.range();
    x.domain(s.map(x2.invert, x2));
    focus.select(".area").attr("d", area);
    focus.select(".areax").attr("d", area);
    focus.select(".intersection").attr("d", area);
    defs.select(".mask").attr("d", area);
    focus.select(".axis--x").call(xAxis);
    svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (s[1] - s[0]))
      .translate(-s[0], 0));
  }

  function zoomed() {
    if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
    var t = d3.event.transform;
    x.domain(t.rescaleX(x2).domain());
    focus.select(".area").attr("d", area);
    focus.select(".areax").attr("d", area);
    focus.select(".intersection").attr("d", area);
    defs.select(".mask").attr("d", area);
    focus.select(".axis--x").call(xAxis);
    context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
  }
</script>

您可以采用的一种更简单的方法是在上方路径上设置不透明度:

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

.area {
  fill: #ffe368;
  opacity: 1;
  clip-path: url(#clip);   
}

.areax {
  fill: #8cffa4;
  opacity: 0.6;
  clip-path: url(#clip);   
}

.zoom {
  cursor: move;
  fill: none;
  pointer-events: all;
}

</style>
<svg width="1200" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var svg = d3.select("svg"),
    margin = {top: 20, right: 20, bottom: 110, left: 40},
    margin2 = {top: 430, right: 20, bottom: 30, left: 40},
    width = +svg.attr("width") - margin.left - margin.right,
    height = +svg.attr("height") - margin.top - margin.bottom,
    height2 = +svg.attr("height") - margin2.top - margin2.bottom;

var parseDate = d3.timeParse("%b %Y");   

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

var xAxis = d3.axisBottom(x),
    xAxis2 = d3.axisBottom(x2),
    yAxis = d3.axisLeft(y);

var brush = d3.brushX()
    .extent([[0, 0], [width, height2]])
    .on("brush end", brushed);

var zoom = d3.zoom()
    .scaleExtent([1, 10])  
    .translateExtent([[0, 0], [width, height]])
    .extent([[0, 0], [width, height]])
    .on("zoom", zoomed);

var area = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) { return x(d.date); })
    .y0(height)
    .y1(function(d) { return y(d.price); });

var area2 = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) { return x2(d.date); })
    .y0(height2)
    .y1(function(d) { return y2(d.price); });

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 + ")");



var data = [{date: 'Jan 2000', price: 9}, {date: 'Feb 2000', price:20},{date: 'Mar 2000', price: 2}, {date: 'Apr 2000', price: 9}, {date: 'May 2000', price: 11}, {date: 'Jun 2000', price: 12}, {date: 'Jul 2000', price: 21}, {date: 'Aug 2000', price: 9}, {date: 'Sep 2000', price: 15}, {date: 'Oct 2000', price: 6}, {date: 'Nov 2000', price: 49}, {date: 'Dec 2000', price: 48}, {date: 'Jan 2001', price: 55}, {date: 'Feb 2001', price:20},{date: 'Mar 2001', price: 2}, {date: 'Apr 2001', price: 11}, {date: 'May 2001', price: 49}, {date: 'Jun 2001', price: 9}, {date: 'Jul 2001', price: 32}, {date: 'Aug 2001', price: 31}, {date: 'Sep 2001', price: 12}, {date: 'Oct 2001', price: 34}, {date: 'Nov 2001', price: 11}, {date: 'Dec 2001', price: 22}];



var datax = [{date: 'Jan 2000', price: 55}, {date: 'Feb 2000', price:3},{date: 'Mar 2000', price: 22}, {date: 'Apr 2000', price: 2}, {date: 'May 2000', price: 11}, {date: 'Jun 2000', price: 23}, {date: 'Jul 2000', price: 21}, {date: 'Aug 2000', price: 19}, {date: 'Sep 2000', price: 15}, {date: 'Oct 2000', price: 16}, {date: 'Nov 2000', price: 9}, {date: 'Dec 2000', price: 18}, {date: 'Jan 2001', price: 55}, {date: 'Feb 2001', price:20},{date: 'Mar 2001', price: 2}, {date: 'Apr 2001', price: 33}, {date: 'May 2001', price: 31}, {date: 'Jun 2001', price: 9}, {date: 'Jul 2001', price: 32}, {date: 'Aug 2001', price: 7}, {date: 'Sep 2001', price: 12}, {date: 'Oct 2001', price: 2}, {date: 'Nov 2001', price: 2}, {date: 'Dec 2001', price: 3}];

data.forEach(function (d) {
  d.date = parseDate(d.date);
  d.price = +d.price;
});

datax.forEach(function (d) {
  d.date = parseDate(d.date);
  d.price = +d.price;
}); 



  x.domain(d3.extent(data, function(d) { return d.date; })); 
  y.domain([d3.min(data, function(d) { return d.price; }), d3.max(data, function(d) { return d.price; })]); 

  x2.domain(x.domain());
  y2.domain(y.domain());

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

  focus.append("path")
      .datum(datax)
      .attr("class", "areax")
      .attr("d", area);

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

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

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

  context.append("path")
      .datum(datax)
      .attr("class", "areax")
      .attr("d", area2);

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

  context.append("g")
      .attr("class", "brush")
      .call(brush)
      .call(brush.move, x.range());

  svg.append("rect")
      .attr("class", "zoom")
      .attr("width", width)  
      .attr("height", height)
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
      .call(zoom);




function brushed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
  var s = d3.event.selection || x2.range();
  x.domain(s.map(x2.invert, x2));
  focus.select(".area").attr("d", area);
  focus.select(".areax").attr("d", area);
  focus.select(".axis--x").call(xAxis);
  svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
      .scale(width / (s[1] - s[0]))
      .translate(-s[0], 0));
}

function zoomed() {
  if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
  var t = d3.event.transform;
  x.domain(t.rescaleX(x2).domain());
  focus.select(".area").attr("d", area);
  focus.select(".areax").attr("d", area);
  focus.select(".axis--x").call(xAxis);
  context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
}


</script>