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">
      <button type="button" class="btn btn-xs" id="new_button">Generate Data</button>
      <button type="button" class="btn btn-xs" id="regression_button">Regression</button>
      <button type="button" class="btn btn-xs" id="predict_button">Predict Line</button>
      <button type="button" class="btn btn-xs" id="clean_button">Clear</button>
    </div>
  </div>
</div>
              
            
!

CSS

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

#chart {
  background-color: #555;
}

.row {
  padding-top: 2px;
}

.circle {
  stroke: #fa6900;
  stroke-width: 1px;
  fill: #fa6900;
  fill-opacity: 1;
}

.line {
  stroke: #ccc;
  stroke-width: 2px;
}

.predict {
  stroke: blue;
  opacity: 0.5;
}
              
            
!

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 regression_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) {
  var line = container
    .append("line")
    .attr("x1", p1.x)
    .attr("y1", p1.y)
    .attr("x2", p2.x)
    .attr("y2", p2.y)
    .classed("line", true);
  return line;
}

function clean() {
  data = [];
  $(".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_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);
    });
  });

  $("#regression_button").click(function() {
    var data_array = [];
    //Turn data into array
    data.map(function(d) {
      data_array.push([d.x, d.y]);
    });

    regression_line = ss.linearRegressionLine(
      ss.linearRegression(data_array)
    );

    var p1 = { x: 0, y: yScale(regression_line(0)) };
    var p2 = { x: xScale(domain_max), y: yScale(regression_line(domain_max)) };
    drawLine(g, p1, p2);
  });
  
  $("#clean_button").click(function() {
    clean();
  });
  
  $("#predict_button").click(function() {
    data.map(function(d) {
      var p1 = { x:xScale(d.x), y:yScale(d.y)};
      var p2 = { x:p1.x, y: yScale(regression_line(d.x))}
      var predict_line = drawLine(g, p1, p2);
      predict_line.classed("predict", true);
    });
  });
});

              
            
!
999px

Console