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

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.

            
              <div class="container" id="main">
  <div class="row">
    <div class="col-xs-12 col-md-12">
      <div id="chart">
         <h3>Gender Earnings Disparity</h3>
         <button class='btn btn-default btn-sm' style='display:none;'>
          Sort
         </button>
       </div>
    </div>
  </div>
</div>

            
          
!
            
              #main {
  margin-top: 20px;
}
#chart h3{
  margin-bottom: 20px;
}
#chart {
  background: #eee;
  padding: 20px;
}

#chart .x-axis-layer path{
  fill: none;
  stroke: none;
}
#chart .x-axis-layer line {
  fill: none;
  stroke: #bbb;
  stroke-dasharray: 5,5;
}

#chart .y-axis-layer path,
#chart .y-axis-layer line {
  fill: none;
  stroke: none;
}

body {
  font-family: "Helvetica";
  font-size: 12px;
}
            
          
!
            
              attachTooltip = (sel, opts) ->
  tip = d3.tip().attr('class', 'd3-tip').html(opts.html);
  d3.select(sel.node().parentNode).call(tip);
  sel.on("mouseover", tip.show);
  sel.on("mouseout", tip.hide);
  sel
chartConstructor = (skeleton) ->
  L = skeleton.getLayerOrganizer()
  S = 
    x: d3.scale.linear()
    y: d3.scale.ordinal()
  A = 
    x1: (d) -> d.Men
    x2: (d) -> d.Women
    y: (d) -> d.School
  L.create(["x-axis", "y-axis", "dumbell", "annotation1", "annotation2"])
  L.get('annotation1').append('text')
  L.get('annotation2').append('text')
  visualize = -> 
    data = skeleton.data()
    opts = skeleton.options()
    H = skeleton.getInnerHeight()
    W = skeleton.getInnerWidth()
    S.x.range([0, W]).domain [60, 180]
    S.y.rangeRoundPoints([0, H], 0.9)
      .domain opts.yDomain || data.map(A.y)
    
    dumbell = L.get('dumbell')
   
    L.get('annotation1').select('text')
      .attr
        x: S.x A.x1(data[0])
        y: S.y A.y(data[0])
        dy: -10
        "text-anchor": "middle"
        fill: "steelblue"
      .text("Men")
      
    L.get('annotation2').select('text')
      .attr
        x: S.x A.x2(data[0])
        y: S.y A.y(data[0])
        dy: -10
        "text-anchor": "middle"
        fill: "darkred"
      .text("Women")
     
    line = dumbell.selectAll(".line").data(data)
    line.enter().append("line.line")
    line
      .attr
        x1: (d) -> S.x A.x2(d)
        x2: (d) -> S.x A.x2(d)
        y1: (d) -> S.y A.y(d)
        y2: (d) -> S.y A.y(d)
        stroke: '#aaa';
        "stroke-width": 1
      .transition()
      .ease("exp")
      .duration(3000)
      .attr
        x2: (d) -> S.x A.x1(d)
    circle_1 = dumbell.selectAll(".circle_1").data(data)
    circle_1.enter().append('circle.circle_1')
    circle_1
      .attr
        cx: (d) -> S.x A.x2(d)
        cy: (d) -> S.y A.y(d)
        r: 3
        fill: 'steelblue'
      .transition()
      .ease("exp")
      .duration(3000)
      .attr
        cx: (d) -> S.x A.x1(d)
      
    circle_2 = dumbell.selectAll(".circle_2").data(data)
    circle_2.enter().append('circle.circle_2')
    circle_2
      .attr
        cx: (d) -> S.x A.x2(d)
        r: 3
        fill: 'darkred'
      .transition()
      .attr
         cy: (d) -> S.y A.y(d)
    
    tFun = (d) ->
      """
      #{A.y(d)}<br/><br/>
      M: #{A.x2(d)}, W: #{A.x1(d)}
      """
    dumbell.selectAll("circle").call attachTooltip, {html:tFun}

        
    xAxis = d3.svg.axis()
      .scale(S.x.nice())
      .orient("bottom")
      .ticks(Math.round(W/70))
      .tickSize(-H, 0)
      .tickFormat (d) -> "$#{d}k"
    L.get('x-axis')
      .translate([0, H])
      .call(xAxis)
      
    yAxis = d3.svg.axis()
      .scale(S.y)
      .orient("left")
    L.get('y-axis')
     .call(yAxis)
     
  skeleton
    .resizeToFitContainer()
    .on('data', visualize)
    .on('resize', visualize)
    .autoResize(true)
      
defaults = 
  margin: {top: 20, bottom: 30, left: 100, right: 60}
  initialHeight: 500
  initialWidth: "auto"
  
Chart = d3Kit.factory.createChart(defaults, [], chartConstructor)

myChart = new Chart('#chart').data(schools)

d3.select('button').on 'click', ->
  myChart.data(
    schools.sort (a, b) -> d3.ascending(a.gap, b.gap)
  )
    
            
          
!
999px
Loading ..................

Console