1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-12-01 17:04:19 +00:00
pds-2021-g2-nest/nest_frontend/components/interactive/BoxVisualizationChart.js

59 lines
1.7 KiB
JavaScript

import React, { useContext, useMemo } from "react"
import BoxFull from "../base/BoxFull"
import BoxChart from "../base/BoxChart"
import Empty from "./Empty"
import ContextRepositoryViewer from "../../contexts/ContextRepositoryViewer"
import useStrings from "../../hooks/useStrings"
export default function BoxVisualizationChart({ ...props }) {
const strings = useStrings()
const { tweets } = useContext(ContextRepositoryViewer)
const chartProps = useMemo(
() => {
const hours = [...Array(24).keys()].map(hour => hour.toString())
const hourlyTweetCount = Array(24).fill(0)
for(const tweet of tweets) {
if(!tweet["post_time"]) {
console.debug("Tweet ", tweet, " has no post time, skipping...")
continue
}
const insertDate = new Date(tweet["post_time"])
const insertHour = insertDate.getHours()
hourlyTweetCount[insertHour] += 1
}
return {
type: "bar",
data: {
labels: hours.map(hour => hour.toString()),
datasets: [
{
label: "Tweets",
data: hourlyTweetCount,
},
],
},
}
},
[tweets],
)
if(tweets.length === 0) {
return (
<BoxFull header={"Hourly graph"} {...props}>
<Empty/>
</BoxFull>
)
}
return (
<BoxChart
header={strings.hourlyGraph}
chartProps={chartProps}
{...props}
/>
)
}