I need to get the bar width in pixels and use it in my chart js settings for the pointRadius: {{barwidth}}
of the overlapping line graph. My chart is also set to responsive so i'd need to update this value if the window was to be resized.
I am stumped. And could really use some help.
See the line on each bar
that is a line chart with the pointStyle: 'line'
set so i can have this effect. Now i need to set the width of that line to the bar with pointRadius: {{barwidth}}
To set the chart size in ChartJS, we recommend using the responsive option, which makes the Chart fill its container. You must wrap the chart canvas tag in a div in order for responsive to take effect. You cannot set the canvas element size directly with responsive .
js convention is to call it ctx . An object literal containing the data and the configuration options that Chart. js will use to build your chart. The required properties are type and data . In our example type is 'line' because we want a line chart.
If you select the Maintain Aspect Ratio check box, the embedded Mimic will retain its original proportions. This means that when you resize the embedded Mimic, its width and height will maintain their relationship, for example, if you reduce the height, the width will also reduce automatically.
You can make the <svg> responsive by adding a preserveAspectRatio attribute of xMinYMin meet , and a viewbox attribute of 0 0 WIDTH HEIGHT . Where WIDTH and HEIGHT are the width and height of your graph. That's the basic responsive SVG done. The only thing left is to create the graph itself.
Usually you can use getDatasetMeta()
method of the chart to get bar width.
However, if you were to change/update the point-radius of the line graph dynamically (on window resize), you would have to use a chart plugin, as such :
Chart.plugins.register({
updated: false,
beforeDraw: function(chart) {
var barWidth = chart.getDatasetMeta(1).data[0]._model.width;
var line = chart.data.datasets[0];
line.pointRadius = barWidth / 2;
line.pointHoverRadius = barWidth / 2;
if (!this.updated) {
chart.update();
this.updated = true;
}
}
});
* add this at the beginning of your script
ᴅᴇᴍᴏ ⧩
Chart.plugins.register({
updated: false,
beforeDraw: function(chart) {
var barWidth = chart.getDatasetMeta(1 /* dataset-index of bar graph */).data[0]._model.width;
var line = chart.data.datasets[0 /* dataset-index of line graph */];
line.pointRadius = barWidth / 2;
line.pointHoverRadius = barWidth / 2;
// update chart at first render with newly added values
if (!this.updated) {
chart.update();
this.updated = true;
}
}
});
var chart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
type: 'line',
label: 'LINE',
data: [3, 1, 4, 2, 5],
backgroundColor: 'rgba(0, 119, 290, 0.5)',
borderColor: 'transparent',
pointBorderColor: '#07C',
fill: false,
pointStyle: 'line'
}, {
label: 'BAR',
data: [3, 1, 4, 2, 5],
backgroundColor: 'rgba(4, 142, 128, 0.5)'
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With