score:91
Seems like var ctx = $('#myChart');
is returning a list of elements. You would need to reference the first by using ctx[0]
.
Also height is a property, not a function.
I did it this way in my code:
var chartEl = document.getElementById("myChart");
chartEl.height = 500;
score:0
Setting the Size of a chartjs diagram can be simply achieved by setting the with of a surrounding container:
<div style="width:400px;">
<canvas id="myChart"></canvas>
</div>
But an important detail is, that you may need to resize
your diagram after creation:
var ctx = $('#myChart');
var myChart = new Chart(ctx, {
......
data: {
........
},
options: {
responsive: false,
maintainAspectRatio: true
}
});
// now resize the new chart to fit into the canvas....
myChart.resize();
score:0
For react, doing this works:
<Doughnut
data={data}
height="200px"
width="200px"
options={{ maintainAspectRatio: false }}
/>
score:0
The problem here is that maintainAspectRatio needs to be inside the options node, not at the same level as in the example.
score:1
Just to add on to the answer given by @numediaweb
In case you're banging your head against the wall because after following the instructions to set maintainAspectRatio=false
: I originally copied my code from an example I got on a website on using Chart.js with Angular 2+:
<div style="display: block">
<canvas baseChart
[datasets]="chartData"
[labels]="chartLabels"
[options]="chartOptions"
[legend]="chartLegend"
[colors]="chartColors"
[chartType]="chartType">
</canvas>
</div>
To make this work correctly you must remove the embedded (and unnecessary) style="display: block"
Instead define a class for the enclosing div, and define its height in CSS.
Once you do that, the chart should have responsive width but fixed height.
score:1
Needed the chart to fill the parent element 100%, rather than setting height manually, and the problem was to force parent div to always fill remaining space.
After setting responsive and ratio options (check out related chartjs doc), the following css did the trick:
html
<div class="panel">
<div class="chart-container">
<canvas id="chart"></canvas>
</div>
</div>
scss:
.panel {
display: flex;
.chart-container {
position: relative;
flex-grow: 1;
min-height: 0;
}
}
score:3
Set the aspectRatio property of the chart to 0 did the trick for me...
var ctx = $('#myChart');
ctx.height(500);
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
maintainAspectRatio: false,
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
myChart.aspectRatio = 0;
score:4
You should use html height attribute for the canvas element as:
<div class="chart">
<canvas id="myChart" height="100"></canvas>
</div>
score:6
He's right. If you want to stay with jQuery you could do this
var ctx = $('#myChart')[0];
ctx.height = 500;
or
var ctx = $('#myChart');
ctx.attr('height',500);
score:7
I created a container and set it the desired height of the view port (depending on the number of charts or chart specific sizes):
.graph-container {
width: 100%;
height: 30vh;
}
To be dynamic to screen sizes I set the container as follows:
*Small media devices specific styles*/
@media screen and (max-width: 800px) {
.graph-container {
display: block;
float: none;
width: 100%;
margin-top: 0px;
margin-right:0px;
margin-left:0px;
height: auto;
}
}
Of course very important (as have been referred to numerous times) set the following option
properties of your chart:
options:{
maintainAspectRatio: false,
responsive: true,
}
score:11
You can also set the dimensions to the canvas
<canvas id="myChart" width="400" height="400"></canvas>
And then set the responsive options to false to always maintain the chart at the size specified.
options: {
responsive: false,
}
score:15
This one worked for me:
I set the height from HTML
canvas#scoreLineToAll.ct-chart.tab-pane.active[height="200"]
canvas#scoreLineTo3Months.ct-chart.tab-pane[height="200"]
canvas#scoreLineToYear.ct-chart.tab-pane[height="200"]
Then I disabled to maintaining aspect ratio
options: {
responsive: true,
maintainAspectRatio: false,
score:21
You can wrap your canvas element in a parent div, relatively positioned, then give that div the height you want, setting maintainAspectRatio: false in your options
//HTML
<div id="canvasWrapper" style="position: relative; height: 80vh/500px/whatever">
<canvas id="chart"></canvas>
</div>
<script>
new Chart(somechart, {
options: {
responsive: true,
maintainAspectRatio: false
/*, your other options*/
}
});
</script>
score:188
The easiest way is to create a container for the canvas and set its height:
<div style="height: 300px">
<canvas id="chart"></canvas>
</div>
and set
options: {
responsive: true,
maintainAspectRatio: false
}
score:502
If you disable the maintain aspect ratio in options then it uses the available height:
var chart = new Chart('blabla', {
type: 'bar',
data: {
},
options: {
maintainAspectRatio: false,
}
});
Source: stackoverflow.com
Related Query
- Set height of chart in Chart.js
- Unable to set width and height on canvas in chart js
- HorizontalBar chart is displayed below canvas when I set height with style attribute for chart div (Chart.js 2.9.4)
- Chart.js: How to set minimum height of chart but still maintain aspect ratio?
- Chartjs bar chart blurry when height is set
- In Chart.js set chart title, name of x axis and y axis?
- Chart.js - How to set a line chart dataset as disabled on load
- chart js 2 how to set bar width
- Set minimum step size in chart js
- How set color family to pie chart in chart.js
- Chart.js line chart set background color
- chartjs : how to set custom scale in bar chart
- Chart JS - set start of week for x axis time series
- set background color to save canvas chart
- Chart.js: width and height of a pie chart not respected
- Chart.js How to set line height only to the points?
- set y-axis scale manually in a bar chart using angular-chart.js
- how to set chart.js grid color for line chart
- How do you set pie chart colors in angular-chart.js
- How to dynamically set ChartJs line chart width based on dataset size?
- Fix the height of Y-Axis of Bar chart in chart.js?
- How to get the actual chart width and height in chart.js
- How to set single color on each bar in angular chart js
- Changing width of chart.js chart without changing height
- React chart : prevent chart's canvas scaling with height and width
- Why would a ChartJS chart end up with width 0 and height 0?
- ng2-charts: How to set Title of chart dynamically in typescript
- Chartjs - Set start and end value into a Bar Chart
- Css style not working well when resizing chart height in angular application
- Responsive Chart.js Doughnut Chart with minimum height
More Query from same tag
- How to remove the x-axis showing up for the bottom?
- time scatter plot w/ chart.js
- Nesting directives and using the data inside the local scope
- Chart.js set global point size not working
- How to use JSON data in creating a chart with chartjs?
- Customize legend AngularJS
- Why is chartjs treating these arrays differently?
- ChartJS does not render correctly
- ChartJS Tooltips with time axis and multiple datasets
- Stacked Horizontal Bars not in align Chart JS
- How to customize horizontal bar in chart.js?
- Decimal in x-axis
- Adding Vertical scroll bar in a bar-chart (chartjs)
- Gradient is too blurred in Chart.js Line Chart
- Chart.js always visible labels
- Chart.js: Is it possible to get the label of a dataset in a stacked bargraph on clicking it?
- Changing width of chart.js chart without changing height
- Chart.js combined line and bar chart with differing data points
- Chart.js How to invert (swap) axes?
- Angular2 ng2-chart customizations?
- On click event to show name of pie chart slice in chartsJS
- Check if element exists before trying to call getContext
- How to display stacked line chartjs
- chart.js - user add the final point of the line
- ChartJS shows date, but not time, when displaying labels in time axis
- convert javascript to dart
- Extending Line Chart with custom line
- How to draw a range label on x axis using chartJS
- Display a single tooltip with Chart.JS?
- Chart.js how to inser values from json