Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Component } from "@odoo/owl";
import { formatLargeNumber, formatValue } from "../../../../../helpers";
import { _t } from "../../../../../translation";
import {
ChartWithDataSetDefinition,
DispatchResult,
Expand All @@ -25,4 +27,11 @@ export class ChartHumanizeNumbers extends Component<Props, SpreadsheetChildEnv>
updateChart: Function,
canUpdateChart: Function,
};

get title() {
const locale = this.env.model.getters.getLocale();
const format = formatLargeNumber({ value: 1234567 }, undefined, locale);
const value = formatValue(1234567, { format, locale });
return _t("E.g. 1234567 -> %(value)s", { value });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<t t-name="o-spreadsheet-ChartHumanizeNumbers">
<Checkbox
name="'humanizeNumbers'"
label.translate="Make numbers human-readable"
label.translate="Use compact format"
title="title"
value="props.definition.humanize ?? true"
onChange="(humanize) => props.updateChart(this.props.chartId, { humanize })"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
className="'mb-2'"
/>
</Section>
<Section class="'pt-1'" title.translate="Number formatting">
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
</t>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Section class="'pt-0'" title.translate="Values">
<ChartShowValues t-props="props"/>
</Section>
<Section class="'pt-1'" title.translate="Number formatting">
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
</t>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<t t-name="o-spreadsheet-GaugeChartDesignPanel">
<GeneralDesignEditor t-props="props">
<t t-set-slot="general-extension">
<Section class="'pt-1'" title.translate="Number formatting">
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
</t>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<option value="left">Bottom left</option>
</select>
</Section>
<Section class="'pt-1'" title.translate="Number formatting">
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
</t>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
className="'mb-2'"
/>
</Section>
<Section class="'pt-1'" title.translate="Number formatting">
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
</t>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Section class="'pt-0'" title.translate="Values">
<ChartShowValues t-props="props"/>
</Section>
<Section class="'pt-1'" title.translate="Number formatting">
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
<PieHoleSize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ChartShowValues t-props="props"/>
<ChartShowDataMarkers t-props="props"/>
</Section>
<Section class="'pt-1'" title.translate="Number formatting">
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
</t>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<t t-set="color_down">Color Down</t>
<GeneralDesignEditor t-props="props" defaultChartTitleFontSize="defaultScorecardTitleFontSize">
<t t-set-slot="general-extension">
<Section class="'pt-1'" title.translate="Number formatting">
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
</t>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SidePanelCollapsible } from "../../components/collapsible/side_panel_co
import { RoundColorPicker } from "../../components/round_color_picker/round_color_picker";
import { Section } from "../../components/section/section";
import { GeneralDesignEditor } from "../building_blocks/general_design/general_design_editor";
import { ChartHumanizeNumbers } from "../building_blocks/humanize_numbers/humanize_numbers";
import { ChartLegend } from "../building_blocks/legend/legend";
import { PieHoleSize } from "../building_blocks/pie_hole_size/pie_hole_size";
import { ChartShowValues } from "../building_blocks/show_values/show_values";
Expand All @@ -36,6 +37,7 @@ export class SunburstChartDesignPanel extends Component<Props, SpreadsheetChildE
RoundColorPicker,
ChartLegend,
PieHoleSize,
ChartHumanizeNumbers,
};
static props = {
chartId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<GeneralDesignEditor t-props="props">
<t t-set-slot="general-extension">
<ChartLegend t-props="props"/>
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
</t>
</GeneralDesignEditor>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { SidePanelCollapsible } from "../../components/collapsible/side_panel_co
import { RoundColorPicker } from "../../components/round_color_picker/round_color_picker";
import { Section } from "../../components/section/section";
import { GeneralDesignEditor } from "../building_blocks/general_design/general_design_editor";
import { ChartHumanizeNumbers } from "../building_blocks/humanize_numbers/humanize_numbers";
import { ChartShowValues } from "../building_blocks/show_values/show_values";
import { TextStyler } from "../building_blocks/text_styler/text_styler";
import { TreeMapCategoryColors } from "./treemap_category_color/treemap_category_color";
Expand Down Expand Up @@ -51,6 +52,7 @@ export class TreeMapChartDesignPanel extends Component<Props, SpreadsheetChildEn
BadgeSelection,
TreeMapCategoryColors,
TreeMapColorScale,
ChartHumanizeNumbers,
};
static props = {
chartId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
hasVerticalAlign="true"
/>
</Section>
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
</t>
</SidePanelCollapsible>
</t>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
className="'mb-2'"
/>
</Section>
<Section class="'pt-1'" title.translate="Number formatting">
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
</t>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ZoomableChartDefinition,
} from "../../../../types/index";
import { Checkbox } from "../../components/checkbox/checkbox";
import { ChartHumanizeNumbers } from "../building_blocks/humanize_numbers/humanize_numbers";
import { ChartWithAxisDesignPanel } from "../chart_with_axis/design_panel";

