Skip to content

Commit

Permalink
search on charts
Browse files Browse the repository at this point in the history
  • Loading branch information
cdietschrun committed Jan 1, 2024
1 parent 5d7ae6f commit ade1754
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
32 changes: 29 additions & 3 deletions client/src/components/GamePieChart.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext } from "react";
import React, { useContext, useState } from "react";
import {
Chart as ChartJS,
ArcElement,
Expand All @@ -9,6 +9,7 @@ import {
import { Pie } from "react-chartjs-2";
import { GameSession } from "../models/GameSession";
import { GoodplaysContext } from "../models/GoodplaysContextType";
import Fuse from "fuse.js";

ChartJS.register(ArcElement, Tooltip, Legend);

Expand Down Expand Up @@ -41,12 +42,28 @@ export const data2 = {

const GamePieChart: React.FC = () => {
const { gameSessions } = useContext(GoodplaysContext);
const [search, setSearch] = useState('');

const fuseOptions = {
keys: ['gameName'],
includeScore: true,
threshold: 0.3
};

const fuse = new Fuse(gameSessions, fuseOptions);

function dataToChartData(
gameSessions: GameSession[]
): ChartData<"pie", number[], unknown> {
let filteredSessions;
if (search) {
const result = fuse.search(search);
filteredSessions = result.map(({ item }) => item);
} else {
filteredSessions = gameSessions;
}
const gameNames = [
...new Set(gameSessions.map((session) => session.gameName)),
...new Set(filteredSessions.map((session) => session.gameName)),
];
const data = gameNames.map(
(gameName) =>
Expand Down Expand Up @@ -77,7 +94,16 @@ const GamePieChart: React.FC = () => {

return (
<div>
<Pie data={dataToChartData(gameSessions)} />;
<input
type="text"
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder="Search game sessions, e.g. 'craft' for StarCraft, Warcraft, and World of Warcraft"
style={{ width: '500px' }}
/>
<br />

<Pie data={dataToChartData(gameSessions)} />;
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/GameSessionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const GameSessionList: React.FC = () => {
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder="Search game sessions, e.g. 'craft' for StarCraft, Warcraft, and World of Warcraft"
style={{ width: '500px' }} // Adjust the width as needed
style={{ width: '500px' }}
/>
<br />

Expand Down
42 changes: 37 additions & 5 deletions client/src/components/GameTreeMapChart.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useContext, useEffect, useRef } from "react";
import React, { useContext, useEffect, useMemo, useRef, useState } from "react";
import * as d3 from "d3";
import { GoodplaysContext } from "../models/GoodplaysContextType";
import { GameSession } from "../models/GameSession";
import Fuse from "fuse.js";

type Data = {
name: string;
Expand All @@ -28,10 +29,28 @@ function transformData(gameSessions: GameSession[]): Data[] {
const GameTreeMapChart: React.FC = () => {
const { gameSessions } = useContext(GoodplaysContext);
const ref = useRef<SVGSVGElement>(null);

const [search, setSearch] = useState('');

const data = useMemo(() => {
const fuseOptions = {
keys: ['gameName'],
includeScore: true,
threshold: 0.3
};

const fuse = new Fuse(gameSessions, fuseOptions);

let filteredSessions;
if (search) {
const result = fuse.search(search);
filteredSessions = result.map(({ item }) => item);
} else {
filteredSessions = gameSessions;
}
return transformData(filteredSessions);
}, [search, gameSessions]);

useEffect(() => {
const data = transformData(gameSessions);

if (ref.current) {
const svg = d3.select(ref.current);

Expand Down Expand Up @@ -88,7 +107,20 @@ const GameTreeMapChart: React.FC = () => {
}
});

return <svg ref={ref} width={1024} height={1024}></svg>;
return (
<div>
<input
type="text"
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder="Search game sessions, e.g. 'craft' for StarCraft, Warcraft, and World of Warcraft"
style={{ width: '500px' }}
/>
<br />

<svg ref={ref} width={1024} height={1024}></svg>
</div>
);
};

export default GameTreeMapChart;

0 comments on commit ade1754

Please sign in to comment.