-
-
Save catalinpit/ede866d28e62928f58904447d9d4ba36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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