Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable separate grid and border dashes for cartesian axes #11188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/axes/styling.md
Expand Up @@ -10,6 +10,8 @@ Namespace: `options.scales[scaleId].grid`, it defines options for the grid lines
| ---- | ---- | :-------------------------------: | :-----------------------------: | ------- | -----------
| `circular` | `boolean` | | | `false` | If true, gridlines are circular (on radar and polar area charts only).
| `color` | [`Color`](../general/colors.md) | Yes | Yes | `Chart.defaults.borderColor` | 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.
| `dash` | `number[]` | Yes | | `[]` | Length and spacing of dashes on grid lines. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash).
| `dashOffset` | `number` | Yes | | `0.0` | Offset for line dashes. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset).
| `display` | `boolean` | | | `true` | If false, do not display grid lines for this axis.
| `drawOnChartArea` | `boolean` | | | `true` | If true, draw lines on the chart area inside the axis lines. This is useful when there are multiple axes and you need to control which grid lines are drawn.
| `drawTicks` | `boolean` | | | `true` | If true, draw lines beside the ticks in the axis area beside the chart.
Expand Down
2 changes: 2 additions & 0 deletions src/core/core.scale.defaults.js
Expand Up @@ -24,6 +24,8 @@ export function applyScaleDefaults(defaults) {

// grid line settings
grid: {
dash: [],
dashOffset: 0,
display: true,
lineWidth: 1,
drawOnChartArea: true,
Expand Down
7 changes: 4 additions & 3 deletions src/core/core.scale.js
Expand Up @@ -1089,12 +1089,11 @@ export default class Scale extends Element {
for (i = 0; i < ticksLength; i += step) {
const context = this.getContext(i);
const optsAtIndex = grid.setContext(context);
const optsAtIndexBorder = border.setContext(context);

const lineWidth = optsAtIndex.lineWidth;
const lineColor = optsAtIndex.color;
const borderDash = optsAtIndexBorder.dash || [];
const borderDashOffset = optsAtIndexBorder.dashOffset;
const borderDash = optsAtIndex.dash || [];
const borderDashOffset = optsAtIndex.dashOffset;

const tickWidth = optsAtIndex.tickWidth;
const tickColor = optsAtIndex.tickColor;
Expand Down Expand Up @@ -1536,6 +1535,8 @@ export default class Scale extends Element {
ctx.save();
ctx.lineWidth = borderOpts.width;
ctx.strokeStyle = borderOpts.color;
ctx.lineDashOffset = borderOpts.dashOffset;
ctx.setLineDash(borderOpts.dash || []);

ctx.beginPath();
ctx.moveTo(x1, y1);
Expand Down
8 changes: 8 additions & 0 deletions src/types/index.d.ts
Expand Up @@ -2934,6 +2934,14 @@ export interface BorderOptions {
}

export interface GridLineOptions {
/**
* @default []
*/
dash: number[];
/**
* @default 0
*/
dashOffset: Scriptable<number, ScriptableScaleContext>;
/**
* @default true
*/
Expand Down
41 changes: 41 additions & 0 deletions test/fixtures/core.scale/grid/borderDash.js
@@ -0,0 +1,41 @@
module.exports = {
config: {
type: 'scatter',
options: {
scales: {
x: {
position: {y: 0},
min: -10,
max: 10,
border: {
dash: [6, 3],
color: 'red',
},
grid: {
color: 'lightGray',
lineWidth: 3,
},
ticks: {
display: false
},
},
y: {
position: {x: 0},
min: -10,
max: 10,
border: {
color: 'red',
dash: [6, 3],
},
grid: {
color: 'lightGray',
lineWidth: 3,
},
ticks: {
display: false
},
}
}
}
}
};
Binary file added test/fixtures/core.scale/grid/borderDash.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -7,11 +7,9 @@ module.exports = {
position: {y: 0},
min: -10,
max: 10,
border: {
dash: (ctx) => ctx.index % 2 === 0 ? [6, 3] : [],
},
grid: {
color: 'lightGray',
dash: (ctx) => ctx.index % 2 === 0 ? [6, 3] : [],
lineWidth: 3,
},
ticks: {
Expand All @@ -22,11 +20,9 @@ module.exports = {
position: {x: 0},
min: -10,
max: 10,
border: {
dash: (ctx) => ctx.index % 2 === 0 ? [6, 3] : [],
},
grid: {
color: 'lightGray',
dash: (ctx) => ctx.index % 2 === 0 ? [6, 3] : [],
lineWidth: 3,
},
ticks: {
Expand Down