-
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): Add new graph about part of publications with implicit …
…datasets mentions only, see #314
- Loading branch information
Showing
9 changed files
with
303 additions
and
13 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/components/Charts/data/general/mentions/datasets-with-implicit-mentions-only.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,86 @@ | ||
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 chartRef = useRef(); | ||
const intl = useIntl(); | ||
const [chartComments, setChartComments] = useState(''); | ||
const { beforeLastObservationSnap, lastObservationSnap } = useGlobals(); | ||
const { allData, isError, isLoading } = useGetData( | ||
beforeLastObservationSnap, | ||
lastObservationSnap, | ||
domain, | ||
); | ||
const { categories, dataGraph } = allData; | ||
const dataTitle = { | ||
observationYear: getObservationLabel(lastObservationSnap, intl), | ||
}; | ||
const idWithDomain = withDomain(id, domain); | ||
const optionsGraph = chartOptions[id].getOptions( | ||
idWithDomain, | ||
intl, | ||
categories, | ||
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.general.mentions.datasets-with-implicit-mentions-only', | ||
}; | ||
Chart.propTypes = { | ||
domain: PropTypes.oneOf(domains), | ||
hasComments: PropTypes.bool, | ||
hasFooter: PropTypes.bool, | ||
id: PropTypes.oneOf(graphIds), | ||
}; | ||
|
||
export default Chart; |
106 changes: 106 additions & 0 deletions
106
src/components/Charts/data/general/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,106 @@ | ||
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, | ||
} 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) => { | ||
const query = getFetchOptions({ | ||
key: 'datasetsWithImplicitMentionsOnly', | ||
domain, | ||
parameters: [lastObservationSnap], | ||
objectType: ['publications'], | ||
}); | ||
const res = await Axios.post(ES_API_URL, query, HEADERS); | ||
const data = res.data.aggregations.by_publication_year.buckets.sort( | ||
(a, b) => a.key - b.key, | ||
); | ||
const bsoDomain = intl.formatMessage({ id: `app.bsoDomain.${domain}` }); | ||
const years = []; | ||
const shared = []; | ||
const noOutline = { | ||
style: { | ||
textOutline: 'none', | ||
}, | ||
}; | ||
data | ||
.filter( | ||
(el) => el.key > 2012 | ||
&& lastObservationSnap.length | ||
&& parseInt(el.key, 10) | ||
< parseInt(lastObservationSnap?.substring(0, 4), 10), | ||
) | ||
.forEach((el) => { | ||
years.push(el.key); | ||
const numberOfDatasetsWithImplicitMentionsOnly = el.is_implicit.buckets.find((item) => item.key === 1)?.doc_count | ||
|| 0; | ||
const numberOfDatasetsWithMixedMentions = el.is_implicit.buckets.find((item) => item.key === 0)?.doc_count | ||
|| 0; | ||
const numberOfDatasets = numberOfDatasetsWithImplicitMentionsOnly | ||
+ numberOfDatasetsWithMixedMentions; | ||
shared.push({ | ||
y: | ||
(numberOfDatasetsWithImplicitMentionsOnly / numberOfDatasets) | ||
* 100, | ||
y_abs: numberOfDatasetsWithImplicitMentionsOnly, | ||
y_tot: numberOfDatasets, | ||
x: el.key, | ||
bsoDomain, | ||
}); | ||
}); | ||
|
||
const dataGraph = [ | ||
{ | ||
name: capitalize( | ||
intl.formatMessage({ | ||
id: 'app.publication', | ||
}), | ||
), | ||
data: shared, | ||
color: getCSSValue('--publication-100'), | ||
dataLabels: noOutline, | ||
}, | ||
]; | ||
|
||
return { | ||
categories: years, | ||
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
Oops, something went wrong.