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

Add stroke lines option around ticks to improve readability #6787

Merged
merged 8 commits into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/axes/styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ The tick configuration is nested under the scale configuration in the `ticks` ke
| `fontStyle` | `string` | `'normal'` | Font style for the tick labels, follows CSS font-style options (i.e. normal, italic, oblique, initial, inherit).
| `lineHeight` | <code>number&#124;string</code> | `1.2` | Height of an individual line of text (see [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/line-height)).
| `reverse` | `boolean` | `false` | Reverses order of tick labels.
| `lineWidth` | `number` | `0` | Stroke width around the text.
| `minor` | `object` | `{}` | Minor ticks configuration. Omitted options are inherited from options above.
| `major` | `object` | `{}` | Major ticks configuration. Omitted options are inherited from options above.
| `padding` | `number` | `0` | Sets the offset of the tick labels from the axis
| `strokeStyle` | `string` | `` | The color of the stroke around the text.
| `z` | `number` | `0` | z-index of tick layer. Useful when ticks are drawn on chart area. Values &lt;= 0 are drawn under datasets, &gt; 0 on top.

## Minor Tick Configuration
Expand Down
18 changes: 17 additions & 1 deletion src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ defaults._set('scale', {
minRotation: 0,
maxRotation: 50,
mirror: false,
lineWidth: 0,
strokeStyle: '',
padding: 0,
display: true,
autoSkip: true,
Expand Down Expand Up @@ -138,7 +140,9 @@ function parseFontOptions(options, nestedOpts) {
fontFamily: valueOrDefault(nestedOpts.fontFamily, options.fontFamily),
fontSize: valueOrDefault(nestedOpts.fontSize, options.fontSize),
fontStyle: valueOrDefault(nestedOpts.fontStyle, options.fontStyle),
lineHeight: valueOrDefault(nestedOpts.lineHeight, options.lineHeight)
lineHeight: valueOrDefault(nestedOpts.lineHeight, options.lineHeight),
benmccann marked this conversation as resolved.
Show resolved Hide resolved
lineWidth: valueOrDefault(nestedOpts.lineWidth, options.lineWidth),
kurkle marked this conversation as resolved.
Show resolved Hide resolved
strokeStyle: valueOrDefault(nestedOpts.strokeStyle, options.strokeStyle),
benmccann marked this conversation as resolved.
Show resolved Hide resolved
}), {
color: resolve([nestedOpts.fontColor, options.fontColor, defaults.fontColor])
});
Expand Down Expand Up @@ -1267,6 +1271,7 @@ class Scale extends Element {

const ctx = me.ctx;
const items = me._labelItems || (me._labelItems = me._computeLabelItems(chartArea));
const useTextWithStroke = optionTicks.lineWidth > 0 && optionTicks.strokeStyle !== '';
let i, j, ilen, jlen;

for (i = 0, ilen = items.length; i < ilen; ++i) {
Expand All @@ -1282,15 +1287,26 @@ class Scale extends Element {
ctx.textBaseline = 'middle';
ctx.textAlign = item.textAlign;

if (useTextWithStroke) {
kurkle marked this conversation as resolved.
Show resolved Hide resolved
ctx.strokeStyle = optionTicks.strokeStyle;
ctx.lineWidth = optionTicks.lineWidth;
}

const label = item.label;
let y = item.textOffset;
if (isArray(label)) {
for (j = 0, jlen = label.length; j < jlen; ++j) {
// We just make sure the multiline element is a string here..
if (useTextWithStroke) {
ctx.strokeText('' + label[j], 0, y);
}
ctx.fillText('' + label[j], 0, y);
y += tickFont.lineHeight;
}
} else {
if (useTextWithStroke) {
ctx.strokeText(label, 0, y);
}
ctx.fillText(label, 0, y);
}
ctx.restore();
Expand Down
1 change: 1 addition & 0 deletions test/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Context.prototype._initMethods = function() {
fill: function() {},
fillRect: function() {},
fillText: function() {},
strokeText: function() {},
lineTo: function() {},
measureText: function(text) {
// return the number of characters * fixed size
Expand Down
2 changes: 2 additions & 0 deletions test/specs/scale.category.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ describe('Category scale tests', function() {
labelOffset: 0,
minor: {},
major: {},
lineWidth: 0,
kurkle marked this conversation as resolved.
Show resolved Hide resolved
strokeStyle: '',
}
});

Expand Down
2 changes: 2 additions & 0 deletions test/specs/scale.linear.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ describe('Linear Scale', function() {
labelOffset: 0,
minor: {},
major: {},
lineWidth: 0,
strokeStyle: '',
}
});

Expand Down
2 changes: 2 additions & 0 deletions test/specs/scale.logarithmic.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ describe('Logarithmic Scale tests', function() {
autoSkipPadding: 0,
labelOffset: 0,
minor: {},
lineWidth: 0,
strokeStyle: '',
major: {
enabled: true
},
Expand Down
2 changes: 2 additions & 0 deletions test/specs/scale.radialLinear.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ describe('Test the radial linear scale', function() {
labelOffset: 0,
minor: {},
major: {},
lineWidth: 0,
strokeStyle: '',
},
});

Expand Down
2 changes: 2 additions & 0 deletions test/specs/scale.time.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ describe('Time scale tests', function() {
major: {
enabled: false
},
lineWidth: 0,
strokeStyle: '',
},
time: {
parser: false,
Expand Down