Spaces:
Running
Running
Commit
·
d9f4ab9
1
Parent(s):
542c4a7
Colores de nodos desde CSV
Browse files
script.js
CHANGED
|
@@ -899,13 +899,14 @@ function exportToCSV() {
|
|
| 899 |
return;
|
| 900 |
}
|
| 901 |
|
| 902 |
-
let csvContent = "Source,Target,Value\r\n";
|
| 903 |
sankeyData.links.forEach(link => {
|
| 904 |
const src = (sankeyData.nodes[link.source] && sankeyData.nodes[link.source].name) || "";
|
| 905 |
const tgt = (sankeyData.nodes[link.target] && sankeyData.nodes[link.target].name) || "";
|
| 906 |
const val = link.value || 0;
|
|
|
|
| 907 |
// Escape quotes
|
| 908 |
-
csvContent += `"${src.replace(/"/g,'""')}","${tgt.replace(/"/g,'""')}",${val}\r\n`;
|
| 909 |
});
|
| 910 |
|
| 911 |
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
|
|
@@ -949,6 +950,7 @@ function importFromCSV(file) {
|
|
| 949 |
const sourceIdx = header.indexOf('Source');
|
| 950 |
const targetIdx = header.indexOf('Target');
|
| 951 |
const valueIdx = header.indexOf('Value');
|
|
|
|
| 952 |
|
| 953 |
if (sourceIdx === -1 || targetIdx === -1 || valueIdx === -1) {
|
| 954 |
alert("CSV must have columns: Source, Target, Value");
|
|
@@ -970,6 +972,7 @@ function importFromCSV(file) {
|
|
| 970 |
const sourceName = values[sourceIdx];
|
| 971 |
const targetName = values[targetIdx];
|
| 972 |
const value = parseFloat(values[valueIdx]);
|
|
|
|
| 973 |
|
| 974 |
if (isNaN(value)) continue;
|
| 975 |
|
|
@@ -991,7 +994,7 @@ function importFromCSV(file) {
|
|
| 991 |
nodeMap[sourceName] = nodeIndex++;
|
| 992 |
sankeyData.nodes.push({
|
| 993 |
name: sourceName,
|
| 994 |
-
color: "#4f46e5",
|
| 995 |
value: 0 // Will be updated later
|
| 996 |
});
|
| 997 |
}
|
|
|
|
| 899 |
return;
|
| 900 |
}
|
| 901 |
|
| 902 |
+
let csvContent = "Source,Target,Value,Color\r\n";
|
| 903 |
sankeyData.links.forEach(link => {
|
| 904 |
const src = (sankeyData.nodes[link.source] && sankeyData.nodes[link.source].name) || "";
|
| 905 |
const tgt = (sankeyData.nodes[link.target] && sankeyData.nodes[link.target].name) || "";
|
| 906 |
const val = link.value || 0;
|
| 907 |
+
const color = (sankeyData.nodes[link.source] && sankeyData.nodes[link.source].color) || "";
|
| 908 |
// Escape quotes
|
| 909 |
+
csvContent += `"${src.replace(/"/g,'""')}","${tgt.replace(/"/g,'""')}",${val},"${color}"\r\n`;
|
| 910 |
});
|
| 911 |
|
| 912 |
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
|
|
|
|
| 950 |
const sourceIdx = header.indexOf('Source');
|
| 951 |
const targetIdx = header.indexOf('Target');
|
| 952 |
const valueIdx = header.indexOf('Value');
|
| 953 |
+
const colorIdx = header.indexOf('Color');
|
| 954 |
|
| 955 |
if (sourceIdx === -1 || targetIdx === -1 || valueIdx === -1) {
|
| 956 |
alert("CSV must have columns: Source, Target, Value");
|
|
|
|
| 972 |
const sourceName = values[sourceIdx];
|
| 973 |
const targetName = values[targetIdx];
|
| 974 |
const value = parseFloat(values[valueIdx]);
|
| 975 |
+
const color = colorIdx !== -1 ? values[colorIdx] : "";
|
| 976 |
|
| 977 |
if (isNaN(value)) continue;
|
| 978 |
|
|
|
|
| 994 |
nodeMap[sourceName] = nodeIndex++;
|
| 995 |
sankeyData.nodes.push({
|
| 996 |
name: sourceName,
|
| 997 |
+
color: color && /^#[0-9A-Fa-f]{6}$/.test(color.trim()) ? color.trim() : "#4f46e5",
|
| 998 |
value: 0 // Will be updated later
|
| 999 |
});
|
| 1000 |
}
|