Skip to content

Instantly share code, notes, and snippets.

@buesing
Created May 23, 2015 15:34
Show Gist options
  • Save buesing/1ee3ec078d98f23a8a63 to your computer and use it in GitHub Desktop.
Save buesing/1ee3ec078d98f23a8a63 to your computer and use it in GitHub Desktop.
// Generated by CoffeeScript 1.4.0
(function() {
var $s, all_clusterings, data, data_source, height, k, mds, width;
$s = function(elem) {
return $(document.createElementNS('http://www.w3.org/2000/svg', elem));
};
width = $(window).width();
height = $(window).height();
d3.select("svg").attr("width", width).attr("height", height);
data = [];
all_clusterings = [];
mds = [];
data_source = "wine.data";
$.when((function() {
var _i, _results;
_results = [];
for (k = _i = 2; _i <= 6; k = ++_i) {
$.getJSON("rest.php", {
action: "kmeans",
algorithm: "Lloyd",
k: k,
data: data_source
}, function(json_data) {
return all_clusterings.push(json_data);
});
_results.push($.getJSON("rest.php", {
action: "kmeans",
algorithm: "Hartigan-Wong",
k: k,
data: data_source
}, function(json_data) {
return all_clusterings.push(json_data);
}));
}
return _results;
})(), $.getJSON("rest.php", {
action: "mds",
k: "5",
data: "wine.data"
}, function(json_data) {
mds = json_data.points;
return console.log("mds done");
})).then(function() {
var color, current_clustering, get_symbol, render, ri_matrix, xMap, xScale, xValue, yMap, yScale, yValue;
xValue = function(d) {
return d[0];
};
xScale = d3.scale.linear().range([0, width]);
xMap = function(d) {
return xScale(xValue(d));
};
yValue = function(d) {
return d[1];
};
yScale = d3.scale.linear().range([0, height]);
yMap = function(d) {
return yScale(yValue(d));
};
xScale.domain([d3.min(mds, xValue) - 20, d3.max(mds, xValue) + 20]);
yScale.domain([d3.min(mds, yValue) - 20, d3.max(mds, yValue) + 20]);
color = d3.scale.ordinal().range(["#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6", "#6a3d9a", "#ffff99", "#b15928"]);
render = function(cluster_i) {
var svg;
svg = d3.select("#cluster-rand");
svg.html("");
return svg.selectAll("path").data(mds).enter().append("path").attr("class", function(d, i) {
return "path" + all_clusterings[cluster_i].indices[i];
}).attr("data-cluster", function(d, i) {
return all_clusterings[cluster_i].indices[i];
}).attr("transform", function(d, i) {
return "translate(" + xMap(d) + "," + yMap(d) + ")";
}).attr("d", function(d, i) {
return get_symbol(0);
}).attr("fill", function(d, i) {
return color(all_clusterings[cluster_i].indices[i]);
});
};
get_symbol = function(i) {
return d3.svg.symbol().type(d3.svg.symbolTypes[i]).size(80);
};
ri_matrix = [];
current_clustering = 0;
render(current_clustering);
return $("#cluster-rand").click(function() {
current_clustering = (current_clustering + 1) % all_clusterings.length;
return render(current_clustering);
});
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment