block by timelyportfolio 6585060

rCharts dimple version of Fantasy

Full Screen

index.html

<!doctype HTML>
<meta charset = 'utf-8'>
<html>
  <head>
    <link rel='stylesheet' href="//netdna.bootstrapcdn.com/bootswatch/2.3.1/cosmo/bootstrap.min.css">
     <link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-responsive.min.css" >
    <link rel='stylesheet' href="//getbootstrap.com/2.3.2/assets/js/google-code-prettify/prettify.css">
    <link rel='stylesheet' href="//aozora.github.io/bootplus/assets/css/docs.css">
    
    <script src='//d3js.org/d3.v3.min.js' type='text/javascript'></script>
    <script src='//dimplejs.org/dist/dimple.v1.min.js' type='text/javascript'></script>
    
    <style>
    .rChart {
      display: block
      margin: auto auto;
      width: 100%;
      height: 400px;
    }
    /*
    body {
      margin-top: 60px;
    }
    */
    </style>
    
  </head>
  <body>
    <div class='container'>
      <div class='row'>
        <div class='span8'>
          <div class="bs-docs-example">
            <div id='chartab07907958' class='rChart nvd3Plot dimple'></div>
          <br/>
<pre><code class='r'>
#hacky way of doing ppr; know this is still not really acceptable
final$player_ppr &lt;- paste0(final$player,&quot;_&quot;,final$ppr)
dPlotPPR &lt;- dPlot(
x = c(&quot;player&quot;,&quot;ppr&quot;),
y = &quot;ave&quot;,
groups = &quot;player&quot;,
data = subset(subset(final,category %in% c(&quot;wr&quot;)), rank &lt; 20),
type = &quot;bubble&quot;,
height = 400,
width = 800,
bounds = list( x = 20, y = 20, width = 700, height = 300)
)
dPlotPPR$xAxis ( orderRule = &quot;ave&quot; )
dPlotPPR

</code></pre>
</div>
        </div>
      </div>
    </div>
      
    <script type="text/javascript">
