cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

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.

            
              ///////////////////////////
// SETTING VIZ PARAMS
///////////////////////////

var w = 400;
var h = 240;

var backgroundColor = "#3c97da";
var drawingColor = "#FFFFFF";
var alphaMax = 1;

var strokeWidth = 1;
var dotWidth = 2;

var numLines = 8;
var numSteps = 9;


///////////////////////////
// CREATE THE DATA SET
///////////////////////////

// FUNCTION TO WRAP THE WHOLE PROCESS; FOR REFRSHING ON CLICK
var drawLines = function(){

  // divide svg width so x positions are equally spaced
  var pointWidth = w / numSteps;
  
  // create a dataset array
  var data = [];
  // creating an array of line point arrays for each line
  for (var i = 0; i < numLines; i++) {
    var lineData = [];
    // random value--rounded to 2 decimals--for line opacity value
    var a = Math.round((Math.random() * alphaMax)*100)/100; 
    
    for (var p = 0; p < numSteps; p++) {
      // setting x values to even steps
      var x = p * pointWidth;
      // random integer for y values--between 0 and svg height
      var y = Math.floor( Math.random() * h ); 
      lineData.push({"x":x, "y":y});
    };
    // defining specific starting and ending points--centered vertically
    lineData[0] = {"x":0, "y": (h / 2), "a":a};
    lineData[numSteps] = ({"x": w  , "y": (h / 2)});
    // pushing each line's data array into the "master" dataset
    data.push(lineData);
  };

  
  ///////////////////////////
  // DRAWING THE SVG
  ///////////////////////////
  
  // create the svg viewport
  d3.select("body").selectAll("*").remove();
  var svgViewport = d3.select("body").append("svg")
    .attr("width", w)
    .attr("height", h)
    .attr("style", "background: " + backgroundColor + ";");
  
  // function for creating lines from dataset values
  var lineFunction = d3.svg.line()
    .x(function(d) { return d.x; })
    .y(function(d) { return d.y; })
    .interpolate("basis"); // "basis" gives smoothest curves
  
  // drawing each line and its dots
  for (var i = 0; i < numLines; i++) {
    // lines
    svgViewport.append("path")
      .datum(data[i])
      .attr("d", lineFunction)
      // .attr("opacity", data[i][0].a) //pulling a value from first line point data
      .attr("fill", "none")
      .attr("stroke-width", strokeWidth)
      .attr("stroke", drawingColor)
    ;
    
    // dots
    svgViewport.selectAll("dot")
        .data(data[i])
      .enter().append("circle")
        .attr("r", dotWidth) //point size
        .attr("cx", function(d) { return ( d.x + 0 ); })
        .attr("cy", function(d) { return ( d.y + 0 ); })
        .attr("fill", drawingColor)
        // .attr("opacity", data[i][0].a);

  };
// END OF FUNCTION TO WRAP THE WHOLE PROCESS; FOR REFRSHING ON CLICK
};


// GENERATE AN INITIAL CHART ON LOAD
drawLines();

// GENERATE NEW CHART ON CLICK
$("body").click(function(){
  drawLines();
});

            
          
!
999px
Loading ..................

Console