Published
Edited
Mar 7, 2022
1 fork
2 stars
Insert cell
Insert cell
{
let data_structure = {
bars : [...new Set(finalData.map(d => d.brandCode))],
bar_names: [...new Set(finalData.map(d => d.brand))],
bar_counts : [],
bar_rows : []
}
return data_structure
}
Insert cell
data_structure = []
Insert cell
{
let data_structure = {
bars : [...new Set(finalData.map(d => d.brandCode))],
bar_names: [...new Set(finalData.map(d => d.brand))],
bar_counts : [],
bar_rows : []
}
for (var i = 0; i < data_structure.bars.length; i++) {
data_structure.bar_counts[i] = finalData.map(d => d.brandCode).reduce(function(n, val) {
console.log(val)
return n + (val === data_structure.bars[i]);
}, 0);
data_structure.bar_rows[i] = Math.ceil(data_structure.bar_counts[i] / 5);
}
return data_structure
}
Insert cell
chart = {
const GRID_SIZE = 15; // controls how much space there is between each square
const GRID_COLS = 4;
let data_structure = {
bars : [...new Set(data.map(d => d.brandCode))],
bar_names: [...new Set(data.map(d => d.brand))],
bar_counts : [],
bar_rows : []
}
for (var i = 0; i < data_structure.bars.length; i++) {
data_structure.bar_counts[i] = data.map(d => d.brandCode).reduce(function(n, val) {
return n + (val === data_structure.bars[i]);
}, 0);
data_structure.bar_rows[i] = Math.ceil(data_structure.bar_counts[i] / GRID_COLS);
}
const sorted = data_structure.bars.map((d,i) => { return {
bar: d,
bar_names: data_structure.bar_names[i],
bar_counts: data_structure.bar_counts[i],
bar_rows: data_structure.bar_rows[i]
}}).sort(function(a,b) {
return b.bar_counts - a.bar_counts;
})
data_structure = {
bars: sorted.map(d => d.bar),
bar_names: sorted.map(d => d.bar_names),
bar_counts: sorted.map(d => d.bar_counts),
bar_rows: sorted.map(d => d.bar_rows)
}
console.log(data_structure)

// var search = 0;
// let count = data.nodes.map(node => node.group).reduce(function(n, val) {
// return n + (val === search);
// }, 0);
// console.log(count)
// const GRID_COLS = 6;
const GRID_ROWS = Math.ceil(data.length / GRID_COLS);
console.log(GRID_ROWS)
let grid = {
cells : [],
init : function() {
this.cells = {};
for (var bar = 0; bar < data_structure.bars.length; bar++) {
let curr_g =
this.cells[bar] = [];
let bar_cells = [];
let cells_count = data_structure.bar_counts[bar];
let start_x = bar * (GRID_COLS+1) * GRID_SIZE;
for(var r = 0; r < GRID_ROWS; r++) {
for(var c = 0; c < GRID_COLS; c++) {
if (cells_count <= 0) continue;

var cell;
cell = {
x : start_x + c * GRID_SIZE,
y : height - r * GRID_SIZE,
occupied : false
};
this.cells[bar].push(cell);
// bar_cells.push(cell);
cells_count--;
};
};
}
},
sqdist : function(a, b) {
return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2);
},

occupyNearest : function(p) {
// if (p.group != 0) return null;
let bar_i = data_structure.bars.indexOf(p.brandCode);
var minDist = 1000000;
var d;
var candidate = null;
for(var i = 0; i < this.cells[bar_i].length; i++) {
if(!this.cells[bar_i][i].occupied && ( d = this.sqdist(p, this.cells[bar_i][i])) < minDist) {
minDist = d;
candidate = this.cells[bar_i][i];
}
}
if(candidate)
candidate.occupied = true;
return candidate;
}
}
// grid.init();
// const nodes = data.map(d => Object.create(d)); //this doesn't seem to be necessary!

const simulation = d3.forceSimulation(data)
.force("center", d3.forceCenter(width / 2, height / 2))
const svg = d3.select(DOM.svg(width, height));

// CIRCLE VERSION
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(data)
.join("circle")
.attr("r", 5)
.attr("fill", color)
// .call(drag(simulation));

node.append("title")
.text(d => d.brand);
simulation.on("tick", () => {
grid.init();
node
.each(function(d) {
let gridpoint = grid.occupyNearest(d);
if (gridpoint) {
// ensures smooth movement towards final positoin
d.x += (gridpoint.x - d.x) * .05;
d.y += (gridpoint.y - d.y) * .05;
// jumps directly into final position
// d.x = gridpoint.x;
// d.y = gridpoint.y
}
})
.attr("cx", d => d.x)
.attr("cy", d => d.y);
});
// // SQUARE VERSION
// const node = svg.append('g')
// .attr("stroke", "#fff")
// .attr("stroke-width", 1.5)
// .selectAll('path')
// .data(data)
// .join('path')
// .attr('d', d => square(d))
// .attr('fill', d => fillScale(d.Perc_Tourist))
// .call(drag(simulation))
// simulation.on('tick', () => {
// grid.init();
// node
// .each(function(d) {
// let gridpoint = grid.occupyNearest(d);
// if (gridpoint) {
// // ensures smooth movement towards final positoin
// d.x += (gridpoint.x - d.x) * .05;
// d.y += (gridpoint.y - d.y) * .05;
// // jumps directly into final position
// // d.x = gridpoint.x;
// // d.y = gridpoint.y
// }
// })
// .attr("x", d => d.x)
// .attr("y", d => d.y)
// .attr('transform', d => `translate(${[d.x, d.y]})`);
// });

invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
Insert cell
Insert cell
data_raw = FileAttachment("Test_Tile_Data@2.csv").csv()
Insert cell
data = data_raw.map(d => { return {
muni : d.Municipality,
codeINE: d.INECode,
codeIdescat : d.IdescatCode,
centroix : +d.V1,
centroiy : +d.V2,
brand : d.brand,
brandCode : +d.brandCode,
Perc_Tourist: +d.Perc_TuristicHousehols
}})
Insert cell
finalData_raw = FileAttachment("RealData.csv").csv()
Insert cell
finalData = finalData_raw.map(d => { return {
Perc_Tourist: +d.Perc_TuristicHouseholds_INE,
mapX: +d.x,
mapY: +d.y,
INECode: d.INECode,
IdescatCode: d.IdescatCode,
municipality: d.Municipality,
brand: d.brand,
province: d.province,
population: +d.Population,
perc_Airbnb: +d.perc_AirbnbOk,
airbnb: +d.airbnb,
brandCode: brandCodes[d.brand],
chunk: d.airbnb > 0 ? 1 : 0
}})
Insert cell
brandCodes = { return {
'Pirineus': 1,
'Costa Barcelona': 2,
'Terres de Lleida': 3,
'Paisatges de Barcelona': 4,
'Costa Brava': 5,
'Costa Daurada': 6,
"Terres de l'Ebre": 7,
"Val d'Aran": 8,
'Barcelona': 9
}}
Insert cell
Insert cell
fillScale = d3.scaleSequential(chroma.scale(["#fb248b", "#20bac3"]))
Insert cell
chroma.scale(["#fb248b", "#20bac3"])
Insert cell
Insert cell
height = 600
Insert cell
color = {
const scale = d3.scaleOrdinal(d3.schemeSet2);
return d => scale(d.brand);
}
Insert cell
drag = simulation => {
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
Insert cell
d3 = require('d3@6')
Insert cell
Insert cell
chroma = require('chroma-js')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more