Skip to content

Commit d2b7032

Browse files
committed
start on chart
1 parent 6f03022 commit d2b7032

File tree

7 files changed

+111
-3
lines changed

7 files changed

+111
-3
lines changed

Diff for: packages/data-visualizer/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"build": "cross-env NODE_ENV=production vite build"
77
},
88
"dependencies": {
9+
"chart.js": "^3.5.1",
910
"classnames": "^2.3.1",
1011
"firebase": "^9.1.1",
1112
"jsvectormap": "^1.3.3",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Chart, ChartType } from 'chart.js'
2+
import { createRef, FunctionalComponent, RefObject } from 'preact'
3+
import { useEffect, useState } from 'preact/hooks'
4+
5+
interface Props {
6+
chartRef: RefObject<any>
7+
type: ChartType
8+
}
9+
10+
const LineChart: FunctionalComponent<Props> = ({ chartRef, type }) => {
11+
const [chart, setChart] = useState<Chart | undefined>()
12+
13+
useEffect(() => {
14+
if (!chart) {
15+
const newChart = new Chart(chartRef.current, {
16+
data: {
17+
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
18+
datasets: [
19+
{
20+
label: '# of Votes',
21+
data: [12, 19, 3, 5, 2, 3],
22+
borderWidth: 1
23+
}
24+
]
25+
},
26+
options: {
27+
scales: {
28+
y: {
29+
beginAtZero: true
30+
}
31+
}
32+
},
33+
type: type
34+
})
35+
setChart(newChart)
36+
}
37+
}, [chart, chartRef, type])
38+
39+
return null
40+
}
41+
42+
export default LineChart
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Chart } from 'chart.js'
2+
import { createRef, FunctionalComponent, RefObject } from 'preact'
3+
import { useEffect, useState } from 'preact/hooks'
4+
5+
interface Props {
6+
chartRef: RefObject<any>
7+
}
8+
9+
const LineChart: FunctionalComponent<Props> = ({ chartRef }) => {
10+
const [chart, setChart] = useState<Chart | undefined>()
11+
12+
useEffect(() => {
13+
if (!chart) {
14+
const newChart = new Chart(chartRef.current, {
15+
data: {
16+
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
17+
datasets: [
18+
{
19+
label: '# of Votes',
20+
data: [12, 19, 3, 5, 2, 3],
21+
borderWidth: 1
22+
}
23+
]
24+
},
25+
options: {
26+
scales: {
27+
y: {
28+
beginAtZero: true
29+
}
30+
}
31+
},
32+
type: 'line'
33+
})
34+
setChart(newChart)
35+
}
36+
}, [chart, chartRef])
37+
38+
return null
39+
}
40+
41+
export default LineChart
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { createRef, FunctionalComponent } from 'preact'
2+
3+
import LineChart from '../common/LineChart'
4+
5+
const DashboardChart: FunctionalComponent = () => {
6+
const chartRef = createRef()
7+
8+
return (
9+
<div>
10+
<canvas ref={chartRef} height={400} width={400}>
11+
<LineChart chartRef={chartRef} />
12+
</canvas>
13+
</div>
14+
)
15+
}
16+
17+
export default DashboardChart

Diff for: packages/data-visualizer/src/components/usageByCountry/CountryDetails.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ const CountryDetails: FunctionalComponent<Props> = ({ country }) => {
9090
<li key={host.countryCode}>
9191
{host.hostOrigin}: {co2Formatter(host.CO2)}{' '}
9292
<span className="text-sm">
93-
({host.numberOfCalls - host.numberOfCallsWithoutSize}{' '}
94-
calls)
93+
({host.numberOfCalls} calls)
9594
</span>
9695
</li>
9796
))}

Diff for: packages/data-visualizer/src/pages/Dashboard.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { FunctionComponent } from 'preact'
22
import { useContext } from 'preact/hooks'
3-
import UsageDisplay from '../components/dashboard/UsageDisplay'
43

4+
import DashboardChart from '../components/dashboard/DashboardChart'
5+
import UsageDisplay from '../components/dashboard/UsageDisplay'
56
import Greeting from '../components/layout/Greeting'
67
import { UserContext } from '../contexts/User/UserContext'
78

@@ -11,6 +12,7 @@ const Dashboard: FunctionComponent = () => {
1112
return (
1213
<div>
1314
<UsageDisplay />
15+
<DashboardChart />
1416
</div>
1517
)
1618
}

Diff for: pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)