Skip to content

Commit

Permalink
DK byor tsv download (#798)
Browse files Browse the repository at this point in the history
* temp save

* Download section data in TSV implemented
  • Loading branch information
dkjang authored Nov 27, 2024
1 parent 98a71ee commit 17a60f2
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/components/researchPortal/ResearchDataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@
>
CSV
</div>
<div
class="convert-2-csv btn-sm"
@click="convertJson2Tsv(filteredData, pageID + sectionId + '_filtered')"
>
TSV
</div>
<div
class="convert-2-csv btn-sm"
@click="saveJson(filteredData, pageID + sectionId + '_filtered')"
Expand Down Expand Up @@ -1176,6 +1182,9 @@ export default Vue.component("research-data-table", {
//next convert json to csv
this.utils.uiUtils.saveByorCsv(jsonData, FILENAME);
},
convertJson2Tsv(DATA, FILENAME) {
this.utils.uiUtils.saveByorTsv(DATA, FILENAME);
},
saveJson(DATA, FILENAME) {
this.utils.uiUtils.saveJson(DATA, FILENAME);
},
Expand Down
131 changes: 131 additions & 0 deletions src/utils/uiUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,136 @@ let saveByorCsv = function (DATA, FILENAME) {
document.body.removeChild(downloadLink);
};

let saveByorTsv = function (DATA, FILENAME) {
let topRows = [];
let features = {};
const downloadFilename = FILENAME || "download";

DATA.map((d) => {
for (let [key, value] of Object.entries(d)) {
if (key != "features") {
if (typeof value == "object" && !value.length) {
for (let [vKey, vValue] of Object.entries(value)) {
let headerKey = key + "__" + vKey;
if (!topRows.includes(headerKey)) {
topRows.push(headerKey);
}
}
} else {
if (!topRows.includes(key)) {
topRows.push(key);
}
}
} else {
for (let [fKey, fValue] of Object.entries(value)) {
if (!features[fKey]) {
features[fKey] = [];
}

for (let [fVKey, fVValue] of Object.entries(fValue[0])) {
if (!features[fKey].includes(fVKey)) {
features[fKey].push(fVKey);
}
}
}
}
}
});

let csvData = [];

if (Object.keys(features).length > 0) {
DATA.map((d) => {
for (let [fKey, fValue] of Object.entries(features)) {
for (let i = 0; i < d.features[fKey].length; i++) {
let dArr = [];

topRows.map((t) => {
let path = t.split("__");
if (path.length == 2) {
let cValue =
d[path[0]][path[1]] !== null
? d[path[0]][path[1]] === 0
? 0
: d[path[0]][path[1]]
: "";
dArr.push(cValue);
} else {
let cValue =
d[path[0]] !== null
? d[path[0]] === 0
? 0
: d[path[0]]
: "";
dArr.push(cValue);
}
});

for (let [fRKey, fRValue] of Object.entries(features)) {
if (fRKey == fKey) {
features[fRKey].map((cKey) => {
dArr.push(d.features[fRKey][i][cKey]);
});
} else {
features[fRKey].map((cKey) => {
dArr.push("");
});
}
}
csvData.push(dArr);
}
}
});
} else if (Object.keys(features).length == 0) {
DATA.map((d) => {
let dArr = [];
topRows.map((t) => {
let path = t.split("__");
if (path.length == 2) {
let cValue =
d[path[0]][path[1]] !== null
? d[path[0]][path[1]] === 0
? 0
: d[path[0]][path[1]]
: "";
dArr.push(cValue);
} else {
let cValue =
d[path[0]] !== null
? d[path[0]] === 0
? 0
: d[path[0]]
: "";
dArr.push(cValue);
}
});
csvData.push(dArr);
});
}

let header = topRows;

for (let [fKey, fValue] of Object.entries(features)) {
fValue.map((fCKey) => {
header.push("feature__" + fKey + "__" + fCKey);
});
}

let csv = [
header.join("\t"), // header row first
...csvData.map((row) => row.join("\t")),
].join("\r\n");

let downloadLink = document.createElement("a");
let blob = new Blob(["\ufeff", csv]);
let url = URL.createObjectURL(blob);
downloadLink.href = url;
downloadLink.download = downloadFilename + ".tsv"; //Name the file here
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
};

let convertJson2Csv = function (DATA, FILENAME) {
const items = DATA;
const downloadFilename = FILENAME || "download";
Expand Down Expand Up @@ -644,6 +774,7 @@ export default {
convertJson2Csv,
convertJson2Tsv,
saveByorCsv,
saveByorTsv,
saveJson,
getAxisTicks,
showTabContent,
Expand Down

0 comments on commit 17a60f2

Please sign in to comment.