chart = {
const GRID_SIZE = 15;
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();
}