✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.

Multiple Transforms in JavaScript

How to use D3.js-based Multiple Transforms in Plotly.js.


Building full-stack apps and dashboards with Plotly.js?
Increase development speed by 2x-10x with Dash. 🏃


New to Plotly?

Plotly is a free and open-source graphing library for JavaScript. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Note: transforms are deprecated and will be removed in a future version of Plotly.js

d3.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv", function(err, rows){

  function unpack(rows, key) {
  return rows.map(function(row) { return row[key]; });
}

  var data = [{
      type: 'scatter',
      mode: 'markers',
      x: unpack(rows, 'lifeExp'),
      y: unpack(rows, 'gdpPercap'),
      text: unpack(rows, 'continent'),
      marker: {
        size: unpack(rows, 'pop'),
        sizemode: "area",
        sizeref: 200000
      },
      transforms: [
        {
        type: 'filter',
        target: unpack(rows, 'year'),
        operation: '=',
        value: '2007'
        }, {
        type: 'groupby',
        groups: unpack(rows, 'continent'),
        styles: [
          {target: 'Asia', value: {marker: {color: 'red'}}},
          {target: 'Europe', value: {marker: {color: 'blue'}}},
          {target: 'Americas', value: {marker: {color: 'orange'}}},
          {target: 'Africa', value: {marker: {color: 'green'}}},
          {target: 'Oceania', value: {marker: {color: 'purple'}}}
        ]
  }]
    }]

var layout = {
  yaxis: {
    type: 'log'
  }
}

Plotly.newPlot('myDiv', data, layout)
});
d3.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv", function(err, rows){

  function unpack(rows, key) {
  return rows.map(function(row) { return row[key]; });
}

  var data = [{
      type: 'scatter',
      mode: 'markers',
      x: unpack(rows, 'lifeExp'),
      y: unpack(rows, 'gdpPercap'),
      text: unpack(rows, 'continent'),
      marker: {
        size: unpack(rows, 'pop'),
        sizemode: "area",
        sizeref: 200000
      },
      transforms: [
        {
          type: 'filter',
          target: unpack(rows, 'year'),
          operation: '=',
          value: '2007'
        }, {
          type: 'aggregate',
          groups: unpack(rows, 'continent'),
          aggregations: [
            {target: 'x', func: 'avg'},
            {target: 'y', func: 'avg'},
            {target: 'marker.size', func: 'sum'}
          ]
       }]
    }]

var layout = {
  yaxis: {
    type: 'log'
  }
}

Plotly.newPlot('myDiv', data, layout)
});
d3.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv", function(err, rows){

  function unpack(rows, key) {
  return rows.map(function(row) { return row[key]; });
}

  var data = [{
      type: 'scatter',
      mode: 'markers',
      x: unpack(rows, 'lifeExp'),
      y: unpack(rows, 'gdpPercap'),
      text: unpack(rows, 'continent'),
      marker: {
        size: unpack(rows, 'pop'),
        sizemode: "area",
        sizeref: 200000
      },
      transforms: [
        {
          type: 'filter',
          target: unpack(rows, 'year'),
          operation: '=',
          value: '2007'
        }, {
          type: 'groupby',
          groups: unpack(rows, 'continent'),
          styles: [
            {target: 'Asia', value: {marker: {color: 'red'}}},
            {target: 'Europe', value: {marker: {color: 'blue'}}},
            {target: 'Americas', value: {marker: {color: 'orange'}}},
            {target: 'Africa', value: {marker: {color: 'green'}}},
            {target: 'Oceania', value: {marker: {color: 'purple'}}}
        ]
        }, {
          type: 'aggregate',
          groups: unpack(rows, 'continent'),
          aggregations: [
            {target: 'x', func: 'avg'},
            {target: 'y', func: 'avg'},
            {target: 'marker.size', func: 'sum'}
          ]
       }]
    }]

var layout = {
  title: '<b>Gapminder</b><br>2007 Average GDP Per Cap & Life Exp. by Continent',
  yaxis: {
    type: 'log'
  }
}

Plotly.newPlot('myDiv', data, layout)
});