Solution For Chart.js Canvas
- To enhance the chart.js canvas provided below:
- Adjust the y axis width to 50px in this case
options: {
scales: {
y: {
afterFit: function (scaleInstance) {
scaleInstance.width = 50; // sets the width to 50px
}
}
}
}
- Utilize a known value for setting graph width via slider input
const sliderToPercentageWidth = (sliderVal) => {
return (50 + (containerWidth - 50) * (sliderVal/100) )
};
- Maintain
containerWidth
on window resize with following script
window.addEventListener("resize", (event) => {
containerWidth = container.offsetWidth;
});
The snippet below encompasses all elements, accessible through (CodePen link):
HTML
<div id="container">
<div id="graph">
<canvas id="myChart"></canvas>
</div>
<input id="width-selector" type="range" value="50">
</div>
CSS
#container{
background-color: #ff000020;
width: 80vw;
height:90vh;
}
#graph{
height: 70vh;
width: 40vw;
background-color: #00ff0020;
}
#width-selector{
width: calc(100% - 50px);
margin-left: 50px;
}
JS
const sliderToPercentageWidth = (sliderVal) => {
return (50 + (containerWidth - 50) * (sliderVal/100) )
};
const rangeInput = document.getElementById("width-selector");
const graph = document.getElementById("graph");
const container = document.getElementById("container");
let containerWidth = container.offsetWidth;
// Match the slider width to graph on first render
const newWidth = sliderToPercentageWidth(rangeInput.value)
graph.style.width = `${newWidth}px`;
window.addEventListener("resize", (event) => {
containerWidth = container.offsetWidth;
});
rangeInput.oninput = () => {
const newWidth = sliderToPercentageWidth(rangeInput.value)
graph.style.width = `${newWidth}px`;
};
// A basic chart
// Option for y axis width set
const ctx = document.getElementById("myChart");
new Chart(ctx, {
type: "bar",
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: "# of Votes",
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
afterFit: function (scaleInstance) {
scaleInstance.width = 50; // sets the width to 50px
}
}
}
}
});
Prior Incorrect Solution
You can monitor slider input changes using JS and update graph CSS styling accordingly.
Here's a basic setup example considering slider value mapping to graph width.
The HTML and CSS are minimalistic, dynamically adjusting the width based on the slider input.
<div class="container">
<div id="graph"></div>
<input id="width-selector" type="range" value="50">
</div>
.container{
background-color: red;
width: 50vw;
height:70vh;
}
#graph{
height: 50vh;
/* Initialize width according to the input slider */
width: 50%;
background-color:blue;
}
#width-selector{
width: 100%;
}
const rangeInput = document.getElementById("width-selector");
const graph = document.getElementById("graph")
rangeInput.oninput = () => {
graph.style.width = `${rangeInput.value}%`
}
If your code structure differs from this example, feel free to share for specific guidance.