Skip to content

Commit

Permalink
feat(graph): Add absolute and relative toggle for retractations by pu…
Browse files Browse the repository at this point in the history
…blisher
  • Loading branch information
ahonestla committed Oct 16, 2023
1 parent 709ecdb commit 749c421
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Toggle } from '@dataesr/react-dsfr';
import Highcharts from 'highcharts';
import HCExportingData from 'highcharts/modules/export-data';
import HCExporting from 'highcharts/modules/exporting';
Expand All @@ -22,15 +23,22 @@ const Chart = ({ domain, hasComments, hasFooter, id }) => {
const chartRef = useRef();
const intl = useIntl();
const [chartComments, setChartComments] = useState('');
const [isPercent, setPercent] = useState(false);
const { observationSnaps } = useGlobals();
const { data, isError, isLoading } = useGetData(observationSnaps, domain);
const { data, isError, isLoading } = useGetData(
observationSnaps,
domain,
isPercent,
);
const { categories, dataGraph } = data;
const idWithDomain = withDomain(id, domain);

const optionsGraph = chartOptions[id].getOptions(
idWithDomain,
intl,
categories,
dataGraph,
isPercent,
);

useEffect(() => {
Expand All @@ -47,6 +55,11 @@ const Chart = ({ domain, hasComments, hasFooter, id }) => {
isError={isError}
isLoading={isLoading || !dataGraph}
>
<Toggle
checked={isPercent}
label={intl.formatMessage({ id: 'app.proportion' })}
onChange={() => setPercent(!isPercent)}
/>
<HighchartsReact
highcharts={Highcharts}
id={idWithDomain}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,30 @@ function useGetData(observationSnaps, domain = '', isPercent = false) {
parameters: [lastObservationSnap],
objectType: ['publications'],
});
const numberOfRetracted = (item) => item.by_retraction.buckets.find((i2) => i2.key === 1)?.doc_count ?? 0;
const response = await Axios.post(ES_API_URL, query, HEADERS);
const buckets = response?.data?.aggregations?.by_publisher?.buckets?.sort(
(a, b) => b.doc_count - a.doc_count,
);
const buckets = response?.data?.aggregations?.by_publisher?.buckets
?.sort((a, b) => numberOfRetracted(b) - numberOfRetracted(a))
.slice(0, 20);
const categories = buckets.map((item) => item.key);
const dataGraph = [
{
data: buckets.map(
(item) => ((item.by_retraction.buckets.find((i2) => i2.key === 1)
?.doc_count ?? 0)
/ item.doc_count)
* 100,
),
},
];
const dataGraph = {
data: buckets.map((item, catIndex) => ({
y_tot: item.doc_count ?? 0,
y_abs: numberOfRetracted(item),
y_rel: (numberOfRetracted(item) / item.doc_count) * 10000,
y: isPercent
? (numberOfRetracted(item) / item.doc_count) * 10000
: numberOfRetracted(item),
x: catIndex,
publisher: categories[catIndex],
})),
};

return {
categories,
dataGraph,
};
}, [domain, lastObservationSnap]);
}, [domain, isPercent, lastObservationSnap]);

