-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for line height CSS values
The title plugin and scale title now accept lineHeight specified using unitless value (1.4), length ('1.4em' or '12px'), percentage ('200%') or keyword ('normal' === 1.2). The line height parsing has been refactored under the 'Chart.helpers.options' namespace. Also fix incorrect text position in the title plugin. https://developer.mozilla.org/en-US/docs/Web/CSS/line-height
- Loading branch information
1 parent
3bb31ca
commit 9dea44a
Showing
14 changed files
with
107 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @namespace Chart.helpers.options | ||
*/ | ||
module.exports = { | ||
/** | ||
* Converts the given line height `value` in pixels for a specific font `size`. | ||
* @param {Number|String} value - The lineHeight to parse (eg. 1.6, '14px', '75%', '1.6em'). | ||
* @param {Number} size - The font size (in pixels) used to resolve relative `value`. | ||
* @returns {Number} The effective line height in pixels (size * 1.2 if value is invalid). | ||
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/line-height | ||
* @since 2.7.0 | ||
*/ | ||
toLineHeight: function(value, size) { | ||
var matches = (''+value).match(/^(normal|(\d+(?:\.\d+)?)(px|em|%)?)$/); | ||
if (!matches || matches[1] === 'normal') { | ||
return size * 1.2; | ||
} | ||
|
||
value = parseFloat(matches[2]); | ||
|
||
switch (matches[3]) { | ||
case 'px': | ||
return value; | ||
case '%': | ||
value /= 100; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return size * value; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
describe('Chart.helpers.options', function() { | ||
var options = Chart.helpers.options; | ||
|
||
describe('toLineHeight', function() { | ||
it ('should support keyword values', function() { | ||
expect(options.toLineHeight('normal', 16)).toBe(16 * 1.2); | ||
}); | ||
it ('should support unitless values', function() { | ||
expect(options.toLineHeight(1.4, 16)).toBe(16 * 1.4); | ||
expect(options.toLineHeight('1.4', 16)).toBe(16 * 1.4); | ||
}); | ||
it ('should support length values', function() { | ||
expect(options.toLineHeight('42px', 16)).toBe(42); | ||
expect(options.toLineHeight('1.4em', 16)).toBe(16 * 1.4); | ||
}); | ||
it ('should support percentage values', function() { | ||
expect(options.toLineHeight('140%', 16)).toBe(16 * 1.4); | ||
}); | ||
it ('should fallback to default (1.2) for invalid values', function() { | ||
expect(options.toLineHeight(null, 16)).toBe(16 * 1.2); | ||
expect(options.toLineHeight(undefined, 16)).toBe(16 * 1.2); | ||
expect(options.toLineHeight('foobar', 16)).toBe(16 * 1.2); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters