Can't resize react-chartjs-2 doughnut chart

后端 未结 2 412
迷失自我
迷失自我 2021-01-13 04:31

I am trying to make a doughnut chart with react and gatsbyjs. The chart works fine but I can not get it to use the full width of the div. It displays too small for the area

相关标签:
2条回答
  • 2021-01-13 05:17

    Have a look in the chartjs docs under responsive.

    In the passed options, set responsive: true, maintainAspectRatio: true and remove width and height.

    import React, { Component } from 'react';
    import { render } from 'react-dom';
    import Hello from './Hello';
    import './style.css';
    import { Doughnut } from 'react-chartjs-2'
    
    class App extends Component {
      constructor() {
        super();
        this.state = {
          name: 'React',
          data: {
            datasets: [{
              data: [10, 20, 30]
            }],
            labels: [
              'Red',
              'Yellow',
              'Blue'
            ]
          }
        }
      }
    
      render() {
        return (
    
          <Doughnut
            data={this.state.data}
            options={{
              responsive: true,
              maintainAspectRatio: true,
            }}
          />
        )
      }
    }
    
    render(<App />, document.getElementById('root'));

    Here is a working StackBlitz

    0 讨论(0)
  • 2021-01-13 05:26

    import React, { Component } from 'react';
    import { render } from 'react-dom';
    import Hello from './Hello';
    import './style.css';
    import { Doughnut } from 'react-chartjs-2'
    
    class App extends Component {
      constructor() {
        super();
        this.state = {
          name: 'React',
          data: {
            datasets: [{
              data: [10, 20, 30]
            }],
            labels: [
              'Red',
              'Yellow',
              'Blue'
            ]
          }
        }
      }
    
      render() {
        return (
    
          <Doughnut
            data={this.state.data}
            options={{
              responsive: true,
              maintainAspectRatio: false,
            }}
          />
        )
      }
    }
    
    render(<App />, document.getElementById('root'));

    0 讨论(0)
提交回复
热议问题