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

              
                table.radar-chart
  thead
    tr
      th Time on page
      th Bounce Rate
      th Page Views
      th New Visitors
      // th Another item
      // th Go crazy
  tbody
    tr
      td 60
      td 80
      td 15
      td 15
      // td 25
      // td 74
    tr
      td 70
      td 50
      td 50
      td 75
      // td 54
      // td 95
              
            
!

CSS

              
                canvas {
  display: block;
  margin: 0 auto;
}
              
            
!

JS

              
                (function($){
	$.fn.radar_chart = function(options){
		var settings = $.extend({
			colors: [
				'#dcdedc',
				'#ed6d63'
			],
			lineWidth: 2,
			lineColor: '#ececec',
			textColor: '#353535'
		}, options);

		$(this).each(function(){
			var table = this;

			if(table.tagName.toUpperCase() == 'TABLE'){
				table.radar = {
					attributes: [],
					records: []
				}

				// add all the attributes
				$(table).find('thead th, thead td').each(function(){
					table.radar.attributes.push(this.innerHTML);
				});

				// add all the values
				$(table).find('tbody tr').each(function(){
					var record = {};
					var $tds = $(this).find('td');

					var i = 0;

					$tds.each(function(){
						record[i] = parseFloat(this.innerHTML);
						i++;
					});

					table.radar.records.push(record);
				});

				// set up the canvas
				table.radar.canvas = document.createElement('canvas');
				table.radar.canvas.width = 800;
				table.radar.canvas.height = 600;

				table.radar.ctx = table.radar.canvas.getContext('2d');

				radar_redraw(table);

				$(table.radar.canvas).insertAfter(table);
				$(table).hide();
			}
		});

		function radar_redraw(table){
			if(table.radar.ctx){
				table.radar.ctx.clearRect(0, 0, table.radar.canvas.width, table.radar.canvas.height);

				var geometry = {
					centre: {
						x: table.radar.canvas.width / 2,
						y: table.radar.canvas.height / 2
					},
					radius: Math.min(table.radar.canvas.width / 3, table.radar.canvas.height / 3),
					text_radius: Math.min(table.radar.canvas.width / 2.8, table.radar.canvas.height / 2.8)
				};

				// grid

				table.radar.ctx.beginPath();

				for(var i = 0; i < 360; i+=(360 / table.radar.attributes.length)){
					var angle = 90 - i;

					table.radar.ctx.moveTo(geometry.centre.x, geometry.centre.y);
					table.radar.ctx.lineWidth = settings.lineWidth;
					table.radar.ctx.strokeStyle = settings.lineColor;
					table.radar.ctx.lineTo(geometry.centre.x + geometry.radius * Math.cos(angle * Math.PI / 180), geometry.centre.y - geometry.radius * Math.sin(angle * Math.PI / 180));
				}

				var segments = 3;

				for(var a = 1; a <= segments; a++){
					table.radar.ctx.moveTo(geometry.centre.x + (geometry.radius / segments * a) * Math.cos(90 * Math.PI / 180), geometry.centre.y - (geometry.radius / segments * a) * Math.sin(90 * Math.PI / 180));

					for(var i = 0; i <= 360; i+=(360 / table.radar.attributes.length)){
						var angle = 90 - i;
						table.radar.ctx.lineTo(geometry.centre.x + (geometry.radius / segments * a) * Math.cos(angle * Math.PI / 180), geometry.centre.y - (geometry.radius / segments * a) * Math.sin(angle * Math.PI / 180));
					}
				}

				table.radar.ctx.stroke();

				// data

				table.radar.ctx.globalCompositeOperation = 'multiply';

				for(var r in table.radar.records){
					table.radar.ctx.beginPath();

					for(var i in table.radar.records[r]){
						var angle = 90 - (360 / table.radar.attributes.length * i);

						if(i == 0){
							table.radar.ctx.moveTo(geometry.centre.x + (geometry.radius * table.radar.records[r][i] / 100) * Math.cos(angle * Math.PI / 180), geometry.centre.y - (geometry.radius * table.radar.records[r][i] / 100) * Math.sin(angle * Math.PI / 180));
						}else{
							table.radar.ctx.lineTo(geometry.centre.x + (geometry.radius * table.radar.records[r][i] / 100) * Math.cos(angle * Math.PI / 180), geometry.centre.y - (geometry.radius * table.radar.records[r][i] / 100) * Math.sin(angle * Math.PI / 180));
						}
					}

					var t = r;
					while(t >= settings.colors.length){
						t -= settings.colors.length;
					}

					table.radar.ctx.fillStyle = settings.colors[t];
					table.radar.ctx.fill();
				}

				table.radar.ctx.globalCompositeOperation = 'source-over';

				// text

				table.radar.ctx.font = 'normal normal 600 14px Lato';
				table.radar.ctx.textAlign = 'center';
				table.radar.ctx.textBaseline = 'middle';
				table.radar.ctx.fillStyle = settings.textColor;
				
				for(var i in table.radar.attributes){
					var angle = 90 - (360 / table.radar.attributes.length * i);
					if(angle < 0){ angle+=360; }

					var dx = geometry.text_radius * Math.cos(angle * Math.PI / 180);
					var dy = geometry.text_radius * Math.sin(angle * Math.PI / 180);

					if(angle <= 80){
						table.radar.ctx.textAlign = 'left';
					}else if(angle <= 100){
						table.radar.ctx.textAlign = 'center';
					}else if(angle <= 260){
						table.radar.ctx.textAlign = 'right';
					}else if(angle <= 280){
						table.radar.ctx.textAlign = 'center';
					}else {
						table.radar.ctx.textAlign = 'left';
					}

					table.radar.ctx.fillText(table.radar.attributes[i].toUpperCase().split('').join(String.fromCharCode(160)), geometry.centre.x + dx, geometry.centre.y - dy);
				}
			}
		}
	};
  
  $(document).ready(function(){
    $('.radar-chart').radar_chart();
  });
})(window.jQuery);
              
            
!
999px

Console