cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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>
  <link rel="stylesheet" href="https://daigofuji.github.io/bbclub-font/css/bbclub-font.css">
  <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>

<body>
  <div id="container">
    <div id="player-info-container">
      <i class="team-logo bb-5x"></i>
      <div id="player-info">
        <div id="player-info-1"></div>
        <div id="player-info-2"></div>
      </div>
    </div>
    <div id="player-pic">
      <img />
    </div>
    <div id="player-stats">
      <div class="chart"></div>
    </div>
  </div>
</body>

</html>
            
          
!
            
              body {
  font-size: 1em;
  font-family: Helvetica, sans-serif;
}

#container {
  position: relative;
  height: 380px;
  width: 640px;
  background-color: #666;
  color: #ccc;
}

#player-info-container {
  position: absolute;
  margin-top: 10px;
}

.team-logo {
  margin-top: 20px;
  margin-left: 20px;
  float: left;
}

#player-info {
  margin-top: 5px;
  margin-left: 10px;
  float: left;
}

#player-info-1 {
  font-size: 3em;
}

#player-info-2 {}

#player-pic {
  position: absolute;
  top: 110px;
  left: 20px;
  border-style: solid;
  border-width: 10px;
  border-color: 10px;
}

#player-stats {
  position: absolute;
  top: 95px;
  left: 245px;
}

.chart div {
  font: 10px sans-serif;
  background-color: steelblue;
  text-align: right;
  padding: 3px;
  margin: 1px;
  color: white;
}

.bar--positive {
  fill: steelblue;
}

.bar--positive:hover {
  fill: red;
}

.bar--negative {
  fill: darkorange;
}

.bar--negative:hover {
  fill: red;
}

.axis text {
  font: 10px sans-serif;
  fill: #fff;
  color: #fff;
}

.axis path,
.axis line {
  fill: none;
  stroke: #fff;
  shape-rendering: crispEdges;
}
            
          
!
            
              // https://codepen.io/jtarnoff/pen/PNxrOO?playerId=545361

// <iframe width="640px" height="400px" src="https://codepen.io/jtarnoff/pen/PNxrOO?playerId=458675"></iframe>

var categories = ['wRC+', 'OPS', 'ISO', 'BsR', 'DRS'];
var defaultId = '545361';

// Use d3.text and d3.csv.parseRows so that we do not need to have a header
// row, and can receive the csv as an array of arrays.
d3.text("http://www.jtarnoff.com/baseball/2016/04/adv-stats-batters/data.txt", function(text) {
  var csv = d3.csv.parseRows(text, function(data) {
    var playerId = getUrlParameter('playerId') === undefined ? defaultId : getUrlParameter('playerId');
    if (data[0] == playerId) {
      loadData(data)
    }
  });
});

var loadData = function(data) {
  $("#player-pic img").attr("src", "http://gdx.mlb.com/images/gameday/mugshots/mlb/" + data[0] + "@3x.jpg");
  $("#player-info-1").html(data[1]);
  $("#player-info-2").html(data[4]);
  $(".team-logo").addClass("bbclub-" + data[3]);

  var graphData = [data[7], data[9], data[11], data[13], data[15]];
  var actualData = [data[6], data[8], data[10], data[12], data[14]];

  var svg = d3.select(".chart").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

  svg.selectAll(".bar")
    .data(graphData)
    .enter().append("rect")
    .attr("class", function(d) {
      return "bar bar--" + (d < 0 ? "negative" : "positive");
    })
    .attr("x", function(d) {
      return x(Math.min(0, d));
    })
    .attr("y", function(d, i) {
      return y.range()[i];
    })
    .attr("width", function(d) {
      return Math.abs(x(d) - x(0));
    })
    .attr("height", y.rangeBand())
    .append("title")
    .text(function(d, i) {
      return y.domain()[i] + ": " + actualData[i] + "\n" + "Z-score: " + d;
    });;

  svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

  svg.append("g")
    .attr("class", "y axis")
    .attr("transform", "translate(" + x(0) + ",0)")
    .call(yAxis);

  svg.append("text") // text label for the x axis
    .attr("x", width / 2)
    .attr("y", 250)
    .style("text-anchor", "middle")
    .style("fill", "#fff")
    .text("Z-score");
}

var margin = {
    top: 20,
    right: 30,
    bottom: 40,
    left: 30
  },
  width = 370 - margin.left - margin.right,
  height = 270 - margin.top - margin.bottom;

var x = d3.scale.linear()
  .domain([-3, 3])
  .range([0, width]);

var y = d3.scale.ordinal()
  .domain(categories)
  .rangeBands([0, height], 0.2);

var xAxis = d3.svg.axis()
  .scale(x)
  .orient("bottom");

var yAxis = d3.svg.axis()
  .scale(y)
  .orient("left")
  .tickSize(0)
  .tickPadding(6);

function type(d) {
  d = +d;
  return d;
}

var getUrlParameter = function getUrlParameter(sParam) {
  var sPageURL = decodeURIComponent(window.location.search.substring(1)),
    sURLVariables = sPageURL.split('&'),
    sParameterName,
    i;

  for (i = 0; i < sURLVariables.length; i++) {
    sParameterName = sURLVariables[i].split('=');

    if (sParameterName[0] === sParam) {
      return sParameterName[1] === undefined ? true : sParameterName[1];
    }
  }
};
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console