React-chartjs-2: How to set automatic width to the bar chart

Created on 21 Dec 2018  路  4Comments  路  Source: reactchartjs/react-chartjs-2

Hello,
The problem is that I have too many labels sometimes (it depends on the request).
when I have more than 200 labels :
capture d ecran 2018-12-21 a 18 01 23
only 3 labels :
capture d ecran 2018-12-21 a 18 02 31

So, I want to set automatic width to the component so it always fits all the labels ( the parent div is set to overflow-x:scroll , so there is no problem even if the width is more than 5000)

Thank you,

Most helpful comment

Hello,
Yes I have fixed this.
I have made a function in the component called calculateWidth()
which returns the width depending on the count of lables.
So after some tests I have defined manually what are the counts that make problems.

            <div className="scroll-horizental-histo">
                <Bar
                    height={400}
                    width={this.calculateWidth()}
                    data={data}
                    options={options}
                />
            </div>
    .scroll-horizental-histo {
      overflow-x: scroll;
    }



md5-abea0460962e276c0d6b120d6be4d7fa



        const options = {
            legend: {
                display: false
            },
            responsive: false,
            tooltips: {
                mode: 'label'
            },
            elements: {
                line: {
                    fill: false
                }
            },
            scales: {
                xAxes: [
                    {
                        barThickness: 'flex',
                        display: true,
                        gridLines: {
                            display: false
                        },
                        labels: x,
                        ticks: {
                            autoSkip: false,
                            maxRotation: 45,
                            minRotation: 45
                        }
                    }
                ],
                yAxes: [
                    {
                        type: 'linear',
                        display: true,
                        position: 'left',
                        id: 'y-axis-1',
                        gridLines: {
                            display: true
                        },
                        labels: {
                            show: false
                        },
                        ticks: {
                            beginAtZero: true
                        }
                    }
                ]
            }
        }



md5-abea0460962e276c0d6b120d6be4d7fa



    calculateWidth() {
        const { x } = this.props
        let length = x ? x.length : 0
        switch (true) {
            case length >= 0 && length <= 3:
                return 400
            case length >= 4 && length <= 50:
                return 1200
            case length >= 51 && length <= 100:
                return 2000
            default:
                return 5000
        }
    }

I am here for any question

All 4 comments

@souuu where you able to fix this?

Hello,
Yes I have fixed this.
I have made a function in the component called calculateWidth()
which returns the width depending on the count of lables.
So after some tests I have defined manually what are the counts that make problems.

            <div className="scroll-horizental-histo">
                <Bar
                    height={400}
                    width={this.calculateWidth()}
                    data={data}
                    options={options}
                />
            </div>
    .scroll-horizental-histo {
      overflow-x: scroll;
    }



md5-abea0460962e276c0d6b120d6be4d7fa



        const options = {
            legend: {
                display: false
            },
            responsive: false,
            tooltips: {
                mode: 'label'
            },
            elements: {
                line: {
                    fill: false
                }
            },
            scales: {
                xAxes: [
                    {
                        barThickness: 'flex',
                        display: true,
                        gridLines: {
                            display: false
                        },
                        labels: x,
                        ticks: {
                            autoSkip: false,
                            maxRotation: 45,
                            minRotation: 45
                        }
                    }
                ],
                yAxes: [
                    {
                        type: 'linear',
                        display: true,
                        position: 'left',
                        id: 'y-axis-1',
                        gridLines: {
                            display: true
                        },
                        labels: {
                            show: false
                        },
                        ticks: {
                            beginAtZero: true
                        }
                    }
                ]
            }
        }



md5-abea0460962e276c0d6b120d6be4d7fa



    calculateWidth() {
        const { x } = this.props
        let length = x ? x.length : 0
        switch (true) {
            case length >= 0 && length <= 3:
                return 400
            case length >= 4 && length <= 50:
                return 1200
            case length >= 51 && length <= 100:
                return 2000
            default:
                return 5000
        }
    }

I am here for any question

Does this make the chart to be horizontal scrollable? I will give a try

Closing. Reason: older than 6 months and not enough or outdated information supplied. Please re-open this issue if the problem persists, but provide more information. (including a sandbox link if possible)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LuizMoreira picture LuizMoreira  路  3Comments

RamonBeast picture RamonBeast  路  4Comments

Holychung picture Holychung  路  4Comments

selimovicz picture selimovicz  路  6Comments

jbbae picture jbbae  路  5Comments