-
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 graph about publications with at least one explicit…
… mention
- Loading branch information
Showing
11 changed files
with
574 additions
and
373 deletions.
There are no files selected for viewing
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
103 changes: 103 additions & 0 deletions
103
src/components/Charts/software/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,103 @@ | ||
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 } from '../../../../../utils/helpers'; | ||
|
||
function useGetData(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: 'codeWithAtLeastOneExplicitMention', | ||
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 publications = []; | ||
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 numberOfSoftwareWithImplicitMentionsOnly = el.is_implicit.buckets.find((item) => item.key === 1)?.doc_count | ||
|| 0; | ||
const numberOfSoftwareWithAtLeastOneExplicitMention = el.is_implicit.buckets.find((item) => item.key === 0)?.doc_count | ||
|| 0; | ||
const numberOfSoftware = numberOfSoftwareWithImplicitMentionsOnly | ||
+ numberOfSoftwareWithAtLeastOneExplicitMention; | ||
publications.push({ | ||
y: | ||
(numberOfSoftwareWithAtLeastOneExplicitMention | ||
/ numberOfSoftware) | ||
* 100, | ||
y_abs: numberOfSoftwareWithAtLeastOneExplicitMention, | ||
y_tot: numberOfSoftware, | ||
x: el.key, | ||
bsoDomain, | ||
}); | ||
}); | ||
|
||
const dataGraph = [ | ||
{ | ||
name: capitalize( | ||
intl.formatMessage({ | ||
id: 'app.publication', | ||
}), | ||
), | ||
data: publications, | ||
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; |
85 changes: 85 additions & 0 deletions
85
...omponents/Charts/software/general/mentions/software-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,85 @@ | ||
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 { lastObservationSnap } = useGlobals(); | ||
const { allData, isError, isLoading } = useGetData( | ||
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: 'software.general.mentions.software-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; |
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.