interface Props {
Expand All @@ -27,6 +28,7 @@ export class GenericZoomableChartDesignPanel<
static components = {
...ChartWithAxisDesignPanel.components,
Checkbox,
ChartHumanizeNumbers,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed here, it's already in the ChartWithAxisDesignPanel.components :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up

};

onToggleZoom(zoomable: boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
className="'mb-2'"
/>
</Section>
<Section class="'pt-0'" title.translate="Number formatting">
<ChartHumanizeNumbers t-props="props"/>
</Section>
</t>
</GeneralDesignEditor>
<SeriesWithAxisDesignEditor t-props="props"/>
Expand Down
22 changes: 13 additions & 9 deletions src/helpers/format/format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toNumber } from "../../functions/helpers";
import { toNumber, tryToNumber } from "../../functions/helpers";
import { _t } from "../../translation";
import {
CellValue,
Expand Down Expand Up @@ -682,14 +682,18 @@ function _roundFormat<T extends InternalFormat>(internalFormat: T): T {
}

export function humanizeNumber({ value, format }: FunctionResultObject, locale: Locale): string {
const numberFormat = formatLargeNumber(
{
value,
format,
},
undefined,
locale
);
const numberValue = tryToNumber(value, locale);
if (numberValue === undefined) {
return "";
}
let numberFormat: Format | undefined = format;
if (Math.abs(numberValue) < 1000) {
const hasDecimal = numberValue % 1 !== 0;
numberFormat = !format && hasDecimal ? "0.####" : format;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need to have 4 decimals, I would have gone to 2, but why not :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally it's dependant on the number of digit before the comma (123.4, 12.34, 123.4). And ideally, we should also probably handle the different formats (a formatLargeNumber function, but for small ones)

Not sure it's worth tho, I went with the fast & dumb approach.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 digits seems quite weird tbh
image

} else {
numberFormat = formatLargeNumber({ value, format }, undefined, locale);
}

return formatValue(value, { format: numberFormat, locale });
}

Expand Down
8 changes: 4 additions & 4 deletions tests/figures/chart/chart_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3874,12 +3874,12 @@ describe("Can make numbers human-readable", () => {
"1"
);
let axis = getChartConfiguration(model, "1").options.scales.y;
const valuesBefore = [1e3, 1e6].map(axis.ticks.callback);
expect(valuesBefore).toEqual(["1,000", "1,000,000"]);
const valuesBefore = [1e3, 1e6, 0.1, 0.123456789].map(axis.ticks.callback);
expect(valuesBefore).toEqual(["1,000", "1,000,000", "0.1", "0.123456789"]);
updateChart(model, "1", { humanize: true });
axis = getChartConfiguration(model, "1").options.scales.y;
const valuesAfter = [1e3, 1e6].map(axis.ticks.callback);
expect(valuesAfter).toEqual(["1,000", "1,000k"]);
const valuesAfter = [1e3, 1e6, 0.1, 0.123456789].map(axis.ticks.callback);
expect(valuesAfter).toEqual(["1,000", "1,000k", "0.1", "0.1235"]);
}
);

Expand Down
14 changes: 14 additions & 0 deletions tests/figures/chart/charts_component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,20 @@ describe("charts", () => {
}
);

test.each<ChartType>(["bar", "line", "waterfall", "treemap", "sunburst"])(
"humanizeNumbers checkbox updates the chart",
async (type: ChartType) => {
createTestChart(type);
await mountChartSidePanel();
await openChartDesignSidePanel(model, env, fixture, chartId);

expect(model.getters.getChartDefinition(chartId).humanize).toBe(true);

await simulateClick("input[name='humanizeNumbers']");
expect(model.getters.getChartDefinition(chartId).humanize).toBe(false);
}
);

describe("aggregate", () => {
test.each(["bar", "pie", "line", "scatter", "combo"] as const)(
"aggregate checkbox is checked for string-count charts",
Expand Down