Guest User

Untitled

a guest
Aug 15th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Arc Chart</title>
  6. <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
  7. <style type="text/css">
  8. .chart-gauge {
  9. width: 400px;
  10. height: 230px;
  11. margin: 100px auto;
  12. }
  13.  
  14. .chart-filled {
  15. fill: steelblue;
  16. }
  17.  
  18. .chart-empty {
  19. fill: #dedede;
  20. }
  21.  
  22. .needle,
  23. .needle-center {
  24. display: none;
  25. }
  26.  
  27. svg {
  28. font: 15px sans-serif;
  29. }
  30.  
  31. .title {
  32. transform: translate( -40px, 10px);
  33. }
  34.  
  35. .chart-gauge .percent {
  36. display: block;
  37. transform: translate( -45px, -30px);
  38. font-size: 50px;
  39. }
  40.  
  41. .chart-gauge .sub-title {
  42. transform: translate(-190px, -165px);
  43. }
  44.  
  45. .chart-gauge .min-perc {
  46. transform: translate(-150px, 20px);
  47. }
  48.  
  49. .chart-gauge .max-perc {
  50. transform: translate(130px, 20px);
  51. }
  52. </style>
  53.  
  54. </head>
  55.  
  56. <body>
  57.  
  58. <div class="chart-gauge"></div>
  59. <script type="text/javascript">
  60. var dataSet = [{
  61. "title": "Dollar Comps",
  62. "subtitle": "Bay 1",
  63. "percent": .45
  64. }, {
  65. "title": "Unit Comps",
  66. "subtitle": "Bay 1",
  67. "percent": .65
  68. }, {
  69. "title": "Dollar Comps",
  70. "subtitle": "Bay 2",
  71. "percent": .35
  72. }, {
  73. "title": "Unit Comps",
  74. "subtitle": "Bay 2",
  75. "percent": .75
  76. } ];
  77.  
  78. function LoadChart(data) {
  79.  
  80. for (i = 0; i < data.length; i++) {
  81. var needle;
  82.  
  83. var barWidth, chart, chartInset, degToRad, repaintGauge,
  84. height, margin, numSections, padRad, percToDeg, percToRad,
  85. radius, sectionIndx, svg, totalPercent, width;
  86.  
  87. numSections = 1;
  88. sectionPerc = 1 / numSections / 2;
  89. padRad = 0.025;
  90. chartInset = 10;
  91.  
  92. // Orientation of gauge:
  93. totalPercent = .75;
  94.  
  95. el = d3.select('.chart-gauge');
  96.  
  97. margin = {
  98. top: 20,
  99. right: 20,
  100. bottom: 30,
  101. left: 20
  102. };
  103.  
  104. width = el[0][0].offsetWidth - margin.left - margin.right;
  105. height = width;
  106. radius = Math.min(width, height) / 2;
  107. barWidth = 40 * width / 300;
  108.  
  109.  
  110. /*
  111. Utility methods
  112. */
  113. percToDeg = function(perc) {
  114. return perc * 360;
  115. };
  116.  
  117. percToRad = function(perc) {
  118. return degToRad(percToDeg(perc));
  119. };
  120.  
  121. degToRad = function(deg) {
  122. return deg * Math.PI / 180;
  123. };
  124.  
  125. // Create SVG element
  126. svg = el.append('svg').attr('width', width + margin.left + margin.right).attr('height', height + margin.top + margin.bottom);
  127.  
  128. // Add layer for the panel
  129. chart = svg.append('g').attr('transform', "translate(" + ((width + margin.left) / 2) + ", " + ((height + margin.top) / 2) + ")");
  130. chart.append('path').attr('class', "arc chart-filled");
  131. chart.append('path').attr('class', "arc chart-empty");
  132. chart.append("text")
  133. .attr("class", "title")
  134. .text(data[i].title);
  135. chart.append("text")
  136. .attr("class", "sub-title")
  137. .text(data[i].subtitle);
  138. chart.append("text")
  139. .attr("class", "min-perc")
  140. .text( "0%" );
  141. chart.append("text")
  142. .attr("class", "max-perc")
  143. .text("100%");
  144.  
  145.  
  146. arc2 = d3.svg.arc().outerRadius(radius - chartInset).innerRadius(radius - chartInset - barWidth)
  147. arc1 = d3.svg.arc().outerRadius(radius - chartInset).innerRadius(radius - chartInset - barWidth)
  148.  
  149. repaintGauge = function(perc) {
  150. var next_start = totalPercent;
  151. arcStartRad = percToRad(next_start);
  152. arcEndRad = arcStartRad + percToRad(perc / 2);
  153. next_start += perc / 2;
  154.  
  155.  
  156. arc1.startAngle(arcStartRad).endAngle(arcEndRad);
  157.  
  158. arcStartRad = percToRad(next_start);
  159. arcEndRad = arcStartRad + percToRad((1 - perc) / 2);
  160. arc2.startAngle(arcStartRad + padRad).endAngle(arcEndRad);
  161.  
  162.  
  163. chart.select(".chart-filled").attr('d', arc1);
  164. chart.select(".chart-empty").attr('d', arc2);
  165. }
  166.  
  167. var Needle = (function() {
  168.  
  169. /**
  170. * Helper function that returns the `d` value
  171. * for moving the needle
  172. **/
  173. var recalcPointerPos = function(perc) {
  174. var centerX, centerY, leftX, leftY, rightX, rightY, thetaRad, topX, topY;
  175. thetaRad = percToRad(perc / 2);
  176. centerX = 0;
  177. centerY = 0;
  178. topX = centerX - this.len * Math.cos(thetaRad);
  179. topY = centerY - this.len * Math.sin(thetaRad);
  180. leftX = centerX - this.radius * Math.cos(thetaRad - Math.PI / 2);
  181. leftY = centerY - this.radius * Math.sin(thetaRad - Math.PI / 2);
  182. rightX = centerX - this.radius * Math.cos(thetaRad + Math.PI / 2);
  183. rightY = centerY - this.radius * Math.sin(thetaRad + Math.PI / 2);
  184. return "M " + leftX + " " + leftY + " L " + topX + " " + topY + " L " + rightX + " " + rightY;
  185. };
  186.  
  187. function Needle(el) {
  188. this.el = el;
  189. this.len = width / 3;
  190. this.radius = this.len / 6;
  191. }
  192.  
  193. Needle.prototype.render = function() {
  194. this.el.append('circle').attr('class', 'needle-center').attr('cx', 0).attr('cy', 0).attr('r', this.radius);
  195. return this.el.append('path').attr('class', 'needle').attr('d', recalcPointerPos.call(this, 0));
  196. };
  197.  
  198. Needle.prototype.moveTo = function(perc) {
  199. var self,
  200. oldValue = this.perc || 0;
  201.  
  202. this.perc = perc;
  203. self = this;
  204.  
  205. // Reset pointer position
  206. this.el.transition().delay(100).ease('quad').duration(200).select('.needle').tween('reset-progress', function() {
  207. return function(percentOfPercent) {
  208. var progress = (1 - percentOfPercent) * oldValue;
  209.  
  210. repaintGauge(progress);
  211. return d3.select(this).attr('d', recalcPointerPos.call(self, progress));
  212. };
  213. });
  214.  
  215. this.el.transition().delay(200).ease("elastic").duration(500).select('.needle').tween('progress', function() {
  216. return function(percentOfPercent) {
  217. var progress = percentOfPercent * perc;
  218.  
  219. repaintGauge(progress);
  220. return d3.select(this).attr('d', recalcPointerPos.call(self, progress));
  221. };
  222. });
  223. };
  224. chart.append("text")
  225. .attr("class", "percent")
  226. .text(data[i].percent * 100 + "%");
  227.  
  228. return Needle;
  229.  
  230. })();
  231. needle = new Needle(chart);
  232. needle.render();
  233. needle.moveTo(data[i].percent);
  234. }
  235. }
  236. //console.log(dataSet);
  237. LoadChart(dataSet);
  238. </script>
  239. </body>
  240. </html>
Advertisement
Add Comment
Please, Sign In to add comment