Skip to content

Commit

Permalink
Bar: fix too thick borders (#9678)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Sep 25, 2021
1 parent 990a289 commit c3a53f0
Show file tree
Hide file tree
Showing 29 changed files with 29 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/charts/bar.md
Expand Up @@ -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` | - | - | `''`
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions docs/configuration/elements.md
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/controllers/controller.bar.js
Expand Up @@ -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 {

/**
Expand Down Expand Up @@ -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);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/elements/element.bar.js
Expand Up @@ -146,17 +146,17 @@ export default class BarElement extends Element {
this.base = undefined;
this.width = undefined;
this.height = undefined;
this.inflateAmount = undefined;

if (cfg) {
Object.assign(this, cfg);
}
}

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();

Expand All @@ -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();
Expand Down Expand Up @@ -211,7 +211,7 @@ BarElement.defaults = {
borderSkipped: 'start',
borderWidth: 0,
borderRadius: 0,
enableBorderRadius: true,
inflateAmount: 'auto',
pointStyle: undefined
};

Expand Down
Binary file modified test/fixtures/controller.bar/bar-base-value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/bar-thickness-flex-offset.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/bar-thickness-flex.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/bar-thickness-offset.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/bar-thickness-reverse.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/bar-thickness-stacked.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/bottom.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/left.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/mid-x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/mid-y.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/right.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/top.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/value-x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/value-y.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/borderRadius/border-radius.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/borderSkipped/middle.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/borderWidth/indexable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/borderWidth/object.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/borderWidth/value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/horizontal-borders.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/minBarLength/vertical.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion types/index.esm.d.ts
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit c3a53f0

Please sign in to comment.