-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphs): Add a graph about proportion of explicit dataset mentio…
…ns by scientific fields
- Loading branch information
Showing
8 changed files
with
317 additions
and
2 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
...omponents/Charts/data/disciplines/mentions/datasets-with-at-least-one-explicit-mention.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,103 @@ | ||
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 { | ||
capitalize, | ||
cleanNumber, | ||
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 chartRef = useRef(); | ||
const intl = useIntl(); | ||
const [chartComments, setChartComments] = useState(''); | ||
const [dataTitle, setDataTitle] = useState({}); | ||
const idWithDomain = withDomain(id, domain); | ||
const { beforeLastObservationSnap, lastObservationSnap } = useGlobals(); | ||
const { allData, isError, isLoading } = useGetData( | ||
beforeLastObservationSnap, | ||
lastObservationSnap, | ||
domain, | ||
); | ||
const { categories, dataGraph } = allData; | ||
|
||
useEffect(() => { | ||
setDataTitle({ | ||
publicationYear: getObservationLabel(beforeLastObservationSnap, intl), | ||
}); | ||
}, [beforeLastObservationSnap, intl]); | ||
|
||
const categoriesLabel = categories?.map((item) => capitalize(intl.formatMessage({ id: `app.discipline.${item.key}` })) | ||
.concat('<br>(') | ||
.concat(intl.formatMessage({ id: 'app.effectif' })) | ||
.concat(' = ') | ||
.concat(cleanNumber(item.staff)) | ||
.concat(')')) || []; | ||
const optionsGraph = chartOptions[id].getOptions( | ||
idWithDomain, | ||
intl, | ||
categoriesLabel, | ||
dataGraph, | ||
dataTitle, | ||
); | ||
const hasBeta = true; | ||
|
||
useEffect(() => { | ||
setChartComments(customComments(allData, idWithDomain, intl)); | ||
}, [allData, idWithDomain, intl]); | ||
|
||
return ( | ||
<WrapperChart | ||
chartRef={chartRef} | ||
dataTitle={dataTitle} | ||
domain={domain} | ||
hasBeta={hasBeta} | ||
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: 'data.disciplines.mentions.datasets-with-at-least-one-explicit-mention', | ||
}; | ||
Chart.propTypes = { | ||
domain: PropTypes.oneOf(domains), | ||
hasComments: PropTypes.bool, | ||
hasFooter: PropTypes.bool, | ||
id: PropTypes.oneOf(graphIds), | ||
}; | ||
|
||
export default Chart; |
124 changes: 124 additions & 0 deletions
124
src/components/Charts/data/disciplines/mentions/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,124 @@ | ||
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'; | ||
import { | ||
capitalize, | ||
getCSSValue, | ||
getObservationLabel, | ||
getPublicationYearFromObservationSnap, | ||
} from '../../../../../utils/helpers'; | ||
|
||
function useGetData(beforeLastObservationSnap, observationSnap, domain) { | ||
const intl = useIntl(); | ||
const [allData, setData] = useState({}); | ||
const [isError, setError] = useState(false); | ||
const [isLoading, setLoading] = useState(true); | ||
|
||
const getDataForLastObservationSnap = useCallback( | ||
async (lastObservationSnap) => { | ||
console.log(lastObservationSnap); | ||
const query = getFetchOptions({ | ||
key: 'datasetsWithAtLeastOneExplicitMentionByDiscipline', | ||
domain, | ||
parameters: [lastObservationSnap], | ||
objectType: ['publications'], | ||
}); | ||
const res = await Axios.post(ES_API_URL, query, HEADERS); | ||
const data = res.data.aggregations.by_discipline.buckets; | ||
const bsoDomain = intl.formatMessage({ id: `app.bsoDomain.${domain}` }); | ||
const categories = []; | ||
const categoriesComments = []; | ||
const publications = []; | ||
const noOutline = { | ||
style: { | ||
textOutline: 'none', | ||
}, | ||
}; | ||
data | ||
.filter((item) => item.key !== 'unknown') | ||
.forEach((item, catIndex) => { | ||
const numberOfDatasetsWithImplicitMentionsOnly = item.is_implicit.buckets.find((item2) => item2.key === 1) | ||
?.doc_count || 0; | ||
const numberOfDatasetsWithAtLeastOneExplicitMention = item.is_implicit.buckets.find((item2) => item2.key === 0) | ||
?.doc_count || 0; | ||
const numberOfDatasets = numberOfDatasetsWithImplicitMentionsOnly | ||
+ numberOfDatasetsWithAtLeastOneExplicitMention; | ||
const nameClean = item.key.replace(/\n/g, '').replace(' ', ' '); | ||
categories.push({ | ||
key: nameClean, | ||
staff: numberOfDatasets, | ||
percent: | ||
(numberOfDatasetsWithAtLeastOneExplicitMention | ||
/ numberOfDatasets) | ||
* 100, | ||
}); | ||
categoriesComments.push( | ||
capitalize( | ||
intl.formatMessage({ id: `app.discipline.${nameClean}` }), | ||
), | ||
); | ||
publications.push({ | ||
bsoDomain, | ||
discipline: categoriesComments[catIndex], | ||
publicationDate: | ||
getPublicationYearFromObservationSnap(lastObservationSnap), | ||
x: catIndex, | ||
y_abs: numberOfDatasetsWithAtLeastOneExplicitMention, | ||
y_tot: numberOfDatasets, | ||
y: | ||
(numberOfDatasetsWithAtLeastOneExplicitMention | ||
/ numberOfDatasets) | ||
* 100, | ||
}); | ||
}); | ||
|
||
const dataGraph = [ | ||
{ | ||
name: capitalize( | ||
intl.formatMessage({ | ||
id: 'app.publication', | ||
}), | ||
), | ||
data: publications, | ||
color: getCSSValue('--orange-soft-100'), | ||
dataLabels: noOutline, | ||
}, | ||
]; | ||
|
||
const comments = { | ||
publicationYear: getObservationLabel(beforeLastObservationSnap, intl), | ||
}; | ||
|
||
return { | ||
categories, | ||
comments, | ||
dataGraph, | ||
}; | ||
}, | ||
[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; |
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