d3:clipPath不剪辑

时间:2017-02-12 11:11:20

标签: javascript d3.js

d3项目中的clipPath不会剪辑:图形溢出左侧的图表窗口。 我在网上看了几个例子,但是我没看到,为什么我的clipPath不起作用。

可以在此处找到工作示例:https://phinetwork.ch/wolf/dynamicgraph.html

代码:

    <style>
    .area {
      fill: darkorange;
}
    .bgrect {
    background-color:#999;
    }
.zoom {
  cursor: move;
  fill: none;
  pointer-events: all;
    background-color:grey;
}
.grid line{
stroke: lightgrey;
stroke-opacity: 0.7;
shape-rendering: crispEdges;
}
.grid text {
        display:none;
    }
.grid path, .xgrid path{
stroke-width: 0;
}
</style>
<svg width="900" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var svg = d3.select("svg"),
    margin = {top: 20, right: 0, bottom: 110, left: 40},
    margin2 = {top: 430, right: 0, 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("%Y-%m-%d");

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)
    .ticks(10),
    yAxis = d3.axisLeft(y)
    .ticks(7),
    xgAxis = d3.axisBottom(x)
    .ticks(10)
    .tickSize(-height),
    ygAxis = d3.axisLeft(x)
    .ticks(10)
    .tickSize(-width - margin.left),
    xAxis2 = d3.axisBottom(x2);

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

var zoom = d3.zoom()
    .scaleExtent([1, Infinity])
    .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.rz); });

var area2 = d3.area()
    .curve(d3.curveMonotoneX)
    .x(function(d) { return x2(d.date); })
    .y0(height2)
    .y1(function(d) { return y2(d.rz); });
//svg.append("defs").append("clipPath")
svg.append("clipPath")
    .attr("id", "clip")
    .append("rect")
    .attr("x", 40)
    .attr("y", 0)
    .attr("width", width-margin.left)
    .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 + ")");

d3.csv("files/institute/testdat.csv", type, function(error, data) {
  if (error) throw error;

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

focus.append("rect")
      .attr("class", "bgrect")
      .attr("width", width)
      .attr("height", height)
      //.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  focus.append("path")
      .datum(data)
      .attr("class", "area")
    .attr("clip-path", "url(#clip)")
      .attr("d", area);

  focus.append("g")
      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(xAxis);
 focus.append("g")
      .attr("class", "axis grid")
      .attr("transform", "translate(0," + height + ")")
      .call(xgAxis);
focus.append("g")
      .attr("class", "axis grid")
    .attr("transform", "translate(0," + -margin.top + ")")
      .call(ygAxis);
  focus.append("g")
      .attr("class", "axis axis--y")
      .call(yAxis);

  context.append("path")
      .datum(data)
      .attr("class", "area")
      .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 - margin.left)
      .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(".axis--x").call(xAxis);
focus.select(".grid").call(xgAxis);
  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(".axis--x").call(xAxis);
    focus.select(".grid").call(xgAxis);
  context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
}

function type(d) {
  d.date = parseDate(d.date);
  d.rz = +d.rz;
  return d;
}
</script>

对此问题的任何建议?

0 个答案:

没有答案