Skip to content

Commit

Permalink
feat(patents): add graph for cpc classification
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihoub2 committed Oct 3, 2024
1 parent 02fad39 commit e91d8bd
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 30 deletions.
55 changes: 55 additions & 0 deletions client/src/api/patents/[id]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,58 @@ export async function getPatentById(id: string): Promise<Patent> {
if (!patent) throw new Error("404");
return { ...patent, _id: data?.hits?.hits?.[0]._id };
}
export async function getCpcAggregation(value: string): Promise<Patent> {
const body: any = {
size: 0,
query: {
bool: {
must: [
{
term: {
"applicants.ids.id.keyword": value,
},
},
],
},
},
aggs: {
byCpc: {
terms: {
field: "cpc.ss_classe.code.keyword",
size: 10000,
},
},
},
};

const res = await fetch(`${patentsIndex}/_search`, {
method: "POST",
body: JSON.stringify(body),
headers: postHeaders,
});

const data = await res.json();

const buckets = data?.aggregations?.byCpc?.buckets;
const hits = data?.hits?.hits;

const labelsByCode = hits.reduce((acc: any, hit: any) => {
const cpcGroups = hit._source.cpc?.ss_classe ?? [];
cpcGroups.forEach((cpc: any) => {
if (!acc[cpc.code]) {
acc[cpc.code] = cpc.label;
}
});
return acc;
}, {});

const patent = buckets.map((bucket: any) => ({
code: bucket.key,
doc_count: bucket.doc_count,
label: labelsByCode[bucket.key] || "Label non trouvé",
}));

if (!patent.length) throw new Error("404");

return patent;
}
78 changes: 78 additions & 0 deletions client/src/components/patent-chart/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from "react";
import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";
import HighchartsMore from "highcharts/highcharts-more";

HighchartsMore(Highcharts);

type CpcChartProps = {
data: {
label: any;
name: string;
doc_count: number;
code: string;
}[];
};

const CpcChart: React.FC<CpcChartProps> = ({ data }) => {
const chartData = data.map((item) => ({
name: item.label,
value: item.doc_count,
code: item.code,
}));

const options = {
chart: {
type: "packedbubble",
layoutAlgorithm: {
splitSeries: false,
},
height: "100%",
backgroundColor: "#f4f4f4",
},
title: {
text: "Répartition des CPC par code",
},
tooltip: {
pointFormat: "<b>{point.name}</b> (Code: {point.code}): {point.value}",
formatter: function () {
return `<b>${this.point.name}</b> (Code: ${this.point.code}): ${this.point.value}`;
},
},
series: [
{
minSize: 20,
maxSize: 100,
data: chartData.map((item) => ({
name: item.name,
value: item.value,
code: item.code,
})),
},
],
plotOptions: {
packedbubble: {
minSize: 10,
maxSize: 100,
zMin: 0,
zMax: 100,
dataLabels: {
enabled: true,
format: "{point.value}",
},
cursor: "pointer",
point: {
events: {
click: function () {
window.location.href = `/search/patents?q=${this.code}`;
},
},
},
},
},
};

return <HighchartsReact highcharts={Highcharts} options={options} />;
};

export default CpcChart;
62 changes: 62 additions & 0 deletions client/src/components/patent-chart/indexA.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from "react";
import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";
import wordcloud from "highcharts/modules/wordcloud";

wordcloud(Highcharts);

type CpcWordChartProps = {
data: {
label: string;
doc_count: number;
code: string;
}[];
};

const CpcWordCloud: React.FC<CpcWordChartProps> = ({ data }) => {
const wordCloudData = data.map((item) => ({
name: item.code,
weight: item.doc_count,
label: item.label,
}));

const options = {
chart: {
type: "wordcloud",
},
title: {
text: "Nuage de mots des CPC par code",
},
series: [
{
type: "wordcloud",
data: wordCloudData,
name: "Occurrences",
},
],
tooltip: {
pointFormat:
"{point.label}: <b>{point.name}</b> (Occurrences: {point.weight})",
},
plotOptions: {
series: {
cursor: "pointer",
point: {
events: {
click: function () {
window.location.href = `/search/patents?q=${this.name}`;
},
},
},
},
wordcloud: {
minFontSize: 10,
maxFontSize: 50,
},
},
};

return <HighchartsReact highcharts={Highcharts} options={options} />;
};

export default CpcWordCloud;
Loading

0 comments on commit e91d8bd

Please sign in to comment.