Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div>
  <button id="sort1">Sort by Country</button>
  <button id="sort2">Sort by Median income (US$, PPP)[3]</button>
  <button id="sort3">Sort by mean PPP USD 2013</button>
  <button id="sort4">Sort both independently</button>
</div>
<svg id="one" width="400" height="600"></svg>
<svg id="two" width="400" height="600"></svg>
              
            
!

CSS

              
                .rivela .background{
  fill:white;
}
.rivela .item.selected rect{
  fill:orange!important;
}
              
            
!

JS

              
                var chartGen = rivela.barchart()
    .width(400).height(600)
    .key(d => d.Country)
    .horizontal(true)
    .margin({left: 100})
    .axisx(d3.axisBottom().tickFormat(d3.format('.0s')).ticks(5))

var originalDataset

d3.csv('https://fabiofranchino.com/disk/datasets/country-median-mean.csv', data => {
  originalDataset = data
  update(data, data)
})

function update (data1, data2) {
  chartGen.value(d => d['Median income (US$, PPP)[3]'])
    .colors('#BE1E2E')
    .title('Median income (US$, PPP)[3]')

  d3.select('#one')
        .datum(data1)
        .call(chartGen)
        .on('over', function () {
          var e = d3.event.detail
          selectSameFromOne(e)
        })

  chartGen
    .value(d => d['mean PPP USD 2013'])
    .colors('#C6D8E4')
    .title('mean PPP USD 2013')

  d3.select('#two')
        .datum(data2)
        .call(chartGen)
        .on('over', function () {
          var e = d3.event.detail
          selectSameFromTwo(e)
        })
}

function selectSameFromOne (dat) {
  var index1 = dat.index
  var index2
  var data2 = d3.select('#two').datum()
  data2.forEach((d, i) => {
    if (d.Country === dat.datum.Country) index2 = i
  })
  selectSame(index1, index2)
}

function selectSameFromTwo (dat) {
  var index2 = dat.index
  var index1
  var data1 = d3.select('#one').datum()
  data1.forEach((d, i) => {
    if (d.Country === dat.datum.Country) index1 = i
  })
  selectSame(index1, index2)
}

function selectSame (id1, id2) {
  console.log(id1, id2)
  d3.select('#one').selectAll('.item')
    .classed('selected', (d, i) => i === id1)
  d3.select('#two').selectAll('.item')
    .classed('selected', (d, i) => i === id2)
}

d3.selectAll('button')
    .on('click', function () {
      var id = d3.select(this).attr('id')

      var k = ''

      if (id === 'sort1') k = 'Country'
      if (id === 'sort2') k = 'Median income (US$, PPP)[3]'
      if (id === 'sort3') k = 'mean PPP USD 2013'

      var ndata1 = originalDataset.sort((a, b) => d3.descending(a[k], b[k]))
      var ndata2 = ndata1

      if (id === 'sort4') {
        var copy = originalDataset.map(d => d)
        ndata1 = copy.sort((a, b) => d3.descending(a['Median income (US$, PPP)[3]'], b['Median income (US$, PPP)[3]']))
        ndata2 = originalDataset.sort((a, b) => d3.descending(a['mean PPP USD 2013'], b['mean PPP USD 2013']))
      }

      update(ndata1, ndata2)
    })

              
            
!
999px

Console