Bar: fix too thick borders (#9678)
@ -86,6 +86,7 @@ Only the `data` option needs to be specified in the dataset namespace.
|
||||
| [`hoverBorderWidth`](#interactions) | `number` | Yes | Yes | `1`
|
||||
| [`hoverBorderRadius`](#interactions) | `number` | Yes | Yes | `0`
|
||||
| [`indexAxis`](#general) | `string` | - | - | `'x'`
|
||||
| [`inflateAmount`](#inflateamount) | `number`\|`'auto'` | Yes | Yes | `'auto'`
|
||||
| [`maxBarThickness`](#maxbarthickness) | `number` | - | - | |
|
||||
| [`minBarLength`](#styling) | `number` | - | - | |
|
||||
| [`label`](#general) | `string` | - | - | `''`
|
||||
@ -176,6 +177,10 @@ If this value is a number, it is applied to all corners of the rectangle (topLef
|
||||
When the border radius is supplied as a number and the chart is stacked, the radius will only be applied to the bars that are at the edges of the stack or where the bar is floating. The object syntax can be used to override this behavior.
|
||||
:::
|
||||
|
||||
#### inflateAmount
|
||||
|
||||
This option can be used to inflate the rects that are used to draw the bars. This can be used to hide artifacts between bars when `barPercentage`(#barpercentage) * `categoryPercentage`(#categorypercentage) is 1. The default value `'auto'` should work in most cases.
|
||||
|
||||
### Interactions
|
||||
|
||||
The interaction with each bar can be controlled with the following properties:
|
||||
|
||||
@ -84,6 +84,7 @@ Namespace: `options.elements.bar`, global bar options: `Chart.defaults.elements.
|
||||
| `borderColor` | [`Color`](/general/colors.md) | `Chart.defaults.borderColor` | Bar stroke color.
|
||||
| `borderSkipped` | `string` | `'start'` | Skipped (excluded) border: `'start'`, `'end'`, `'middle'`, `'bottom'`, `'left'`, `'top'`, `'right'` or `false`.
|
||||
| `borderRadius` | `number`\|`object` | `0` | The bar border radius (in pixels).
|
||||
| `inflateAmount` | `number`\|`'auto'` | `'auto'` | The amount of pixels to inflate the bar rectangle(s) when drawing.
|
||||
| [`pointStyle`](#point-styles) | `string`\|`Image`\|`HTMLCanvasElement` | `'circle'` | Style of the point for legend.
|
||||
|
||||
## Arc Configuration
|
||||
|
||||
@ -244,6 +244,12 @@ function startEnd(v, start, end) {
|
||||
return v === 'start' ? start : v === 'end' ? end : v;
|
||||
}
|
||||
|
||||
function setInflateAmount(properties, {inflateAmount}, ratio) {
|
||||
properties.inflateAmount = inflateAmount === 'auto'
|
||||
? ratio === 1 ? 0.33 : 0
|
||||
: inflateAmount;
|
||||
}
|
||||
|
||||
export default class BarController extends DatasetController {
|
||||
|
||||
/**
|
||||
@ -369,7 +375,9 @@ export default class BarController extends DatasetController {
|
||||
if (includeOptions) {
|
||||
properties.options = sharedOptions || this.resolveDataElementOptions(i, bars[i].active ? 'active' : mode);
|
||||
}
|
||||
setBorderSkipped(properties, properties.options || bars[i].options, stack, index);
|
||||
const options = properties.options || bars[i].options;
|
||||
setBorderSkipped(properties, options, stack, index);
|
||||
setInflateAmount(properties, options, ruler.ratio);
|
||||
this.updateElement(bars[i], i, properties, mode);
|
||||
}
|
||||
}
|
||||
|
||||
@ -146,6 +146,7 @@ export default class BarElement extends Element {
|
||||
this.base = undefined;
|
||||
this.width = undefined;
|
||||
this.height = undefined;
|
||||
this.inflateAmount = undefined;
|
||||
|
||||
if (cfg) {
|
||||
Object.assign(this, cfg);
|
||||
@ -153,10 +154,9 @@ export default class BarElement extends Element {
|
||||
}
|
||||
|
||||
draw(ctx) {
|
||||
const options = this.options;
|
||||
const {inflateAmount, options: {borderColor, backgroundColor}} = this;
|
||||
const {inner, outer} = boundingRects(this);
|
||||
const addRectPath = hasRadius(outer.radius) ? addRoundedRectPath : addNormalRectPath;
|
||||
const inflateAmount = 0.33;
|
||||
|
||||
ctx.save();
|
||||
|
||||
@ -165,13 +165,13 @@ export default class BarElement extends Element {
|
||||
addRectPath(ctx, inflateRect(outer, inflateAmount, inner));
|
||||
ctx.clip();
|
||||
addRectPath(ctx, inflateRect(inner, -inflateAmount, outer));
|
||||
ctx.fillStyle = options.borderColor;
|
||||
ctx.fillStyle = borderColor;
|
||||
ctx.fill('evenodd');
|
||||
}
|
||||
|
||||
ctx.beginPath();
|
||||
addRectPath(ctx, inflateRect(inner, inflateAmount, outer));
|
||||
ctx.fillStyle = options.backgroundColor;
|
||||
addRectPath(ctx, inflateRect(inner, inflateAmount));
|
||||
ctx.fillStyle = backgroundColor;
|
||||
ctx.fill();
|
||||
|
||||
ctx.restore();
|
||||
@ -211,7 +211,7 @@ BarElement.defaults = {
|
||||
borderSkipped: 'start',
|
||||
borderWidth: 0,
|
||||
borderRadius: 0,
|
||||
enableBorderRadius: true,
|
||||
inflateAmount: 'auto',
|
||||
pointStyle: undefined
|
||||
};
|
||||
|
||||
|
||||
BIN
test/fixtures/controller.bar/bar-base-value.png
vendored
|
Before Width: | Height: | Size: 4.6 KiB After Width: | Height: | Size: 4.4 KiB |
|
Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 4.5 KiB |
BIN
test/fixtures/controller.bar/bar-thickness-flex.png
vendored
|
Before Width: | Height: | Size: 5.0 KiB After Width: | Height: | Size: 4.5 KiB |
|
Before Width: | Height: | Size: 6.4 KiB After Width: | Height: | Size: 6.2 KiB |
|
Before Width: | Height: | Size: 2.7 KiB After Width: | Height: | Size: 5.4 KiB |
|
Before Width: | Height: | Size: 5.5 KiB After Width: | Height: | Size: 5.5 KiB |
BIN
test/fixtures/controller.bar/baseLine/bottom.png
vendored
|
Before Width: | Height: | Size: 985 B After Width: | Height: | Size: 923 B |
BIN
test/fixtures/controller.bar/baseLine/left.png
vendored
|
Before Width: | Height: | Size: 764 B After Width: | Height: | Size: 659 B |
BIN
test/fixtures/controller.bar/baseLine/mid-x.png
vendored
|
Before Width: | Height: | Size: 850 B After Width: | Height: | Size: 763 B |
BIN
test/fixtures/controller.bar/baseLine/mid-y.png
vendored
|
Before Width: | Height: | Size: 988 B After Width: | Height: | Size: 965 B |
BIN
test/fixtures/controller.bar/baseLine/right.png
vendored
|
Before Width: | Height: | Size: 753 B After Width: | Height: | Size: 624 B |
BIN
test/fixtures/controller.bar/baseLine/top.png
vendored
|
Before Width: | Height: | Size: 966 B After Width: | Height: | Size: 908 B |
BIN
test/fixtures/controller.bar/baseLine/value-x.png
vendored
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 955 B |
BIN
test/fixtures/controller.bar/baseLine/value-y.png
vendored
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 966 B |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 9.5 KiB After Width: | Height: | Size: 9.1 KiB |
|
Before Width: | Height: | Size: 9.5 KiB After Width: | Height: | Size: 9.3 KiB |
|
Before Width: | Height: | Size: 6.3 KiB After Width: | Height: | Size: 5.9 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
|
Before Width: | Height: | Size: 5.0 KiB After Width: | Height: | Size: 4.7 KiB |
BIN
test/fixtures/controller.bar/borderWidth/object.png
vendored
|
Before Width: | Height: | Size: 5.4 KiB After Width: | Height: | Size: 4.9 KiB |
BIN
test/fixtures/controller.bar/borderWidth/value.png
vendored
|
Before Width: | Height: | Size: 5.0 KiB After Width: | Height: | Size: 4.7 KiB |
BIN
test/fixtures/controller.bar/horizontal-borders.png
vendored
|
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.1 KiB |
|
Before Width: | Height: | Size: 9.2 KiB After Width: | Height: | Size: 9.0 KiB |
9
types/index.esm.d.ts
vendored
@ -1915,7 +1915,7 @@ export interface BarOptions extends CommonElementOptions {
|
||||
base: number;
|
||||
|
||||
/**
|
||||
* Skipped (excluded) border: 'start', 'end', 'left', 'right', 'bottom', 'top' or false (none).
|
||||
* Skipped (excluded) border: 'start', 'end', 'left', 'right', 'bottom', 'top' or false (none).
|
||||
* @default 'start'
|
||||
*/
|
||||
borderSkipped: 'start' | 'end' | 'left' | 'right' | 'bottom' | 'top' | false;
|
||||
@ -1925,6 +1925,13 @@ export interface BarOptions extends CommonElementOptions {
|
||||
* @default 0
|
||||
*/
|
||||
borderRadius: number | BorderRadius;
|
||||
|
||||
/**
|
||||
* Amount to inflate the rectangle(s). This can be used to hide artifacts between bars.
|
||||
* Unit is pixels. 'auto' translates to 0.33 pixels when barPercentage * categoryPercentage is 1, else 0.
|
||||
* @default 'auto'
|
||||
*/
|
||||
inflateAmount: number | 'auto';
|
||||
}
|
||||
|
||||
export interface BorderRadius {
|
||||
|
||||