Skip to content

Instantly share code, notes, and snippets.

@yaph
Last active December 19, 2015 06:49
Show Gist options
  • Save yaph/5913997 to your computer and use it in GitHub Desktop.
Save yaph/5913997 to your computer and use it in GitHub Desktop.
Word cloud from words in top 100 city names.
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://www.jasondavies.com/wordcloud/d3.layout.cloud.js"></script>
<script src="script.js"></script>
$('.content_table tr').each(function(idx, item) { if ('undefined' !== typeof $(item).find('td')[1]) console.log($($(item).find('td')[1]).text()) } )
(function() {
var words = [
"San Jose", "San Antonio", "Santa Rosa", "Santa Cruz", "San Juan", "San Miguel", "Santa Maria", "San Pedro", "San Francisco", "San Isidro", "Buena Vista", "Santa Ana", "San Rafael", "Santa Rita", "Santa Lucia", "Buenavista", "San Vicente", "San Luis", "Santa Barbara", "San Lorenzo", "La Esperanza", "Santo Domingo", "San Pablo", "Buenos Aires", "Concepcion", "Phumi Ba", "San Agustin", "Santa Teresa", "Santa Elena", "Victoria", "Santa Clara", "Santiago", "San Nicolas", "San Carlos", "Santa Isabel", "San Andres", "San Roque", "San Martin", "El Carmen", "Pueblo Nuevo", "Santa Fe", "San Felipe", "Rosario", "La Union", "La Laguna", "Esperanza", "El Porvenir", "La Palma", "San Ramon", "El Rosario", "Las Delicias", "La Florida", "San Fernando", "San Joaquin", "Ojo De Agua", "Bella Vista", "La Cruz", "San Cristobal", "El Palmar", "El Limon", "Guadalupe", "El Paraiso", "El Rincon", "Las Palmas", "La Ceiba", "Santo Tomas", "San Marcos", "La Loma", "Belen", "Las Mercedes", "Las Flores", "Mikhaylovka", "Union", "Manga", "La Paz", "Carrizal", "San Ignacio", "La Victoria", "Kamenka", "Paraiso", "Aleksandrovka", "El Rodeo", "Santa Ines", "Ivanovka", "Nikolayevka", "Las Cruces", "Miraflores", "San Jeronimo", "Soledad", "Candelaria", "Alekseyevka", "Berezovka", "A", "La Concepcion", "Los Angeles", "El Naranjo", "Bellavista", "Dolores", "Carmen", "San Sebastian"
];
var fill = d3.scale.category20();
wordmap = {};
words.map(function(d) {
word_list = d.split(' ');
for (i in word_list) {
w = word_list[i];
if (!wordmap.hasOwnProperty(w)) {
wordmap[w] = 0;
}
wordmap[w]++;
}
});
d3.layout.cloud().size([900, 600])
.words(d3.entries(wordmap).map(function(d) {
return {text: d.key, size: d.value};
}))
.padding(1)
.rotate(0)
.font("Impact")
.fontSize(function(d) { return d.size * 10; })
.on("end", draw)
.start();
function draw(words) {
d3.select("body").append("svg")
.attr("width", 900)
.attr("height", 600)
.append("g")
.attr("transform", "translate(450,300)")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) { return d.size + "px"; })
.style("font-family", "Impact")
.style("fill", function(d, i) { return fill(i); })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.text; });
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment