Refactor code

This commit is contained in:
niclas 2024-02-07 11:29:15 +01:00
parent b6ef08a664
commit ce55d8799d
2 changed files with 10 additions and 20 deletions

View file

@ -345,7 +345,6 @@ def create_index(galaxies):
def get_top_x(dict, x, big_to_small=True): def get_top_x(dict, x, big_to_small=True):
sorted_dict = sorted(dict.items(), key=operator.itemgetter(1), reverse=big_to_small)[:x] sorted_dict = sorted(dict.items(), key=operator.itemgetter(1), reverse=big_to_small)[:x]
top_x = [key for key, value in sorted_dict] top_x = [key for key, value in sorted_dict]
# top_x = ", ".join(top_x)
top_x_values = sorted(dict.values(), reverse=big_to_small)[:x] top_x_values = sorted(dict.values(), reverse=big_to_small)[:x]
return top_x, top_x_values return top_x, top_x_values

View file

@ -4,7 +4,6 @@ document$.subscribe(function () {
const NODE_COLOR = "#69b3a2"; const NODE_COLOR = "#69b3a2";
const Parent_Node_COLOR = "#ff0000"; const Parent_Node_COLOR = "#ff0000";
// Function to parse the table data
function parseFilteredTable(tf) { function parseFilteredTable(tf) {
var data = []; var data = [];
tf.getFilteredData().forEach((row, i) => { tf.getFilteredData().forEach((row, i) => {
@ -14,7 +13,6 @@ document$.subscribe(function () {
return data; return data;
} }
// Function to parse table data wthout filtering
function parseTable(table) { function parseTable(table) {
var data = []; var data = [];
table.querySelectorAll("tr").forEach((row, i) => { table.querySelectorAll("tr").forEach((row, i) => {
@ -27,16 +25,20 @@ document$.subscribe(function () {
} }
function processNewData(newData) { function processNewData(newData) {
// Extracting new nodes
var newNodes = Array.from(new Set(newData.flatMap(d => [d.source, d.target]))) var newNodes = Array.from(new Set(newData.flatMap(d => [d.source, d.target])))
.map(id => ({ id })); .map(id => ({ id }));
// Preparing the links in the required format
var newLinks = newData.map(d => ({ source: d.source, target: d.target })); var newLinks = newData.map(d => ({ source: d.source, target: d.target }));
return { newNodes, newLinks }; return { newNodes, newLinks };
} }
// Function to create Force-Directed Graph function filterTableAndGraph(tf, simulation) {
var filteredData = parseFilteredTable(tf);
var { newNodes, newLinks } = processNewData(filteredData);
simulation.update({ newNodes: newNodes, newLinks: newLinks });
}
function createForceDirectedGraph(data, elementId) { function createForceDirectedGraph(data, elementId) {
// Extract nodes and links // Extract nodes and links
var nodes = Array.from(new Set(data.flatMap(d => [d.source, d.target]))) var nodes = Array.from(new Set(data.flatMap(d => [d.source, d.target])))
@ -51,7 +53,6 @@ document$.subscribe(function () {
// Set up the dimensions of the graph // Set up the dimensions of the graph
var width = 1000, height = 1000; var width = 1000, height = 1000;
// Append SVG for the graph
var svg = d3.select(elementId).append("svg") var svg = d3.select(elementId).append("svg")
.attr("width", width) .attr("width", width)
.attr("height", height); .attr("height", height);
@ -114,7 +115,6 @@ document$.subscribe(function () {
// Apply drag behavior to nodes // Apply drag behavior to nodes
node.call(drag); node.call(drag);
// Drag functions
function dragstarted(event, d) { function dragstarted(event, d) {
if (!event.active) simulation.alphaTarget(0.3).restart(); if (!event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x; d.fx = d.x;
@ -250,26 +250,17 @@ document$.subscribe(function () {
}); });
tf.init(); tf.init();
// var data = parseFilteredTable(tf);
var data = parseTable(table); var data = parseTable(table);
var graphId = "graph" + index; var graphId = "graph" + index;
var div = document.createElement("div"); var div = document.createElement("div");
div.id = graphId; div.id = graphId;
// table.after(div);
table.parentNode.insertBefore(div, table); table.parentNode.insertBefore(div, table);
var simulation = createForceDirectedGraph(data, "#" + graphId); var simulation = createForceDirectedGraph(data, "#" + graphId);
// Function to filter the table data and update the graph
function filterTableAndGraph() {
var filteredData = parseFilteredTable(tf);
var { newNodes, newLinks } = processNewData(filteredData);
// Restart the simulation with filtered data
simulation.update({ newNodes: newNodes, newLinks: newLinks });
}
// Listen for table filtering events // Listen for table filtering events
tf.emitter.on(['after-filtering'], filterTableAndGraph); tf.emitter.on(['after-filtering'], function () {
filterTableAndGraph(tf, simulation);
});
} }
}); });
}); });