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 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.

            
              * {
  box-sizing: border-box;  
}

svg {
  width: 100vw;
  background-color: rgba(10,20,0,0.03);
}

.bug-line {
  cursor: pointer;
  animation: .1s linear appears;
}

@keyframes appears {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
            
          
!
            
              function getRandom2016Date(){
  return new Date(2016, Math.floor(Math.random()*100 % 11) +1, Math.floor(Math.random()*100 % 29) +1);
} 
var bugs = Array.from({length: 100}, function(){
  var d = getRandom2016Date();
  var bug = {
    id: (Math.floor(Math.random() * 1000000)),
    start : d,
    end: new Date(d.getTime() + (1000 * 60 * 60 * 24 * ((Math.floor(Math.random() * 100) + 1) % 45 ) ))
  }; 
  return bug;
}).sort((a,b) => a.start - b.start);

const COLORS = ["rgb(244, 67, 54)","rgb(233, 30, 99)","rgb(156, 39, 176)","rgb(103, 58, 183)","rgb(63, 81, 181)","rgb(33, 150, 243)","rgb(3, 169, 244)","rgb(0, 188, 212)","rgb(0, 150, 136)","rgb(76, 175, 80)","rgb(139, 195, 74)","rgb(255, 193, 7)","rgb(255, 152, 0)","rgb(255, 87, 34)","rgb(121, 85, 72)","rgb(96, 125, 139)"];



var svg = document.querySelector('svg');

const jan4 = new Date('2016-01-04');  
const X_PADDING = 17;
const LANE_HEIGHT = 10;
const BOTTOM = svg.viewBox.baseVal.height;
const MILLISECOND_A_DAY = (1000*60*60*24);
var lanes = [];


drawWeeks();
drawToday();
bugs.forEach(drawBug);

function drawBug(bug, idx){
  setTimeout(function(){
  var colorIndex = (bug.id % (COLORS.length - 1));
  var bugColor = COLORS[colorIndex];
  var startDayNumber = ((bug.start - jan4)/(1000*60*60*24));
  var endDayNumber = (((bug.end || new Date('2016-12-31')) - jan4)/MILLISECOND_A_DAY);
  
  var startPoint = X_PADDING + startDayNumber;
  var endPoint = X_PADDING + endDayNumber;
    
  var lane = findLane(startPoint,endPoint);
  if(!lanes[lane]){
    lanes[lane] = [];
  }
  lanes[lane].push([startPoint,endPoint]);
  var y = (lane + 1) * LANE_HEIGHT;
  var bugGroup = document.createElementNS("http://www.w3.org/2000/svg", "g");
  bugGroup.classList.add('bug-line'); 

  var bugLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
  bugLine.setAttribute('x1', startPoint); 
  bugLine.setAttribute('y1', y);
  bugLine.setAttribute('x2', endPoint);
  bugLine.setAttribute('y2', y);
  bugLine.setAttribute('stroke', bugColor);
  bugLine.setAttribute('stroke-width', 2);
  bugLine.setAttribute('stroke-linecap', "round");
  bugGroup.appendChild(bugLine);
  
  if(bug.end){
    var endCircle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
    endCircle.setAttribute('cx',endPoint);
    endCircle.setAttribute('cy', y);
    endCircle.setAttribute('r', 2);
    endCircle.setAttribute('fill', bugColor);
    bugGroup.appendChild(endCircle);
  } 
  
  svg.appendChild(bugGroup);
  },idx * 50);
}

function drawWeeks(){
  for(var i = 0; i <= 52; i++){
    var weekLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
    var x = X_PADDING + (i * 7);
    weekLine.setAttribute('x1', x);
    weekLine.setAttribute('y1', 0);
    weekLine.setAttribute('x2', x);
    weekLine.setAttribute('y2', BOTTOM);
    weekLine.setAttribute('stroke', 'rgba(0,0,0,0.3)');
    weekLine.setAttribute('stroke-width', 0.1);
    svg.appendChild(weekLine); 
  }
}

function drawToday(){
  var todayNumber = (((new Date()) - jan4)/MILLISECOND_A_DAY);
  var todayX = todayNumber + X_PADDING;

  var todayLine = document.createElementNS("http://www.w3.org/2000/svg", "line");
  todayLine.setAttribute('x1', todayX);
  todayLine.setAttribute('y1', 0);
  todayLine.setAttribute('x2', todayX);
  todayLine.setAttribute('y2', BOTTOM);
  todayLine.setAttribute('stroke', 'red');
  todayLine.setAttribute('stroke-width', 0.2);
  svg.appendChild(todayLine); 
}

function findLane(start, end){
  var lane = 0;
  var safe_space = 6;
  start = start - safe_space;
  end = end + safe_space;
  for(;lane < lanes.length;lane++){
    var fit = lanes[lane].every(function(xs){
      return !( 
        ( xs[0] >= start && xs[0] <= end ) ||
        ( start >= xs[0] && start <= xs[1] )
      );
    });
    if(fit === true){
      break;
    }
  }
  return lane;
}
            
          
!
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