search

Home  >  Q&A  >  body text

Place DIV elements within the SVG viewport

How to position HTML DIV elements relative to SVG elements?

I used D3 to generate the SVG chart:

I want to add a dropdown next to the legend in the lower right corner. I do have the corresponding element (a regular HTML DIV element), but I don't know how to position it correctly on the chart.

I understand positioning of SVG elements, but I don't know how to apply something like a DIV element.

Any ideas other than placing it outside the viewport (like below)?

P粉803444331P粉803444331484 days ago596

reply all(1)I'll reply

  • P粉529581199

    P粉5295811992023-09-08 16:47:49

    ViewCSS Location. You can create a relatively positioned DIV to hold your SVG. You can then place an absolutely positioned DIV on top of it to accommodate the select element; you can use its left and top styles to position the second DIV Place anywhere you want.

    Everything put together:

    let w = 500;
    let h = 300;
    let pad = 10;
    let xmin = -2 * Math.PI;
    let xmax = 2 * Math.PI;
    let ymin = -1.1;
    let ymax = 1.1;
    
    let svg = d3.select("#graph")
        .append("svg")
        .attr("max-width", `${w}px`)
        .attr("viewBox", [0, 0, w, h]);
        
    let x_scale = d3
      .scaleLinear()
      .domain([xmin, xmax])
      .range([pad, w - pad]);
    let y_scale = d3
      .scaleLinear()
      .domain([ymin, ymax])
      .range([h - pad, pad]);
    let path = d3
      .line()
      .x((d) => x_scale(d[0]))
      .y((d) => y_scale(d[1]));
    
    svg
      .append("g")
      .attr("transform", `translate(0, ${y_scale(0)})`)
      .call(d3.axisBottom(x_scale).tickFormat(d3.format("d")).tickSizeOuter(0));
    svg
      .append("g")
      .attr("transform", `translate(${x_scale(0)})`)
      .call(d3.axisLeft(y_scale).ticks(5).tickSizeOuter(0));
      
    d3.select('#function')
      .on('change', function() {
        draw_graph(this.value)
      })
      
      
    draw_graph('sine')
      
    
    function draw_graph(f_string) {
      svg.select('path.graph').remove()
      let f
      if(f_string == "sine") {
        f = Math.sin;
      }
      else {
        f = Math.cos;
      }
      let pts = d3.range(xmin, xmax, 0.01)
        .map((x) => [x, f(x)]);
      svg
        .append("path")
        .attr('class', 'graph')
        .attr("d", path(pts))
        .attr("fill", "none")
        .attr("stroke", "black")
        .attr("stroke-width", 3);
    }
    <script src="https://d3js.org/d3.v7.min.js"></script>
    
    <div style="position: relative" id="viz">
      <svg id="graph" width=500 height=300 style="border: solid 2px black"></svg>
      <div style="border: solid 0.5px black; background-color: rgba(255,255,255,0.7); position: absolute; left: 20px; top: 30px">
        <label for="function">function:</label>
        <select name="function" id="function">
          <option value="sine">sine</option>
          <option value="cosine">cosine</option>
        </select>
       </div>
    </div>

    reply
    0
  • Cancelreply