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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <div class="container">
  <h2>Visualization of timeseries data using rickshaw js</h2>
  <span >by: <a href="https://twitter.com/superpikar" target="_blank">@superpikar</a> | JSON data from <a href="https://codepen.io/superpikar/pen/pvWrwQ.js" target="_blank">https://codepen.io/superpikar/pen/pvWrwQ.js</a></span>
  <div class="well">
    <div id="rickshaw-axis-y" class="rickshaw-axis-y pull-left"></div>
    <div id="chart" class="rickshaw-chart"></div>
    <div id="rickshaw-axis-x" class="rickshaw-axis-x"></div>
    <div id="preview" class="rickshaw-preview"></div>
    
    <div id="legend_container">
		<div id="smoother" title="Smoothing"></div>
		<div id="legend" class="rickshaw-legend"></div>
	</div>
	
  </div>
</div>
              
            
!

CSS

              
                
.container, .well, #chart, #legend, #preview{
  margin-top:20px;
}

.rickshaw-axis-y {
    height: 250px;
    width: 50px;
}

.rickshaw-axis-y svg{
  top: -5px !important;
}

.rickshaw-axis-x {
    margin-left:50px;
}

.rickshaw-chart, .rickshaw-preview, .rickshaw-legend{
  margin-left: 50px;
}

.rickshaw_graph .detail .x_label { 
  display: none 
}
.rickshaw_graph .detail .item { 
  line-height: 1.4; 
  padding: 0.5em 
}
.detail_swatch { 
  
  display: inline-block; 
  width: 10px; 
  height: 10px; 
  margin: 0 4px 0 0 
}
.rickshaw_graph .detail .date { 
  color: #a0a0a0 
}
              
            
!

JS

              
                Rickshaw.namespace('Bab.Rickshaw.Graph.Axis.BabTime');
Rickshaw.Graph.Axis.BabTime = function(args) {  
  var self = this;
  this.graph = args.graph;
  this.elements = [];
  this.ticksTreatment = args.ticksTreatment || 'plain';
  this.fixedTimeUnit = args.timeUnit;
  var time = args.timeFixture || new Rickshaw.Fixtures.Time();
  
  this.appropriateTimeUnit = function() {
		var unit;
		var units = time.units;
		var domain = this.graph.x.domain();
		units.forEach( function(u) {
			if (Math.floor(domain[1] / (u.seconds * 1000) ) >= 2) {
				unit = unit || u;
          console.log(unit);
			}
		} );
		return (unit || time.units[time.units.length - 1]);
	};
  
	this.tickOffsets = function() {
    /* set unit use new format */
    var unit = this.fixedTimeUnit || this.appropriateTimeUnit();
    unit.formatter = printTimeDuration; console.log(unit);
    
    var domain = this.graph.x.domain();
    var count = Math.floor(domain[1] / (unit.seconds * 1000));   
    var offsets = []; 
    var runningTick = 0; 
     /*var newUnit =  {
            name: 'duration',
            seconds: 3600 * 1000, 
            formatter: printTimeDuration
          };*/
     for (var i = 0; i < count; i++) {
			runningTick = runningTick + (unit.seconds * 1000) ;
			offsets.push( { value: runningTick, unit: unit } );
		}
    
     console.log(offsets);
		return offsets;
	};

	this.render = function() {
		this.elements.forEach( function(e) {
			e.parentNode.removeChild(e);
		} );
		this.elements = [];
		var offsets = this.tickOffsets();
		offsets.forEach( function(o) {			
			if (self.graph.x(o.value) > self.graph.x.range()[1]) return;	
			var element = document.createElement('div');
			element.style.left = self.graph.x(o.value) + 'px';
			element.classList.add('x_tick');
			element.classList.add(self.ticksTreatment);
			var title = document.createElement('div');
			title.classList.add('title');
			title.innerHTML = o.unit.formatter(o.value);
			element.appendChild(title);
			self.graph.element.appendChild(element);
			self.elements.push(element);
		} );
	};
	this.graph.onUpdate( function() { self.render() } );
}



