cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Untitled

A Pen By Captain Anonymous

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <html>
  <head>
    <title>D3</title>
    <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
  </head>
  <body>
    <h2>D3js example</h2>
    <svg width=500 height=300>
    </svg>
    <script>

      var crimesData = [
          {
                  "Crime type" : "Anti-social behaviour",
                  "total" : 5593
          },
          {
                  "Crime type" : "Burglary",
                  "total" : 888
          },
          {
                  "Crime type" : "Criminal damage and arson",
                  "total" : 1215
          },
          {
                  "Crime type" : "Other theft",
                  "total" : 1343
          },
          {
                  "Crime type" : "Violence and sexual offences",
                  "total" : 2123
          },
          {
                  "Crime type" : "Drugs",
                  "total" : 292
          },
          {
                  "Crime type" : "Public order",
                  "total" : 577
          },
          {
                  "Crime type" : "Shoplifting",
                  "total" : 1019
          },
          {
                  "Crime type" : "Vehicle crime",
                  "total" : 689
          },
          {
                  "Crime type" : "Possession of weapons",
                  "total" : 22
          },
          {
                  "Crime type" : "Bicycle theft",
                  "total" : 271
          },
          {
                  "Crime type" : "Other crime",
                  "total" : 88
          },
          {
                  "Crime type" : "Theft from the person",
                  "total" : 172
          },
          {
                  "Crime type" : "Robbery",
                  "total" : 91
          }
      ]

      var extent = d3.extent(crimesData, function(d){return d.total})
      var widthScale = d3.scale.linear().domain(extent).range([0, 500])
      var colorScale = d3.scale.category10()

      d3.select("svg").selectAll("rect")
        .data(crimesData)
      .enter()
        .append("rect")
        .attr("x", function(d, i){ return 0 })
        .attr("y", function(d, i){ return i * 30})
        .attr("width", function(d, i){ return widthScale(d.total)})
        .attr("height", function(d, i){ return 25})
        .attr("fill", function(d, i){ return colorScale(i)})
    </script>
  </body>
</html>

            
          
!
999px
Loading ..................

Console