【问题标题】:svg: child element alters its parent(svg container)'s sizesvg:子元素改变其父元素(svg 容器)的大小
【发布时间】:2013-12-02 09:22:35
【问题描述】:

看下图:

每当图表放大 (d3js) 时,svg 容器就会变大。子元素(svg:g)的大小变化如何影响其父容器(svg)的大小?我该如何防止这种情况发生?

var svgContainer = d3.select(".graph-container").append("svg")
  .attr("id", "firstGraph")
  .attr("width", w + margin[2] + margin[3]) // labels are placed in the margins
  .attr("height", h + margin[0] + margin[1])
  //.attr("viewBox", "0 0 "+(w+margin[2]+margin[3])+" "+containerHeight)
  //.attr("preserveAspectRation", "xMidYMid")
  .attr("transform", "translate(" + margin[2] + "," + margin[0] + ")")
  //.append("svg:g")
  //.attr("transform", "translate(" + margin[2] + "," + margin[0] + ")"); //origin at (marginLeft, marginTop)

var graph = svgContainer.append("svg:g")
  .attr("width", w)
  .attr("height", h)
  .call(zoom);

var rect = graph.append("svg:rect")
  .attr("width", w)
  .attr("height", h)
  .attr("class", "plot");

//draw price data line
graph.append("path")
  .attr("d", line1(priceData))
  .attr("class", "price");

//draw difficulty data line
graph.append("path")
  .attr("d", line2(difficultyData))
  .attr("class", "difficulty");

【问题讨论】:

    标签: javascript css svg d3.js


    【解决方案1】:

    使用clipPath

    svgContainer.append("defs").append("clipPath")
        .attr("id", "clip")
      .append("rect")
        .attr("width", w)
        .attr("height", h);
    

    ...

    graph.append("path")
      .attr("d", line1(priceData))
      .attr("clip-path", "url(#clip)")
      .attr("class", "price");
    

    MDN on clipPaths

    【讨论】:

    猜你喜欢
    • 2018-08-23
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2017-05-03
    • 1970-01-01
    相关资源
    最近更新 更多