Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- function ParseData(data) {
- var parsed = {};
- 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'];
- data.forEach(function(item) {
- var week = parseInt(item.WeekNumber.slice(4)),
- month = Math.floor((week - 1) / 4);
- if (!parsed[month]) parsed[month] = {
- weeks: [],
- points: 0,
- month: months[month - 1]
- }
- var weekLabel = "Week " + week + ": " + item.pointsRewarded;
- parsed[month].weeks.push(weekLabel);
- parsed[month].points += item.pointsRewarded;
- });
- return FormatData(parsed);
- }
- function FormatData(data) {
- var formatted = {
- points: [],
- weeks: [],
- months: []
- };
- for (var month in data) {
- var now = data[month];
- formatted.points.push(now.points);
- formatted.months.push(now.month);
- formatted.weeks.push(now.weeks.join("<br>"));
- }
- return formatted;
- }
- function InitChart(formattedData) {
- let colors = ['#67001f', '#b2182b', '#d6604d', '#f4a582', '#fddbc7', '#e0e0e0', '#bababa', '#878787', '#4d4d4d', '#1a1a1a', 'white', 'white'];
- var width = document.querySelector('.chart-wrapper').offsetWidth,
- height = document.querySelector('.chart-wrapper').offsetHeight,
- minOfWH = Math.min(width, height) / 2,
- initialAnimDelay = 300,
- arcAnimDelay = 150,
- arcAnimDur = 3000,
- secDur = 1000,
- secIndividualdelay = 150;
- var radius = undefined;
- // calculate minimum of width and height to set chart radius
- if (minOfWH > 200) {
- radius = 200;
- } else {
- radius = minOfWH;
- }
- // append svg
- var svg = d3.select('.chart-wrapper').append('svg').attr({
- 'width': width,
- 'height': height,
- 'class': 'pieChart'
- }).append('g');
- svg.attr({
- 'transform': 'translate(' + width / 2 + ', ' + height / 2 + ')'
- });
- // for drawing slices
- var arc = d3.svg.arc().outerRadius(radius * 0.6).innerRadius(radius * 0.45);
- // for labels and polylines
- var outerArc = d3.svg.arc().innerRadius(radius * 0.85).outerRadius(radius * 0.85);
- // d3 color generator
- // let c10 = d3.scale.category10();
- var tooltip = d3.select("body").append("div").attr("class", "tooltip").style("opacity", 0);
- var pie = d3.layout.pie().value(function(d) {
- return d;
- }).sort(null);
- var draw = function draw() {
- svg.append("g").attr("class", "lines");
- svg.append("g").attr("class", "slices");
- svg.append("g").attr("class", "labels");
- // define slice
- var slice = svg.select('.slices').datum(formattedData.points).selectAll('path').data(pie);
- slice.enter().append('path').attr({
- 'fill': function fill(d, i) {
- return colors[i];
- },
- 'd': arc,
- 'stroke-width': '25px'
- }).attr('transform', function(d, i) {
- return 'rotate(-180, 0, 0)';
- }).style('opacity', 0).transition().delay(function(d, i) {
- return i * arcAnimDelay + initialAnimDelay;
- }).duration(arcAnimDur).ease('elastic').style('opacity', 1).attr('transform', 'rotate(0,0,0)');
- slice.transition().delay(function(d, i) {
- return arcAnimDur + i * secIndividualdelay;
- }).duration(secDur).attr('stroke-width', '5px');
- var midAngle = function midAngle(d) {
- return d.startAngle + (d.endAngle - d.startAngle) / 2;
- };
- var text = svg.select(".labels").selectAll("text").data(pie(formattedData.points));
- text.enter().append('text').attr('dy', '0.35em').style("opacity", 0).attr("cursor", "default").style('fill', function(d, i) {
- return colors[i];
- }).text(function(d, i) {
- return formattedData.months[i];
- }).attr('transform', function(d) {
- // calculate outerArc centroid for 'this' slice
- var pos = outerArc.centroid(d);
- // define left and right alignment of text labels
- pos[0] = radius * (midAngle(d) < Math.PI ? 1 : -1);
- return 'translate(' + pos + ')';
- }).style('text-anchor', function(d) {
- return midAngle(d) < Math.PI ? "start" : "end";
- }).transition().delay(function(d, i) {
- return arcAnimDur + i * secIndividualdelay;
- }).duration(secDur).style('opacity', 1);
- text.on("mousemove", function(d, i) {
- tooltip.html(formattedData.weeks[i])
- .style('top', d3.event.pageY - 6 + 'px')
- .style('left', d3.event.pageX + 14 + 'px')
- .style("opacity", 1);
- }).on("mouseout", function(d) {
- tooltip.style("opacity", 0);
- });
- var polyline = svg.select(".lines").selectAll("polyline").data(pie(formattedData.points));
- polyline.enter().append("polyline").style("opacity", 0.5).attr('points', function(d) {
- var pos = outerArc.centroid(d);
- pos[0] = radius * 0.95 * (midAngle(d) < Math.PI ? 1 : -1);
- return [arc.centroid(d), arc.centroid(d), arc.centroid(d)];
- }).transition().duration(secDur).delay(function(d, i) {
- return arcAnimDur + i * secIndividualdelay;
- }).attr('points', function(d) {
- var pos = outerArc.centroid(d);
- pos[0] = radius * 0.95 * (midAngle(d) < Math.PI ? 1 : -1);
- return [arc.centroid(d), outerArc.centroid(d), pos];
- });
- };
- draw();
- var button = document.querySelector('button');
- var replay = function replay() {
- d3.selectAll('.slices').transition().ease('back').duration(500).delay(0).style('opacity', 0).attr('transform', 'translate(0, 250)').remove();
- d3.selectAll('.lines').transition().ease('back').duration(500).delay(100).style('opacity', 0).attr('transform', 'translate(0, 250)').remove();
- d3.selectAll('.labels').transition().ease('back').duration(500).delay(200).style('opacity', 0).attr('transform', 'translate(0, 250)').remove();
- setTimeout(draw, 800);
- };
- }
- //Get data, and render chart
- $.get("path/to/data.php", function(data){
- //Get data
- var parsedData = ParseData(data);
- //Render chart
- InitChart(parsedData);
- });
Advertisement
Add Comment
Please, Sign In to add comment