Skip to content

Instantly share code, notes, and snippets.

@catalinpit
Created March 16, 2022 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catalinpit/ede866d28e62928f58904447d9d4ba36 to your computer and use it in GitHub Desktop.
Save catalinpit/ede866d28e62928f58904447d9d4ba36 to your computer and use it in GitHub Desktop.
import { useSubscription } from "@apollo/client";
import React from "react";
import { Chart } from "react-google-charts";
import { Error, Loading } from "./Components";
import { SUBSCRIPTION_RESULT } from "./GraphQL";
export const Result = ({ pollId }) => {
const { data, loading, error } = useSubscription(SUBSCRIPTION_RESULT, {
variables: { pollId },
});
const hasResults = data?.poll_results.length > 0;
if (loading) return <Loading />;
if (error) return <Error message={error.message} />;
return (
<div>
{hasResults ? <PollChart data={data?.poll_results} /> : <p>No result</p>}
</div>
);
};
const PollChart = ({ data }) => {
const COLOR = "color: #4285f4";
const d = [
["Option", "No. of votes", { role: "annotation" }, { role: "style" }],
];
data.forEach(({ option, votes }) =>
d.push([option.text, parseInt(votes), parseInt(votes), COLOR])
);
return (
<Chart
className="poll-result-chart-container"
chartType="BarChart"
loader={<div>Loading Chart</div>}
data={d}
options={chartOptions}
/>
);
};
const chartOptions = {
height: "100%",
chart: {
title: "Realtime results",
},
legend: { position: "none" },
animation: {
duration: 1000,
easing: "out",
startup: true,
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment