Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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 Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <div class="container">
  <div class="row">
    <div class="col-sm-6">
      <div id="chart"></div>
    </div>
    <div class="col-sm-6">
      <div class="row">
        <button type="button" class="btn btn-xs" id="new_data_button">Generate Data</button>
        <span id="category_info"></span>
        <button type="button" class="btn btn-xs" id="pca_button">PCA</button>
        <button type="button" class="btn btn-xs" id="project_button">Project</button>
        <button type="button" class="btn btn-xs" id="clean_button">Clear</button>
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                body {
  background-color: #555;
  margin-top: 10px;
}

#chart {
  background-color: #555;
}

.row {
  padding-top: 2px;
}
.circle {
  stroke: #000;
  stroke-width: 1px;
  fill: #fa6900;
  fill-opacity: 1;
}

.line {
  stroke: #cccccc;
  stroke-width: 3px;
}

              
            
!

JS

              
                var size = 400; //The size of the canvas
var margin_size = 50;
var point_size = 8;
var colors = d3.scaleOrdinal(d3.schemeCategory10);
var domain_max = 100;
var data = [];
var pca = undefined;
var line = undefined;

function drawCircle(container, p, r, color) {
  var circle = container
    .append("circle")
    .attr("cx", p.x)
    .attr("cy", p.y)
    .attr("r", r)
    .classed("circle", true);

  if (color) {
    circle.style("fill", color);
  }
  return circle;
}

function drawLine(container, p1, p2, color) {
  var line = container
    .append("line")
    .attr("x1", p1.x)
    .attr("y1", p1.y)
    .attr("x2", p2.x)
    .attr("y2", p2.y)
    .classed("line", true);
  
  if (color) {
    line.style("stroke", color);
  }
  return line;
}

function pca_line() {
  var PCA = ML.Stat.PCA;
  var pca_data = [];
  data.map(function(d) {
    pca_data.push([d.x, d.y]);
  });
  pca = new PCA(pca_data);
  var model = pca.toJSON();

  var alpha = Math.atan(model.U[0][1] / model.U[0][0]);
  var y0 = model.means[1] - Math.tan(alpha) * model.means[0];
  var x0 = Math.tan(alpha) * domain_max + y0;

  //TODO : this line sometime is not the principal line, need fix
  var pca_line = [[0, y0], [domain_max, x0]];
  return pca_line;
}

function projection(p1, p2, p3) {
  // Looking for vector/matrix method
  var k = (p2.y - p1.y) / (p2.x - p1.x);
  var b = p1.y - k * p1.x;

  var x = (k * (p3.y - b) + p3.x) / (k * k + 1);
  var y = k * x + b;

  return { x: x, y: y };
}

function project_lines() {
  var result = [];
  var p1 = { x: line[0][0], y: line[0][1] };
  var p2 = { x: line[1][0], y: line[1][1] };

  data.map(function(item) {
    result.push([projection(p1, p2, item), item]);
  });
  return result;
}

function clean() {
  data = [];
  pca = undefined;
  $(".circle").remove();
  $(".line").remove();
}

$(function() {
  var margin = {
    top: margin_size,
    right: margin_size,
    bottom: margin_size,
    left: margin_size
  },
    width = size - margin.left - margin.right,
    height = size - margin.top - margin.bottom;

  var root = d3
    .select("#chart")
    .append("svg")
    .attr("width", size)
    .attr("height", size);

  var g = root
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

  //Draw Axis
  var xScale = d3.scaleLinear().rangeRound([0, width]);
  var yScale = d3.scaleLinear().rangeRound([height, 0]);

  xScale.domain([0, domain_max]);
  yScale.domain([0, domain_max]);

  g
    .append("g")
    .attr("transform", "translate(0," + height + ")")
    .call(d3.axisBottom(xScale));

  g.append("g").call(d3.axisLeft(yScale));

  // Generate Data
  $("#new_data_button").click(function() {
    clean();
    root.on("click", function() {
      var coords = d3.mouse(this);
      var mapped_coords = [coords[0] - margin_size, coords[1] - margin_size];
      var newData = {
        x: Math.round(xScale.invert(mapped_coords[0])), // Takes the pixel number to convert to number
        y: Math.round(yScale.invert(mapped_coords[1]))
      };
      drawCircle(g, { x: mapped_coords[0], y: mapped_coords[1] }, point_size);
      data.push(newData);
    });
  });

  $("#clean_button").click(function() {
    clean();
  });

  $("#pca_button").click(function() {
    line = pca_line();
    var p1 = {},
      p2 = {};
    p1.x = xScale(line[0][0]);
    p1.y = yScale(line[0][1]);
    p2.x = xScale(line[1][0]);
    p2.y = yScale(line[1][1]);
    // remove previsou pca line
    $(".line").remove();
    drawLine(g, p1, p2);
  });

  $("#project_button").click(function() {
    var lines = project_lines();
    lines.map(function(l) {
      var p1 = {},
        p2 = {};
      p1.x = xScale(l[0].x);
      p1.y = yScale(l[0].y);
      p2.x = xScale(l[1].x);
      p2.y = yScale(l[1].y);
      drawLine(g, p1, p2, "blue");
    });
  });
});

              
            
!
999px

Console