New to Javascript, I’m trying to adapt a script used to display SVG images, currently the script reads the values form within the script, I want to adapt this to read the values from a CSV, the issue I have is the script doesn’t seem to interpret the value correctly, the column I’m having issues with is "nodeStyle" ,if I change the column variable from nodeStyle to {width:100,height:100} it works but it wont read it from CSV, any help would be great
//Data.CSV format
//id, x, y, href, nodeStyleW,nodeStyle,properties,label_text
//NODE22,650,50,/icon.png,{width:100,height:100},,NODE22
fetch('/data.csv')
.then(response => response.text())
.then(data => {
const rows = data.split('n').slice(1); // Skip header row
rows.forEach(row => {
const [id, x, y, href, nodeStyle, properties, label_text] = row.split(/,(?![^{]*})/);
net.addNode(id,parseInt(x),parseInt(y),href,nodeStyle,properties,label_text);
});
});
// Mode added via script
net.addNode("NODE2",600,50,"/images/icon.png",{width:100,height:100},"","","NODE2");
I have tried adding the value directly and this works
net.addNode(id,parseInt(x),parseInt(y),href,nodeStyle,properties,label_text);
net.addNode(id,parseInt(x),parseInt(y),href,{width:100,height:100},properties,label_text);
2
Answers
Resolved - Changing the code fixed the issue , I just needed to amended the CSV to {"width":100,"height":100}
It seems like you’re encountering issues parsing the nodeStyle from your CSV file correctly. The problem likely lies in how you’re representing the nodeStyle in your CSV and how you’re interpreting it in your JavaScript code.
In your CSV file, you seem to be attempting to represent nodeStyle as an object, but CSV doesn’t inherently support nested structures like JavaScript objects. Therefore, you need to find an appropriate representation for nodeStyle that you can easily parse in your JavaScript code.
One approach could be to represent nodeStyle as a string in a format that you can later parse in your JavaScript code. For example, instead of writing
directly in the CSV, you could represent it as a JSON string like
Here’s how you can modify your code to achieve this:
With this modification, your CSV would look like:
This way, you’re representing nodeStyle as a JSON string in the CSV, and then parsing it back to an object in your JavaScript code before passing it to net.addNode(). This should resolve your issue with interpreting the nodeStyle correctly from the CSV.
Good luck!