【问题标题】:Svg clip-path within rectangle does not work矩形内的 Svg 剪辑路径不起作用
【发布时间】:2012-05-07 18:07:37
【问题描述】:

我有一个带有 x 轴和 y 轴的简单图表。我不希望我在其中绘制的绘图区域与轴重叠。

我正在使用 d3 创建图表,但剪辑路径不起作用:

http://jsfiddle.net/EqLBJ/

var margin = {top: 19.5, right: 19.5, bottom: 19.5, left: 39.5},
    width = 960 - margin.right,
    height = 500 - margin.top - margin.bottom;

var xScale = d3.scale.linear().
    domain([xMin, xMax]). // your data minimum and maximum
    range([0, width]); // the pixels to map to, e.g., the width of the diagram.

var yScale = d3.scale.linear().
    domain([yMax, yMin]). 
    range([0, height]); 

var xAxis = d3.svg.axis().orient("bottom").scale(xScale).ticks(10, d3.format(",d")),
    yAxis = d3.svg.axis().orient("left").scale(yScale);


var chart = d3.select("#chart").append("svg")
    .attr("class", "chart")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .attr("pointer-events", "all")
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
    .call(d3.behavior.zoom().scaleExtent([0.2, 5]).on("zoom", redraw));


var rect = chart.append('svg:rect')
    .attr('width', width)
    .attr('height', height)
    .attr('fill', 'white');


var line = d3.svg.line()
        .interpolate("basis")
        .x(function(d, i) { return xScale(d.time); })
        .y(function(d) { return yScale(d.value); });

var clip = chart.append("svg:clipPath")
    .attr("id", "clip");

clip.append("svg:rect")
    .attr("id", "clip-rect")
    .attr("width", width)
    .attr("height", height);
    // .attr("fill", "white");

var path = chart.append("svg:path")
    .attr("clip-path", "url(#clip-rect)")
    .data([data])
    .attr("class", "line")
    .attr("fill", "none")
    .attr("stroke", "maroon")
    .attr("stroke-width", 2)
    .attr("d", line);

// x-axis label
chart.append("text")
    .attr("class", "x label")
    .attr("text-anchor", "end")
    .attr("x", width)
    .attr("y", height - 6)
    .text("time");

// y-axis label
chart.append("text")
    .attr("class", "y label")
    .attr("text-anchor", "end")
    .attr("y", 6)
    .attr("dy", ".75em")
    .attr("transform", "rotate(-90)")
    .text("value");

// x-axis
var xaxis = chart.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

// y-axis
var yaxis = chart.append("g")
    .attr("class", "y axis")
    .call(yAxis);



function redraw() 
{
    console.log("here", d3.event.translate, d3.event.scale);

    path.transition()       
        .ease("linear")
        .attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")");

}

【问题讨论】:

    标签: svg d3.js


    【解决方案1】:

    你想要这样的东西:

    http://jsfiddle.net/dsummersl/EqLBJ/1/

    具体来说:

    • 使用“clip”而不是“clip-rect”
    • 将要剪辑的内容放入“g”元素中,并指定“clip-path”属性和“g”元素的变换。

    【讨论】:

    • 剪辑路径不能合法地指向每个w3.org/TR/SVG/masking.html#EstablishingANewClippingPath元素
    • 我不确定你指的是什么。根据clip-path属性的定义,它适用于<g>标签恰好是其成员的容器元素。见:w3.org/TR/SVG/masking.html#ClipPathProperty——除此之外,我的例子很有效......
    • 我没有成功地试图说明,虽然 元素上可以存在剪辑路径属性,但剪辑路径的 xlink:href 不能指向 元素。
    • 啊...你的意思是'clip-path'必须引用'clipPath',否则会被忽略?
    • 是的,虽然我说得很糟糕,而且clipPath不能包含元素。
    猜你喜欢
    • 2017-07-04
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多