Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.9 kB
3
Indexable
Never
import React from 'react';
import { ResponsiveLine } from '@nivo/line';
import { useTheme } from '@mui/material';
import { tokens } from '../theme';
import { mockLineData as data } from '../data/mockData';
import useMediaQuery from '@mui/material/useMediaQuery';

const LineChart = ({ isCustomLineColors = false, isDashboard = false }) => {
    const theme = useTheme();
    const colors = tokens(theme.palette.mode);
    const matchesSm = useMediaQuery(theme.breakpoints.down('sm'));

    const lineColors = isCustomLineColors ? { datum: 'color' } : { scheme: 'nivo' };
    const dashboardColors = isDashboard ? { datum: 'color' } : { scheme: 'nivo' };

    const legendsAccessibility = [
        {
            anchor: 'top-right',
            direction: 'column',
            justify: false,
            translateX: 70,
            translateY: -30,
            itemsSpacing: 0,
            itemDirection: 'left-to-right',
            itemWidth: 80,
            itemHeight: 20,
            itemOpacity: 0.75,
            symbolSize: 12,
            symbolShape: 'circle',
            symbolBorderColor: 'rgba(0, 0, 0, .5)',
            effects: [
                {
                    on: 'hover',
                    style: {
                        itemBackground: 'rgba(0, 0, 0, .03)',
                        itemOpacity: 1
                    }
                }
            ]
        }
    ];

    return (
        <ResponsiveLine
            data={data}
            theme={{
                axis: {
                    domain: {
                        line: {
                            stroke: colors.grey[100],
                        },
                    },
                    legend: {
                        text: {
                            fill: colors.grey[100],
                        },
                    },
                    ticks: {
                        line: {
                            stroke: matchesSm ? 'transparent' : colors.grey[100],
                            strokeWidth: 1,
                        },
                        text: {
                            fill: matchesSm ? 'transparent' : colors.grey[100],
                        },
                    },
                },
                legends: {
                    text: {
                        fill: colors.grey[100],
                    },
                },
                tooltip: {
                    container: {
                        color: colors.grey[300]
                    }
                }
            }}
            colors={isDashboard ? dashboardColors : lineColors}
            margin={{ top: 50, right: 45, bottom: 50, left: 45 }}
            xScale={{ type: 'point' }}
            yScale={{
                type: 'linear',
                min: 'auto',
                max: 'auto',
                stacked: true,
                reverse: false
            }}
            yFormat=" >-.2f"
            axisTop={null}
            axisRight={null}
            axisBottom={{
                tickSize: 5,
                tickPadding: 5,
                tickRotation: 0,
                legend: 'transportation',
                legendOffset: 36,
                legendPosition: 'middle'
            }}
            axisLeft={{
                tickValues: 5,
                tickSize: 5,
                tickPadding: 5,
                tickRotation: 0,
                legend: 'count',
                legendOffset: -40,
                legendPosition: 'middle'
            }}
            enableGridX={false}
            enableGridY={false}
            pointColor={{ theme: 'background' }}
            pointBorderWidth={2}
            pointBorderColor={{ from: 'serieColor' }}
            pointLabelYOffset={-12}
            useMesh={true}
            legends={legendsAccessibility}
        />
    )
}

export default LineChart;
Leave a Comment