score:2
There are three things that need to be done to support this feature:
- Use the category scale to calculate bar thickness in proportion to axis size (width for vertical, height for horizontal)
- Override the
#getPixelForValue()
method in the category scale so that the category widths reflect the variant thickness, instead of being uniform - Change the dimensions of the rectangles drawn by the controller using the values calculated by the scale
Implemented these as a custom chart in this package: https://github.com/swayable/chartjs-chart-superbar using Chart.js's new chart and new axes customization features.
Solved (1) by adding behavior like this to the category scale:
getBarThickness(thicknessPercentage) {
let thickness = thicknessPercentage * this._axisSize(),
spacing = this.options.spacing * 2
return thickness - spacing
}
_axisSize() {
return this.isHorizontal() ? this.width : this.height
}
(2) is a bit more complicated because the pixel for each value in the category scale needs to take into account all of the various thicknesses for each previous item in the dataset when calculating the distance from the edge (left
for horizontal, or top
for vertical). Otherwise the thicknesses of the categories in the axis will not match the thicknesses of the bars, and the category axis labels will not be centered on the bars.
Relevant file for (1) and (2): https://github.com/swayable/chartjs-chart-superbar/blob/master/src/scale.thickCategory.js
(3) is solved by changing the relevant dimension in the element's _view
and _model
properties using the Scale#getBarThickness()
method above.
Relevant file for (3): https://github.com/swayable/chartjs-chart-superbar/blob/master/src/mixin.barThickness.js
Figured most of this out by reading the source code for Chart.js, especially scale.category.js and core.datasetController.js. Also took some inspiration from the source code for Chart.smith.js.
Source: stackoverflow.com
Related Query
- Specify varying thickness of each bar in Chart.js bar chart
- ChartJS bar chart with legend which corresponds to each bar
- Different color for each column in angular-chartjs bar chart
- Can I specify a different font size for each row of text in Chart Title?
- How to set single color on each bar in angular chart js
- ChartJS horizontal chart display legend on each bar
- angular-chart.js : how to show numbers in each bar of stacked bar chart
- How to use set the color for each bar in a bar chart using chartjs?
- How to set X coordinate for each bar with react chart js 2?
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- How do I get a different label for each bar in a bar chart in ChartJS?
- 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 write labels along with data on top and bottom of each stack in bar chart
- ChartJS vertical bar chart - measure percentage of the data in each column based on a specified max
- How to custom index label on each bar chart using chartjs?
- Grouped bar chart having each group with different data using chart.js
- Chart.js manipulate each bar in chart with different labels
- ChartJS Bar chart with time scale - Bars overlap with each other
- subcategories for each bar in multibar chart using chartjs
- Bar chart with vertical lines in each bar
- How can i launch a modal after clicking each bar in Bar chart in Chartjs and also how can i get its data?
- How to add data labels in each bar in stacked bar chart in chart.js?
- How to set different colors in each stacked bar chart cell?
- How to set percentage value by default on each bars in horizontal bar chart js
- how to create bar chart with group and sam color for each group using chart.js?
- Different color for each bar in a bar chart; ChartJS
- chart js 2 how to set bar width
- Chartjs Bar Chart showing old data when hovering
- Chart.js: Bar Chart Click Events
- Chartjs v2.0: stacked bar chart
More Query from same tag
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- How can i get my chartjs to show up graphically?
- Extend bar chart on Chart JS 2 into a new type of Chart
- Chart.js line from annotations plugin is not appearing
- How to show tooltips always on Chart.js 2
- How to set labels align left in Horizontal Bar using chart.js?
- Chartjs: Align first tick in first index of axis
- Chart point hover error
- Angular Chart JS prevent x label causing overflow
- Want to show small part of js Bar Chart when value is zero
- Control which lines have points?
- Passing text to a popup window opened in a JS function
- Remove white space in ChartJS when value is null
- Scrollable x axis with chart.js 2.1.4
- Adding data to line chart using addData() method in Chart.js
- How to create a chart with chartjs.org with data from an array?
- Chart JS show multiple data points for the same label
- Chart.js: How can I set paddings and draw a background box in a line chart?
- Radar chart - show values near vertices in chart.js
- Chart.js remove zero value sector in pie chart
- Chart.js label and point getting cutoff on the right
- Chartjs: make square legends
- Is it possible to scale Chart.js background Image
- Chart.js Picture inside doughnut segment
- Chart.js letter spacing very awkward
- Why do Bar Pie Chart values not line up?
- Canvas doens't display in SAPUI5
- react-chartjs-2 chart.js: How to make strokewidth of dougnut chart thin?
- Chart.js Y Axis Wrong Step Count
- Chart JS how to display an array of data objects as bar chart