react-garden
Version:
React + TypeScript + ThreeJS app using Material UI on NextJS, Apollo Client, GraphQL + WordPress REST APIs, for ThreeD web development.. a part of the threed.ai code family.
150 lines (137 loc) • 3.61 kB
JavaScript
// ** React Imports
import { forwardRef, useState } from 'react'
// ** MUI Imports
import Card from '@mui/material/Card'
import TextField from '@mui/material/TextField'
import CardHeader from '@mui/material/CardHeader'
import CardContent from '@mui/material/CardContent'
import InputAdornment from '@mui/material/InputAdornment'
// ** Third Party Imports
import format from 'date-fns/format'
import { Bar } from 'react-chartjs-2'
import DatePicker from 'react-datepicker'
// ** Icons Imports
import ChevronDown from 'mdi-material-ui/ChevronDown'
import CalendarOutline from 'mdi-material-ui/CalendarOutline'
const ChartjsBarChart = props => {
// ** Props
const { yellow, labelColor, borderColor, gridLineColor } = props
// ** States
const [endDate, setEndDate] = useState(null)
const [startDate, setStartDate] = useState(new Date())
const options = {
responsive: true,
maintainAspectRatio: false,
animation: { duration: 500 },
scales: {
x: {
grid: {
borderColor,
color: gridLineColor
},
ticks: { color: labelColor }
},
y: {
min: 0,
max: 400,
grid: {
borderColor,
color: gridLineColor
},
ticks: {
stepSize: 100,
color: labelColor
}
}
},
plugins: {
legend: { display: false }
}
}
const data = {
labels: [
'7/12',
'8/12',
'9/12',
'10/12',
'11/12',
'12/12',
'13/12',
'14/12',
'15/12',
'16/12',
'17/12',
'18/12',
'19/12'
],
datasets: [
{
maxBarThickness: 15,
backgroundColor: yellow,
borderColor: 'transparent',
borderRadius: { topRight: 15, topLeft: 15 },
data: [275, 90, 190, 205, 125, 85, 55, 87, 127, 150, 230, 280, 190]
}
]
}
const CustomInput = forwardRef(({ ...props }, ref) => {
const startDate = format(props.start, 'MM/dd/yyyy')
const endDate = props.end !== null ? ` - ${format(props.end, 'MM/dd/yyyy')}` : null
const value = `${startDate}${endDate !== null ? endDate : ''}`
return (
<TextField
{...props}
size='small'
value={value}
inputRef={ref}
InputProps={{
startAdornment: (
<InputAdornment position='start'>
<CalendarOutline />
</InputAdornment>
),
endAdornment: (
<InputAdornment position='end'>
<ChevronDown />
</InputAdornment>
)
}}
/>
)
})
const handleOnChange = dates => {
const [start, end] = dates
setStartDate(start)
setEndDate(end)
}
return (
<Card>
<CardHeader
title='Latest Statistics'
titleTypographyProps={{ variant: 'h6' }}
sx={{
flexDirection: ['column', 'row'],
alignItems: ['flex-start', 'center'],
'& .MuiCardHeader-action': { mb: 0 },
'& .MuiCardHeader-content': { mb: [2, 0] }
}}
action={
<DatePicker
selectsRange
id='chartjs-bar'
endDate={endDate}
selected={startDate}
startDate={startDate}
onChange={handleOnChange}
placeholderText='Click to select a date'
customInput={<CustomInput start={startDate} end={endDate} />}
/>
}
/>
<CardContent>
<Bar data={data} options={options} height={400} />
</CardContent>
</Card>
)
}
export default ChartjsBarChart