├── .gitattributes ├── .gitignore ├── README.md ├── demo ├── index.html └── style.css └── jquery.drawDoughnutChart.js /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jquery.drawDoughnutChart.js 2 | ================= 3 | 4 | A SVG doughnut chart with animation and tooltip. 5 | Inspired by Chart.js(http://www.chartjs.org/). 6 | 7 | [Demo on my codepen](http://codepen.io/githiro/details/ICfFE) 8 | 9 | Licensed under the MIT License. 10 | -------------------------------------------------------------------------------- /demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |' + settings.summaryTitle + '
').appendTo($summary); 110 | $summaryTitle.css('font-size', getScaleFontSize( $summaryTitle, settings.summaryTitle )); // In most of case useless 111 | var $summaryNumber = $('').appendTo($summary).css({opacity: 0}); 112 | 113 | for (var i = 0, len = data.length; i < len; i++) { 114 | segmentTotal += data[i].value; 115 | $paths[i] = $(document.createElementNS('http://www.w3.org/2000/svg', 'path')) 116 | .attr({ 117 | "stroke-width": settings.segmentStrokeWidth, 118 | "stroke": settings.segmentStrokeColor, 119 | "fill": data[i].color, 120 | "data-order": i 121 | }) 122 | .appendTo($pathGroup) 123 | .on("mouseenter", pathMouseEnter) 124 | .on("mouseleave", pathMouseLeave) 125 | .on("mousemove", pathMouseMove) 126 | .on("click", pathClick); 127 | } 128 | 129 | //Animation start 130 | animationLoop(drawPieSegments); 131 | 132 | //Functions 133 | function getHollowCirclePath(doughnutRadius, cutoutRadius) { 134 | //Calculate values for the path. 135 | //We needn't calculate startRadius, segmentAngle and endRadius, because base doughnut doesn't animate. 136 | var startRadius = -1.570,// -Math.PI/2 137 | segmentAngle = 6.2831,// 1 * ((99.9999/100) * (PI*2)), 138 | endRadius = 4.7131,// startRadius + segmentAngle 139 | startX = centerX + cos(startRadius) * doughnutRadius, 140 | startY = centerY + sin(startRadius) * doughnutRadius, 141 | endX2 = centerX + cos(startRadius) * cutoutRadius, 142 | endY2 = centerY + sin(startRadius) * cutoutRadius, 143 | endX = centerX + cos(endRadius) * doughnutRadius, 144 | endY = centerY + sin(endRadius) * doughnutRadius, 145 | startX2 = centerX + cos(endRadius) * cutoutRadius, 146 | startY2 = centerY + sin(endRadius) * cutoutRadius; 147 | var cmd = [ 148 | 'M', startX, startY, 149 | 'A', doughnutRadius, doughnutRadius, 0, 1, 1, endX, endY,//Draw outer circle 150 | 'Z',//Close path 151 | 'M', startX2, startY2,//Move pointer 152 | 'A', cutoutRadius, cutoutRadius, 0, 1, 0, endX2, endY2,//Draw inner circle 153 | 'Z' 154 | ]; 155 | cmd = cmd.join(' '); 156 | return cmd; 157 | }; 158 | function pathMouseEnter(e) { 159 | var order = $(this).data().order; 160 | if (settings.showTip) { 161 | $tip.text(data[order].title + ": " + data[order].value) 162 | .fadeIn(200); 163 | } 164 | if(settings.showLabel) { 165 | $summaryTitle.text(data[order].title).css('font-size', getScaleFontSize( $summaryTitle, data[order].title)); 166 | var tmpNumber = settings.shortInt ? shortKInt(data[order].value) : data[order].value; 167 | $summaryNumber.html(tmpNumber).css('font-size', getScaleFontSize( $summaryNumber, tmpNumber)); 168 | } 169 | settings.onPathEnter.apply($(this),[e,data]); 170 | } 171 | function pathMouseLeave(e) { 172 | if (settings.showTip) $tip.hide(); 173 | if(settings.showLabel) { 174 | $summaryTitle.text(settings.summaryTitle).css('font-size', getScaleFontSize( $summaryTitle, settings.summaryTitle)); 175 | var tmpNumber = settings.shortInt ? shortKInt(segmentTotal) : segmentTotal; 176 | $summaryNumber.html(tmpNumber).css('font-size', getScaleFontSize( $summaryNumber, tmpNumber)); 177 | } 178 | settings.onPathLeave.apply($(this),[e,data]); 179 | } 180 | function pathMouseMove(e) { 181 | if (settings.showTip) { 182 | $tip.css({ 183 | top: e.pageY + settings.tipOffsetY, 184 | left: e.pageX - $tip.width() / 2 + settings.tipOffsetX 185 | }); 186 | } 187 | } 188 | function pathClick(e){ 189 | var order = $(this).data().order; 190 | if (typeof data[order].action != "undefined") 191 | data[order].action(); 192 | } 193 | function drawPieSegments (animationDecimal) { 194 | var startRadius = -PI / 2,//-90 degree 195 | rotateAnimation = 1; 196 | if (settings.animation && settings.animateRotate) rotateAnimation = animationDecimal;//count up between0~1 197 | 198 | drawDoughnutText(animationDecimal, segmentTotal); 199 | 200 | $pathGroup.attr("opacity", animationDecimal); 201 | 202 | //If data have only one value, we draw hollow circle(#1). 203 | if (data.length === 1 && (4.7122 < (rotateAnimation * ((data[0].value / segmentTotal) * (PI * 2)) + startRadius))) { 204 | $paths[0].attr("d", getHollowCirclePath(doughnutRadius, cutoutRadius)); 205 | return; 206 | } 207 | for (var i = 0, len = data.length; i < len; i++) { 208 | var segmentAngle = rotateAnimation * ((data[i].value / segmentTotal) * (PI * 2)), 209 | endRadius = startRadius + segmentAngle, 210 | largeArc = ((endRadius - startRadius) % (PI * 2)) > PI ? 1 : 0, 211 | startX = centerX + cos(startRadius) * doughnutRadius, 212 | startY = centerY + sin(startRadius) * doughnutRadius, 213 | endX2 = centerX + cos(startRadius) * cutoutRadius, 214 | endY2 = centerY + sin(startRadius) * cutoutRadius, 215 | endX = centerX + cos(endRadius) * doughnutRadius, 216 | endY = centerY + sin(endRadius) * doughnutRadius, 217 | startX2 = centerX + cos(endRadius) * cutoutRadius, 218 | startY2 = centerY + sin(endRadius) * cutoutRadius; 219 | var cmd = [ 220 | 'M', startX, startY,//Move pointer 221 | 'A', doughnutRadius, doughnutRadius, 0, largeArc, 1, endX, endY,//Draw outer arc path 222 | 'L', startX2, startY2,//Draw line path(this line connects outer and innner arc paths) 223 | 'A', cutoutRadius, cutoutRadius, 0, largeArc, 0, endX2, endY2,//Draw inner arc path 224 | 'Z'//Cloth path 225 | ]; 226 | $paths[i].attr("d", cmd.join(' ')); 227 | startRadius += segmentAngle; 228 | } 229 | } 230 | function drawDoughnutText(animationDecimal, segmentTotal) { 231 | $summaryNumber 232 | .css({opacity: animationDecimal}) 233 | .text((segmentTotal * animationDecimal).toFixed(1)); 234 | var tmpNumber = settings.shortInt ? shortKInt(segmentTotal) : segmentTotal; 235 | $summaryNumber.html(tmpNumber).css('font-size', getScaleFontSize( $summaryNumber, tmpNumber)); 236 | } 237 | function animateFrame(cnt, drawData) { 238 | var easeAdjustedAnimationPercent =(settings.animation)? CapValue(easingFunction(cnt), null, 0) : 1; 239 | drawData(easeAdjustedAnimationPercent); 240 | } 241 | function animationLoop(drawData) { 242 | var animFrameAmount = (settings.animation)? 1 / CapValue(settings.animationSteps, Number.MAX_VALUE, 1) : 1, 243 | cnt =(settings.animation)? 0 : 1; 244 | requestAnimFrame(function() { 245 | cnt += animFrameAmount; 246 | animateFrame(cnt, drawData); 247 | if (cnt <= 1) { 248 | requestAnimFrame(arguments.callee); 249 | } else { 250 | settings.afterDrawed.call($this); 251 | } 252 | }); 253 | } 254 | function Max(arr) { 255 | return Math.max.apply(null, arr); 256 | } 257 | function Min(arr) { 258 | return Math.min.apply(null, arr); 259 | } 260 | function isNumber(n) { 261 | return !isNaN(parseFloat(n)) && isFinite(n); 262 | } 263 | function CapValue(valueToCap, maxValue, minValue) { 264 | if (isNumber(maxValue) && valueToCap > maxValue) return maxValue; 265 | if (isNumber(minValue) && valueToCap < minValue) return minValue; 266 | return valueToCap; 267 | } 268 | function shortKInt (int) { 269 | int = int.toString(); 270 | var strlen = int.length; 271 | if(strlen<5) 272 | return int; 273 | if(strlen<8) 274 | return '' + int.substring(0, strlen-3) + 'K'; 275 | return '' + int.substring( 0, strlen-6) + 'M'; 276 | } 277 | function getScaleFontSize(block, newText) { 278 | block.css('font-size', ''); 279 | newText = newText.toString().replace(/(<([^>]+)>)/ig,""); 280 | var newFontSize = block.width() / newText.length * settings.ratioFont; 281 | // Not very good : http://stephensite.net/WordPressSS/2008/02/19/how-to-calculate-the-character-width-accross-fonts-and-points/ 282 | // But best quick way the 1.5 number is to affinate in function of the police 283 | var maxCharForDefaultFont = block.width() - newText.length * block.css('font-size').replace(/px/, '') / settings.ratioFont; 284 | if(maxCharForDefaultFont<0) 285 | return newFontSize+'px'; 286 | else 287 | return ''; 288 | } 289 | /** 290 | function getScaleFontSize(block, newText) { 291 | block.css('font-size', ''); 292 | newText = newText.toString().replace(/(<([^>]+)>)/ig,""); 293 | var newFontSize = block.width() / newText.length; 294 | if(newFontSize