Skip to content

Commit

Permalink
Enable override settings for the axis border - chartjs#4041
Browse files Browse the repository at this point in the history
Adds two new options to the cartesian axis: `borderColor` and `borderWidth`
which are used to control the border drawn at the edge of the axis area.
If these options are not set, the first grid line settings are used.
  • Loading branch information
etimberg committed Dec 31, 2019
1 parent ab39286 commit c9f2e5b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/axes/styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ The grid line configuration is nested under the scale configuration in the `grid
| Name | Type | Default | Description
| ---- | ---- | ------- | -----------
| `display` | `boolean` | `true` | If false, do not display grid lines for this axis.
| `borderColor` | `Color` | | If set, used as the colour of the border line. If unset, the first `color` option is resolved and used.
| `borderWidth` | `number` | | If set, used as the width of the border line. If unset, the first `lineWidth` option is resolved and used.
| `circular` | `boolean` | `false` | If true, gridlines are circular (on radar chart only).
| `color` | <code>Color&#124;Color[]&#124;function</code> | `'rgba(0, 0, 0, 0.1)'` | The color of the grid lines. If specified as an array, the first color applies to the first grid line, the second to the second grid line and so on.
| `borderDash` | `number[]` | `[]` | Length and spacing of dashes on grid lines. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash).
Expand Down
6 changes: 3 additions & 3 deletions src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ class Scale extends Element {
scale: me,
tick: ticks[0],
};
var axisWidth = gridLines.drawBorder ? resolve([gridLines.lineWidth, 0], context, 0) : 0;
var axisWidth = gridLines.drawBorder ? resolve([gridLines.borderWidth, gridLines.lineWidth, 0], context, 0) : 0;
var axisHalfWidth = axisWidth / 2;
var alignBorderValue = function(pixel) {
return alignPixel(chart, pixel, axisWidth);
Expand Down Expand Up @@ -1186,7 +1186,7 @@ class Scale extends Element {
scale: me,
tick: me.ticks[0],
};
var axisWidth = gridLines.drawBorder ? resolve([gridLines.lineWidth, 0], context, 0) : 0;
var axisWidth = gridLines.drawBorder ? resolve([gridLines.borderWidth, gridLines.lineWidth, 0], context, 0) : 0;
var items = me._gridLineItems || (me._gridLineItems = me._computeGridLineItems(chartArea));
var width, color, i, ilen, item;

Expand Down Expand Up @@ -1243,7 +1243,7 @@ class Scale extends Element {
}

ctx.lineWidth = axisWidth;
ctx.strokeStyle = resolve([gridLines.color], context, 0);
ctx.strokeStyle = resolve([gridLines.borderColor, gridLines.color], context, 0);
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
Expand Down
58 changes: 58 additions & 0 deletions test/fixtures/core.scale/cartesian-axis-border-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"config": {
"type": "scatter",
"data": {
"datasets": [{
"data": [{
"x": -20,
"y": -30
}, {
"x": 0,
"y": 0
}, {
"x": 20,
"y": 15
}]
}]
},
"options": {
"legend": false,
"title": false,
"scales": {
"x": {
"axis": "x",
"min": -100,
"max": 100,
"gridLines": {
"borderColor": "blue",
"borderWidth": 5,
"color": "red",
"drawBorder": true,
"drawOnChartArea": false
},
"ticks": {
"display": false
}
},
"y": {
"axis": "y",
"min": -100,
"max": 100,
"gridLines": {
"color": "red",
"drawOnChartArea": false
},
"ticks": {
"display": false
}
}
}
}
},
"options": {
"canvas": {
"height": 256,
"width": 512
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c9f2e5b

Please sign in to comment.