/* Delicious Data Pie Chart jQuery Plugin v2.3 — 30 June 2013 by Andy Griffin http://www.agriffindesign.com/downloads/delicious_data Free for private and commercial use with this attribution intact. */
(function($){
$.fn.ddPieChart = function(par) {
var p = $.extend({
label: 'none', // value || percent || none
label_color: '#000000',
label_hover: false,
center_x: this.width()/2,
center_y: this.height()/2,
parent: {w: this.width(), h: this.height()},
radius: this.height()/2,
thickness: 10,
type: 'pie',
wedges: []
}, par);
p.total = getTotal(p.wedges);
p.wedges = processNums(p.wedges, p.total);
var dynamicLabel;
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width', this.width());
svg.setAttribute('height', this.height());
for(var x=0; x
')
this.append(dynamicLabel);
}
function calcOffset(angle){ //changes offset of text around bottom half of circle to account for text height
angle = angle%360;
return (angle>90 && angle<270) ? Math.abs(180-(Math.abs(180-angle)))/15+10 : 10;
}
function circlePoint(center_x, center_y, radius, angle){ //center point of circle within the element, radius, angle
var x1 = center_x + (radius)*Math.cos(Math.PI*angle/180-(Math.PI/2));
var y1 = center_y + (radius)*Math.sin(Math.PI*angle/180-(Math.PI/2));
return {x: x1, y: y1};
}
function drawWedge(wdg, svg){ //start & end in degrees, color
var longarc = (wdg.end-wdg.start<180) ? 0 : 1;
var instructions = null;
if(p.type=='ring'){
var outside = p.radius, inside = p.radius-p.thickness;
var o1 = circlePoint(p.center_x, p.center_y, outside, wdg.start);
var o2 = circlePoint(p.center_x, p.center_y, outside, wdg.end);
var i1 = circlePoint(p.center_x, p.center_y, inside, wdg.start);
var i2 = circlePoint(p.center_x, p.center_y, inside, wdg.end);
instructions = 'M '+o1.x+' '+o1.y+' A '+outside+' '+outside+' 0 '+longarc+' 1 '+o2.x+' '+o2.y+' L '+i2.x+' '+i2.y+' A '+inside+' '+inside+' 0 '+longarc+' 0 '+i1.x+' '+i1.y+' Z';
}else{
var angle1 = circlePoint(p.center_x, p.center_y, p.radius, wdg.start);
var angle2 = circlePoint(p.center_x, p.center_y, p.radius, wdg.end);
var x1 = angle1.x, y1 = angle1.y;
var x2 = angle2.x, y2 = angle2.y;
instructions = 'M '+p.center_x+' '+p.center_y+' L '+x1+' '+y1+' A '+p.radius+' '+p.radius+' 0 '+longarc+' 1 '+x2+' '+y2+' Z';
}
var wedge = document.createElementNS('http://www.w3.org/2000/svg', 'path');
wedge.setAttributeNS(null, "d", instructions);
wedge.setAttributeNS(null, "fill", wdg.color);
wedge.setAttributeNS(null, "class", "dd_wedge");
wedge.setAttributeNS(null, "data-idx", wdg.index);
if(p.label!='none' && !p.label_hover){ staticLabel(wdg, svg); }
svg.appendChild(wedge);
}
function formatPercent(decimal){
var num = (decimal*100);
num = (num%1 != 0) ? num.toFixed(2): num;
return num+'%';
}
function getLabel(wdg){ //gets the text for the label
var spacer = (typeof(wdg.name)=='undefined') ? '' : (p.label_hover) ? ' • ' : ' • ';
var lbl = (typeof(wdg.name)!='undefined') ? wdg.name : '';
lbl += (p.label=='name_only') ? '' : (p.label=='percent') ? spacer+formatPercent(wdg.percent) : spacer+wdg[p.label];
return lbl;
}
function getTotal(wdg){ //calculates sum of wedge values
var t = 0;
for(var x=0; x=270){ //top hemisphere
dynamicLabel.css({"bottom": p.parent.h-wdg.label_point.y, "top": ""});
}else{ //bottom hemisphere
dynamicLabel.css({"top": wdg.label_point.y, "bottom": ""});
}
if(wdg.mid_angle<=180){ //right hemisphere
dynamicLabel.css({"left": wdg.label_point.x, "right": ""}).addClass('right_hemis').removeClass('left_hemis');
}else{ //left hemisphere
dynamicLabel.css({"right": p.parent.w-wdg.label_point.x, "left": ""}).addClass('left_hemis').removeClass('right_hemis');
}
dynamicLabel.addClass('dd_label_visible')
dynamicLabel.css("opacity", 1);
}
function staticLabel(wdg, svg){
var label = document.createElementNS('http://www.w3.org/2000/svg', 'text');
label.setAttributeNS(null, "fill", p.label_color);
label.setAttributeNS(null, "x", Math.floor(wdg.label_point.x));
label.setAttributeNS(null, "y", Math.floor(wdg.label_point.y));
label.setAttributeNS(null, "style", 'font-family: Helvetica, sans-serif; font-size: 13px');
if(wdg.mid_angle%360>180){ label.setAttributeNS(null, "text-anchor", 'end'); }
var theTxt = getLabel(wdg);
var innerTxt = document.createTextNode(theTxt);
label.appendChild(innerTxt);
svg.appendChild(label);
}
function timeToDegrees(time){ //hh:mm
var timepc = time.split(':');
return ((timepc[0]*60)+parseInt(timepc[1]))/2;
}
/*
this.find('.dd_wedge').click(function(){
if(dynamicLabel.hasClass('dd_label_visible')){
hideLabel();
}else{
showLabel(p.wedges[$(this).attr('data-idx')]);
}
});
*/
this.find('.dd_wedge').mouseover(function(){
showLabel(p.wedges[$(this).attr('data-idx')]);
}).mouseout(function(){
hideLabel();
});
return this;
};
})(jQuery);