Skip to content

Instantly share code, notes, and snippets.

lvngd / rectCollide.js
Created September 22, 2021 16:00
rectCollide for D3 V6
View rectCollide.js
function rectCollide() {
var nodes,sizes,masses;
var strength = 1;
var iterations = 1;
var nodeCenterX;
var nodeMass;
var nodeCenterY;
function force() {
View albumYear.js
let yearAlbum = {};
let yearCount = new Map();
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[i].bandAlbums.length; j++) {
if (!yearCount.has(data[i].bandAlbums[j].albumYear)) {
yearCount.set(`${data[i].bandAlbums[j].albumYear}`, 1)
} else {
yearCount.set(`${data[i].bandAlbums[j].albumYear}`, yearCount.get(`${data[i].bandAlbums[j].albumYear}`) + 1)
lvngd / jsonToPostgres.js
Last active March 25, 2021 16:15
Code to insert darklyrics JSON data into postgres using Node + Knex
View jsonToPostgres.js
const knex = require('knex')({
client: 'pg',
connection: 'postgresql://darklyricsuser:darklyricspassword@localhost/darklyrics',
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('/Users/christina/Downloads/results(1).json', 'utf8'));
async function insertBand(band) {
lvngd / createTables.js
Last active March 25, 2021 16:18
Node + Knex code to create postgres tables for darklyrics data
View createTables.js
1st create database:
-go into the interactive interpreter
psql postgres
Here we're creating:
1. a databased called 'darklyrics'
2. a user 'darklyricsuser'
3. with password 'darklyricspassword'
lvngd / d3_random_subsets.js
Created March 10, 2021 19:52
Demo of D3 data joins - displaying random subsets from a grid of circles.
View d3_random_subsets.js
<!DOCTYPE html>
<meta charset="utf-8">
<script src=""></script>
<div id="chart"></div>
View portuguese_stop_words
View english_stop_words
View wordCountExample.js
let words = ['cat','dog','mouse','rabbit','dog','dog','mouse','cat'];
let wordsSet = new Set(words)
let wordMap = new Map();
for(word in words){
let current = words[word];
if (wordMap.has(current)){
//is in the map, so get the current value and increment by one
View gist:812bfe687e16dcce0188da753ae4ae22
let svg ='#chart')
.classed('svg-container', true)
.attr("preserveAspectRatio", "xMinYMin meet")
.attr("viewBox", `0 0 ${width} ${height}`)
.classed('svg-content-responsive', true)
.attr('transform', `translate(${margin.left}, ${})`)
View flattenJSON.js
function flattenData(data){
let flatData = [];
data.forEach(band => {
band.bandAlbums.forEach(album => {
album.albumSongs.forEach(song =>{
flatData.push({bandName: band.bandName,
bandUrl: band.bandUrl,
albumName: album.albumName,
albumType: album.albumType,
albumYear: album.albumYear,