function setLocalJSON (uri) {
  var myJson = null;
  $.ajax({
    'async' : false,
    'global' : false,
    'url' : uri,
    'dataType' : "json",
    'success' : function(data) {
      myJson = data;
    }
  });
  return myJson;
}

var formatMapping = function (n) {
	var map = {
      3600000: '1h',
      7200000: '2h', 
      9540000: 'end'
	};
   console.log(n);
	return map[n];
};

function printPretty(theString, isPretty){
  switch(theString){
    case 'years':
      return isPretty?'y':' years';
    case 'months':
      return isPretty?'m':' months';
    case 'days':
      return isPretty?'d':' days';
    case 'hours':
      return isPretty?'h':' hours';
    case 'minutes':
      return isPretty?"'":' minutes';
    case 'seconds':
      return isPretty?'"':' seconds';
  }
}

function printTimeDurationPretty(seconds) {
  var isPretty = true;
  var objDate = moment.duration(seconds)._data;
  // console.log(objDate);
  var result = '';
  if (objDate.years != 0) {
    result += ' ' + objDate.years + printPretty('years', true);
    if (objDate.months != 0) {
      result += ' ' + objDate.months + printPretty('months', true);
    } else if (objDate.days != 0) {
      result += ' ' + objDate.days + printPretty('days', true);
    } else if (objDate.hours != 0) {
      result += ' ' + objDate.hours + printPretty('hours', true);
    } else if (objDate.minutes != 0) {
      result += ' ' + objDate.minutes + printPretty('minutes', true);
    } else if (objDate.seconds != 0) {
      result += ' ' + objDate.seconds + printPretty('seconds', true);
    }
  } else if (objDate.months != 0) {
    result += ' ' + objDate.months + printPretty('months', true);
    if (objDate.days != 0) {
      result += ' ' + objDate.days + printPretty('days', true);
    } else if (objDate.hours != 0) {
      result += ' ' + objDate.hours + printPretty('hours', true);
    } else if (objDate.minutes != 0) {
      result += ' ' + objDate.minutes + printPretty('minutes', true);
    } else if (objDate.seconds != 0) {
      result += ' ' + objDate.seconds + printPretty('seconds', true);
    }
  } else if (objDate.days != 0) {
    result += ' ' + objDate.days + printPretty('days', true);;
    if (objDate.hours != 0) {
      result += ' ' + objDate.hours + printPretty('hours', true);
    } else if (objDate.minutes != 0) {
      result += ' ' + objDate.minutes + printPretty('minutes', true);
    } else if (objDate.seconds != 0) {
      result += ' ' + objDate.seconds + printPretty('seconds', true);
    }
  } else if (objDate.hours != 0) {
    result += ' ' + objDate.hours + printPretty('hours', true);
    if (objDate.minutes != 0) {
      result += ' ' + objDate.minutes + printPretty('minutes', true);
    } else if (objDate.seconds != 0) {
      result += ' ' + objDate.seconds + printPretty('seconds', true);
    }
  } else if (objDate.minutes != 0) {
    result += ' ' + objDate.minutes + printPretty('minutes', true);
    if (objDate.seconds != 0) {
      result += ' ' + objDate.seconds + printPretty('seconds', true);
    }
  } else if (objDate.seconds != 0) {
    result += ' ' + objDate.seconds + printPretty('seconds', true);
  } else if (objDate.seconds == 0) {
    result = '0 secs';
  }

  return result;
}

