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 formatter option #48

Merged
merged 12 commits into from
Oct 28, 2021
18 changes: 12 additions & 6 deletions src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,12 @@ function buildData(dataset, mainRect, font) {
function drawLabels(ctx, item, rect) {
const opts = rect.options;
const lh = opts.font.lineHeight;
const labels = (opts.label || item.g + '\n' + item.v).split('\n');
const y = rect.y + rect.height / 2 - labels.length * lh / 4;
labels.forEach((l, i) => ctx.fillText(l, rect.x + rect.width / 2, y + i * lh));
const label = opts.formatter;
if (label || item.g) {
const labels = ((label || item.g + '\n' + item.v) + '').split('\n');
const y = rect.y + rect.height / 2 - labels.length * lh / 4;
labels.forEach((l, i) => ctx.fillText(l, rect.x + rect.width / 2, y + i * lh));
}
kurkle marked this conversation as resolved.
Show resolved Hide resolved
}

export default class TreemapController extends DatasetController {
Expand Down Expand Up @@ -225,7 +228,7 @@ export default class TreemapController extends DatasetController {
if (!rect.hidden) {
rect.draw(ctx);
const opts = rect.options;
if (shouldDrawCaption(rect, opts.font) && item.g) {
if (shouldDrawCaption(rect, opts.font)) {
drawCaption(ctx, rect, item, opts, levels);
}
}
Expand Down Expand Up @@ -283,8 +286,11 @@ TreemapController.overrides = {
label(item) {
const dataset = item.dataset;
const dataItem = dataset.data[item.dataIndex];
const label = dataItem.g || dataset.label;
return (label ? label + ': ' : '') + dataItem.v;
if (dataItem.g) {
return dataItem.g + ': ' + dataItem.v;
}
const value = item.element.options.formatter;
return value || dataItem.v;
kurkle marked this conversation as resolved.
Show resolved Hide resolved
}
}
},
Expand Down
1 change: 1 addition & 0 deletions src/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ TreemapElement.defaults = {
groupDividers: false,
groupLabels: undefined,
spacing: undefined,
formatter: undefined,
kurkle marked this conversation as resolved.
Show resolved Hide resolved
label: undefined,
rtl: undefined
};
Expand Down
26 changes: 26 additions & 0 deletions test/fixtures/basic/formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default {
config: {
type: 'treemap',
data: {
datasets: [{
label: 'Simple treemap',
data: [6, 6, 4, 3, 2, 2, 1],
backgroundColor: 'red',
formatter(ctx) {
return ctx.type === 'data' ? ctx.raw.v : '';
}
}]
},
options: {
layout: {
padding: 20
}
}
},
options: {
canvas: {
height: 256,
width: 512
}
}
};
Binary file added test/fixtures/basic/formatter.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion types/index.esm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export interface TreemapControllerDatasetOptions<DType> {
hoverBorderColor?: Scriptable<Color, ScriptableContext<'treemap'>>;
hoverBorderWidth?: number;

label?: Scriptable<string, ScriptableContext<'treemap'>>;
formatter?: Scriptable<string, ScriptableContext<'treemap'>>;
label?: string;

data: TreemapDataPoint[]; // This will be auto-generated from `tree`
groups?: Array<keyof DType>;
Expand Down