Skip to content

Commit 0eac83a

Browse files
authored
Merge pull request #586 from shinyichen/SCIX-513-word-cloud-download
add download to word cloud
2 parents 3878b5c + aea0466 commit 0eac83a

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/components/Visualizations/Containers/ConceptCloudPageContainer.tsx

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Box, Flex, Text, useBreakpointValue } from '@chakra-ui/react';
2-
32
import axios from 'axios';
43
import { useRouter } from 'next/router';
5-
import { ReactElement, Reducer, useMemo, useReducer } from 'react';
4+
import { ReactElement, Reducer, useCallback, useMemo, useReducer } from 'react';
65
import { ISliderRange } from '../types';
76
import { buildWCDict } from '../utils';
87
import { FilterSearchBar, IFilterSearchBarProps } from '../Widgets';
@@ -14,6 +13,7 @@ import { SimpleLink } from '@/components/SimpleLink';
1413
import { makeSearchParams } from '@/utils/common/search';
1514
import { IADSApiSearchParams } from '@/api/search/types';
1615
import { useGetWordCloud } from '@/api/vis/vis';
16+
import { DataDownloader } from '@/components/DataDownloader';
1717

1818
const MAX_ROWS_TO_FETCH = 100;
1919

@@ -83,6 +83,14 @@ export const ConceptCloudPageContainer = ({ query }: IConceptCloudPageContainerP
8383
{ enabled: !!query && !!query.q && query.q.length > 0 },
8484
);
8585

86+
const getCSVDataContent = useCallback(() => {
87+
let output = 'Word, Idf, Record Count, Total Occurrences\n';
88+
Object.entries(data).map(([key, value]) => {
89+
output += `"${key}","${value.idf}","${value.record_count}","${value.total_occurrences}"\n`;
90+
});
91+
return output;
92+
}, [data]);
93+
8694
// build the word list for graph
8795
const { wordList, fill } = useMemo(() => {
8896
if (!data) {
@@ -132,7 +140,7 @@ export const ConceptCloudPageContainer = ({ query }: IConceptCloudPageContainerP
132140
)}
133141
{isLoading && <LoadingMessage message="Fetching concept cloud data" />}
134142
{!isLoading && isSuccess && data && (
135-
<Box>
143+
<Flex direction="column" gap={2}>
136144
<Expandable
137145
title="About Concept Cloud"
138146
description={
@@ -158,6 +166,13 @@ export const ConceptCloudPageContainer = ({ query }: IConceptCloudPageContainerP
158166
</Flex>
159167
}
160168
/>
169+
{data && (
170+
<DataDownloader
171+
label="Download CSV Data"
172+
getFileContent={() => getCSVDataContent()}
173+
fileName="word-cloud.csv"
174+
/>
175+
)}
161176
<FilterSearchBar
162177
tagItems={filterTagItems}
163178
onRemove={(tag) => dispatch({ type: 'REMOVE_FILTER', payload: tag.id as string })}
@@ -176,7 +191,7 @@ export const ConceptCloudPageContainer = ({ query }: IConceptCloudPageContainerP
176191
currentSliderValue={state.currentSliderValue}
177192
selectedWords={state.filters}
178193
/>
179-
</Box>
194+
</Flex>
180195
)}
181196
</Box>
182197
);

0 commit comments

Comments
 (0)