Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

reactjs - ChartJS not resizing to be mobile friendly

I am using react and i can't seem to be able to resize my graph correctly when sizing down to mobile.

I am using the library ChartJS React 2

I have tried to use responsive in the options, I just can't figure out why on mobile this isn't working for me.

The following is my code and image of my graph

Thanks in advance!

Styles:

    cardBody: {
        display: 'flex',
        flexDirection: 'column',
        [theme.breakpoints.down('sm')]: {
            flexDirection: 'column',
            alignItems: 'center',
        },
    },
    graph: {
        flex: 1,
        padding: '30px 15px',
    },

Code:

<div className={classes.cardBody}>
                <div className={classes.graph}>
                    <Line
                        data={{
                            labels: ['6:00', '9:00', '12:00', '15:00', '18:00', '21:00', '24:00'],
                            datasets: [
                                {
                                    label: 'My First dataset',
                                    data: [65, 59, 80, 81, 56, 55, 40],
                                    borderColor: '#5c75dc',
                                    pointBackgroundColor: '#fff',
                                    pointBorderColor: '#5c75dc',
                                    pointHoverBackgroundColor: '#5c75dc',
                                    pointHoverBorderColor: '#fff',
                                    pointRadius: 4,
                                    pointHoverRadius: 4,
                                    fill: false,
                                },
                            ],
                        }}
                        options={{
                            responsive: true,
                            layout: {
                                padding: {
                                    top: 10,
                                    bottom: 100,
                                    left: 10,
                                    right: 10,
                                },
                            },
                            legend: {
                                display: false,
                            },
                            scales: {
                                xAxes: [
                                    {
                                        position: 'top',
                                        gridLines: { display: true, drawBorder: false, zeroLineColor: '#e2e2e2' },
                                        ticks: { display: true },
                                    },
                                ],
                                yAxes: [
                                    {
                                        gridLines: { display: false },
                                        ticks: { display: false },
                                    },
                                ],
                            },
                        }}
                    />
                </div>
            </div>

enter image description here

EDIT: I've removed Height and width and still not resizing working EDIT2: This seems to be a resizing issue. When entering the site on mobile browser it works fine, but when resizing the screen there seems to be issues.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...