block by curran 2c4d5eccd668085b39e2c9015e916b98

Data Driven SVG Text

Full Screen

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Shopping App Prototype</title>
    <script src="https://d3js.org/d3.v4.min.js"></script>
  </head>
  <body>
    <svg width="500" height="960"></svg>
    <script>
      const textValue = d => `${d.name} $${d.price}`

      const data = [
        { name: 'Milk', price: 3 }
      ];

      const svg = d3.select('svg');

      svg.selectAll('text').data(data)
        .enter().append('text')
          .style('font-size', '32pt')
          .style('font-family', 'Sans-Serif')
          .attr('x', 250)
          .attr('y', 50)
          .text(textValue);
    </script>
  </body>
</html>