Skip to content

Commit

Permalink
[FIX] Charts: format of negative values
Browse files Browse the repository at this point in the history
- Rectified the display format of negative values within tooltips and
  axis labels in charts.

Task ID: 3829782

closes #3935

Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
  • Loading branch information
dhrp-odoo committed Mar 29, 2024
1 parent c2eed35 commit 06db896
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/helpers/figures/charts/bar_chart.ts
Expand Up @@ -233,7 +233,10 @@ function getBarConfiguration(
value = Number(value);
if (isNaN(value)) return value;
const { locale, format } = localeFormat;
return formatValue(value, { locale, format: !format && value > 1000 ? "#,##" : format });
return formatValue(value, {
locale,
format: !format && Math.abs(value) >= 1000 ? "#,##" : format,
});
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/figures/charts/chart_ui_common.ts
Expand Up @@ -136,7 +136,7 @@ export function getDefaultChartJsRuntime(
const xLabel = tooltipItem.dataset?.label || tooltipItem.label;
// tooltipItem.parsed.y can be an object or a number for pie charts
const yLabel = tooltipItem.parsed.y ?? tooltipItem.parsed;
const toolTipFormat = !format && yLabel > 1000 ? "#,##" : format;
const toolTipFormat = !format && Math.abs(yLabel) >= 1000 ? "#,##" : format;
const yLabelStr = formatValue(yLabel, { format: toolTipFormat, locale });
return xLabel ? `${xLabel}: ${yLabelStr}` : yLabelStr;
},
Expand Down
5 changes: 4 additions & 1 deletion src/helpers/figures/charts/line_chart.ts
Expand Up @@ -336,7 +336,10 @@ function getLineConfiguration(
value = Number(value);
if (isNaN(value)) return value;
const { locale, format } = localeFormat;
return formatValue(value, { locale, format: !format && value > 1000 ? "#,##" : format });
return formatValue(value, {
locale,
format: !format && Math.abs(value) >= 1000 ? "#,##" : format,
});
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/figures/charts/pie_chart.ts
Expand Up @@ -227,7 +227,7 @@ function getPieConfiguration(

const xLabel = tooltipItem.label || tooltipItem.dataset.label;
const yLabel = tooltipItem.parsed.y ?? tooltipItem.parsed;
const toolTipFormat = !format && yLabel > 1000 ? "#,##" : format;
const toolTipFormat = !format && Math.abs(yLabel) >= 1000 ? "#,##" : format;
const yLabelStr = formatValue(yLabel, { format: toolTipFormat, locale });

return xLabel ? `${xLabel}: ${yLabelStr} (${percentage}%)` : `${yLabelStr} (${percentage}%)`;
Expand Down
47 changes: 38 additions & 9 deletions tests/figures/chart/chart_plugin.test.ts
Expand Up @@ -1568,6 +1568,10 @@ describe("Chart design configuration", () => {
expect(runtime.chartJsConfig.options.scales.y?.ticks.callback!(60000000)).toEqual(
"60,000,000"
);
//@ts-ignore
expect(runtime.chartJsConfig.options.scales.y?.ticks.callback!(-60000000)).toEqual(
"-60,000,000"
);
}
);

Expand All @@ -1581,6 +1585,10 @@ describe("Chart design configuration", () => {
expect(runtime.chartJsConfig.options.scales.y?.ticks.callback!(60000000)).toEqual(
"60 000 000"
);
// @ts-ignore
expect(runtime.chartJsConfig.options.scales.y?.ticks.callback!(-60000000)).toEqual(
"-60 000 000"
);
}
);

Expand All @@ -1603,7 +1611,7 @@ describe("Chart design configuration", () => {
});

test.each(["bar", "line"])(
"Basic chart tooltip label, cell without format: thousand separator",
"Basic chart tooltip label, cell without format: thousand separator for positive values",
(chartType) => {
setCellContent(model, "A2", "60000000");
createChart(model, { ...defaultChart, type: chartType as "bar" | "line" | "pie" }, "42");
Expand All @@ -1614,14 +1622,17 @@ describe("Chart design configuration", () => {
}
);

test.each(["bar", "line"])("Basic chart tooltip label, cell with format", (chartType) => {
setCellContent(model, "A2", "6000");
setCellFormat(model, "A2", "[$$]#,##0.00");
createChart(model, { ...defaultChart, type: chartType as "bar" | "line" | "pie" }, "42");
const runtime = model.getters.getChartRuntime("42") as BarChartRuntime;
const label = getTooltipLabel(runtime, 0, 0);
expect(label).toEqual("$6,000.00");
});
test.each(["bar", "line"])(
"Basic chart tooltip label, cell without format: thousand separator for negative values",
(chartType) => {
setCellContent(model, "A2", "-60000000");
createChart(model, { ...defaultChart, type: chartType as "bar" | "line" | "pie" }, "42");
const runtime = model.getters.getChartRuntime("42") as BarChartRuntime;
const label = getTooltipLabel(runtime, 0, 0);

expect(label).toEqual("-60,000,000");
}
);

test.each(["bar", "line"])("Basic chart tooltip label, date format is ignored", (chartType) => {
setCellContent(model, "A2", "6000");
Expand Down Expand Up @@ -1672,6 +1683,24 @@ describe("Chart design configuration", () => {
expect(label).toBe("P1: $6,000.00 (100.00%)");
});
});

test("pie chart tooltip label with negative value", () => {
setCellContent(model, "A1", "P1");
setCellContent(model, "A2", "-6000");
createChart(
model,
{
dataSets: ["A1:A2"],
labelRange: "A1",
type: "pie",
},
"1"
);
const chart = model.getters.getChartRuntime("1") as PieChartRuntime;
const label = getTooltipLabel(chart, 0, 0);

expect(label).toBe("P1: -6,000 (100.00%)");
});
});

describe("Chart aggregate labels", () => {
Expand Down

0 comments on commit 06db896

Please sign in to comment.