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

Align font options with CSS #8066

Merged
merged 2 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all 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/docs/axes/_common_ticks.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
| `font` | `Font` | Yes | `defaults.font` | See [Fonts](../general/fonts.md)
| `major` | `object` | | `{}` | [Major ticks configuration](./styling.mdx#major-tick-configuration).
| `padding` | `number` | | `0` | Sets the offset of the tick labels from the axis
| `textStrokeColor` | `string` | `` | The color of the stroke around the text.
| `textStrokeWidth` | `number` | `0` | Stroke width around the text.
| `z` | `number` | | `0` | z-index of tick layer. Useful when ticks are drawn on chart area. Values <= 0 are drawn under datasets, > 0 on top.
7 changes: 2 additions & 5 deletions docs/docs/general/fonts.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ There are special global settings that can change all of the fonts on the chart.
For example, in this chart the text will all be red except for the labels in the legend.

```javascript
Chart.defaults.font.color = 'red';
Chart.defaults.font.size = 16;
let chart = new Chart(ctx, {
type: 'line',
data: data,
Expand All @@ -16,7 +16,7 @@ let chart = new Chart(ctx, {
labels: {
// This more specific font property overrides the global property
font: {
color: 'black'
size: 14
}
}
}
Expand All @@ -26,14 +26,11 @@ let chart = new Chart(ctx, {

| Name | Type | Default | Description
| ---- | ---- | ------- | -----------
| `color` | `Color` | `'#666'` | Default font color for all text.
| `family` | `string` | `"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"` | Default font family for all text, follows CSS font-family options.
| `size` | `number` | `12` | Default font size (in px) for text. Does not apply to radialLinear scale point labels.
| `style` | `string` | `'normal'` | Default font style. Does not apply to tooltip title or footer. Does not apply to chart title. Follows CSS font-style options (i.e. normal, italic, oblique, initial, inherit).
| `weight` | `string` | `undefined` | Default font weight (boldness). (see [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight)).
| `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)).
| `lineWidth` | `number` | `0` | Stroke width around the text. Currently only supported by [ticks](../axes/styling#tick-configuration).
| `strokeStyle` | `string` | `` | The color of the stroke around the text. Currently only supported by [ticks](../axes/styling#tick-configuration).

## Missing Fonts

Expand Down
8 changes: 4 additions & 4 deletions docs/docs/getting-started/v3-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ A number of changes were made to the configuration options passed to the `Chart`
* `global` namespace was removed from `defaults`. So `Chart.defaults.global` is now `Chart.defaults`
* Dataset controller defaults were relocate to `controllers`. For example `Chart.defaults.line` is now `Chart.defaults.controllers.line`
* `default` prefix was removed from defaults. For example `Chart.defaults.global.defaultColor` is now `Chart.defaults.color`
* `defaultColor` was renamed to `color`
* `defaultFontColor` was renamed to `font.color`
* `defaultColor` was split to `color`, `borderColor` and `backgroundColor`
* `defaultFontColor` was renamed to `color`
* `defaultFontFamily` was renamed to `font.family`
* `defaultFontSize` was renamed to `font.size`
* `defaultFontStyle` was renamed to `font.style`
Expand Down Expand Up @@ -169,11 +169,11 @@ options: {
major: {
enabled: true
},
color: (context) => context.tick && context.tick.major && '#FF0000',
font: function(context) {
if (context.tick && context.tick.major) {
return {
style: 'bold',
color: '#FF0000'
style: 'bold'
};
}
}
Expand Down
21 changes: 10 additions & 11 deletions src/core/core.defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ function getScope(node, key) {
*/
export class Defaults {
constructor() {
this.color = 'rgba(0,0,0,0.1)';
this.backgroundColor = 'rgba(0,0,0,0.1)';
this.borderColor = 'rgba(0,0,0,0.1)';
this.color = '#666';
this.controllers = {};
this.elements = {};
this.events = [
'mousemove',
Expand All @@ -33,31 +36,27 @@ export class Defaults {
'touchmove'
];
this.font = {
color: '#666',
family: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
size: 12,
style: 'normal',
lineHeight: 1.2,
weight: null,
lineWidth: 0,
strokeStyle: undefined
weight: null
};
this.hover = {
onHover: null
};
this.interaction = {
mode: 'nearest',
intersect: true
};
this.hover = {
onHover: null
};
this.maintainAspectRatio = true;
this.onHover = null;
this.onClick = null;
this.responsive = true;
this.showLine = true;
this.plugins = {};
this.responsive = true;
this.scale = undefined;
this.scales = {};
this.controllers = {};
this.showLine = true;
}
/**
* @param {string} scope
Expand Down
16 changes: 10 additions & 6 deletions src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ defaults.set('scale', {
// grid line settings
gridLines: {
display: true,
color: 'rgba(0,0,0,0.1)',
lineWidth: 1,
drawBorder: true,
drawOnChartArea: true,
Expand Down Expand Up @@ -76,6 +75,10 @@ defaults.set('scale', {
}
});

defaults.route('scale.ticks', 'color', '', 'color');
defaults.route('scale.gridLines', 'color', '', 'borderColor');
defaults.route('scale.scaleLabel', 'color', '', 'color');

/**
* Returns a new array containing numItems from arr
* @param {any[]} arr
Expand Down Expand Up @@ -1402,6 +1405,7 @@ export default class Scale extends Element {
rotation,
label,
font,
color: optionTicks.color,
textOffset,
textAlign,
textBaseline,
Expand Down Expand Up @@ -1574,20 +1578,20 @@ export default class Scale extends Element {
for (i = 0, ilen = items.length; i < ilen; ++i) {
const item = items[i];
const tickFont = item.font;
const useStroke = tickFont.lineWidth > 0 && tickFont.strokeStyle !== '';
const useStroke = optionTicks.textStrokeWidth > 0 && optionTicks.textStrokeColor !== '';

// Make sure we draw text in the correct color and font
ctx.save();
ctx.translate(item.x, item.y);
ctx.rotate(item.rotation);
ctx.font = tickFont.string;
ctx.fillStyle = tickFont.color;
ctx.fillStyle = item.color;
ctx.textAlign = item.textAlign;
ctx.textBaseline = item.textBaseline;

if (useStroke) {
ctx.strokeStyle = tickFont.strokeStyle;
ctx.lineWidth = tickFont.lineWidth;
ctx.strokeStyle = optionTicks.textStrokeColor;
ctx.lineWidth = optionTicks.textStrokeWidth;
}

const label = item.label;
Expand Down Expand Up @@ -1678,7 +1682,7 @@ export default class Scale extends Element {
ctx.rotate(rotation);
ctx.textAlign = textAlign;
ctx.textBaseline = 'middle';
ctx.fillStyle = scaleLabelFont.color;
ctx.fillStyle = scaleLabel.color;
ctx.font = scaleLabelFont.string;
ctx.fillText(scaleLabel.labelString, 0, 0);
ctx.restore();
Expand Down
5 changes: 4 additions & 1 deletion src/core/core.typedRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ function registerDefaults(item, scope, parentScope) {

function routeDefaults(scope, routes) {
Object.keys(routes).forEach(property => {
const propertyParts = property.split('.');
const sourceName = propertyParts.pop();
const sourceScope = [scope].concat(propertyParts).join('.');
const parts = routes[property].split('.');
const targetName = parts.pop();
const targetScope = parts.join('.');
defaults.route(scope, property, targetScope, targetName);
defaults.route(sourceScope, sourceName, targetScope, targetName);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/elements/element.arc.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,5 @@ ArcElement.defaults = {
* @type {any}
*/
ArcElement.defaultRoutes = {
backgroundColor: 'color'
backgroundColor: 'backgroundColor'
};
4 changes: 2 additions & 2 deletions src/elements/element.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,6 @@ BarElement.defaults = {
* @type {any}
*/
BarElement.defaultRoutes = {
backgroundColor: 'color',
borderColor: 'color'
backgroundColor: 'backgroundColor',
borderColor: 'borderColor'
};
4 changes: 2 additions & 2 deletions src/elements/element.line.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,6 @@ LineElement.defaults = {
* @type {any}
*/
LineElement.defaultRoutes = {
backgroundColor: 'color',
borderColor: 'color'
backgroundColor: 'backgroundColor',
borderColor: 'borderColor'
};
4 changes: 2 additions & 2 deletions src/elements/element.point.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ PointElement.defaults = {
* @type {any}
*/
PointElement.defaultRoutes = {
backgroundColor: 'color',
borderColor: 'color'
backgroundColor: 'backgroundColor',
borderColor: 'borderColor'
};
3 changes: 0 additions & 3 deletions src/helpers/helpers.options.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,11 @@ export function toFont(options, fallback) {
}

const font = {
color: valueOrDefault(options.color, fallback.color),
family: valueOrDefault(options.family, fallback.family),
lineHeight: toLineHeight(valueOrDefault(options.lineHeight, fallback.lineHeight), size),
lineWidth: valueOrDefault(options.lineWidth, fallback.lineWidth),
size,
style: valueOrDefault(options.style, fallback.style),
weight: valueOrDefault(options.weight, fallback.weight),
strokeStyle: valueOrDefault(options.strokeStyle, fallback.strokeStyle),
string: ''
};

Expand Down
6 changes: 3 additions & 3 deletions src/plugins/plugin.legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export class Legend extends Element {
const rtlHelper = getRtlAdapter(opts.rtl, me.left, me._minSize.width);
const ctx = me.ctx;
const labelFont = toFont(labelOpts.font, me.chart.options.font);
const fontColor = labelFont.color;
const fontColor = labelOpts.color;
const fontSize = labelFont.size;
let cursor;

Expand Down Expand Up @@ -542,8 +542,8 @@ export class Legend extends Element {
// Canvas setup
ctx.textAlign = rtlHelper.textAlign(textAlign);
ctx.textBaseline = 'middle';
ctx.strokeStyle = titleFont.color;
ctx.fillStyle = titleFont.color;
ctx.strokeStyle = titleOpts.color;
ctx.fillStyle = titleOpts.color;
ctx.font = titleFont.string;

ctx.fillText(titleOpts.text, x, y);
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/plugin.title.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class Title extends Element {

ctx.save();

ctx.fillStyle = fontOpts.color;
ctx.fillStyle = opts.color;
ctx.font = fontOpts.string;

ctx.translate(titleX, titleY);
Expand Down Expand Up @@ -267,5 +267,9 @@ export default {
position: 'top',
text: '',
weight: 2000 // by default greater than legend (1000) to be above
},

defaultRoutes: {
color: 'color'
}
};
14 changes: 7 additions & 7 deletions src/plugins/plugin.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ export class Tooltip extends Element {
titleFont = options.titleFont;
titleSpacing = options.titleSpacing;

ctx.fillStyle = options.titleFont.color;
ctx.fillStyle = options.titleColor;
ctx.font = titleFont.string;

for (i = 0; i < length; ++i) {
Expand Down Expand Up @@ -745,7 +745,7 @@ export class Tooltip extends Element {
pt.x = getAlignedX(me, bodyAlignForCalculation);

// Before body lines
ctx.fillStyle = bodyFont.color;
ctx.fillStyle = options.bodyColor;
each(me.beforeBody, fillLineOfText);

xLinePadding = displayColors && bodyAlignForCalculation !== 'right'
Expand Down Expand Up @@ -803,7 +803,7 @@ export class Tooltip extends Element {

footerFont = options.footerFont;

ctx.fillStyle = options.footerFont.color;
ctx.fillStyle = options.footerColor;
ctx.font = footerFont.string;

for (i = 0; i < length; ++i) {
Expand Down Expand Up @@ -1098,22 +1098,22 @@ export default {
custom: null,
position: 'average',
backgroundColor: 'rgba(0,0,0,0.8)',
titleColor: '#fff',
titleFont: {
style: 'bold',
color: '#fff',
},
titleSpacing: 2,
titleMarginBottom: 6,
titleAlign: 'left',
bodyColor: '#fff',
bodySpacing: 2,
bodyFont: {
color: '#fff',
},
bodyAlign: 'left',
footerColor: '#fff',
footerSpacing: 2,
footerMarginTop: 6,
footerFont: {
color: '#fff',
style: 'bold',
},
footerAlign: 'left',
Expand Down Expand Up @@ -1190,7 +1190,7 @@ export default {
};
},
labelTextColor() {
return this.options.bodyFont.color;
return this.options.bodyColor;
},
labelPointStyle(tooltipItem) {
const meta = tooltipItem.chart.getDatasetMeta(tooltipItem.datasetIndex);
Expand Down
11 changes: 8 additions & 3 deletions src/scales/scale.radialLinear.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ function drawPointLabels(scale) {
const context = scale.getContext(i);
const plFont = toFont(resolve([pointLabelOpts.font], context, i), scale.chart.options.font);
ctx.font = plFont.string;
ctx.fillStyle = plFont.color;
ctx.fillStyle = pointLabelOpts.color;

const angle = toDegrees(scale.getIndexAngle(i));
ctx.textAlign = getTextAlignForAngle(angle);
Expand Down Expand Up @@ -499,7 +499,7 @@ export default class RadialLinearScale extends LinearScaleBase {
);
}

ctx.fillStyle = tickFont.color;
ctx.fillStyle = tickOpts.color;
ctx.fillText(tick.label, 0, -offset);
});

Expand All @@ -526,7 +526,6 @@ RadialLinearScale.defaults = {

angleLines: {
display: true,
color: 'rgba(0,0,0,0.1)',
lineWidth: 1,
borderDash: [],
borderDashOffset: 0.0
Expand Down Expand Up @@ -568,3 +567,9 @@ RadialLinearScale.defaults = {
}
}
};

RadialLinearScale.defaultRoutes = {
'angleLines.color': 'borderColor',
'pointLabels.color': 'color',
'ticks.color': 'color'
};
4 changes: 2 additions & 2 deletions test/specs/controller.bubble.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ describe('Chart.controllers.bubble', function() {
expect(meta.data[i].x).toBeCloseToPixel(expected.x);
expect(meta.data[i].y).toBeCloseToPixel(expected.y);
expect(meta.data[i].options).toEqual(jasmine.objectContaining({
backgroundColor: Chart.defaults.color,
borderColor: Chart.defaults.color,
backgroundColor: Chart.defaults.backgroundColor,
borderColor: Chart.defaults.borderColor,
borderWidth: 1,
hitRadius: 1,
radius: expected.r
Expand Down
Loading