Guest User

Untitled

a guest
Jan 11th, 2017
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. function ParseData(data) {
  4.  
  5.   var parsed = {};
  6.  
  7.   var months = ['January - 2016', 'February - 2016', 'March - 2016', 'April - 2016', 'May - 2016', 'June - 2016', 'July - 2016', 'August - 2016', 'September - 2016', 'October - 2016', 'November - 2016', 'December - 2016'];
  8.  
  9.   data.forEach(function(item) {
  10.     var week = parseInt(item.WeekNumber.slice(4)),
  11.       month = Math.floor((week - 1) / 4);
  12.  
  13.     if (!parsed[month]) parsed[month] = {
  14.       weeks: [],
  15.       points: 0,
  16.       month: months[month - 1]
  17.     }
  18.  
  19.     var weekLabel = "Week " + week + ": " + item.pointsRewarded;
  20.  
  21.     parsed[month].weeks.push(weekLabel);
  22.     parsed[month].points += item.pointsRewarded;
  23.  
  24.   });
  25.  
  26.   return FormatData(parsed);
  27. }
  28.  
  29. function FormatData(data) {
  30.  
  31.   var formatted = {
  32.     points: [],
  33.     weeks: [],
  34.     months: []
  35.   };
  36.  
  37.   for (var month in data) {
  38.  
  39.     var now = data[month];
  40.  
  41.     formatted.points.push(now.points);
  42.     formatted.months.push(now.month);
  43.     formatted.weeks.push(now.weeks.join("<br>"));
  44.   }
  45.  
  46.   return formatted;
  47. }
  48.  
  49. function InitChart(formattedData) {
  50.   let colors = ['#67001f', '#b2182b', '#d6604d', '#f4a582', '#fddbc7', '#e0e0e0', '#bababa', '#878787', '#4d4d4d', '#1a1a1a', 'white', 'white'];
  51.  
  52.  
  53.   var width = document.querySelector('.chart-wrapper').offsetWidth,
  54.     height = document.querySelector('.chart-wrapper').offsetHeight,
  55.     minOfWH = Math.min(width, height) / 2,
  56.     initialAnimDelay = 300,
  57.     arcAnimDelay = 150,
  58.     arcAnimDur = 3000,
  59.     secDur = 1000,
  60.     secIndividualdelay = 150;
  61.  
  62.   var radius = undefined;
  63.  
  64.   // calculate minimum of width and height to set chart radius
  65.   if (minOfWH > 200) {
  66.     radius = 200;
  67.   } else {
  68.     radius = minOfWH;
  69.   }
  70.  
  71.   // append svg
  72.   var svg = d3.select('.chart-wrapper').append('svg').attr({
  73.     'width': width,
  74.     'height': height,
  75.     'class': 'pieChart'
  76.   }).append('g');
  77.  
  78.   svg.attr({
  79.     'transform': 'translate(' + width / 2 + ', ' + height / 2 + ')'
  80.   });
  81.  
  82.   // for drawing slices
  83.   var arc = d3.svg.arc().outerRadius(radius * 0.6).innerRadius(radius * 0.45);
  84.  
  85.   // for labels and polylines
  86.   var outerArc = d3.svg.arc().innerRadius(radius * 0.85).outerRadius(radius * 0.85);
  87.  
  88.   // d3 color generator
  89.   // let c10 = d3.scale.category10();
  90.  
  91.   var tooltip = d3.select("body").append("div").attr("class", "tooltip").style("opacity", 0);
  92.  
  93.   var pie = d3.layout.pie().value(function(d) {
  94.     return d;
  95.   }).sort(null);
  96.  
  97.   var draw = function draw() {
  98.  
  99.     svg.append("g").attr("class", "lines");
  100.     svg.append("g").attr("class", "slices");
  101.     svg.append("g").attr("class", "labels");
  102.  
  103.     // define slice
  104.     var slice = svg.select('.slices').datum(formattedData.points).selectAll('path').data(pie);
  105.     slice.enter().append('path').attr({
  106.       'fill': function fill(d, i) {
  107.         return colors[i];
  108.       },
  109.       'd': arc,
  110.       'stroke-width': '25px'
  111.     }).attr('transform', function(d, i) {
  112.       return 'rotate(-180, 0, 0)';
  113.     }).style('opacity', 0).transition().delay(function(d, i) {
  114.       return i * arcAnimDelay + initialAnimDelay;
  115.     }).duration(arcAnimDur).ease('elastic').style('opacity', 1).attr('transform', 'rotate(0,0,0)');
  116.  
  117.     slice.transition().delay(function(d, i) {
  118.       return arcAnimDur + i * secIndividualdelay;
  119.     }).duration(secDur).attr('stroke-width', '5px');
  120.  
  121.     var midAngle = function midAngle(d) {
  122.       return d.startAngle + (d.endAngle - d.startAngle) / 2;
  123.     };
  124.  
  125.     var text = svg.select(".labels").selectAll("text").data(pie(formattedData.points));
  126.  
  127.     text.enter().append('text').attr('dy', '0.35em').style("opacity", 0).attr("cursor", "default").style('fill', function(d, i) {
  128.       return colors[i];
  129.     }).text(function(d, i) {
  130.       return formattedData.months[i];
  131.     }).attr('transform', function(d) {
  132.       // calculate outerArc centroid for 'this' slice
  133.       var pos = outerArc.centroid(d);
  134.       // define left and right alignment of text labels
  135.       pos[0] = radius * (midAngle(d) < Math.PI ? 1 : -1);
  136.       return 'translate(' + pos + ')';
  137.     }).style('text-anchor', function(d) {
  138.       return midAngle(d) < Math.PI ? "start" : "end";
  139.     }).transition().delay(function(d, i) {
  140.       return arcAnimDur + i * secIndividualdelay;
  141.     }).duration(secDur).style('opacity', 1);
  142.  
  143.     text.on("mousemove", function(d, i) {
  144.       tooltip.html(formattedData.weeks[i])
  145.         .style('top', d3.event.pageY - 6 + 'px')
  146.         .style('left', d3.event.pageX + 14 + 'px')
  147.         .style("opacity", 1);
  148.     }).on("mouseout", function(d) {
  149.       tooltip.style("opacity", 0);
  150.     });
  151.  
  152.  
  153.     var polyline = svg.select(".lines").selectAll("polyline").data(pie(formattedData.points));
  154.  
  155.     polyline.enter().append("polyline").style("opacity", 0.5).attr('points', function(d) {
  156.       var pos = outerArc.centroid(d);
  157.       pos[0] = radius * 0.95 * (midAngle(d) < Math.PI ? 1 : -1);
  158.       return [arc.centroid(d), arc.centroid(d), arc.centroid(d)];
  159.     }).transition().duration(secDur).delay(function(d, i) {
  160.       return arcAnimDur + i * secIndividualdelay;
  161.     }).attr('points', function(d) {
  162.       var pos = outerArc.centroid(d);
  163.       pos[0] = radius * 0.95 * (midAngle(d) < Math.PI ? 1 : -1);
  164.       return [arc.centroid(d), outerArc.centroid(d), pos];
  165.     });
  166.   };
  167.  
  168.   draw();
  169.  
  170.   var button = document.querySelector('button');
  171.  
  172.   var replay = function replay() {
  173.  
  174.     d3.selectAll('.slices').transition().ease('back').duration(500).delay(0).style('opacity', 0).attr('transform', 'translate(0, 250)').remove();
  175.     d3.selectAll('.lines').transition().ease('back').duration(500).delay(100).style('opacity', 0).attr('transform', 'translate(0, 250)').remove();
  176.     d3.selectAll('.labels').transition().ease('back').duration(500).delay(200).style('opacity', 0).attr('transform', 'translate(0, 250)').remove();
  177.  
  178.     setTimeout(draw, 800);
  179.   };
  180. }
  181.  
  182. //Get data, and render chart
  183. $.get("path/to/data.php", function(data){
  184.   //Get data
  185.   var parsedData = ParseData(data);
  186.   //Render chart
  187.   InitChart(parsedData);
  188. });
Advertisement
Add Comment
Please, Sign In to add comment