-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feat/devtools
- Loading branch information
Showing
5 changed files
with
167 additions
and
93 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import React, { useCallback } from "react"; | ||
import styled, { useTheme } from "styled-components"; | ||
import { | ||
Chart as ChartJS, | ||
BarElement, | ||
Tooltip, | ||
CategoryScale, | ||
LinearScale, | ||
PointElement, | ||
LineElement, | ||
TimeScale, | ||
ChartOptions, | ||
} from "chart.js"; | ||
import ChartDataLabels from "chartjs-plugin-datalabels"; | ||
import { Bar } from "react-chartjs-2"; | ||
import "chartjs-adapter-moment"; | ||
|
||
ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, TimeScale, Tooltip); | ||
const formatter = new Intl.NumberFormat("en", { notation: "compact" }); | ||
|
||
const BarContainer = styled.div` | ||
height: 220px; | ||
margin-top: 16px; | ||
`; | ||
|
||
ChartJS.register(BarElement); | ||
|
||
export interface IBarChartData { | ||
labels: string[]; | ||
data: number[]; | ||
total: number; | ||
} | ||
|
||
interface IBarChartProps { | ||
chartData: IBarChartData; | ||
} | ||
|
||
const BarChart: React.FC<IBarChartProps> = ({ chartData }) => { | ||
const theme = useTheme(); | ||
const getPercentValue = useCallback( | ||
(value: number) => `${Math.floor((value * 100) / chartData.total)} %`, | ||
[chartData] | ||
); | ||
|
||
const formatPNKValue = useCallback((value: number) => formatter.format(value), []); | ||
|
||
const tickSize = 5; // suggested, if that many labels can't fit, chart will use even labels | ||
|
||
const options: ChartOptions<"bar"> = { | ||
responsive: true, | ||
maintainAspectRatio: false, | ||
scales: { | ||
x: { | ||
grid: { display: false }, | ||
ticks: { | ||
color: theme.secondaryText, | ||
}, | ||
}, | ||
y: { | ||
grid: { color: theme.stroke, borderDash: [4, 4] }, | ||
ticks: { | ||
color: theme.secondaryText, | ||
stepSize: (chartData.total * tickSize) / 100, | ||
callback: (value) => getPercentValue(value), | ||
}, | ||
max: chartData.total, | ||
}, | ||
}, | ||
plugins: { | ||
datalabels: { | ||
anchor: "end", | ||
align: "top", | ||
offset: -4, | ||
color: theme.primaryText, | ||
font: { | ||
weight: "bold", | ||
}, | ||
formatter: formatPNKValue, | ||
}, | ||
tooltip: { | ||
backgroundColor: theme.whiteBackground, | ||
titleColor: theme.primaryText, | ||
borderColor: theme.stroke, | ||
borderWidth: 1, | ||
displayColors: false, | ||
callbacks: { | ||
label: (context) => getPercentValue(context.parsed.y), | ||
labelTextColor: () => theme.primaryText, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
return ( | ||
<BarContainer> | ||
<Bar | ||
data={{ | ||
labels: chartData.labels, | ||
datasets: [ | ||
{ | ||
data: chartData.data, | ||
backgroundColor: theme.secondaryPurple, | ||
hoverBackgroundColor: theme.primaryBlue, | ||
maxBarThickness: 60, | ||
}, | ||
], | ||
}} | ||
options={options} | ||
plugins={[ChartDataLabels]} | ||
/> | ||
</BarContainer> | ||
); | ||
}; | ||
|
||
export default BarChart; |
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
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
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
20 changes: 20 additions & 0 deletions
20
web/src/pages/Home/CourtOverview/StakedPNKByCourtsChart.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from "react"; | ||
import BarChart, { IBarChartData } from "./BarChart"; | ||
|
||
export type StakedPNKByCourtsChartData = { labels: string[]; stakes: number[]; totalStake: number }; | ||
|
||
interface IStakedPNKByCourtsChart { | ||
data: StakedPNKByCourtsChartData; | ||
} | ||
|
||
const StakedPNKByCourtsChart: React.FC<IStakedPNKByCourtsChart> = ({ data }) => { | ||
const chartData: IBarChartData = { | ||
labels: data.labels, | ||
data: data.stakes, | ||
total: data.totalStake, | ||
}; | ||
|
||
return <BarChart chartData={chartData} />; | ||
}; | ||
|
||
export default StakedPNKByCourtsChart; |