Generate realistic sample data
This commit is contained in:
13
src/App.tsx
13
src/App.tsx
@@ -8,7 +8,7 @@ import Legend from "./components/Legend";
|
|||||||
import QRCode from "./components/QRCode";
|
import QRCode from "./components/QRCode";
|
||||||
import { WaffleChart } from "./components/WaffleChart";
|
import { WaffleChart } from "./components/WaffleChart";
|
||||||
|
|
||||||
import { fetchGoogleSheet } from "./lib/parser";
|
import { getSampleData } from "./lib/data";
|
||||||
import "./styles/App.scss";
|
import "./styles/App.scss";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
@@ -18,9 +18,9 @@ function App() {
|
|||||||
});
|
});
|
||||||
const responseQuery = useQuery({
|
const responseQuery = useQuery({
|
||||||
queryKey: ["responses"],
|
queryKey: ["responses"],
|
||||||
queryFn: fetchGoogleSheet,
|
// queryFn: fetchGoogleSheet,
|
||||||
// queryFn: getSampleData,
|
queryFn: getSampleData,
|
||||||
refetchInterval: 5 * 1000, // Refresh every 5 seconds
|
refetchInterval: 1 * 1000, // Refresh every 5 seconds
|
||||||
});
|
});
|
||||||
|
|
||||||
if (metadataQuery.isPending || responseQuery.isPending)
|
if (metadataQuery.isPending || responseQuery.isPending)
|
||||||
@@ -29,19 +29,16 @@ function App() {
|
|||||||
return <div>Error loading data</div>;
|
return <div>Error loading data</div>;
|
||||||
|
|
||||||
// Sort responses by timestamp to easily find the latest response
|
// Sort responses by timestamp to easily find the latest response
|
||||||
const responses = responseQuery.data.sort(
|
const responses = [...responseQuery.data].sort(
|
||||||
(a, b) => a.timestamp - b.timestamp
|
(a, b) => a.timestamp - b.timestamp
|
||||||
);
|
);
|
||||||
const categoryMetadata = metadataQuery.data;
|
const categoryMetadata = metadataQuery.data;
|
||||||
|
|
||||||
if (!responses.length) return null;
|
|
||||||
|
|
||||||
// Group data by question (outside the component)
|
// Group data by question (outside the component)
|
||||||
const questionGroups = Array.from(
|
const questionGroups = Array.from(
|
||||||
d3.group(responses, (d) => d.question).entries()
|
d3.group(responses, (d) => d.question).entries()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Create scales
|
|
||||||
return (
|
return (
|
||||||
<div className="layout">
|
<div className="layout">
|
||||||
<div className="chart-container">
|
<div className="chart-container">
|
||||||
|
|||||||
@@ -1,29 +1,27 @@
|
|||||||
|
import { fetchCategoryMetadata } from "./metadata";
|
||||||
import { ResponseData } from "./parser";
|
import { ResponseData } from "./parser";
|
||||||
|
|
||||||
export function getSampleData(): Promise<ResponseData[]> {
|
function randInt(min: number, max: number): number {
|
||||||
const questions = [
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
"Service Quality",
|
}
|
||||||
"Value for Money",
|
|
||||||
"Ease of Use",
|
export async function getSampleData(): Promise<ResponseData[]> {
|
||||||
"Recommendation",
|
// Use the actual categories
|
||||||
"Overall Satisfaction",
|
const questions = (await fetchCategoryMetadata()).map(
|
||||||
"Customer Support",
|
(metadata) => metadata.category
|
||||||
"Product Features",
|
);
|
||||||
];
|
|
||||||
const sampleData: ResponseData[] = [];
|
const sampleData: ResponseData[] = [];
|
||||||
let id = 1;
|
const numResponses = randInt(10, 20);
|
||||||
questions.forEach((question) => {
|
|
||||||
const numResponses = Math.floor(Math.random() * 50) + 30;
|
|
||||||
for (let i = 0; i < numResponses; i++) {
|
for (let i = 0; i < numResponses; i++) {
|
||||||
const response = Math.floor(Math.random() * 5);
|
questions.forEach((question) => {
|
||||||
|
const response = randInt(0, 4); // Likert scale response (0-4)
|
||||||
sampleData.push({
|
sampleData.push({
|
||||||
timestamp: id++,
|
timestamp: i, // Group all responses by the same timestamp to mimic Google Forms behavior
|
||||||
position: "",
|
|
||||||
question: question,
|
question: question,
|
||||||
response: response,
|
response: response,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
// Simulate a delay to mimic fetching actual data
|
// Simulate a delay to mimic fetching actual data
|
||||||
return new Promise<ResponseData[]>((resolve) => {
|
return new Promise<ResponseData[]>((resolve) => {
|
||||||
setTimeout(() => resolve(sampleData), 500);
|
setTimeout(() => resolve(sampleData), 500);
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ const url = `https://docs.google.com/spreadsheets/d/${sheet_id}/gviz/tq?tqx=out:
|
|||||||
|
|
||||||
export interface ResponseData {
|
export interface ResponseData {
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
position: string;
|
|
||||||
question: string;
|
question: string;
|
||||||
response: number;
|
response: number;
|
||||||
}
|
}
|
||||||
@@ -40,7 +39,6 @@ export function parseCSV(csv: string): ResponseData[] {
|
|||||||
});
|
});
|
||||||
return Object.entries(responses).flatMap(([category, response]) => ({
|
return Object.entries(responses).flatMap(([category, response]) => ({
|
||||||
timestamp: new Date(row["Timestamp"]).getTime(),
|
timestamp: new Date(row["Timestamp"]).getTime(),
|
||||||
position: row["Position"],
|
|
||||||
question: category,
|
question: category,
|
||||||
response: response,
|
response: response,
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user