Working on a custom chart that can determine the thickness of a bar in a bar or horizontal bar chart based on a value provided by the data, not a pre-set width as possible with barThickness or barPercentage / categoryPercentage.
Goal: I can create a horizontal bar chart with data that contains an x
value and a thickness
, where the thickness becomes the relative thickness of that bar in relation to the other bars.
For example, this code:
const data = {
labels: ['Northeast', 'Midwest', 'South', 'West'],
datasets: [
{
data: [
{ x: 10, thickness: 18 },
{ x: 20, thickness: 8 },
{ x: 13, thickness: 5 },
{ x: 4, thickness: 12 }
],
},
],
}
const context = document.querySelector('#myChart').getContext('2d')
const barChart = new Chart(context, {
type: 'horizontalBar',
data: data,
})
... would render a chart looking something like this:
... instead of this:
(didn't include irrelevant style options here).
Key features going for:
- Tick labels are centered on the bar
- Tick intervals are determined by bar thickness
- Bars are evenly spaced
How can this be done?
I know I can hack the drawing of bar chart controllers to specify explicit widths to the rectangles drawn, but this fails to produce bars that are evenly spaced. Instead they have variable width within their fixed-width category, which is not the look I'm going for:
My hunch is that I need to extend a new axis but this seems complex.
Hoping to get any suggestions on best approaches! Thanks!
Similar to this unanswered question. Encountered these answers in my search which seemed promising but ultimately weren't the right answers: