Skip to content

Commit

Permalink
Add font options to the controller (#48)
Browse files Browse the repository at this point in the history
* add font options to the controller

* fixes access to the right font instance in me.options and not dataset

* uses toFont (normalizing also lineHeight) instead of toFontString

* uses font.lineHeight instead of font.size to set the text height
  • Loading branch information
stockiNail authored Sep 1, 2021
1 parent 088219c commit 3af9f55
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/controller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import {DatasetController} from 'chart.js';
import {valueOrDefault, isNullOrUndef} from 'chart.js/helpers';
import {valueOrDefault, toFont, isNullOrUndef} from 'chart.js/helpers';
import {layout} from './layout';

/**
Expand Down Expand Up @@ -228,11 +228,15 @@ export default class SankeyController extends DatasetController {
* @private
*/
_drawLabel(label, y, height, ctx, textX) {
const lines = isNullOrUndef(label) ? [] : this.toTextLines(label);
const me = this;
const font = toFont(me.options.font, me.chart.options.font);
const lines = isNullOrUndef(label) ? [] : me.toTextLines(label);
const linesLength = lines.length;
const middle = y + height / 2;
const padding = 7.5;
const textHeight = 15;
const textHeight = font.lineHeight;

ctx.font = font.string;

if (linesLength > 1) {
const top = middle - (textHeight * linesLength / 2) + padding;
Expand Down
Binary file modified test/fixtures/labels-multiline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion types/index.esm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
Chart,
ChartComponent,
DatasetController,
Element
Element,
FontSpec
} from 'chart.js';

declare module 'chart.js' {
Expand Down Expand Up @@ -30,6 +31,7 @@ declare module 'chart.js' {
nodeWidth?: number /* defaults to 10 */
color?: string /* defaults to 'black' */
borderColor?: string /* defaults to 'black' */
font: FontSpec /* defaults to chart.options.font */
}

type FromToElement = {
Expand Down

0 comments on commit 3af9f55

Please sign in to comment.