score:19
Accepted answer
As pointed out in https://stackoverflow.com/a/68778432/360067, this is natively implemented in v3.
Original Answer
Here is how you extend Chart.js to draw a bar chart with rounded corners.
Chart.types.Bar.extend({
name: "BarAlt",
initialize: function (data) {
Chart.types.Bar.prototype.initialize.apply(this, arguments);
if (this.options.curvature !== undefined && this.options.curvature <= 1) {
var rectangleDraw = this.datasets[0].bars[0].draw;
var self = this;
var radius = this.datasets[0].bars[0].width * this.options.curvature * 0.5;
// override the rectangle draw with ours
this.datasets.forEach(function (dataset) {
dataset.bars.forEach(function (bar) {
bar.draw = function () {
// draw the original bar a little down (so that our curve brings it to its original position)
var y = bar.y;
// the min is required so animation does not start from below the axes
bar.y = Math.min(bar.y + radius, self.scale.endPoint - 1);
// adjust the bar radius depending on how much of a curve we can draw
var barRadius = (bar.y - y);
rectangleDraw.apply(bar, arguments);
// draw a rounded rectangle on top
Chart.helpers.drawRoundedRectangle(self.chart.ctx, bar.x - bar.width / 2, bar.y - barRadius + 1, bar.width, bar.height, barRadius);
ctx.fill();
// restore the y value
bar.y = y;
}
})
})
}
}
});
var lineChartData = {
labels: ["January", "February", "March", "April", "May", "June"],
datasets: [
{
fillColor: "#79D1CF",
strokeColor: "#79D1CF",
data: [60, 80, 81, 56, 55, 40]
},
{
fillColor: "#D1CF79",
strokeColor: "#D1CF79",
data: [34, 43, 43, 12, 65, 65]
}
]
};
var ctx = document.getElementById("myChart").getContext("2d");
var myLine = new Chart(ctx).BarAlt(lineChartData, {
// 0 (flat) to 1 (more curvy)
curvature: 1
});
You could simplify the code a bit if you don't need the animation.
Fiddle - http://jsfiddle.net/0dzp3jxw/
score:3
Starting on Chart.js v3 you can just specify borderRadius: NUMBER
on the dataset.
new Chart(document.querySelector('canvas').getContext('2d'), {
type: 'bar',
data: {
labels: ['1','2','3','4','5','6'],
datasets: [{
label: 'Sales',
data: [20,30,10,20,30,10],
fill: true,
borderRadius: 15,
borderColor: '#5eb8ff',
backgroundColor: '#5eb8ff',
datalabels: {
display: false
}
}]
}
});
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<canvas></canvas>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chart.js@3.0.0/dist/chart.min.js"></script>
</body>
</html>
Source: stackoverflow.com
Related Query
- How to put rounded corners on a Chart.js Bar chart
- How do you create rounded corners on the top and bottom of a bar chart and the between 2 part still draw like image?
- How to keep rounded bar corners when data is filtered chartJs?
- How to create chart.js vertical bar chat with rounded corners in angular 6?
- chart js 2 how to set bar width
- How to create rounded bars for Bar Chart.js v2?
- Chart.js Bar Chart - how to chart bars from 0
- chartjs : how to set custom scale in bar chart
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to create stacked bar chart using react-chartjs-2?
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- How to display inline values in a stacked bar chart with Chart.js?
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- How to have solid colored bars in angular-chart bar chart
- how to make a chart.js bar chart scrollable
- How to draw Horizontal line on Bar Chart Chartjs
- how to add a title to my ng2-charts bar chart
- PrimeNg bar chart how to show a label for the y-axis
- How to set single color on each bar in angular chart js
- How to make bar chart animation where all bars grow at the same speed?
- How to show data values in top of bar chart and line chart in chart.js 3
- How to change Chart.js horizontal bar chart Width?
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- How to add background image in bar chart column in Chart.js?
- Stacked bar chart with rounded corner of bar using Chart.js
- How to hide value in Chart JS bar
- How to show gradient vertically on chart js grouped bar chart?
- PrimeNg bar chart how do I hide the bar labels?
- how to customize tool tip while mouse go over bars on Chart js bar chart
- How to make chartJs stacked bar always rounded when data is 0?
More Query from same tag
- Parsing json in jQuery error
- How to make chartJs stacked bar always rounded when data is 0?
- How to remove background color and color example from tooltip in Chart.js?
- How to rename properties in objects that are in an array. What is the right way to solve the task?
- Chart.js legend took up too much spaces
- Parsing JSON data into Chart.js bar chart
- Legend color not working with randomly generated background colors in chartjs pie chart
- Chart.js How to set line height only to the points?
- ChartJS horizontal label are 1-2px clipped
- Exporting dynamic chartjs to jspdf
- ChartJS - Line Chart with different size datasets
- Cursor pointer change onhover not working in chartjsv3
- How to display variables from Laravel controller to view javascript with specific index
- Chart.js - Toggle visibility of charts
- How to update chart.js chart with new data?
- Chart.js Vertically Aligned points on single x axis
- how to use chartjs-plugin-crosshair in Angular 6+?
- Add rectangle fill to line chart
- ChartJS: Remove padding to the right of chart caused by tick labels
- Show Dates in x-axis when using chart.js
- Chart js bar with the highest value is a different color among the rest - React
- Chartjs change the specific label color in x axis in callback function
- React Native Updating State with Api
- Chart.js get array of currently visible points on graph
- How to append text or symbol to tooltip of chart.js
- Determining which chart the user has clicked on
- How to set the data of chart in angular which is taken from the backend nodejs api that is store in mongodb and showing on the html page
- Chart.js - tooltiptemplate with one decimal value in pie chart
- Bar chart labels in Chart.JS
- legend not displayed for radar Angular-chart.js