function printTimeDuration(seconds) {
  var objDate = moment.duration(seconds)._data;
  // console.log(objDate);
  var result = '';
  if (objDate.years != 0) {
    result += ' ' + objDate.years + ' years';
    if (objDate.months != 0) {
      result += ' ' + objDate.months + ' months';
    } else if (objDate.days != 0) {
      result += ' ' + objDate.days + ' days';
    } else if (objDate.hours != 0) {
      result += ' ' + objDate.hours + ' hours';
    } else if (objDate.minutes != 0) {
      result += ' ' + objDate.minutes + ' mins';
    } else if (objDate.seconds != 0) {
      result += ' ' + objDate.seconds + ' secs';
    }
  } else if (objDate.months != 0) {
    result += ' ' + objDate.months + ' months';
    if (objDate.days != 0) {
      result += ' ' + objDate.days + ' days';
    } else if (objDate.hours != 0) {
      result += ' ' + objDate.hours + ' hours';
    } else if (objDate.minutes != 0) {
      result += ' ' + objDate.minutes + ' mins';
    } else if (objDate.seconds != 0) {
      result += ' ' + objDate.seconds + ' secs';
    }
  } else if (objDate.days != 0) {
    result += ' ' + objDate.days + ' days';
    if (objDate.hours != 0) {
      result += ' ' + objDate.hours + ' hours';
    } else if (objDate.minutes != 0) {
      result += ' ' + objDate.minutes + ' mins';
    } else if (objDate.seconds != 0) {
      result += ' ' + objDate.seconds + ' secs';
    }
  } else if (objDate.hours != 0) {
    result += ' ' + objDate.hours + ' hours';
    if (objDate.minutes != 0) {
      result += ' ' + objDate.minutes + ' mins';
    } else if (objDate.seconds != 0) {
      result += ' ' + objDate.seconds + ' secs';
    }
  } else if (objDate.minutes != 0) {
    result += ' ' + objDate.minutes + ' mins';
    if (objDate.seconds != 0) {
      result += ' ' + objDate.seconds + ' secs';
    }
  } else if (objDate.seconds != 0) {
    result += ' ' + objDate.seconds + ' secs';
  } else if (objDate.seconds == 0) {
    result = '0 secs';
  }

  return result;
}

var data = [];

var jsonData = setLocalJSON('https://codepen.io/superpikar/pen/pvWrwQ.js');
  
var objList = jsonData.caseDurations ;
//console.log(objList);

var values = _.values(jsonData.caseDurations);

var minY = math.min(values),
    maxY = math.max(values);

for(var row in objList){
  data.push({x: Number(row), y: objList[row]});
}

var graph = new Rickshaw.Graph( {
        element: document.querySelector("#chart"),
        width: 580,
        height: 250,
        renderer: 'line',
        interpolation: 'linear',
        series: [ {
                name: 'number of case',
                color: 'steelblue',
                data: data
        } ]
} );

var legend = new Rickshaw.Graph.Legend( {
	graph: graph,
	element: document.querySelector('#legend')

} );

console.log(graph);

var xAxis = new Rickshaw.Graph.Axis.BabTime({
  graph: graph
});

new Rickshaw.Graph.Axis.Y({
  element: document.querySelector('#rickshaw-axis-y'),
  graph: graph,
  orientation: 'left',
});

var hoverDetail = new Rickshaw.Graph.HoverDetail( {
	graph: graph,
	formatter: function(series, x, y) {
		var date = '<span class="date">' + printTimeDuration(x) + '</span>';
		var swatch = '<span class="detail_swatch" style="background-color: ' + series.color + '"></span>';
		var content = swatch + series.name + ": " + parseInt(y) + '<br>' + date;
		return content;
	}
} );

graph.render();

var preview = new Rickshaw.Graph.RangeSlider( {
	graph: graph,
	element: document.querySelector('#preview'),
} );

var ticksTreatment = 'glow';
var previewXAxis = new Rickshaw.Graph.Axis.Time({
	graph: preview.previews[0],
	timeFixture: new Rickshaw.Fixtures.Time.Local(),
	ticksTreatment: ticksTreatment
});

previewXAxis.render();

              
            
!
999px

Console