useEffect(() => {
async function getData() {
Expand Down
4 changes: 4 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@
"app.observational.end": "End of<br>the observational<br>study",
"app.observationals": "observational studies",
"app.project": "The project",
"app.proportion": "Proportion",
"app.promoteurs.impact.chart-classement-pays.tooltip": "<b>Trials completed, in collaboration with {point.country}</b><br>{point.y:.2f}% share scientific results or publication ({point.y_abs} / {point.y_tot})",
"app.promoteurs.impact.chart-repartition.tooltip": "<b>Trials completed in {point.year}, {series.name}</b><br>Trials contribution with {point.name}: {point.y:.2f}% of the trials ({point.y_abs} / {point.y_tot})",
"app.publi.affiliations": "Affiliations",
Expand Down Expand Up @@ -943,7 +944,10 @@
"app.publi.navigation.langues": "The languages of publication",
"app.publi.navigation.voies": "The opening routes",
"app.publi.nb-publications": "Number of publications",
"app.publi.nb-publications-retracted": "Number of retracted publications",
"app.national-publi.others.retractions.chart-by-publisher.tooltip": "<b>{point.publisher}</b><br>• Share of retracted publications :<br>{point.y_rel:.1f} ‱ ({point.y_abs} / {point.y_tot})",
"app.publi.percent-publications-retracted": "Percent of retracted publications",
"app.publi.pertenthousand-publications-retracted": "Share of retracted publications (per ten thousand)",
"app.publi.part-publications-archive": "Share of publications in open access on archive",
"app.publi.percentage-publi": "Proportion of publications",
"app.publi.percentage-publi-bealls": "Share of publications whose journal or<br>publisher is in the Beall's list",
Expand Down
4 changes: 4 additions & 0 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,7 @@
"app.observational.end": "Fin de l'étude<br>observationnelle",
"app.observationals": "études observationnelles",
"app.project": "Le projet",
"app.proportion": "Proportion",
"app.promoteurs.impact.chart-classement-pays.tooltip": "<b>Essais terminés, en collaboration avec {point.country}</b><br>{point.y:.2f} % partagent des résultats ou une publication scientifique ({point.y_abs} / {point.y_tot})",
"app.promoteurs.impact.chart-repartition.tooltip": "<b>Essais terminés en {point.year}, {series.name}</b><br>Contribution des essais avec {point.name} : {point.y:.2f} % des essais ({point.y_abs} / {point.y_tot})",
"app.publi.affiliations": "Affiliations",
Expand Down Expand Up @@ -1105,7 +1106,10 @@
"app.publi.navigation.langues": "Les langues de publication",
"app.publi.navigation.voies": "Les voies d'ouverture",
"app.publi.nb-publications": "Nombre de publications",
"app.publi.nb-publications-retracted": "Nombre de publications retirées",
"app.national-publi.others.retractions.chart-by-publisher.tooltip": "<b>{point.publisher}</b><br>• Part de publications retirées :<br>{point.y_rel:.1f} ‱ ({point.y_abs} / {point.y_tot})",
"app.publi.percent-publications-retracted": "Pourcentage de publications retirées",
"app.publi.pertenthousand-publications-retracted": "Part des publications retirées (pour dix mille)",
"app.publi.part-publications-archive": "Part des publications en accès ouvert sur archive",
"app.publi.percentage-publi": "Proportion de publications",
"app.publi.percentage-publi-bealls": "Proportion de publications dont la revue ou<br>l'éditeur est dans la liste de Beall",
Expand Down
1 change: 1 addition & 0 deletions src/utils/chartFetchOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,7 @@ export default function getFetchOptions({
by_publisher: {
terms: {
field: 'publisher_normalized.keyword',
size: 50,
},
aggs: {
by_retraction: {
Expand Down
16 changes: 11 additions & 5 deletions src/utils/chartOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
cleanNumber,
getCSSValue,
getPercentageYAxis,
getPertenthousandYAxis,
getSource,
getURLSearchParams,
withtStudyType,
Expand Down Expand Up @@ -4195,29 +4196,34 @@ export const chartOptions = {
},
},
'publi.others.retractions.chart-by-publisher': {
getOptions: (id, intl, categories, data, dataTitle) => {
getOptions: (id, intl, categories, graph, isPercent, dataTitle) => {
const options = getGraphOptions({ id, intl, dataTitle });
options.chart.type = 'column';
options.xAxis = {
title: { text: intl.formatMessage({ id: 'app.publishers' }) },
categories,
};
options.yAxis = getPercentageYAxis();
options.yAxis = getPertenthousandYAxis(false, null, !isPercent);
options.yAxis.title.text = intl.formatMessage({
id: 'app.publi.percent-publications-retracted',
id: isPercent
? 'app.publi.pertenthousand-publications-retracted'
: 'app.publi.nb-publications-retracted',
});
options.legend.enabled = false;
options.plotOptions = {
series: {
dataLabels: {
enabled: true,
formatter() {
return this.y === 0 ? '' : this.y.toFixed(3).concat(' %');
if (isPercent) {
return this.y === 0 ? '' : this.y.toFixed(1).concat(' ‱');
}
return this.y === 0 ? '' : this.y.toFixed();
},
},
},
};
options.series = data;
options.series = [graph];
options.exporting.chartOptions.legend.enabled = false;
return options;
},
Expand Down
33 changes: 33 additions & 0 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,39 @@ export function getPercentageYAxis(
return axis;
}

export function getPertenthousandYAxis(
showTotal = true,
max = null,
absolute = false,
precision = 0,
) {
const suffix = absolute ? '' : ' ‱';
const axis = {
title: { text: '' },
stackLabels: {
enabled: true,
formatter() {
return showTotal && this.total
? this.total.toFixed(precision).concat(suffix)
: '';
},
style: {
fontWeight: 'bold',
fontSize: '15px',
},
},
labels: {
formatter() {
return this.axis.defaultLabelFormatter.call(this).concat(suffix);
},
},
};
if (max) {
axis.max = max;
}
return axis;
}

/**
*
* @param keys
Expand Down

0 comments on commit 749c421

Please sign in to comment.