24

How do I set a chart's height using ng2-charts? I'm making a line chart, just like the demo on the ng2-charts site.

I've searched the documentation for ng2-charts and chart.js. In chart.js it looks like you set a height property on the canvas element like <canvas height="400">, but that element is obscured by the ng2-charts component.

BrandonReid
  • 1,264
  • 1
  • 13
  • 17

7 Answers7

54

Figured it out.

If responsive: true and maintainAspectRatio: false settings are set, you can then set the css height property of the <base-chart> element.

BrandonReid
  • 1,264
  • 1
  • 13
  • 17
  • 2
    It must be in options attribute. – Natanael Jan 21 '17 at 20:58
  • 1
    You can find a explanation for this in [Chart.js documentation](http://www.chartjs.org/docs/latest/general/responsive.html#responsive-charts) – Charmin Oct 26 '17 at 09:04
  • responsive is also a property for pie chart ? if not how can I set height for pie chart ? – k11k2 May 14 '18 at 06:01
  • Where in the hierarchy of configuration do these fit? The documentation is poor regarding placing parameters in context. – Jason K. Oct 23 '22 at 23:14
14

Just set a height property of baseChart.

<canvas baseChart height="300" ...></canvas>
Dennis
  • 151
  • 1
  • 3
10

In the ts file, declare chartOptions as ::

chartOptions = {
    responsive: true,
    maintainAspectRatio: false,
}

Likewise, In html file property bind [options]="chartOptions" and set required height, width in div tag

<div style="display: block; width: 500px; height: 400px;">
  <canvas
      baseChart
      [chartType]="'line'"
      [datasets]="chartData"
      [labels]="chartLabels"
      [colors]="lineChartColors"
      [options]="chartOptions"
      [legend]="true"
      (chartClick)="onChartClick($event)">
  </canvas>
</div>

This will work.

Ashish
  • 6,791
  • 3
  • 26
  • 48
Saumyajit
  • 678
  • 8
  • 10
5

just set height and width after basechart

 <canvas   baseChart
       height="40vh" width="80vw"
       [data]="doughnutChart.data"
       [labels]="doughnutChart.label"
       [chartType]="doughnutChartType"
       (chartHover)="chartHovered($event)"
       (chartClick)="chartClicked($event)">
    </canvas>
Arash
  • 3,458
  • 7
  • 32
  • 50
2

To provide the height or width you need to use a wrapper <div><div/> element around the <canvas><canvas/> element and the div element should then be decorated with relative position style and the height and width with reference to the view port size(browser window size).

This would look like below:

<div class="chart-container" style="position: relative; height:50vh; width:50vw">
      <canvas baseChart
        -----chart property binding here------
      </canvas>
</div>
adiga
  • 34,372
  • 9
  • 61
  • 83
Ashish Dehariya
  • 216
  • 3
  • 10
1

Set the chart options to be responsive

chartOptions: ChartOptions = {
  responsive: true,
  maintainAspectRatio: false,
};

The view width can be set in CSS

.myclass {
   width: 100vw;
 }
Interlated
  • 5,108
  • 6
  • 48
  • 79
0

setting

<canvas style="width: inherit;height: inherit;"

will adapt according to the chart's parent container size

e03050
  • 1,392
  • 15
  • 12