question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chart width and height not working

See original GitHub issue

I am testing this library to create a chart for an app. I saw the sample dataset below from this tutorial.

import React, { Component } from 'react'
import { Line } from 'react-chartjs-2'

class CurrencyChart extends Component {
  render() {
    const chartData = {
        labels: ['M', 'T', 'W', 'T', 'F', 'S', 'S'],
        datasets: [{
          label: 'apples',
          data: [12, 19, 3, 17, 6, 3, 7],
          backgroundColor: "rgba(153,255,51,0.6)"
        }, {
          label: 'oranges',
          data: [2, 29, 5, 5, 2, 3, 10],
          backgroundColor: "rgba(255,153,0,0.6)"
        }]
      }
    return (
      <Line data={chartData} width={600} height={250}></Line>
    )
  }
}

export default CurrencyChart

I added bootstrap and jquery from a cdn in the html file. This is the output (The buttons on the top come from the parent component):

Output

I tried using the chart.js without the react-chartjs-2 package but it is either not being rendered or with no background color.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:12

github_iconTop GitHub Comments

19reactions
jamaldinecommented, May 25, 2020

hello, simply you add this in options: <Pie data={state} width={10} height={5} options={{maintainAspectRatio: false}} />

10reactions
gor181commented, Dec 3, 2016
Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting width and height [closed] - Stack Overflow
Show activity on this post. This works. The key here is to use an outer div and set the width and height on...
Read more >
Responsive Charts - Chart.js
When it comes to changing the chart size based on the window size, a major limitation is that the canvas render size (...
Read more >
Chartjs taking canvas width and height attribute, but ignoring ...
For non responsive charts, some work has been done in #3356: style width and height are now used to initialize the canvas size...
Read more >
Chart.js chart with fixed height and variable width - CodePen
Chart.js chart with fixed height and variable width. Adjust the width of the window to test.
Read more >
Why doesn't the chart maintain its width/height? - react-chartjs-2
In order for Chart.js to obey the custom size you need to set maintainAspectRatio to false:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found