-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphs): new graph of publications evolution per language
- Loading branch information
Showing
8 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/components/Charts/publications/general/langues-evolution/get-data.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import Axios from 'axios'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useIntl } from 'react-intl'; | ||
|
||
import { ES_API_URL, HEADERS } from '../../../../../config/config'; | ||
import getFetchOptions from '../../../../../utils/chartFetchOptions'; | ||
|
||
function useGetData(observationSnap, domain) { | ||
const intl = useIntl(); | ||
const [allData, setData] = useState({}); | ||
const [isLoading, setLoading] = useState(true); | ||
const [isError, setError] = useState(false); | ||
|
||
const getDataForLastObservationSnap = useCallback( | ||
async (lastObservationSnap) => { | ||
const query = getFetchOptions({ | ||
key: 'publicationsByYear', | ||
domain, | ||
parameters: [lastObservationSnap, 'lang.keyword'], | ||
objectType: ['publications'], | ||
}); | ||
const res = await Axios.post(ES_API_URL, query, HEADERS); | ||
const data = res.data.aggregations.by_year.buckets; | ||
data.sort((a, b) => a.key - b.key); // Tri pour avoir les années dans l'ordre d'affichage du graphe | ||
|
||
const categories = Object.values(data).map((d) => d.key); // Elements d'abscisse | ||
|
||
const dataByLang = {}; | ||
data.forEach((year) => year.by_custom.buckets.forEach((lang) => { | ||
dataByLang[lang.key] = [ | ||
...(dataByLang?.[lang.key] ?? []), | ||
{ year: year.key, count: lang.doc_count }, | ||
]; | ||
})); | ||
|
||
const dataGraph = Object.entries(dataByLang).map(([key, values]) => ({ | ||
name: intl.formatMessage({ id: `app.lang.${key}` }), | ||
data: values.map((v) => ({ | ||
name: v.year, | ||
y: v.count, | ||
lang: intl.formatMessage({ id: `app.lang.${key}` }), | ||
})), | ||
})); | ||
|
||
const sum = (array) => array.reduce((acc, value) => acc + value, 0); | ||
dataGraph.sort( | ||
(a, b) => sum(b.data.map(({ y }) => y)) - sum(a.data.map(({ y }) => y)), | ||
); | ||
|
||
return { | ||
categories, | ||
dataGraph: dataGraph.slice(0, 5), | ||
}; | ||
}, | ||
[domain, intl], | ||
); | ||
|
||
useEffect(() => { | ||
async function getData() { | ||
try { | ||
const dataGraph = await getDataForLastObservationSnap(observationSnap); | ||
setData(dataGraph); | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
setError(true); | ||
} finally { | ||
setLoading(false); | ||
} | ||
} | ||
getData(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [observationSnap]); | ||
|
||
return { allData, isError, isLoading }; | ||
} | ||
export default useGetData; |
84 changes: 84 additions & 0 deletions
84
src/components/Charts/publications/general/langues-evolution/langues-by-year.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import Highcharts from 'highcharts'; | ||
import HCExportingData from 'highcharts/modules/export-data'; | ||
import HCExporting from 'highcharts/modules/exporting'; | ||
import HighchartsReact from 'highcharts-react-official'; | ||
import PropTypes from 'prop-types'; | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import { useIntl } from 'react-intl'; | ||
|
||
import customComments from '../../../../../utils/chartComments'; | ||
import { chartOptions } from '../../../../../utils/chartOptions'; | ||
import { domains, graphIds } from '../../../../../utils/constants'; | ||
import { getObservationLabel, withDomain } from '../../../../../utils/helpers'; | ||
import useGlobals from '../../../../../utils/Hooks/useGetGlobals'; | ||
import WrapperChart from '../../../../WrapperChart'; | ||
import GraphComments from '../../../graph-comments'; | ||
import useGetData from './get-data'; | ||
|
||
HCExporting(Highcharts); | ||
HCExportingData(Highcharts); | ||
|
||
const Chart = ({ domain, hasComments, hasFooter, id }) => { | ||
const [chartComments, setChartComments] = useState(''); | ||
const chartRef = useRef(); | ||
const intl = useIntl(); | ||
const { beforeLastObservationSnap, lastObservationSnap } = useGlobals(); | ||
const { allData, isError, isLoading } = useGetData( | ||
lastObservationSnap, | ||
domain, | ||
); | ||
const { dataGraph, categories } = allData; | ||
const dataTitle = { | ||
publicationYear: getObservationLabel(beforeLastObservationSnap, intl), | ||
}; | ||
const idWithDomain = withDomain(id, domain); | ||
const optionsGraph = chartOptions[id].getOptions( | ||
idWithDomain, | ||
intl, | ||
categories, | ||
dataGraph, | ||
dataTitle, | ||
); | ||
|
||
useEffect(() => { | ||
setChartComments(customComments(allData, idWithDomain, intl)); | ||
}, [allData, idWithDomain, intl]); | ||
|
||
return ( | ||
<WrapperChart | ||
chartRef={chartRef} | ||
dataTitle={dataTitle} | ||
domain={domain} | ||
hasComments={false} | ||
hasFooter={hasFooter} | ||
id={id} | ||
isError={isError} | ||
isLoading={isLoading || !dataGraph || !categories} | ||
> | ||
<HighchartsReact | ||
highcharts={Highcharts} | ||
id={idWithDomain} | ||
options={optionsGraph} | ||
ref={chartRef} | ||
/> | ||
{hasComments && chartComments && ( | ||
<GraphComments comments={chartComments} hasFooter={hasFooter} /> | ||
)} | ||
</WrapperChart> | ||
); | ||
}; | ||
|
||
Chart.defaultProps = { | ||
domain: '', | ||
hasComments: true, | ||
hasFooter: true, | ||
id: 'publi.general.langues.chart-publications-by-year', | ||
}; | ||
Chart.propTypes = { | ||
domain: PropTypes.oneOf(domains), | ||
hasComments: PropTypes.bool, | ||
hasFooter: PropTypes.bool, | ||
id: PropTypes.oneOf(graphIds), | ||
}; | ||
|
||
export default Chart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bf11978
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #313