MCPcopy
hub / github.com/chartjs/Chart.js / computeFlexCategoryTraits

Function computeFlexCategoryTraits

src/controllers/controller.bar.js:89–115  ·  view source on GitHub ↗

* Computes an "optimal" category that globally arranges bars side by side (no gap when * percentage options are 1), based on the previous and following categories. This mode * generates bars with different widths when data are not evenly spaced. * @private

(index, ruler, options, stackCount)

Source from the content-addressed store, hash-verified

87 * @private
88 */
89function computeFlexCategoryTraits(index, ruler, options, stackCount) {
90 const pixels = ruler.pixels;
91 const curr = pixels[index];
92 let prev = index > 0 ? pixels[index - 1] : null;
93 let next = index < pixels.length - 1 ? pixels[index + 1] : null;
94 const percent = options.categoryPercentage;
95
96 if (prev === null) {
97 // first data: its size is double based on the next point or,
98 // if it's also the last data, we use the scale size.
99 prev = curr - (next === null ? ruler.end - ruler.start : next - curr);
100 }
101
102 if (next === null) {
103 // last data: its size is also double based on the previous point.
104 next = curr + curr - prev;
105 }
106
107 const start = curr - (curr - Math.min(prev, next)) / 2 * percent;
108 const size = Math.abs(next - prev) / 2 * percent;
109
110 return {
111 chunk: size / stackCount,
112 ratio: options.barPercentage,
113 start
114 };
115}
116
117function parseFloatBar(entry, item, vScale, i) {
118 const startValue = vScale.parse(entry[0], i);

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected