1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-10-16 20:17:25 +00:00

🔧 Don't rerender chart at every state change

This commit is contained in:
Steffo 2021-05-23 16:16:04 +02:00
parent 2c57bd0238
commit f734299ac2
Signed by: steffo
GPG key ID: 6965406171929D01
2 changed files with 42 additions and 23 deletions

View file

@ -1,4 +1,4 @@
import React from "react" import React, { useMemo } from "react"
import BoxFull from "./BoxFull" import BoxFull from "./BoxFull"
import ChartComponent from "react-chartjs-2" import ChartComponent from "react-chartjs-2"
@ -9,8 +9,8 @@ export default function BoxChart({ chartProps, ...props }) {
return computedStyle.getPropertyValue(variable).trim() return computedStyle.getPropertyValue(variable).trim()
} }
return ( const chart = useMemo(
<BoxFull {...props}> () => (
<ChartComponent <ChartComponent
options={{ options={{
responsive: true, responsive: true,
@ -51,6 +51,13 @@ export default function BoxChart({ chartProps, ...props }) {
}} }}
{...chartProps} {...chartProps}
/> />
),
[chartProps]
)
return (
<BoxFull {...props}>
{chart}
</BoxFull> </BoxFull>
) )
} }

View file

@ -1,4 +1,4 @@
import React, { useContext } from "react" import React, { useContext, useMemo } from "react"
import BoxFull from "../base/BoxFull" import BoxFull from "../base/BoxFull"
import BoxChart from "../base/BoxChart" import BoxChart from "../base/BoxChart"
import Empty from "./Empty" import Empty from "./Empty"
@ -10,13 +10,36 @@ export default function BoxVisualizationChart({ ...props }) {
const { strings } = useContext(ContextLanguage) const { strings } = useContext(ContextLanguage)
const { tweets } = useContext(ContextRepositoryViewer) const { tweets } = useContext(ContextRepositoryViewer)
const hours = [...Array(24).keys()].map(hour => hour.toString()) const chartProps = useMemo(
const hourlyTweetCount = Array(24).fill(0) () => {
for(const tweet of tweets) { const hours = [...Array(24).keys()].map(hour => hour.toString())
const insertDate = new Date(tweet["insert_time"]) const hourlyTweetCount = Array(24).fill(0)
const insertHour = insertDate.getHours() for(const tweet of tweets) {
hourlyTweetCount[insertHour] += 1 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) { if(tweets.length === 0) {
return ( return (
@ -29,18 +52,7 @@ export default function BoxVisualizationChart({ ...props }) {
return ( return (
<BoxChart <BoxChart
header={strings.hourlyGraph} header={strings.hourlyGraph}
chartProps={{ chartProps={chartProps}
type: "bar",
data: {
labels: hours.map(hour => hour.toString()),
datasets: [
{
label: "Tweets",
data: hourlyTweetCount,
},
],
},
}}
{...props} {...props}
/> />
) )