I have this graph with scroll in the x axis. (chartjs version 2.7.2)
https://jsfiddle.net/r70v96ta/2/
At the end part of the code, where the data is added, you can choose how many records to add:
addData(5, chartTest);
If I add more records the graph goes bigger in the Y axis. I want the data to grow longer in the X axis along with the scroll, but keep the same height. I tried hardcoding heights everywere the canvas is manipulated, but did not get it to work. My ideal would be once the Y axis scale is fixed, to be able to introduce datasets with variable number of records and keep the graph size and scale in Y axis, making the X axis scroll bigger (meaning longer) if the dataset is bigger and viceversa.
Edit:
I also tried to set parameters to the css classes with the purpose of obtaining the Y axis aspect ratio constraint I am after. Seems this could be achieved with the corresponding arragment of the canvas aspect-ratio
In case in could make any difference, my graph is wrapped in a div
element with this css class:
.mainBlank
{
background-color: white;
width: 100%;
min-height: 100vh;
max-height: 100%;
}
Edit2:
Using this html:
<div style="width: 100%; overflow-x: auto; overflow-y: hidden">
<div id="canvasParent" style="width: 2000px; height: 300px">
<canvas id="chart1" height="300" width="0"></canvas>
</div>
</div>
And setting the width in the client when data changes:
const newWidth = (chart.data.datasets[0].data.length * 30).toString() + "px";
$('#canvasParent').css({ width : newWidth});
I achieve the desired effect. However I dont understand why in the fiddle's example the chart height changes.