Add a d3js script inside a post

How could I add insert a d3js script inside a post which would show me the charts there .I see the results in a black window and it works on hover.

This is the correct one on a html page right now.

  <svg width="900" height="640"></svg>

 <script src="" type="text/JavaScript"></script>
   <script src=""></script>
   <script src=""></script>
   var svg ="svg"),
       width = +svg.attr("width"),
       height = +svg.attr("height");

   var projection = d3.geoAlbers()
       .translate([width / 2, height / 2])

   var radius = d3.scaleSqrt()
       .domain([0, 100])
       .range([0, 14]);

   var path = d3.geoPath()

       var circles = svg.append("svg:nflteams")
    .attr("name", "circles");

   var voronoi = d3.voronoi()
       .extent([[-1, -1], [width + 1, height + 1]]);

              .defer(d3.json, "us.json")
              .defer(d3.csv, "nflteams.csv", typeTeam)
              .defer(d3.csv, "nflresults2017.csv", typeSeason)

              function typeTeam(d) {
              d[0] = +d.longitude;
              d[1] = +d.latitude;
              d.arcs = {type: "MultiLineString", coordinates: []};
              return d;

  function ready(error, us, nflteams, nflresults2017) {
          if (error) throw error;

          var teambyName =, function(d) { return; });

          nflresults2017.forEach(function(nfl2017) {
            var source = teambyName.get(nfl2017.origin),
                target = teambyName.get(nfl2017.destination);
            source.arcs.coordinates.push([source, target]);
            target.arcs.coordinates.push([target, source]);

          nflteams = nflteams
              .filter(function(d) { return d.arcs.coordinates.length; });

              .attr("class", "land")
              .attr("d", path);

              .datum(topojson.mesh(us, us.objects.states, function(a, b) {return a !== b;}))
              .attr("class", "state-boundary")
              .attr("d", path);

            .datum({type: "MultiPoint", coordinates: nflteams})
            .attr("class", "nflteam-dots")
            .attr("d", path);

        var nflteam = svg.selectAll(".nflteam")
            .attr("class", "nflteam");

      .text(function(d) { return + "\n" + d.wins + " wins"; });

      .attr("class", "nflteam-arc")
      .attr("d", function(d) { return path(d.arcs) })
        .text(function(d){return "test" + d.wins;});

      .attr("class", "nflteam-cell")
      .attr("d", function(d) { return d ? "M" + d.join("L") + "Z" : null; });

        .attr("r", function(d) { return d.wins*100; });


      function typeSeason(d) {
      d.result= +d.result
      return d;

   </script><img src="//" width="690" height="404">

Hey @senthil_thyagarajan,

Although I would not suggest you to do such a part as render-blocking inline-script, however you can simply change the file extension of your content from .md to .html and the content will be rendered as regular HTML. This should do the trick.

as far as I see you have var svg ="svg") - so also make sure you include an SVG-Tag in your content.

Thank you @rwi.

Is there any reason why an just changing the file to have an HTML extension wouldn’t work?

I’m experiencing a similar problem, but I have an HTML extension. My code works outside Hugo, but when I add it to a post I only get the code displayed.

This is probably a really stupid error, but in my defense I’ve only been using Hugo for a couple of days. My post code is as follows:

date: 2018-05-06T21:33:05-04:00
description: "Comorbitity Git HTML D3 Gist test"
featured_image: ""
tags: []
title: "Comorbitity Gist D3"
<!--script src=""></script>
<!-- script src="//"></script-->
<script src=""></script>

Any advice welcome!!

When you paste that script and/or any needed HTML into the markdown file, and render using hugo server what are you seeing in the browser console?

I just see the code where I would expect to see a graph:

Hmm, looks like it’s just showing the content of that gist rather than running it. I’m not sure if you can run js directly from github gist. Have you tried copying the js to /static/js and sourcing it from there?

 <script src="/js/some.js...


Since it’s html mixed with styling and js I’ve just copied it into the md file to get it working, but I was hoping to host the page by just providing a reference file rather than copy and pasting the code from one place to the other.

That was Rick’s suggestion. You can certainly do that, just move the code into a page in the appropriate subfolder of your /static folder.