Skip to content

Instantly share code, notes, and snippets.

@jeremy6462
Created April 6, 2018 22:32
Show Gist options
  • Save jeremy6462/f17149365cc94119d57ebeb4b6aeff61 to your computer and use it in GitHub Desktop.
Save jeremy6462/f17149365cc94119d57ebeb4b6aeff61 to your computer and use it in GitHub Desktop.
Time-based Visualization for CS 590V
(function(global, factory) {
typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("d3-scale")) :
typeof define === "function" && define.amd ? define(["exports", "d3-scale"], factory) :
(factory(global.d3 = global.d3 || {}, global.d3));
}(this, function(exports, d3Scale) {
'use strict';
function square(x) {
return x * x;
}
function radial() {
var linear = d3Scale.scaleLinear();
function scale(x) {
return Math.sqrt(linear(x));
}
scale.domain = function(_) {
return arguments.length ? (linear.domain(_), scale) : linear.domain();
};
scale.nice = function(count) {
return (linear.nice(count), scale);
};
scale.range = function(_) {
return arguments.length ? (linear.range(_.map(square)), scale) : linear.range().map(Math.sqrt);
};
scale.ticks = linear.ticks;
scale.tickFormat = linear.tickFormat;
return scale;
}
exports.scaleRadial = radial;
Object.defineProperty(exports, '__esModule', {value: true});
}));
/**
* @author Dimitry Kudrayvtsev
* @version 2.0
*
* Ported to d3 v4 by Keyvan Fatehi on October 16th, 2016
*/
// found here: http://bl.ocks.org/baramuyu/be8e3005cfcb0aba5f763963c75f3c7e
d3.gantt = function() {
var FIT_TIME_DOMAIN_MODE = "fit";
var FIXED_TIME_DOMAIN_MODE = "fixed";
var margin = {
top : 20,
right : 40,
bottom : 20,
left : 150
};
var timeDomainStart = d3.timeDay.offset(new Date(),-3);
var timeDomainEnd = d3.timeHour.offset(new Date(),+3);
var timeDomainMode = FIT_TIME_DOMAIN_MODE;// fixed or fit
var taskTypes = [];
var taskStatus = [];
var height = document.body.clientHeight - margin.top - margin.bottom-5;
var width = document.body.clientWidth - margin.right - margin.left-5;
var tickFormat = "%H:%M";
var keyFunction = function(d) {
return d.startDate + d.taskName + d.endDate;
};
var rectTransform = function(d) {
return "translate(" + x(d.startDate) + "," + y(d.taskName) + ")";
};
var x,y,xAxis,yAxis;
initAxis();
var initTimeDomain = function() {
if (timeDomainMode === FIT_TIME_DOMAIN_MODE) {
if (tasks === undefined || tasks.length < 1) {
timeDomainStart = d3.time.day.offset(new Date(), -3);
timeDomainEnd = d3.time.hour.offset(new Date(), +3);
return;
}
tasks.sort(function(a, b) {
return a.endDate - b.endDate;
});
timeDomainEnd = tasks[tasks.length - 1].endDate;
tasks.sort(function(a, b) {
return a.startDate - b.startDate;
});
timeDomainStart = tasks[0].startDate;
}
};
function initAxis() {
x = d3.scaleTime().domain([ timeDomainStart, timeDomainEnd ]).range([ 0, width ]).clamp(true);
y = d3.scaleBand().domain(taskTypes).rangeRound([ 0, height - margin.top - margin.bottom ], .1);
xAxis = d3.axisBottom().scale(x).tickFormat(d3.timeFormat(tickFormat))
.tickSize(8).tickPadding(8);
yAxis = d3.axisLeft().scale(y).tickSize(0);
};
function gantt(tasks) {
initTimeDomain();
initAxis();
var svg = d3.select("body")
.append("svg")
.attr("class", "chart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("class", "gantt-chart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("transform", "translate(" + margin.left + ", " + margin.top + ")");
svg.selectAll(".chart")
.data(tasks, keyFunction).enter()
.append("rect")
.attr("rx", 5)
.attr("ry", 5)
.attr("class", function(d){
if(taskStatus[d.status] == null){ return "bar";}
return taskStatus[d.status];
})
.attr("y", 0)
.attr("transform", rectTransform)
.attr("height", function(d) { return 70; })
.attr("width", function(d) {
return (x(d.endDate) - x(d.startDate));
});
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0, " + (height - margin.top - margin.bottom) + ")")
.transition()
.call(xAxis);
svg.append("g").attr("class", "y axis").transition().call(yAxis);
return gantt;
};
gantt.redraw = function(tasks) {
initTimeDomain();
initAxis();
var svg = d3.select("svg");
var ganttChartGroup = svg.select(".gantt-chart");
var rect = ganttChartGroup.selectAll("rect").data(tasks, keyFunction);
rect.enter()
.insert("rect",":first-child")
.attr("rx", 5)
.attr("ry", 5)
.attr("class", function(d){
if(taskStatus[d.status] == null){ return "bar";}
return taskStatus[d.status];
})
.transition()
.attr("y", 0)
.attr("transform", rectTransform)
.attr("height", function(d) { return y.range()[1]; })
.attr("width", function(d) {
return (x(d.endDate) - x(d.startDate));
});
rect.transition()
.attr("transform", rectTransform)
.attr("height", function(d) { return y.range()[1]; })
.attr("width", function(d) {
return (x(d.endDate) - x(d.startDate));
});
rect.exit().remove();
svg.select(".x").transition().call(xAxis);
svg.select(".y").transition().call(yAxis);
return gantt;
};
gantt.margin = function(value) {
if (!arguments.length)
return margin;
margin = value;
return gantt;
};
gantt.timeDomain = function(value) {
if (!arguments.length)
return [ timeDomainStart, timeDomainEnd ];
timeDomainStart = +value[0], timeDomainEnd = +value[1];
return gantt;
};
/**
* @param {string}
* vale The value can be "fit" - the domain fits the data or
* "fixed" - fixed domain.
*/
gantt.timeDomainMode = function(value) {
if (!arguments.length)
return timeDomainMode;
timeDomainMode = value;
return gantt;
};
gantt.taskTypes = function(value) {
if (!arguments.length)
return taskTypes;
taskTypes = value;
return gantt;
};
gantt.taskStatus = function(value) {
if (!arguments.length)
return taskStatus;
taskStatus = value;
return gantt;
};
gantt.width = function(value) {
if (!arguments.length)
return width;
width = +value;
return gantt;
};
gantt.height = function(value) {
if (!arguments.length)
return height;
height = +value;
return gantt;
};
gantt.tickFormat = function(value) {
if (!arguments.length)
return tickFormat;
tickFormat = value;
return gantt;
};
return gantt;
};
clocked_in clocked_out duration earnings taskName
1/1/17 22:27 1/1/17 22:55 0.5 1.75 1
1/2/17 19:35 1/2/17 20:12 0.67 2.33 2
1/2/17 20:16 1/2/17 21:51 1.58 5.54 1
1/2/17 22:58 1/3/17 1:11 2.25 7.88 2
1/3/17 10:56 1/3/17 11:27 0.58 2.04 4
1/3/17 11:32 1/3/17 12:07 0.58 2.04 5
1/3/17 22:50 1/4/17 0:41 1.92 6.71 5
1/4/17 21:32 1/4/17 23:41 2.17 7.58 1
1/5/17 12:08 1/5/17 14:01 1.92 6.71 5
1/5/17 14:11 1/5/17 16:19 2.17 7.58 3
1/5/17 22:30 1/6/17 0:28 2 7 1
1/6/17 10:52 1/6/17 11:10 0.33 1.17 2
1/6/17 11:15 1/6/17 11:35 0.33 1.17 5
1/6/17 12:30 1/6/17 14:40 2.17 7.58 5
1/6/17 15:12 1/6/17 15:30 0.33 1.17 5
1/6/17 15:55 1/6/17 16:15 0.33 1.17 1
1/6/17 16:54 1/6/17 17:25 0.58 2.04 5
1/6/17 17:31 1/6/17 17:37 0.17 0.58 5
1/6/17 20:52 1/6/17 21:50 1 3.5 4
1/6/17 22:10 1/6/17 22:59 0.83 2.92 1
1/6/17 23:43 1/6/17 23:56 0.25 0.88 4
1/6/17 23:57 1/7/17 0:14 0.33 1.17 3
1/7/17 0:25 1/7/17 1:52 1.5 5.25 1
1/7/17 13:39 1/7/17 14:28 0.83 2.92 5
1/7/17 14:39 1/7/17 15:20 0.75 2.62 2
1/8/17 23:10 1/9/17 2:30 3.33 11.67 5
1/10/17 20:58 1/11/17 2:25 5.5 19.25 4
1/11/17 11:00 1/11/17 12:00 1 3.5 5
1/11/17 12:30 1/11/17 12:52 0.42 1.46 4
1/11/17 14:01 1/11/17 14:23 0.42 1.46 1
1/11/17 20:58 1/11/17 21:53 0.92 3.21 2
1/11/17 22:51 1/12/17 0:26 1.58 5.54 4
1/13/17 12:31 1/13/17 12:46 0.25 0.88 3
1/13/17 13:03 1/13/17 13:08 0.08 0.29 4
1/13/17 13:43 1/13/17 14:00 0.33 1.17 5
1/13/17 17:30 1/13/17 18:08 0.67 2.33 2
1/13/17 18:17 1/13/17 18:41 0.42 1.46 5
1/13/17 19:40 1/13/17 19:57 0.33 1.17 4
1/13/17 20:57 1/13/17 21:32 0.58 2.04 2
1/16/17 14:20 1/16/17 17:30 3.17 11.08 4
1/17/17 15:25 1/17/17 15:48 0.42 1.46 4
1/18/17 13:00 1/18/17 13:57 1 3.5 5
1/18/17 14:00 1/18/17 14:19 0.33 1.17 4
1/18/17 14:39 1/18/17 15:06 0.5 1.75 3
1/18/17 22:00 1/18/17 22:48 0.83 2.92 4
1/19/17 10:02 1/19/17 11:30 1.5 5.25 5
1/24/17 1:03 1/24/17 1:29 0.5 1.75 5
1/24/17 14:25 1/24/17 14:36 0.25 0.88 3
1/24/17 15:13 1/24/17 15:18 0.08 0.29 5
1/24/17 15:25 1/24/17 15:38 0.25 0.88 5
1/24/17 20:32 1/24/17 21:03 0.58 2.04 4
1/24/17 21:21 1/24/17 21:29 0.17 0.58 2
1/24/17 21:29 1/24/17 22:01 0.58 2.04 2
1/24/17 22:06 1/24/17 22:15 0.17 0.58 5
1/24/17 22:25 1/24/17 22:32 0.17 0.58 4
1/24/17 23:30 1/24/17 23:35 0.08 0.29 1
1/26/17 10:03 1/26/17 12:02 2 7 3
1/27/17 8:47 1/27/17 10:33 1.83 6.42 2
1/27/17 16:25 1/27/17 17:05 0.67 2.33 3
1/28/17 11:47 1/28/17 11:51 0.08 0.29 1
1/30/17 8:29 1/30/17 11:05 2.67 9.33 3
1/30/17 19:06 1/30/17 19:39 0.58 2.04 2
1/31/17 18:20 1/31/17 18:49 0.5 1.75 5
2/1/17 23:20 2/1/17 23:35 0.25 0.88 2
2/1/17 23:40 2/1/17 23:53 0.25 0.88 4
2/2/17 0:02 2/2/17 0:05 0.08 0.29 4
2/2/17 10:03 2/2/17 11:27 1.42 4.96 2
2/2/17 11:30 2/2/17 12:23 0.92 3.21 1
2/2/17 14:38 2/2/17 15:42 1.08 3.79 1
2/3/17 8:48 2/3/17 10:55 2.17 7.58 1
2/6/17 8:30 2/6/17 9:37 1.17 4.08 2
2/7/17 20:32 2/7/17 21:07 0.58 2.04 1
2/9/17 13:06 2/9/17 13:12 0.17 0.58 2
2/9/17 15:11 2/9/17 15:17 0.17 0.58 1
2/9/17 15:25 2/9/17 15:28 0.08 0.29 3
2/9/17 15:36 2/9/17 16:59 1.42 4.96 2
2/9/17 18:04 2/9/17 18:08 0.08 0.29 1
2/10/17 8:31 2/10/17 10:27 2 7 1
2/10/17 12:16 2/10/17 14:28 2.25 7.88 3
2/13/17 1:32 2/13/17 2:17 0.75 2.62 3
2/13/17 14:28 2/13/17 15:16 0.83 2.92 5
2/13/17 17:05 2/13/17 17:14 0.17 0.58 3
2/15/17 10:02 2/15/17 11:12 1.17 4.08 1
2/15/17 17:10 2/15/17 18:19 1.17 4.08 5
2/15/17 18:35 2/15/17 18:53 0.33 1.17 3
2/15/17 19:02 2/15/17 19:28 0.5 1.75 4
2/15/17 22:48 2/15/17 23:37 0.83 2.92 1
2/16/17 9:30 2/16/17 11:51 2.42 8.46 5
2/17/17 8:45 2/17/17 9:47 1.08 3.79 3
2/17/17 21:39 2/17/17 22:09 0.5 1.75 3
2/17/17 22:16 2/17/17 23:08 0.92 3.21 5
2/20/17 20:39 2/20/17 22:04 1.42 4.96 5
2/22/17 12:17 2/22/17 13:08 0.92 3.21 5
2/22/17 17:40 2/22/17 18:07 0.5 1.75 1
2/22/17 21:23 2/22/17 22:31 1.17 4.08 2
2/22/17 22:38 2/22/17 23:34 1 3.5 2
2/23/17 0:28 2/23/17 1:32 1.08 3.79 2
2/24/17 8:45 2/24/17 10:50 2.08 7.29 3
2/27/17 0:04 2/27/17 0:48 0.75 2.62 1
2/27/17 8:27 2/27/17 10:05 1.67 5.83 3
2/27/17 22:05 2/27/17 23:19 1.25 4.38 5
2/27/17 23:20 2/27/17 23:44 0.42 1.46 2
2/28/17 0:07 2/28/17 0:20 0.25 0.88 5
2/28/17 23:44 3/1/17 0:03 0.33 1.17 2
3/1/17 21:16 3/1/17 22:15 1 3.5 5
3/2/17 18:20 3/2/17 19:38 1.33 4.67 3
3/2/17 19:43 3/2/17 20:21 0.67 2.33 3
3/2/17 23:03 3/2/17 23:46 0.75 2.62 5
3/3/17 8:46 3/3/17 10:35 1.83 6.42 5
3/6/17 15:34 3/6/17 15:57 0.42 1.46 1
3/6/17 23:34 3/7/17 0:38 1.08 3.79 1
3/7/17 0:38 3/7/17 1:14 0.67 2.33 1
3/8/17 22:47 3/8/17 23:47 1 3.5 1
3/9/17 1:02 3/9/17 1:40 0.67 2.33 1
3/9/17 11:46 3/9/17 11:51 0.08 0.29 3
3/9/17 14:19 3/9/17 14:42 0.42 1.46 2
3/9/17 14:42 3/9/17 15:50 1.17 4.08 1
3/9/17 17:19 3/9/17 18:05 0.83 2.92 2
3/9/17 18:05 3/9/17 18:12 0.17 0.58 2
3/9/17 19:44 3/9/17 19:52 0.17 0.58 5
3/9/17 20:03 3/9/17 20:24 0.42 1.46 1
3/9/17 20:45 3/9/17 21:48 1.08 3.79 2
3/11/17 12:45 3/11/17 13:37 0.92 3.21 1
3/11/17 14:05 3/11/17 14:37 0.58 2.04 2
3/11/17 14:55 3/11/17 16:07 1.25 4.38 4
3/12/17 13:45 3/12/17 14:34 0.83 2.92 1
3/12/17 14:47 3/12/17 15:16 0.5 1.75 1
3/12/17 19:19 3/12/17 19:19 0 0 1
3/12/17 19:33 3/12/17 19:51 0.33 1.17 1
3/12/17 20:15 3/12/17 22:49 2.58 9.04 1
3/12/17 22:50 3/12/17 23:28 0.67 2.33 5
3/13/17 11:41 3/13/17 12:09 0.5 1.75 1
3/13/17 13:31 3/13/17 13:35 0.08 0.29 5
3/13/17 13:39 3/13/17 13:44 0.08 0.29 4
3/13/17 13:48 3/13/17 14:04 0.33 1.17 5
3/13/17 14:20 3/13/17 16:30 2.17 7.58 2
3/13/17 18:48 3/13/17 19:08 0.33 1.17 1
3/14/17 13:20 3/14/17 13:34 0.25 0.88 1
3/14/17 14:16 3/14/17 14:33 0.33 1.17 4
3/14/17 14:40 3/14/17 14:52 0.25 0.88 1
3/14/17 19:15 3/14/17 19:43 0.5 1.75 1
3/14/17 21:19 3/14/17 21:22 0.08 0.29 1
3/14/17 21:46 3/14/17 21:54 0.17 0.58 1
3/15/17 13:16 3/15/17 13:35 0.33 1.17 2
3/16/17 11:40 3/16/17 13:40 2 7 5
3/16/17 13:55 3/16/17 14:33 0.67 2.33 3
3/16/17 16:26 3/16/17 16:45 0.33 1.17 4
3/16/17 23:26 3/16/17 23:53 0.5 1.75 1
3/18/17 19:00 3/18/17 19:27 0.5 1.75 1
3/18/17 20:32 3/18/17 20:56 0.42 1.46 4
3/18/17 21:30 3/18/17 23:34 2.08 7.29 3
3/19/17 12:22 3/19/17 14:48 2.5 8.75 5
3/20/17 10:01 3/20/17 11:00 1 3.5 3
3/20/17 16:37 3/20/17 16:49 0.25 0.88 2
3/20/17 19:26 3/20/17 20:00 0.58 2.04 3
3/21/17 8:00 3/21/17 11:54 3.92 13.71 4
3/22/17 9:30 3/22/17 11:00 1.5 5.25 2
3/23/17 20:35 3/23/17 20:54 0.33 1.17 2
3/23/17 21:11 3/23/17 21:15 0.08 0.29 4
3/23/17 21:53 3/23/17 22:37 0.75 2.62 2
3/23/17 22:37 3/23/17 23:03 0.5 1.75 3
3/24/17 8:45 3/24/17 10:28 1.75 6.12 4
3/24/17 12:13 3/24/17 14:30 2.33 8.17 4
3/27/17 16:22 3/27/17 16:36 0.25 0.88 3
3/27/17 23:13 3/28/17 1:25 2.25 7.88 2
3/29/17 9:42 3/29/17 10:44 1.08 3.79 2
3/29/17 21:12 3/29/17 22:43 1.58 5.54 3
3/29/17 22:59 3/29/17 23:27 0.5 1.75 1
3/29/17 23:40 3/30/17 0:15 0.58 2.04 2
3/30/17 1:55 3/30/17 2:30 0.58 2.04 2
3/30/17 14:45 3/30/17 15:06 0.42 1.46 4
3/30/17 23:22 3/31/17 1:06 1.75 6.12 2
3/31/17 8:30 3/31/17 11:00 2.5 8.75 4
3/31/17 15:22 3/31/17 16:06 0.75 2.62 4
4/3/17 8:40 4/3/17 9:35 0.92 3.21 1
4/6/17 11:45 4/6/17 12:46 1.08 3.79 4
4/6/17 18:48 4/6/17 19:39 0.92 3.21 3
4/6/17 23:52 4/7/17 0:40 0.83 2.92 5
4/7/17 1:53 4/7/17 2:37 0.75 2.62 1
4/7/17 8:26 4/7/17 11:08 2.75 9.62 1
4/10/17 10:02 4/10/17 10:59 1 3.5 2
4/12/17 20:03 4/12/17 20:15 0.25 0.88 4
4/12/17 20:30 4/12/17 20:45 0.25 0.88 4
4/13/17 0:26 4/13/17 1:21 0.92 3.21 5
4/13/17 10:09 4/13/17 11:32 1.42 4.96 4
4/13/17 18:52 4/13/17 19:48 1 3.5 1
4/13/17 22:06 4/14/17 0:24 2.33 8.17 4
4/14/17 0:47 4/14/17 1:32 0.75 2.62 1
4/14/17 2:08 4/14/17 3:10 1.08 3.79 2
4/14/17 8:25 4/14/17 11:08 2.75 9.62 3
4/14/17 15:20 4/14/17 15:41 0.42 1.46 4
4/14/17 15:59 4/14/17 16:20 0.42 1.46 3
4/14/17 16:37 4/14/17 18:17 1.67 5.83 2
4/14/17 18:22 4/14/17 19:05 0.75 2.62 1
4/15/17 16:10 4/15/17 16:52 0.75 2.62 2
4/16/17 13:10 4/16/17 15:18 2.17 7.58 5
4/16/17 15:21 4/16/17 15:41 0.33 1.17 5
4/16/17 15:56 4/16/17 16:18 0.42 1.46 5
4/16/17 22:08 4/16/17 22:36 0.5 1.75 2
4/16/17 23:20 4/17/17 2:47 3.5 12.25 1
4/17/17 10:28 4/17/17 11:50 1.42 4.96 1
4/17/17 17:42 4/17/17 18:10 0.5 1.75 1
4/18/17 17:05 4/18/17 17:46 0.75 2.62 1
4/20/17 11:50 4/20/17 12:54 1.08 3.79 4
4/20/17 22:23 4/20/17 22:30 0.17 0.58 4
4/21/17 8:30 4/21/17 10:30 2 7 5
4/21/17 13:10 4/21/17 13:34 0.42 1.46 4
4/21/17 14:00 4/21/17 15:23 1.42 4.96 3
4/21/17 17:57 4/21/17 18:32 0.58 2.04 5
4/24/17 8:32 4/24/17 10:30 2 7 2
4/24/17 16:19 4/24/17 16:45 0.5 1.75 2
4/25/17 22:26 4/25/17 23:42 1.33 4.67 2
4/27/17 11:21 4/27/17 12:23 1.08 3.79 2
4/27/17 14:14 4/27/17 14:47 0.58 2.04 3
4/27/17 15:05 4/27/17 15:14 0.17 0.58 5
4/29/17 17:30 4/29/17 17:44 0.25 0.88 5
4/29/17 17:45 4/29/17 17:51 0.17 0.58 3
4/30/17 19:11 4/30/17 19:44 0.58 2.04 3
4/30/17 20:30 4/30/17 21:00 0.5 1.75 4
5/1/17 8:32 5/1/17 10:23 1.92 6.71 4
5/3/17 20:12 5/3/17 20:25 0.25 0.88 1
5/3/17 22:20 5/3/17 22:31 0.25 0.88 3
5/3/17 23:28 5/4/17 0:03 0.58 2.04 4
5/4/17 2:10 5/4/17 2:18 0.17 0.58 4
5/4/17 13:26 5/4/17 14:40 1.25 4.38 5
5/4/17 14:57 5/4/17 16:05 1.17 4.08 4
5/4/17 20:00 5/4/17 21:04 1.08 3.79 5
5/4/17 21:27 5/4/17 21:52 0.42 1.46 5
5/4/17 22:48 5/4/17 23:49 1.08 3.79 1
5/5/17 1:37 5/5/17 2:45 1.17 4.08 3
5/5/17 7:17 5/5/17 7:45 0.5 1.75 2
5/5/17 10:02 5/5/17 11:02 1 3.5 5
5/5/17 11:20 5/5/17 11:47 0.5 1.75 3
5/5/17 18:00 5/5/17 18:43 0.75 2.62 2
5/6/17 12:44 5/6/17 13:25 0.75 2.62 2
5/6/17 14:00 5/6/17 15:08 1.17 4.08 5
5/6/17 16:22 5/6/17 17:59 1.67 5.83 3
5/6/17 17:59 5/6/17 18:04 0.08 0.29 1
5/6/17 18:58 5/6/17 19:52 0.92 3.21 4
5/6/17 20:30 5/6/17 21:19 0.83 2.92 5
5/6/17 21:52 5/6/17 23:10 1.33 4.67 5
5/6/17 23:38 5/7/17 0:10 0.58 2.04 5
5/7/17 0:47 5/7/17 0:54 0.17 0.58 1
5/7/17 1:21 5/7/17 1:31 0.17 0.58 1
5/7/17 1:38 5/7/17 3:35 2 7 1
5/7/17 12:52 5/7/17 13:36 0.75 2.62 1
5/8/17 2:20 5/8/17 4:27 2.17 7.58 1
5/11/17 10:30 5/11/17 12:00 1.5 5.25 2
5/11/17 14:10 5/11/17 15:20 1.17 4.08 1
5/13/17 12:00 5/13/17 12:39 0.67 2.33 1
5/13/17 14:40 5/13/17 15:34 0.92 3.21 1
5/14/17 0:28 5/14/17 0:42 0.25 0.88 4
5/14/17 0:55 5/14/17 1:11 0.33 1.17 1
5/15/17 22:35 5/15/17 23:02 0.5 1.75 3
5/15/17 23:30 5/16/17 1:26 2 7 3
5/16/17 15:31 5/16/17 15:31 0 0 5
5/16/17 23:29 5/17/17 1:00 1.58 5.54 1
5/17/17 16:20 5/17/17 17:04 0.75 2.62 1
5/17/17 17:22 5/17/17 17:52 0.5 1.75 5
5/18/17 12:52 5/18/17 12:59 0.17 0.58 2
5/18/17 21:36 5/18/17 23:51 2.25 7.88 4
5/19/17 0:00 5/19/17 0:57 1 3.5 2
5/19/17 13:02 5/19/17 13:09 0.17 0.58 2
5/19/17 13:22 5/19/17 13:46 0.42 1.46 4
5/19/17 14:03 5/19/17 14:36 0.58 2.04 3
5/19/17 15:29 5/19/17 15:44 0.25 0.88 3
5/20/17 15:02 5/20/17 15:15 0.25 0.88 3
5/21/17 23:23 5/22/17 0:20 1 3.5 1
5/22/17 12:30 5/22/17 12:47 0.33 1.17 3
5/22/17 12:47 5/22/17 12:59 0.25 0.88 2
5/22/17 13:00 5/22/17 14:05 1.08 3.79 2
5/23/17 21:25 5/24/17 0:49 3.42 11.96 4
5/24/17 0:56 5/24/17 1:25 0.5 1.75 1
5/24/17 1:55 5/24/17 2:17 0.42 1.46 2
5/24/17 10:51 5/24/17 10:58 0.17 0.58 5
5/24/17 12:07 5/24/17 12:42 0.58 2.04 2
5/24/17 15:51 5/24/17 16:22 0.58 2.04 2
5/24/17 16:25 5/24/17 16:41 0.33 1.17 5
5/24/17 17:15 5/24/17 18:30 1.25 4.38 3
5/24/17 19:22 5/24/17 21:01 1.67 5.83 4
5/25/17 10:31 5/25/17 12:04 1.58 5.54 3
5/25/17 18:04 5/25/17 18:35 0.33 1.17 4
5/26/17 1:25 5/26/17 1:58 0.58 2.04 1
5/28/17 15:00 5/28/17 16:15 1.25 4.38 2
5/28/17 16:04 5/28/17 16:23 0.33 1.17 4
5/28/17 16:34 5/28/17 18:27 1.92 6.71 1
5/29/17 14:52 5/29/17 15:26 0.58 2.04 5
5/31/17 7:30 5/31/17 8:30 1 3.5 2
6/3/17 23:45 6/4/17 2:30 2.75 9.62 5
6/5/17 22:22 6/6/17 0:21 2 7 1
6/7/17 19:56 6/7/17 20:22 0.5 1.75 2
6/9/17 7:00 6/9/17 8:00 1 3.5 4
6/10/17 21:00 6/10/17 21:30 0.5 1.75 4
6/13/17 19:50 6/13/17 20:22 0.58 2.04 3
6/13/17 22:45 6/13/17 23:08 0.42 1.46 5
6/14/17 8:00 6/14/17 8:40 0.67 2.33 2
6/14/17 21:29 6/14/17 22:05 0.67 2.33 5
6/16/17 8:00 6/16/17 8:27 0.5 1.75 1
6/16/17 23:15 6/17/17 0:05 0.83 2.92 5
6/18/17 11:55 6/18/17 12:20 0.42 1.46 4
6/18/17 13:30 6/18/17 13:54 0.42 1.46 3
6/18/17 18:14 6/18/17 18:42 0.5 1.75 5
6/18/17 20:40 6/18/17 22:02 1.42 4.96 5
6/19/17 18:30 6/19/17 19:30 1 3.5 5
6/19/17 23:09 6/19/17 23:53 0.75 2.62 3
6/20/17 8:24 6/20/17 8:34 0.17 0.58 2
6/20/17 9:22 6/20/17 9:35 0.25 0.88 3
6/20/17 18:40 6/20/17 18:55 0.25 0.88 5
6/20/17 22:01 6/20/17 22:06 0.08 0.29 2
6/20/17 22:06 6/20/17 22:29 0.42 1.46 3
6/26/17 20:20 6/26/17 21:17 1 3.5 3
6/28/17 17:29 6/28/17 19:57 2.5 8.75 3
6/28/17 20:06 6/28/17 20:20 0.25 0.88 5
6/29/17 8:50 6/29/17 9:02 0.25 0.88 4
7/2/17 12:14 7/2/17 12:30 0.33 1.17 5
7/2/17 13:46 7/2/17 13:55 0.17 0.58 2
7/4/17 21:26 7/4/17 22:01 0.58 2.04 5
7/5/17 7:30 7/5/17 8:27 1 3.5 2
7/8/17 11:20 7/8/17 14:00 2.67 9.33 4
7/8/17 12:51 7/8/17 15:25 2.58 9.04 2
7/11/17 21:30 7/11/17 22:30 1 3.5 4
7/12/17 8:30 7/12/17 9:30 1 3.5 2
7/12/17 22:00 7/12/17 23:02 1.08 3.79 5
7/13/17 22:21 7/14/17 0:05 1.75 6.12 2
7/17/17 20:50 7/17/17 22:59 2.17 7.58 5
7/21/17 8:40 7/21/17 9:41 1.08 3.79 5
7/23/17 13:31 7/23/17 17:06 3.58 12.54 3
7/23/17 17:56 7/23/17 20:31 2.58 9.04 5
7/23/17 20:27 7/23/17 20:35 0.17 0.58 3
7/25/17 8:30 7/25/17 9:00 0.5 1.75 2
7/26/17 23:05 7/26/17 23:29 0.42 1.46 1
7/30/17 0:13 7/30/17 1:47 1.58 5.54 5
7/30/17 11:40 7/30/17 13:00 1.33 4.67 4
7/30/17 12:47 7/30/17 14:53 2.17 7.58 5
8/2/17 7:39 8/2/17 8:27 0.83 2.92 5
8/5/17 22:00 8/5/17 22:39 0.67 2.33 1
8/8/17 22:51 8/8/17 23:55 1.08 3.79 4
8/9/17 7:59 8/9/17 9:22 1.42 4.96 3
8/13/17 12:29 8/13/17 14:59 2.5 8.75 5
8/13/17 21:21 8/13/17 23:18 2 7 2
8/14/17 6:00 8/14/17 7:35 1.58 5.54 3
8/14/17 22:34 8/14/17 22:55 0.42 1.46 4
8/16/17 7:16 8/16/17 8:17 1.08 3.79 4
8/20/17 8:00 8/20/17 12:00 4 14 5
8/20/17 13:21 8/20/17 13:30 0.17 0.58 1
8/22/17 12:59 8/22/17 14:01 1.08 3.79 4
8/22/17 16:03 8/22/17 17:22 1.33 4.67 4
8/22/17 18:20 8/22/17 19:08 0.83 2.92 4
8/23/17 13:06 8/23/17 13:50 0.75 2.62 5
8/23/17 16:10 8/23/17 16:41 0.58 2.04 1
8/23/17 17:03 8/23/17 17:21 0.33 1.17 4
8/24/17 10:00 8/24/17 11:05 1.08 3.79 1
8/24/17 11:30 8/24/17 11:51 0.42 1.46 3
8/25/17 10:50 8/25/17 11:13 0.42 1.46 5
8/28/17 5:27 8/28/17 5:48 0.42 1.46 5
8/28/17 14:17 8/28/17 15:31 1.25 4.38 3
8/28/17 16:07 8/28/17 16:23 0.33 1.17 1
9/10/17 20:17 9/10/17 20:49 0.58 2.04 4
9/11/17 9:02 9/11/17 11:00 2 7 4
9/12/17 8:45 9/12/17 10:13 1.5 5.25 5
9/12/17 12:09 9/12/17 12:47 0.67 2.33 2
9/13/17 11:39 9/13/17 12:00 0.42 1.46 5
9/14/17 9:30 9/14/17 10:30 1 3.5 4
9/14/17 10:30 9/14/17 11:24 0.92 3.21 4
9/14/17 15:00 9/14/17 17:00 2 7 1
9/17/17 8:00 9/17/17 9:00 1 3.5 2
9/18/17 14:26 9/18/17 17:10 2.75 9.62 2
9/19/17 14:22 9/19/17 15:20 1 3.5 1
9/19/17 17:03 9/19/17 17:11 0.17 0.58 3
9/19/17 21:44 9/19/17 23:50 2.17 7.58 4
9/20/17 9:15 9/20/17 9:22 0.17 0.58 3
9/20/17 9:39 9/20/17 11:05 1.5 5.25 5
9/20/17 15:13 9/20/17 15:44 0.58 2.04 1
9/20/17 20:25 9/20/17 20:56 0.58 2.04 4
9/20/17 22:00 9/20/17 23:45 1.75 6.12 4
9/21/17 1:00 9/21/17 1:49 0.83 2.92 5
9/21/17 7:39 9/21/17 8:09 0.5 1.75 5
9/21/17 8:34 9/21/17 8:43 0.17 0.58 4
9/21/17 9:32 9/21/17 11:05 1.58 5.54 3
9/21/17 22:55 9/21/17 23:12 0.33 1.17 4
9/24/17 13:13 9/24/17 13:38 0.42 1.46 3
9/24/17 14:59 9/24/17 17:13 2.25 7.88 2
9/24/17 23:59 9/25/17 1:17 1.33 4.67 4
9/25/17 14:26 9/25/17 17:30 3.08 10.79 1
9/28/17 9:19 9/28/17 11:30 2.25 7.88 4
9/28/17 14:30 9/28/17 17:00 2.5 8.75 5
10/1/17 21:27 10/1/17 22:15 0.83 2.92 3
10/2/17 14:46 10/2/17 16:54 2.17 7.58 2
10/2/17 21:45 10/2/17 22:06 0.42 1.46 2
10/4/17 15:33 10/4/17 17:16 1.75 6.12 2
10/4/17 20:44 10/4/17 21:40 1 3.5 3
10/5/17 9:30 10/5/17 10:50 1.33 4.67 5
10/5/17 14:40 10/5/17 17:00 2.33 8.17 4
10/8/17 0:11 10/8/17 1:25 1.25 4.38 5
10/8/17 18:31 10/8/17 19:00 0.5 1.75 1
10/9/17 11:20 10/9/17 11:40 0.33 1.17 4
10/9/17 12:36 10/9/17 12:45 0.17 0.58 4
10/9/17 22:50 10/10/17 0:39 1.83 6.42 4
10/10/17 16:22 10/10/17 17:00 0.67 2.33 2
10/11/17 14:54 10/11/17 16:18 1.42 4.96 5
10/11/17 23:00 10/11/17 23:59 1 3.5 5
10/12/17 9:27 10/12/17 11:21 1.92 6.71 5
10/12/17 14:39 10/12/17 20:58 6.33 22.17 4
10/14/17 10:17 10/14/17 11:18 1.08 3.79 1
10/15/17 1:49 10/15/17 2:57 1.17 4.08 4
10/15/17 2:21 10/15/17 4:25 2.08 7.29 3
10/15/17 14:00 10/15/17 14:21 0.42 1.46 4
10/15/17 14:37 10/15/17 17:04 2.5 8.75 4
10/15/17 18:45 10/15/17 21:20 2.58 9.04 1
10/15/17 22:30 10/16/17 0:02 1.58 5.54 4
10/16/17 14:49 10/16/17 17:04 2.25 7.88 4
10/19/17 14:43 10/19/17 17:21 2.67 9.33 3
10/23/17 15:00 10/23/17 17:20 2.33 8.17 4
10/26/17 9:30 10/26/17 11:07 1.67 5.83 4
10/26/17 14:38 10/26/17 17:09 2.58 9.04 5
10/29/17 16:12 10/29/17 18:30 2.33 8.17 4
10/30/17 14:38 10/30/17 19:17 4.67 16.33 5
11/1/17 16:36 11/1/17 17:34 1 3.5 4
11/1/17 21:39 11/2/17 0:24 2.75 9.62 2
11/2/17 10:15 11/2/17 19:38 9.42 32.96 5
11/5/17 20:05 11/5/17 23:53 3.83 13.42 4
11/10/17 11:38 11/10/17 12:05 0.5 1.75 1
11/10/17 16:36 11/10/17 17:44 1.17 4.08 4
11/11/17 17:13 11/11/17 19:33 2.33 8.17 3
11/13/17 15:15 11/13/17 17:50 2.58 9.04 2
11/14/17 10:50 11/14/17 12:32 1.75 6.12 2
11/14/17 22:38 11/15/17 1:04 2.5 8.75 2
11/15/17 15:00 11/15/17 17:43 2.75 9.62 4
11/16/17 12:02 11/16/17 12:03 0.08 0.29 5
11/16/17 13:00 11/16/17 13:30 0.5 1.75 2
11/16/17 19:17 11/16/17 19:47 0.5 1.75 2
11/18/17 14:26 11/18/17 14:36 0.17 0.58 1
11/18/17 14:37 11/18/17 15:56 1.33 4.67 5
11/18/17 16:34 11/18/17 16:44 0.17 0.58 5
11/18/17 17:10 11/18/17 17:18 0.17 0.58 3
11/18/17 21:55 11/19/17 0:55 3 10.5 2
11/20/17 5:18 11/20/17 12:39 7.42 25.96 1
11/20/17 15:45 11/20/17 17:27 1.75 6.12 3
11/20/17 23:24 11/21/17 0:10 0.83 2.92 1
11/21/17 0:16 11/21/17 0:22 0.17 0.58 3
11/22/17 12:02 11/22/17 12:03 0.08 0.29 3
11/27/17 14:34 11/27/17 17:00 2.5 8.75 4
12/4/17 14:55 12/4/17 18:13 3.33 11.67 5
12/5/17 22:39 12/6/17 1:03 2.42 8.46 1
12/6/17 22:29 12/6/17 23:53 1.42 4.96 1
12/11/17 21:26 12/11/17 2:16 0 0 1
12/11/17 15:00 12/11/17 17:00 2 7 2
12/15/17 13:05 12/15/17 14:25 1.33 4.67 3
12/27/17 9:31 12/27/17 12:11 2.67 9.33 3
12/30/17 16:45 12/30/17 17:41 1 3.5 1
12/30/17 18:21 12/30/17 19:54 1.58 5.54 2
12/30/17 20:19 12/30/17 21:52 1.58 5.54 4
12/30/17 23:26 12/31/17 0:33 1.17 4.08 2
<!DOCTYPE html>
<!-- This lab is based on the example Radial Line Chart
https://bl.ocks.org/tlfrd/fd6991b2d1947a3cb9e0bd20053899d6 -->
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="d3-scale-radial.js"></script>
<script type="text/javascript" src="gantt.js"></script>
<style>
text {
font-size: 14px;
font-family: monospace;
}
.axis path,.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: #33b5e5;
}
.bar-failed {
fill: #CC0000;
}
.bar-running {
fill: #669900;
}
.bar-succeeded {
fill: #33b5e5;
}
.bar-killed {
fill: #ffbb33;
}
#sliderContainer {
text-align: center;
top: 600px;
}
div.tooltip {
position: absolute;
text-align: center;
width: 160px;
height: 108px;
padding: 2px;
font: 12px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
</style>
</head>
<body>
<h1>Hourly Worker's Earnings in 2017</h1>
<h4>I work an hourly job in the college of education and I track whenever I work with the start/end dates. I've exported this data to be used for this assignment. I changed my pay to 3.50/hr and I added a random task (1-5) to each time entry (as instructured by the professor to add another data column). This data set has 450 entries. I added two charts, one to show my work patterns throughout the year based on how much I make on each day. The second is a gantt chart to show when I worked on each task and for how long. This data is useful for me to track my work patterns and to see when in the year I work the most. The user can hover over the earning's axis to see which months I had 1+ days where I earned that much money and they can use the time slider to highlight each month in the gantt chart. </h4>
<svg width="960" height="500"></svg>
<div id="sliderContainer">
<input id="timeslide" type="range" min="0" max="12" value="0" step="1"/><br>
<span id="range">Time Showing: Full Year</span>
</div>
<script type='text/javascript' src='script.js'></script>
</body>
const margin = {top: 20, right: 10, bottom: 20, left: 10};
const innerWidth = 960 - margin.left - margin.right;
const innerHeight = 500 - margin.top - margin.bottom;
const svg = d3.select("svg");
const g = svg.append("g")
.attr("transform", "translate(" + innerWidth / 2 + "," + innerHeight / 2 + ")");
const innerRadius = 100;
const outerRadius = Math.min(innerWidth, innerHeight) / 2 - 6;
const fullCircle = 2 * Math.PI;
const parseTime = d3.timeParse("%m/%d/%Y %H:%M");
const formatMonth = d3.timeFormat("%b");
const xScale = d3.scaleTime()
.range([0, fullCircle]);
const yScale = d3.scaleRadial()
.range([innerRadius, outerRadius]);
const lineScale = d3.lineRadial()
.angle(function(d) { return xScale(d.clocked_in); }) // x: angle in radians; 0 at -y (1/1/17)
.radius(function(d) { return yScale(d.earnings); }); // y: distance from origin (0,0)
// gantt.js seems to require a global tasks variable
var tasks;
var allData;
var ganttGraph;
// create tool tip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// keep track of the current month being shown. null means the full year
var currentMonthIndex = 0;
var months = [
"Full Year",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
d3.csv("hourly_earnings.csv" ,function(d) {
// format the data for display
d.clocked_in = parseTime(d.clocked_in);
d.startDate = d.clocked_in;
d.endDate = parseTime(d.clocked_out);
d.earnings = +d.earnings;
return d;
}, function(error, data) {
if (error) throw error;
allData = data; // store for slider
tasks = data; // for gantt.js
// make sure the scales work based on the actual provided data
xScale.domain(d3.extent(data, d => d.clocked_in));
yScale.domain(d3.extent(data, d => d.earnings));
// draw the line plot around the circle
var linePlot = g.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "#4099ff")
.attr("d", lineScale);
var yAxis = g.append("g")
.attr("text-anchor", "middle");
var yTick = yAxis
.selectAll("g")
.data(yScale.ticks(5))
.enter().append("g");
// adds guiding circles to help with comparing values
yTick.append("circle")
.attr("fill", "none")
.attr("stroke", "black")
.attr("opacity", 0.2)
.attr("r", yScale);
yAxis.append("circle")
.attr("fill", "none")
.attr("stroke", "black")
.attr("opacity", 0.2)
.attr("r", function() { return yScale(yScale.domain()[0])});
// shows the y axis dolar amounts
yTick.append("text")
.attr("y", function(d) { return -yScale(d); })
.attr("dy", "0.35em")
.text(function(d) { return "$" + d; })
.on("mouseover", function(earningVal) {
var monthsEarnedMinVal = [];
for (var i = 0; i < allData.length; i++) {
if (allData[i].earnings >= earningVal) {
var monthIndex = allData[i].clocked_in.getMonth()+1;
var monthText = months[monthIndex];
if (monthsEarnedMinVal.includes(monthText) == false) {
monthsEarnedMinVal.push(monthText);
}
}
}
div.transition()
.duration(200)
.style("opacity", .9);
div.html("In " + monthsEarnedMinVal.join(", ") + ", Jeremy had 1 or more days where he earned $" + earningVal + " or more.")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
var xAxis = g.append("g");
// x axis month names
var xTick = xAxis
.selectAll("g")
.data(xScale.ticks(12))
.enter().append("g")
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "rotate(" + ((xScale(d)) * 180 / Math.PI - 90) + ")translate(" + innerRadius + ",0)";
});
// x axis ticks
xTick.append("line")
.attr("x2", -5)
.attr("stroke", "#000");
// formats text on angle
xTick.append("text")
.attr("transform", function(d) {
var angle = xScale(d);
return ((angle < Math.PI / 2) || (angle > (Math.PI * 3 / 2))) ? "rotate(90)translate(0,22)" : "rotate(-90)translate(0, -15)"; })
.text(function(d) {return formatMonth(d);})
.style("font-size", 10)
.attr("opacity", 0.6)
// add the description to the center of the visualization
var title = g.append("g")
.attr("class", "title")
.append("text")
.attr("dy", "-0.2em")
.attr("text-anchor", "middle")
.text("Daily Earnings")
var subtitle = g.append("text")
.attr("dy", "1em")
.attr("text-anchor", "middle")
.attr("opacity", 0.6)
.text("1/1/17 - 12/31/17");
// update which points are highlighted based on the slider value
d3.select("#timeslide").on("input", function() {
update(+this.value);
});
// called when the slider updates it's value
function update(sliderValue) {
document.getElementById("range").innerHTML= "Time Showing: " + months[sliderValue];
currentMonthIndex = sliderValue;
d3.selectAll(".bar,.bar-failed")
.attr("class", monthMatch);
}
// highlight the records in the specific month we're viewing
function monthMatch(data, value) {
if (data.startDate.getMonth() + 1 == currentMonthIndex) {
return "bar-failed";
} else {
return "bar";
};
}
// set up gantt chart
ganttGraph = d3.gantt()
.taskTypes(["1", "2", "3", "4", "5"])
.tickFormat("%m/%d");
ganttGraph(tasks);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment