var bubble = (function(g, w, h) {
var BubbleChart, root,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
BubbleChart = (function() {
function BubbleChart(data) {
this.hide_details = __bind(this.hide_details, this);
this.show_details = __bind(this.show_details, this);
this.hide_caregivers = __bind(this.hide_caregivers, this);
this.display_caregivers = __bind(this.display_caregivers, this);
this.move_towards_caregiver = __bind(this.move_towards_caregiver, this);
this.display_by_caregiver = __bind(this.display_by_caregiver, this);
this.move_towards_center = __bind(this.move_towards_center, this);
this.display_group_all = __bind(this.display_group_all, this);
this.start = __bind(this.start, this);
this.create_vis = __bind(this.create_vis, this);
this.create_nodes = __bind(this.create_nodes, this);
this.max_amount = 6;
this.data = data;
this.width = w;
this.height = h;
//this.tooltip = CustomTooltip("gates_tooltip", 240);
this.center = {
x: this.width / 2,
y: (this.height / 2) + 10
};
this.caregiver_centers = {
"Cyrus": {
x: 220,
y: this.height / 2
},
"Jen": {
x: this.width - 240,
y: (this.height / 2) + 10
}
};
this.layout_gravity = -0.01;
this.damper = 0.1;
this.vis = null;
this.nodes = [];
this.force = null;
this.circles = null;
this.fill_color = d3.scale.ordinal().domain(["low", "medium", "high"]).range(["#d84b2a", "#beccae", "#7aa25c"]);
max_amount = 150;
this.radius_scale = d3.scale.pow().exponent(0.5).domain([0, max_amount]).range([2, 85]);
this.create_nodes();
this.create_vis();
}
BubbleChart.prototype.create_nodes = function() {
this.data.forEach((function(_this) {
return function(d, i) {
var node;
node = {
id: i,
caregiver: d.caregiver,
caregiver_id: d.caregiver_id,
radius: _this.radius_scale(parseInt(Math.floor((Math.random() * _this.max_amount) + 1))),
value: Math.floor((Math.random() * _this.max_amount) + 1),
x: Math.random() * w,
y: Math.random() * h
};
return _this.nodes.push(node);
};
})(this));
return this.nodes.sort(function(a, b) {
return b.value - a.value;
});
};
BubbleChart.prototype.create_vis = function() {
/*var that;
this.vis = g;
this.circles = this.vis.selectAll("circle").data(this.nodes, function(d) {
return d.id;
});
that = this;*/
this.vis = g;
this.circles = g.selectAll("circle");
this.circles.data(this.nodes).enter().append("circle")
.attr("class", "bubble")
.attr("opacity", 0)
.attr("r", 0)
.attr("fill", "#008080")
.attr("stroke-width", 2).attr("stroke", (function(_this) {
return function(d) {
return "#2f4f4f";
};
})(this)).attr("id", function(d) {
return "bubble_" + d.id;
})/*.on("mouseover", function(d, i) {
return that.show_details(d, i, this);
}).on("mouseout", function(d, i) {
return that.hide_details(d, i, this);
});*/
return this.circles.transition().duration(2000).attr("r", function(d) {
return d.radius;
});
};
BubbleChart.prototype.charge = function(d) {
return -Math.pow(d.radius, 2.0) / 8;
};
BubbleChart.prototype.start = function() {
return this.force = d3.layout.force().nodes(this.nodes).size([this.width, this.height]);
};
BubbleChart.prototype.display_group_all = function() {
this.force.gravity(this.layout_gravity).charge(this.charge).friction(0.9).on("tick", (function(_this) {
return function(e) {
return _this.circles.each(_this.move_towards_center(e.alpha)).attr("cx", function(d) {
return d.x;
}).attr("cy", function(d) {
return d.y;
});
};
})(this));
this.force.start();
return this.hide_caregivers();
};
BubbleChart.prototype.move_towards_center = function(alpha) {
return (function(_this) {
return function(d) {
d.x = d.x + (_this.center.x - d.x) * (_this.damper + 0.02) * alpha;
return d.y = d.y + (_this.center.y - d.y) * (_this.damper + 0.02) * alpha;
};
})(this);
};
BubbleChart.prototype.display_by_caregiver = function() {
this.force.gravity(this.layout_gravity).charge(this.charge).friction(0.9).on("tick", (function(_this) {
return function(e) {
return _this.circles.each(_this.move_towards_caregiver(e.alpha)).attr("cx", function(d) {
return d.x;
}).attr("cy", function(d) {
return d.y;
});
};
})(this));
this.force.start();
return this.display_caregivers();
};
BubbleChart.prototype.move_towards_caregiver = function(alpha) {
return (function(_this) {
return function(d) {
var target;
target = _this.caregiver_centers[d.caregiver];
d.x = d.x + (target.x - d.x) * (_this.damper + 0.02) * alpha * 1.1;
return d.y = d.y + (target.y - d.y) * (_this.damper + 0.02) * alpha * 1.1;
};
})(this);
};
BubbleChart.prototype.display_caregivers = function() {
var caregivers, caregivers_data, caregivers_x;
caregivers_x = {
"Cyrus": 100,
"Jen": this.width - 220
};
caregivers_data = d3.keys(caregivers_x);
caregivers = this.vis.selectAll(".caregivers").data(caregivers_data);
return caregivers.enter().append("text").attr("class", "caregivers").attr("x", (function(_this) {
return function(d) {
return caregivers_x[d];
};
})(this)).attr("y", 40).attr("text-anchor", "middle").text(function(d) {
return d;
});
};
BubbleChart.prototype.hide_caregivers = function() {
var caregivers;
return caregivers = g.selectAll(".caregivers").remove();
};
BubbleChart.prototype.show_details = function(data, i, element) {
var content;
d3.select(element).attr("stroke", "black");
content = "Caregiver: " + data.name + "
";
content += "Amount: $" + (addCommas(data.value)) + "
";
content += "Year: " + data.year + "";
return this.tooltip.showTooltip(content, d3.event);
};
BubbleChart.prototype.hide_details = function(data, i, element) {
d3.select(element).attr("stroke", (function(_this) {
return function(d) {
return d3.rgb(_this.fill_color(d.group)).darker();
};
})(this));
return this.tooltip.hideTooltip();
};
BubbleChart.prototype.color_by_caregiver = function() {
this.circles
.transition()
.duration(3000)
.attr("fill", function(d) {
return d.caregiver_id === 0 ? "#D8B365" : "#008080";
});
};
BubbleChart.prototype.color_by_all = function() {
this.circles
.transition()
.duration(800)
.attr("fill", function(d) {
return "#008080";
});
};
return BubbleChart;
})();
function getDiaperData(rawData) {
var diaperData = rawData.filter(function(d) {
return d.Activity.includes('Diaper');
});
diaperData.forEach(function(d) {
d.caregiver_id = (d.Caregiver.includes('Cyrus') ? 0 : 1);
d.caregiver = (d.Caregiver.includes('Cyrus') ? "Cyrus" : "Jen");
});
return diaperData;
}
root = typeof exports !== "undefined" && exports !== null ? exports : this;
$(function() {
var chart, render_vis;
chart = null;
render_vis = function(csv) {
var diaperdata = getDiaperData(csv);
chart = new BubbleChart(diaperdata);
chart.start();
//return root.display_all();
};
root.display_all = (function(_this) {
return function() {
return chart.display_group_all();
};
})(this);
root.display_caregivers = (function(_this) {
return function() {
return chart.display_by_caregiver();
};
})(this);
root.color_by_caregiver = (function(_this) {
return function() {
return chart.color_by_caregiver();
};
})(this);
root.color_by_all = (function(_this) {
return function() {
return chart.color_by_all();
};
})(this);
root.toggle_view = (function(_this) {
return function(view_type) {
if (view_type === 'caregiver') {
return root.display_caregivers();
} else {
return root.display_all();
}
};
})(this);
return d3.csv("changings.csv", render_vis);
});
return root;
});