Skip to content

Commit e6d13d9

Browse files
committed
Enable separate grid and border dashes for cartesian axes
1 parent 8fdf76f commit e6d13d9

File tree

8 files changed

+60
-9
lines changed

8 files changed

+60
-9
lines changed

docs/axes/styling.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Namespace: `options.scales[scaleId].grid`, it defines options for the grid lines
1010
| ---- | ---- | :-------------------------------: | :-----------------------------: | ------- | -----------
1111
| `circular` | `boolean` | | | `false` | If true, gridlines are circular (on radar and polar area charts only).
1212
| `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.
13+
| `dash` | `number[]` | Yes | | `[]` | Length and spacing of dashes on grid lines. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash).
14+
| `dashOffset` | `number` | Yes | | `0.0` | Offset for line dashes. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset).
1315
| `display` | `boolean` | | | `true` | If false, do not display grid lines for this axis.
1416
| `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.
1517
| `drawTicks` | `boolean` | | | `true` | If true, draw lines beside the ticks in the axis area beside the chart.

src/core/core.scale.defaults.js

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export function applyScaleDefaults(defaults) {
2424

2525
// grid line settings
2626
grid: {
27+
dash: [],
28+
dashOffset: 0,
2729
display: true,
2830
lineWidth: 1,
2931
drawOnChartArea: true,

src/core/core.scale.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1089,12 +1089,11 @@ export default class Scale extends Element {
10891089
for (i = 0; i < ticksLength; i += step) {
10901090
const context = this.getContext(i);
10911091
const optsAtIndex = grid.setContext(context);
1092-
const optsAtIndexBorder = border.setContext(context);
10931092

10941093
const lineWidth = optsAtIndex.lineWidth;
10951094
const lineColor = optsAtIndex.color;
1096-
const borderDash = optsAtIndexBorder.dash || [];
1097-
const borderDashOffset = optsAtIndexBorder.dashOffset;
1095+
const borderDash = optsAtIndex.dash || [];
1096+
const borderDashOffset = optsAtIndex.dashOffset;
10981097

10991098
const tickWidth = optsAtIndex.tickWidth;
11001099
const tickColor = optsAtIndex.tickColor;
@@ -1536,6 +1535,8 @@ export default class Scale extends Element {
15361535
ctx.save();
15371536
ctx.lineWidth = borderOpts.width;
15381537
ctx.strokeStyle = borderOpts.color;
1538+
ctx.lineDashOffset = borderOpts.dashOffset
1539+
ctx.setLineDash(borderOpts.dash || [])
15391540

15401541
ctx.beginPath();
15411542
ctx.moveTo(x1, y1);

src/types/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -2934,6 +2934,14 @@ export interface BorderOptions {
29342934
}
29352935

29362936
export interface GridLineOptions {
2937+
/**
2938+
* @default []
2939+
*/
2940+
dash: number[];
2941+
/**
2942+
* @default 0
2943+
*/
2944+
dashOffset: Scriptable<number, ScriptableScaleContext>;
29372945
/**
29382946
* @default true
29392947
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module.exports = {
2+
config: {
3+
type: 'scatter',
4+
options: {
5+
scales: {
6+
x: {
7+
position: {y: 0},
8+
min: -10,
9+
max: 10,
10+
border: {
11+
dash: [6, 3],
12+
color: 'red',
13+
},
14+
grid: {
15+
color: 'lightGray',
16+
lineWidth: 3,
17+
},
18+
ticks: {
19+
display: false
20+
},
21+
},
22+
y: {
23+
position: {x: 0},
24+
min: -10,
25+
max: 10,
26+
border: {
27+
color: 'red',
28+
dash: [6, 3],
29+
},
30+
grid: {
31+
color: 'lightGray',
32+
lineWidth: 3,
33+
},
34+
ticks: {
35+
display: false
36+
},
37+
}
38+
}
39+
}
40+
}
41+
};
42+
14.5 KB
Loading

test/fixtures/core.scale/grid/scriptable-borderDash.js renamed to test/fixtures/core.scale/grid/scriptable-dash.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ module.exports = {
77
position: {y: 0},
88
min: -10,
99
max: 10,
10-
border: {
11-
dash: (ctx) => ctx.index % 2 === 0 ? [6, 3] : [],
12-
},
1310
grid: {
1411
color: 'lightGray',
12+
dash: (ctx) => ctx.index % 2 === 0 ? [6, 3] : [],
1513
lineWidth: 3,
1614
},
1715
ticks: {
@@ -22,11 +20,9 @@ module.exports = {
2220
position: {x: 0},
2321
min: -10,
2422
max: 10,
25-
border: {
26-
dash: (ctx) => ctx.index % 2 === 0 ? [6, 3] : [],
27-
},
2823
grid: {
2924
color: 'lightGray',
25+
dash: (ctx) => ctx.index % 2 === 0 ? [6, 3] : [],
3026
lineWidth: 3,
3127
},
3228
ticks: {

0 commit comments

Comments
 (0)