Fill rect with pattern

后端 未结 2 853
天涯浪人
天涯浪人 2020-12-31 02:28

I am using d3.js for graph. at some point i have to show data with some special part of graph for example if the values is cross some boundary then show that part with filli

2条回答
  •  青春惊慌失措
    2020-12-31 03:31

    How about this:

    Live Demo

    JS

    var svg = d3.select("body").append("svg");
    
    svg
      .append('defs')
      .append('pattern')
        .attr('id', 'diagonalHatch')
        .attr('patternUnits', 'userSpaceOnUse')
        .attr('width', 4)
        .attr('height', 4)
      .append('path')
        .attr('d', 'M-1,1 l2,-2 M0,4 l4,-4 M3,5 l2,-2')
        .attr('stroke', '#000000')
        .attr('stroke-width', 1);
    
    svg.append("rect")
          .attr("x", 0)
          .attr("width", 100)
          .attr("height", 100)
          .style("fill", 'yellow');
    
    svg.append("rect")
        .attr("x", 0)
        .attr("width", 100)
        .attr("height", 100)
        .attr('fill', 'url(#diagonalHatch)');
    

    Results

    enter image description here

提交回复
热议问题