(function(){
  var opts = {
 "dom": "chartab07907958",
"width":    800,
"height":    400,
"x": [ "player", "ppr" ],
"y": "ave",
"groups": "player",
"type": "bubble",
"bounds": {
 "x":     20,
"y":     20,
"width":    700,
"height":    300 
},
"id": "chartab07907958" 
},
    data = [{"rank":1,"player":"Calvin Johnson","best":1,"worst":14,"ave":1.8,"stddev":1.7,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Calvin Johnson_N"},{"rank":2,"player":"Brandon Marshall","best":1,"worst":11,"ave":3.6,"stddev":1.9,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Brandon Marshall_N"},{"rank":3,"player":"Demaryius Thomas","best":1,"worst":15,"ave":3.8,"stddev":2.6,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Demaryius Thomas_N"},{"rank":4,"player":"A.J. Green","best":1,"worst":18,"ave":4.2,"stddev":2.5,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"A.J. Green_N"},{"rank":5,"player":"Julio Jones","best":1,"worst":18,"ave":7,"stddev":3.3,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Julio Jones_N"},{"rank":6,"player":"Andre Johnson","best":1,"worst":37,"ave":7.6,"stddev":3.8,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Andre Johnson_N"},{"rank":7,"player":"Vincent Jackson","best":2,"worst":17,"ave":7.9,"stddev":3.2,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Vincent Jackson_N"},{"rank":8,"player":"Dez Bryant","best":2,"worst":23,"ave":9,"stddev":4.6,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Dez Bryant_N"},{"rank":9,"player":"Victor Cruz","best":2,"worst":30,"ave":9.2,"stddev":3.7,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Victor Cruz_N"},{"rank":10,"player":"Randall Cobb","best":3,"worst":24,"ave":10.6,"stddev":3.8,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Randall Cobb_N"},{"rank":11,"player":"Larry Fitzgerald","best":1,"worst":35,"ave":10.7,"stddev":6.8,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Larry Fitzgerald_N"},{"rank":12,"player":"Reggie Wayne","best":5,"worst":27,"ave":13.1,"stddev":3.4,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Reggie Wayne_N"},{"rank":13,"player":"Jordy Nelson","best":2,"worst":27,"ave":13.1,"stddev":4,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Jordy Nelson_N"},{"rank":14,"player":"Wes Welker","best":4,"worst":31,"ave":14.7,"stddev":4.6,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Wes Welker_N"},{"rank":15,"player":"DeSean Jackson","best":5,"worst":35,"ave":15.5,"stddev":4.7,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"DeSean Jackson_N"},{"rank":16,"player":"Pierre Garcon","best":5,"worst":39,"ave":17.3,"stddev":5.8,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Pierre Garcon_N"},{"rank":17,"player":"Hakeem Nicks","best":4,"worst":36,"ave":18,"stddev":5.1,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Hakeem Nicks_N"},{"rank":18,"player":"Marques Colston","best":5,"worst":54,"ave":18.7,"stddev":6.8,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Marques Colston_N"},{"rank":19,"player":"Steve Smith","best":7,"worst":40,"ave":20.7,"stddev":5.2,"category":"wr","ppr":"N","pos":"NA","matchup":"NA","player_ppr":"Steve Smith_N"},{"rank":1,"player":"Calvin Johnson","best":1,"worst":11,"ave":2.3,"stddev":2.1,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Calvin Johnson_5"},{"rank":2,"player":"Brandon Marshall","best":1,"worst":10,"ave":3.2,"stddev":1.9,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Brandon Marshall_5"},{"rank":3,"player":"Demaryius Thomas","best":1,"worst":16,"ave":4.8,"stddev":3.4,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Demaryius Thomas_5"},{"rank":4,"player":"A.J. Green","best":1,"worst":19,"ave":4.8,"stddev":3.1,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"A.J. Green_5"},{"rank":5,"player":"Andre Johnson","best":1,"worst":17,"ave":7,"stddev":3.5,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Andre Johnson_5"},{"rank":6,"player":"Victor Cruz","best":1,"worst":22,"ave":7.8,"stddev":3.7,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Victor Cruz_5"},{"rank":7,"player":"Julio Jones","best":1,"worst":23,"ave":8.9,"stddev":4.9,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Julio Jones_5"},{"rank":8,"player":"Vincent Jackson","best":2,"worst":18,"ave":9.4,"stddev":4,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Vincent Jackson_5"},{"rank":9,"player":"Randall Cobb","best":3,"worst":23,"ave":9.9,"stddev":3.6,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Randall Cobb_5"},{"rank":10,"player":"Larry Fitzgerald","best":1,"worst":45,"ave":11.3,"stddev":8.5,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Larry Fitzgerald_5"},{"rank":11,"player":"Reggie Wayne","best":2,"worst":23,"ave":11.3,"stddev":4,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Reggie Wayne_5"},{"rank":12,"player":"Dez Bryant","best":2,"worst":38,"ave":11.6,"stddev":6.6,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Dez Bryant_5"},{"rank":13,"player":"Wes Welker","best":4,"worst":27,"ave":12.6,"stddev":4.6,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Wes Welker_5"},{"rank":14,"player":"Jordy Nelson","best":2,"worst":28,"ave":15.3,"stddev":5,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Jordy Nelson_5"},{"rank":15,"player":"DeSean Jackson","best":4,"worst":34,"ave":15.7,"stddev":5.6,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"DeSean Jackson_5"},{"rank":16,"player":"Pierre Garcon","best":2,"worst":49,"ave":18,"stddev":7.1,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Pierre Garcon_5"},{"rank":17,"player":"Hakeem Nicks","best":7,"worst":37,"ave":18.4,"stddev":5.6,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Hakeem Nicks_5"},{"rank":18,"player":"Marques Colston","best":5,"worst":48,"ave":20.3,"stddev":7.8,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Marques Colston_5"},{"rank":19,"player":"Steve Smith","best":5,"worst":32,"ave":20.5,"stddev":5.3,"category":"wr","ppr":"5","pos":"NA","matchup":"NA","player_ppr":"Steve Smith_5"}],
    xAxis = {
 "type": "addCategoryAxis",
"showPercent": false,
"orderRule": "ave" 
},
    yAxis = {
 "type": "addMeasureAxis",
"showPercent": false 
},
    zAxis = [],
    colorAxis = [],
    defaultColors = [],
    legend = [];
  var svg = dimple.newSvg("#" + opts.id, opts.width, opts.height);

  //data = dimple.filterData(data, "Owner", ["Aperture", "Black Mesa"])
  var myChart = new dimple.chart(svg, data);
  if (opts.bounds) {
    myChart.setBounds(opts.bounds.x, opts.bounds.y, opts.bounds.width, opts.bounds.height);//myChart.setBounds(80, 30, 480, 330);
  }
  //dimple allows use of custom CSS with noFormats
  if(opts.noFormats) { myChart.noFormats = opts.noFormats; };
  //for markimekko and addAxis also have third parameter measure
  //so need to evaluate if measure provided
  //x axis
  var x;
  if(xAxis.measure) {
    x = myChart[xAxis.type]("x",opts.x,xAxis.measure);
  } else {
    x = myChart[xAxis.type]("x", opts.x);
  };
  if(!(xAxis.type === "addPctAxis")) x.showPercent = xAxis.showPercent;
  if (xAxis.orderRule) x.addOrderRule(xAxis.orderRule);
  if (xAxis.grouporderRule) x.addGroupOrderRule(xAxis.grouporderRule);  
  if (xAxis.overrideMin) x.overrideMin = xAxis.overrideMin;
  if (xAxis.overrideMax) x.overrideMax = xAxis.overrideMax;
  if (xAxis.overrideMax) x.overrideMax = xAxis.overrideMax;
  if (xAxis.inputFormat) x.dateParseFormat = xAxis.inputFormat;
  if (xAxis.outputFormat) x.tickFormat = xAxis.outputFormat;
  //y axis
  var y;
  if(yAxis.measure) {
    y = myChart[yAxis.type]("y",opts.y,yAxis.measure);
  } else {
    y = myChart[yAxis.type]("y", opts.y);
  };
  if(!(yAxis.type === "addPctAxis")) y.showPercent = yAxis.showPercent;
  if (yAxis.orderRule) y.addOrderRule(yAxis.orderRule);
  if (yAxis.grouporderRule) y.addGroupOrderRule(yAxis.grouporderRule);
  if (yAxis.overrideMin) y.overrideMin = yAxis.overrideMin;
  if (yAxis.overrideMax) y.overrideMax = yAxis.overrideMax;
  if (yAxis.inputFormat) y.dateParseFormat = yAxis.inputFormat;
  if (yAxis.outputFormat) y.tickFormat = yAxis.outputFormat;
//z for bubbles
    var z;
  if (!(typeof(zAxis) === 'undefined') && zAxis.type){
    if(zAxis.measure) {
      z = myChart[zAxis.type]("z",opts.z,zAxis.measure);
    } else {
      z = myChart[zAxis.type]("z", opts.z);
    };
    if(!(zAxis.type === "addPctAxis")) z.showPercent = zAxis.showPercent;
    if (zAxis.orderRule) z.addOrderRule(zAxis.orderRule);
    if (zAxis.overrideMin) z.overrideMin = zAxis.overrideMin;
    if (zAxis.overrideMax) z.overrideMax = zAxis.overrideMax;
  }
  if(d3.keys(colorAxis).length > 0) {
    myChart[colorAxis.type](colorAxis.colorSeries,colorAxis.palette) ;
  }

  //allow manipulation of default colors to use with dimple
  if(defaultColors.length) {
    defaultColors = defaultColors[0];
    if (typeof(defaultColors) == "function") {
      //assume this is a d3 scale
      //for now loop through first 20 but need a better way to handle
      defaultColorsArray = [];
      for (var n=0;n<20;n++) {
        defaultColorsArray.push(defaultColors(n));
      };
      defaultColors = defaultColorsArray;
    }
    defaultColors.forEach(function(d,i) {
      defaultColors[i] = new dimple.color(d);
    })
    myChart.defaultColors = defaultColors;
  }
  
  //here need think I need to evaluate group and if missing do null
  //as the first argument
  //if provided need to use groups from opts
  if(opts.hasOwnProperty("groups")) {
    var s = myChart.addSeries( opts.groups, dimple.plot[opts.type] );
    //series offers an aggregate method that we will also need to check if available
    //options available are avg, count, max, min, sum
    if (!(typeof(opts.aggregate) === 'undefined')) {
      s.aggregate = eval(opts.aggregate);
    }
    if (!(typeof(opts.lineWeight) === 'undefined')) {
      s.lineWeight = eval(opts.lineWeight);
    }
    if (!(typeof(opts.barGap) === 'undefined')) {
      s.barGap = eval(opts.barGap);
    }    
  } else var s = myChart.addSeries( null, dimple.plot[opts.type] );
  //unsure if this is best but if legend is provided (not empty) then evaluate
  if(d3.keys(legend).length > 0) {
    var l =myChart.addLegend();
    d3.keys(legend).forEach(function(d){
      l[d] = legend[d];
    });
  }
  //quick way to get this going but need to make this cleaner
  if(opts.storyboard) {
    myChart.setStoryboard(opts.storyboard);
  };

  //catch all for other options
  //these can be provided by dMyChart$chart( ... )
  

  myChart.draw();
})();
</script>
    
  </body>
  <!-- Google Prettify -->
  <script src="//cdnjs.cloudflare.com/ajax/libs/prettify/188.0.0/prettify.js"></script>
  <script 
    src='https://google-code-prettify.googlecode.com/svn-history/r232/trunk/src/lang-r.js'>
  </script>
  <script>
    var pres = document.getElementsByTagName("pre");
    for (var i=0; i < pres.length; ++i) {
      pres[i].className = "prettyprint linenums";
    }
    prettyPrint();
  </script>
</html>

code.R


#hacky way of doing ppr; know this is still not really acceptable
final$player_ppr <- paste0(final$player,"_",final$ppr)
dPlotPPR <- dPlot(
x = c("player","ppr"),
y = "ave",
groups = "player",
data = subset(subset(final,category %in% c("wr")), rank < 20),
type = "bubble",
height = 400,
width = 800,
bounds = list( x = 20, y = 20, width = 700, height = 300)
)
dPlotPPR$xAxis ( orderRule = "ave" )
dPlotPPR