score:9
Accepted answer
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>
Source: stackoverflow.com
Related Query
- Chart js - Get bar width after render
- How to get bar chart width in pixel
- How do you get the width of a datalabel from the Chartjs plugin after the chart animates?
- How can i launch a modal after clicking each bar in Bar chart in Chartjs and also how can i get its data?
- chart js 2 how to set bar width
- ChartJs bar chart - keep bars left instead of equally spread across the width
- Can't get bar chart colors in Chart js working in React Js
- How to get the actual chart width and height in chart.js
- ChartJS bar chart fixed width for dynamic data sets
- Bar Chart of ChartJS does not Render
- Chart.js: How to get x-axis labels to show on top of bars in bar chart
- Charts js and laravel: Render chart after passing in json data
- Click on interactive chart.js bar chart and get value for labels and groups in JS
- Problems hovering over ChartJS bar chart after being redrawn
- Bar chart from Vue-chartjs doesn't render
- Stacked Bar chart fixed width bar chart issue - Chartjs
- How do I get a different label for each bar in a bar chart in ChartJS?
- Chartjs bar order adjustment after bar chart is drawn
- ChartJs Memory Leak | Garbage Collection does not clean Chart Object or Arrays after render
- How to get clicked bar chart tooltip data?
- How can I get my Chart.JS bar chart to stack two data values together on each bar, and print a calculated value on each bar?
- How to prevent empty bars from taking up width in Chart.js bar chart
- Why ChartJS's Bar Chart does not render bar for a specific value?
- chartJS pie chart not updating after axios GET
- How to show symbols after the label and before the numeric value using chart.js Bar chart in Angular
- Chart js - set width to a specific bar
- VueJS + Chartjs - Chart only renders after code change
- How can I change the background colors of a bar chart after it has been created?
- Chart js: bar width
- After rendering one chart when trying to render a new one chart.js gives "Check that a complete date adapter is provided."
More Query from same tag
- Dynamically pass the JSON data in chartjs
- Updating chart.js with JSON from Razor page
- Chart.JS: How can I only display data labels when the bar width is big enough for the text?
- Charts.js Pie Chart
- Chart.js v2.6: Add arrows to pie chart output values
- ChartJS V3 Radar chart Label Font Size
- Chartjs linechart with only one point - how to center
- Chart.js - How to place text outside of radar chart
- Chartjs with multiple y axes and different scales
- JavaScript doughnut chart with centered hover label
- How to cut line graph in ChartJS
- Chart.js two columns on page - left column is squished until page resize
- Chart JS Tooltip Currency Problem - Stacked Bar Chart
- How can I place single data on the center in Chart.js?
- How to include Chart.js in my AngularJS project?
- Using vue-charts.js, I cannot get hoverOffset to work with doughnut
- Chart.js maxBarThickness option not working
- ChartJS changing graphs based upon Drop Down selection
- chart js put meter square / superscript on y axis
- Custom Tooltips On Line Chart Using Chart.js
- Chart.js: Disable Y-Axis, when graph is disabled via Legend
- Chart.js Box Annotations Fill Entire Chart
- Chart js footer
- Chart.JS To Not Start At 0
- setting uneven tick stepsize spacing with chart.js
- Mixed Chart calculating difference between two bars - ChartJS
- Multiple bubble chart datasets for chartjs
- Are we able to position the y axis in the center of the x axis?
- Type error in using chartjs and tc-angular-chartjs
- Resize the width and height of the ng2-charts