how to pass variables to Chart() ? #10096
-
I'd like to use different aspect ratios for my charts, depending on the user's display width by quering How can I send this value/variable down to the options section of the chart's data definitions, ie stick it Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
You can just pass a variable with your calculated aspectRatio like so: const max = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)
console.log(max)
const aspectRatio = max / max // Calculate correct aspectRatio here
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
aspectRatio: aspectRatio
}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options); |
Beta Was this translation helpful? Give feedback.
-
its sad to see people forced to use old versions of chart.js because the documentation is complete dogshit and all the examples on stackoverflow are from old versions and the bars do not stack any more ... |
Beta Was this translation helpful? Give feedback.
-
i never said there is no suport.. just that the examples are really bad.. stop using constants and include the whole thing in one example instead of 3 confusing tabs.. regarding stackoverflow, they all contain either: https://stackoverflow.com/questions/43689252/how-to-show-stacked-and-unstacked-bar-graphs-together-in-chart-js if someone else gets here this worked for me.. and it can be placed in a php function to have multiple charts on same page (no constants used)
|
Beta Was this translation helpful? Give feedback.
You can just pass a variable with your calculated aspectRatio like so: