Skip to content

Commit

Permalink
Merge pull request #311 from ading-be/#310
Browse files Browse the repository at this point in the history
- Adjust getColor to support RGB (TODO: HSL colors)
  • Loading branch information
scmmishra authored Nov 26, 2020
2 parents 92a3df9 + 1547a85 commit e04b8f7
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 17 deletions.
9 changes: 8 additions & 1 deletion dist/frappe-charts.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,12 @@ function isValidColor(string) {
}

const getColor = (color) => {
// When RGB color, convert to hexadecimal (alpha value is omitted)
if((/rgb[a]{0,1}\([\d, ]+\)/gim).test(color)) {
return (/\D+(\d*)\D+(\d*)\D+(\d*)/gim).exec(color)
.map((x, i) => (i !== 0 ? Number(x).toString(16) : '#'))
.reduce((c, ch) => `${c}${ch}`);
}
return PRESET_COLOR_MAP[color] || color;
};

Expand Down Expand Up @@ -1821,7 +1827,8 @@ class AggregationChart extends BaseChart {
configure(args) {
super.configure(args);

this.config.formatTooltipY = args.tooltipOptions.formatTooltipY;
// Catch undefined tooltipOptions
this.config.formatTooltipY = (args.tooltipOptions || {}).formatTooltipY;
this.config.maxSlices = args.maxSlices || 20;
this.config.maxLegendPoints = args.maxLegendPoints || 20;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/frappe-charts.min.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.cjs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/frappe-charts.min.iife.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/js/frappe-charts.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/js/frappe-charts.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/js/charts/AggregationChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class AggregationChart extends BaseChart {
configure(args) {
super.configure(args);

this.config.formatTooltipY = args.tooltipOptions.formatTooltipY;
this.config.formatTooltipY = (args.tooltipOptions || {}).formatTooltipY;
this.config.maxSlices = args.maxSlices || 20;
this.config.maxLegendPoints = args.maxLegendPoints || 20;
}
Expand Down
6 changes: 6 additions & 0 deletions src/js/utils/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@ export function isValidColor(string) {
}

export const getColor = (color) => {
// When RGB color, convert to hexadecimal (alpha value is omitted)
if((/rgb[a]{0,1}\([\d, ]+\)/gim).test(color)) {
return (/\D+(\d*)\D+(\d*)\D+(\d*)/gim).exec(color)
.map((x, i) => (i !== 0 ? Number(x).toString(16) : '#'))
.reduce((c, ch) => `${c}${ch}`);
}
return PRESET_COLOR_MAP[color] || color;
};
14 changes: 14 additions & 0 deletions src/js/utils/test/colors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const assert = require('assert');
const colors = require('../colors');

describe('utils.colors', () => {
it('should return #aaabac for RGB()', () => {
assert.equal(colors.getColor('rgb(170, 171, 172)'), '#aaabac');
});
it('should return #ff5858 for the named color red', () => {
assert.equal(colors.getColor('red'), '#ff5858d');
});
it('should return #1a5c29 for the hex color #1a5c29', () => {
assert.equal(colors.getColor('#1a5c29'), '#1a5c29');
});
});
14 changes: 7 additions & 7 deletions src/js/utils/test/helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const assert = require('assert')
const helpers = require('../helpers')
const assert = require('assert');
const helpers = require('../helpers');

describe('utils.helpers', () => {
it('should return a value fixed upto 2 decimals', () => {
assert.equal(helpers.floatTwo(1.234), 1.23);
assert.equal(helpers.floatTwo(1.456), 1.46);
assert.equal(helpers.floatTwo(1), 1.00);
});
it('should return a value fixed upto 2 decimals', () => {
assert.equal(helpers.floatTwo(1.234), 1.23);
assert.equal(helpers.floatTwo(1.456), 1.46);
assert.equal(helpers.floatTwo(1), 1.00);
});
});

0 comments on commit e04b8f7

Please sign in to comment.