Skip to content

Commit

Permalink
[feature] export all statistics tabs into one CSV file #47
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-rind committed Dec 5, 2021
1 parent c8e22e2 commit c11831c
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/assets/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function download(filename: string, dataURL: string): void {
export function downloadText(filename: string, text: string): void {
download(
filename,
"data:text/plain;charset=utf-8," + encodeURIComponent(text)
"data:text/plain;charset=utf-8,\ufeff" + encodeURIComponent(text)
);
}

Expand Down
26 changes: 19 additions & 7 deletions src/components/SideMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@
<span class="icon">
<font-awesome-icon icon="file-image" />
</span>
<span>PNG speichern</span>
<span>PNG exportieren</span>
</button>

<button class="button" @click="exportCSV">
<span class="icon">
<font-awesome-icon icon="file-csv" />
</span>
<span>Kennzahlen exportieren</span>
</button>

<p><br /></p>
Expand Down Expand Up @@ -103,6 +110,7 @@ import { computed, defineComponent, ref } from "vue";
// @ is an alias to /src
import { useStore } from "@/store";
import { downloadSVGasPNG, downloadText } from "@/assets/utils";
import { statisticsCSV } from "@/data/statisticsCSV";
export default defineComponent({
setup(props, { emit }) {
Expand Down Expand Up @@ -157,10 +165,6 @@ export default defineComponent({
});
};
const exportPNG = () => {
downloadSVGasPNG(store.state.nwk.ego.name + ".png", "svg#nwkmap");
};
const horizons = computed(() => store.state.view.horizons);
const toggleHorizons = () => {
Expand All @@ -176,7 +180,15 @@ export default defineComponent({
open,
save,
openDemoData,
exportPNG,
exportPNG: () => {
downloadSVGasPNG(store.state.nwk.ego.name + ".png", "svg#nwkmap");
},
exportCSV: () => {
downloadText(
store.state.nwk.ego.name + ".csv",
statisticsCSV(store.state.nwk)
);
},
showStatistics: () => {
store.commit("view/enable", "statistics");
Expand Down Expand Up @@ -227,7 +239,7 @@ export default defineComponent({
#sidepanel.shown {
transition: visibility 0s, width 1s; /* 1 second transition effect to slide in the sidepanel */
visibility: visible;
width: 250px;
width: 280px;
}
/* The sidepanel links */
Expand Down
11 changes: 9 additions & 2 deletions src/components/StatisticsTable.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="panel-block" style="display: block">
<table class="table">
<table class="table is-fullwidth">
<!-- <thead>
<tr>
<th><abbr title="Position">Pos</abbr></th>
Expand All @@ -18,7 +18,14 @@
</tr>
<tr>
<th>Dichte gesamt</th>
<td>{{ density.toFixed(3) }}</td>
<td>
{{
density.toLocaleString(undefined, {
minimumFractionDigits: 3,
maximumFractionDigits: 3,
})
}}
</td>
</tr>
<tr>
<th>Star(s)</th>
Expand Down
10 changes: 8 additions & 2 deletions src/components/StatisticsTableCategories.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="panel-block" style="display: block">
<table class="table">
<table class="table is-fullwidth">
<thead>
<tr>
<th></th>
Expand Down Expand Up @@ -124,7 +124,13 @@ export default defineComponent({
for (const cat of categoryLabels.value) {
const { alterConnectable, intConnCount } = getOrInit(analysis, cat);
result.push(
calculateDensity(alterConnectable, intConnCount).toFixed(3)
calculateDensity(alterConnectable, intConnCount).toLocaleString(
undefined,
{
minimumFractionDigits: 3,
maximumFractionDigits: 3,
}
)
);
}
return result;
Expand Down
120 changes: 120 additions & 0 deletions src/data/statisticsCSV.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import {
allAlterCategorizationKeys,
getAlterCategorization,
} from "@/data/AlterCategories";
import {
analyseNWKbyCategory,
calculateDensity,
getOrInit,
NetworkAnalysis,
} from "@/data/NetworkAnalysis";
import { NWK } from "@/data/NWK";

const SEP = ";";

export function statisticsCSV(nwk: NWK): string {
let output = "Auswertung" + SEP + nwk.ego.name;

for (const cat of allAlterCategorizationKeys) {
// loop each tab of the statistics panel (below each other on single sheet)
const categorization = getAlterCategorization(cat);
const networkAnalysis = analyseNWKbyCategory(nwk, categorization);

output += "\n\n" + categorization.label + "\n\n";

output += "Kennzahl";
for (const label of categorization.categories) {
output += SEP + label;
}

output += "\nNetzwerkgröße";
for (const label of categorization.categories) {
output +=
SEP + getOrInit(networkAnalysis, label).alterConnected.toFixed(0);
}

output += "\nBeziehungsgewicht";
for (const label of categorization.categories) {
output += SEP + getOrInit(networkAnalysis, label).naehenSum.toFixed(0);
}

output += "\nDichte der Kategorie";
for (const label of categorization.categories) {
const { alterConnectable, intConnCount } = getOrInit(
networkAnalysis,
label
);
output +=
SEP +
calculateDensity(alterConnectable, intConnCount).toLocaleString(
undefined,
{
minimumFractionDigits: 3,
maximumFractionDigits: 3,
}
);
}

output += "\nStar(s)";
output += categorization.categories
.map((label) => {
const { stars, maxDegree } = getOrInit(networkAnalysis, label);
if (stars.length > 0 && maxDegree > 0) {
return stars.map((a) => a.name).join(", ");
// + " (" + maxDegree + " Beziehungen)"
} else {
return "-";
}
})
.reduce((prev, curr) => prev + SEP + curr, "");

output += "\nBrücken";
for (const label of categorization.categories) {
output +=
SEP + getOrInit(networkAnalysis, label).bridges.length.toFixed(0);
}

output += "\nBrückenperson(en)";
output += makeComputedAlterGroup(
networkAnalysis,
categorization.categories,
"bridgePersons"
).reduce((prev, curr) => prev + SEP + curr, "");

output += "\nIsolierte";
output += makeComputedAlterGroup(
networkAnalysis,
categorization.categories,
"isolated"
).reduce((prev, curr) => prev + SEP + curr, "");

output += "\nPersonen ohne Kante zur Ankerperson";
output += makeComputedAlterGroup(
networkAnalysis,
categorization.categories,
"alterZeroEdge"
).reduce((prev, curr) => prev + SEP + curr, "");
}

return output;
}

function makeComputedAlterGroup(
networkAnalysis: Map<string, NetworkAnalysis>,
categoryLabels: string[],
group: "stars" | "isolated" | "bridgePersons" | "alterZeroEdge"
) {
return categoryLabels.map((cat) => {
const analysis = getOrInit(networkAnalysis, cat);
if (analysis[group].length > 0) {
return (
analysis[group].length +
" (" +
analysis[group].map((a) => a.name).join(", ") +
")"
);
} else {
return "0";
}
});
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
faFile,
faSave,
faFileImage,
faFileCsv,
// faChevronDown,
// faUndoAlt,
// faRedoAlt,
Expand All @@ -50,6 +51,7 @@ library.add(
faFile,
faSave,
faFileImage,
faFileCsv,
// faChevronDown,
// faUndoAlt,
// faRedoAlt,
Expand Down

0 comments on commit c11831c

Please sign in to comment.